Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include <osmocom/gsm/gsm23003.h> |
| 27 | #include <osmocom/core/utils.h> |
| 28 | |
| 29 | #define BOOL_STR(b) ((b)? "true" : "false") |
| 30 | |
| 31 | static struct { |
| 32 | const char *imsi; |
| 33 | bool expect_ok; |
| 34 | } test_imsis[] = { |
| 35 | { "", false }, |
| 36 | { " ", false }, |
| 37 | { "1", false }, |
| 38 | { "123", false }, |
| 39 | { "12345", false }, |
| 40 | { "123456", true }, |
| 41 | { "1234567", true }, |
| 42 | { "1234567890123", true }, |
| 43 | { "123456789012345", true }, |
| 44 | { "000000000000000", true }, |
| 45 | { "999999999999999", true }, |
| 46 | { "1234567890123456", false }, |
| 47 | { "a23456789012345", false }, |
| 48 | { "1234567b9012345", false }, |
| 49 | { "12345678901234c", false }, |
| 50 | { "123456789 01234", false }, |
| 51 | { "1234567\n123456", false }, |
| 52 | { "123456\t123456", false }, |
| 53 | { "123456\r123456", false }, |
Neels Hofmeyr | 4b7c791 | 2017-10-07 04:45:01 +0200 | [diff] [blame] | 54 | { NULL, false }, |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | bool test_valid_imsi() |
| 58 | { |
| 59 | int i; |
| 60 | bool pass = true; |
| 61 | bool ok = true; |
| 62 | printf("----- %s\n", __func__); |
| 63 | |
| 64 | for (i = 0; i < ARRAY_SIZE(test_imsis); i++) { |
| 65 | ok = osmo_imsi_str_valid(test_imsis[i].imsi); |
| 66 | pass = pass && (ok == test_imsis[i].expect_ok); |
| 67 | printf("%2d: expect=%s result=%s imsi='%s'\n", |
| 68 | i, BOOL_STR(test_imsis[i].expect_ok), BOOL_STR(ok), |
| 69 | test_imsis[i].imsi); |
| 70 | } |
| 71 | return pass; |
| 72 | } |
| 73 | |
| 74 | static struct { |
| 75 | const char *msisdn; |
| 76 | bool expect_ok; |
| 77 | } test_msisdns[] = { |
| 78 | { "", false }, |
| 79 | { " ", false }, |
| 80 | { "1", true }, |
| 81 | { "123", true }, |
| 82 | { "12345", true }, |
| 83 | { "123456", true }, |
| 84 | { "1234567", true }, |
| 85 | { "1234567890123", true }, |
| 86 | { "123456789012345", true }, |
| 87 | { "000000000000000", true }, |
| 88 | { "999999999999999", true }, |
| 89 | { "1234567890123456", false }, |
| 90 | { "a23456789012345", false }, |
| 91 | { "1234567b9012345", false }, |
| 92 | { "12345678901234c", false }, |
| 93 | { "123456789 01234", false }, |
| 94 | { "1234567\n123456", false }, |
| 95 | { "123456\t123456", false }, |
| 96 | { "123456\r123456", false }, |
Neels Hofmeyr | 4b7c791 | 2017-10-07 04:45:01 +0200 | [diff] [blame] | 97 | { NULL, false }, |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | bool test_valid_msisdn() |
| 101 | { |
| 102 | int i; |
| 103 | bool pass = true; |
| 104 | bool ok = true; |
| 105 | printf("----- %s\n", __func__); |
| 106 | |
| 107 | for (i = 0; i < ARRAY_SIZE(test_msisdns); i++) { |
| 108 | ok = osmo_msisdn_str_valid(test_msisdns[i].msisdn); |
| 109 | pass = pass && (ok == test_msisdns[i].expect_ok); |
| 110 | printf("%2d: expect=%s result=%s msisdn='%s'\n", |
| 111 | i, BOOL_STR(test_msisdns[i].expect_ok), BOOL_STR(ok), |
| 112 | test_msisdns[i].msisdn); |
| 113 | } |
| 114 | return pass; |
| 115 | } |
| 116 | |
| 117 | int main(int argc, char **argv) |
| 118 | { |
| 119 | bool pass = true; |
| 120 | |
| 121 | pass = pass && test_valid_imsi(); |
| 122 | pass = pass && test_valid_msisdn(); |
| 123 | |
| 124 | OSMO_ASSERT(pass); |
| 125 | |
| 126 | return EXIT_SUCCESS; |
| 127 | } |