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> |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <strings.h> |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 27 | |
| 28 | #include <osmocom/gsm/gsm23003.h> |
| 29 | #include <osmocom/core/utils.h> |
| 30 | |
| 31 | #define BOOL_STR(b) ((b)? "true" : "false") |
| 32 | |
| 33 | static struct { |
| 34 | const char *imsi; |
| 35 | bool expect_ok; |
| 36 | } test_imsis[] = { |
| 37 | { "", false }, |
| 38 | { " ", false }, |
| 39 | { "1", false }, |
| 40 | { "123", false }, |
| 41 | { "12345", false }, |
| 42 | { "123456", true }, |
| 43 | { "1234567", true }, |
| 44 | { "1234567890123", true }, |
| 45 | { "123456789012345", true }, |
| 46 | { "000000000000000", true }, |
| 47 | { "999999999999999", true }, |
| 48 | { "1234567890123456", false }, |
| 49 | { "a23456789012345", false }, |
| 50 | { "1234567b9012345", false }, |
| 51 | { "12345678901234c", false }, |
| 52 | { "123456789 01234", false }, |
| 53 | { "1234567\n123456", false }, |
| 54 | { "123456\t123456", false }, |
| 55 | { "123456\r123456", false }, |
Neels Hofmeyr | 4b7c791 | 2017-10-07 04:45:01 +0200 | [diff] [blame] | 56 | { NULL, false }, |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | bool test_valid_imsi() |
| 60 | { |
| 61 | int i; |
| 62 | bool pass = true; |
| 63 | bool ok = true; |
| 64 | printf("----- %s\n", __func__); |
| 65 | |
| 66 | for (i = 0; i < ARRAY_SIZE(test_imsis); i++) { |
| 67 | ok = osmo_imsi_str_valid(test_imsis[i].imsi); |
| 68 | pass = pass && (ok == test_imsis[i].expect_ok); |
| 69 | printf("%2d: expect=%s result=%s imsi='%s'\n", |
| 70 | i, BOOL_STR(test_imsis[i].expect_ok), BOOL_STR(ok), |
| 71 | test_imsis[i].imsi); |
| 72 | } |
| 73 | return pass; |
| 74 | } |
| 75 | |
| 76 | static struct { |
| 77 | const char *msisdn; |
| 78 | bool expect_ok; |
| 79 | } test_msisdns[] = { |
| 80 | { "", false }, |
| 81 | { " ", false }, |
| 82 | { "1", true }, |
| 83 | { "123", true }, |
| 84 | { "12345", true }, |
| 85 | { "123456", true }, |
| 86 | { "1234567", true }, |
| 87 | { "1234567890123", true }, |
| 88 | { "123456789012345", true }, |
| 89 | { "000000000000000", true }, |
| 90 | { "999999999999999", true }, |
| 91 | { "1234567890123456", false }, |
| 92 | { "a23456789012345", false }, |
| 93 | { "1234567b9012345", false }, |
| 94 | { "12345678901234c", false }, |
| 95 | { "123456789 01234", false }, |
| 96 | { "1234567\n123456", false }, |
| 97 | { "123456\t123456", false }, |
| 98 | { "123456\r123456", false }, |
Neels Hofmeyr | 4b7c791 | 2017-10-07 04:45:01 +0200 | [diff] [blame] | 99 | { NULL, false }, |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | bool test_valid_msisdn() |
| 103 | { |
| 104 | int i; |
| 105 | bool pass = true; |
| 106 | bool ok = true; |
| 107 | printf("----- %s\n", __func__); |
| 108 | |
| 109 | for (i = 0; i < ARRAY_SIZE(test_msisdns); i++) { |
| 110 | ok = osmo_msisdn_str_valid(test_msisdns[i].msisdn); |
| 111 | pass = pass && (ok == test_msisdns[i].expect_ok); |
| 112 | printf("%2d: expect=%s result=%s msisdn='%s'\n", |
| 113 | i, BOOL_STR(test_msisdns[i].expect_ok), BOOL_STR(ok), |
| 114 | test_msisdns[i].msisdn); |
| 115 | } |
| 116 | return pass; |
| 117 | } |
| 118 | |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 119 | struct test_mnc_from_str_result { |
| 120 | int rc; |
| 121 | uint16_t mnc; |
| 122 | bool mnc_3_digits; |
| 123 | }; |
| 124 | |
| 125 | struct test_mnc_from_str { |
| 126 | const char *mnc_str; |
| 127 | struct test_mnc_from_str_result expect; |
| 128 | }; |
| 129 | |
| 130 | static struct test_mnc_from_str test_mnc_from_strs[] = { |
| 131 | { "0", { 0, 0, false } }, |
| 132 | { "00", { 0, 0, false } }, |
| 133 | { "000", { 0, 0, true } }, |
| 134 | { "1", { 0, 1, false } }, |
| 135 | { "01", { 0, 1, false } }, |
| 136 | { "001", { 0, 1, true } }, |
| 137 | { "", { -EINVAL, 0, false } }, |
| 138 | { " ", { -EINVAL, 0, false } }, |
Neels Hofmeyr | 20f7d0e | 2018-03-05 04:19:21 +0100 | [diff] [blame] | 139 | { "-1", { -EINVAL, 0, false } }, |
| 140 | { "1000", { -EINVAL, 0, false } }, |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 141 | { "0x", { -EINVAL, 0, false } }, |
Neels Hofmeyr | 20f7d0e | 2018-03-05 04:19:21 +0100 | [diff] [blame] | 142 | { " 23", { -EINVAL, 0, false } }, |
| 143 | { "23 ", { -EINVAL, 0, false } }, |
| 144 | { " 023", { -EINVAL, 0, false } }, |
| 145 | { "023 ", { -EINVAL, 0, false } }, |
| 146 | { "023 ", { -EINVAL, 0, false } }, |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static bool test_mnc_from_str() |
| 150 | { |
| 151 | int i; |
| 152 | bool pass = true; |
| 153 | printf("----- %s\n", __func__); |
| 154 | |
| 155 | for (i = 0; i < ARRAY_SIZE(test_mnc_from_strs); i++) { |
| 156 | struct test_mnc_from_str *t = &test_mnc_from_strs[i]; |
Neels Hofmeyr | 20f7d0e | 2018-03-05 04:19:21 +0100 | [diff] [blame] | 157 | struct test_mnc_from_str_result result = {}; |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 158 | bool ok; |
| 159 | |
| 160 | result.rc = osmo_mnc_from_str(t->mnc_str, &result.mnc, |
| 161 | &result.mnc_3_digits); |
Neels Hofmeyr | 5b8b8c6 | 2018-03-01 19:32:11 +0100 | [diff] [blame] | 162 | ok = (result.rc == t->expect.rc) |
| 163 | && (result.mnc == t->expect.mnc) |
| 164 | && (result.mnc_3_digits == t->expect.mnc_3_digits); |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 165 | printf("%2d: \"%s\" rc=%d mnc=%u mnc_3_digits=%u %s\n", |
| 166 | i, osmo_escape_str(t->mnc_str, -1), result.rc, result.mnc, result.mnc_3_digits, |
| 167 | ok ? "pass" : "FAIL"); |
| 168 | pass = pass && ok; |
| 169 | } |
| 170 | return pass; |
| 171 | } |
| 172 | |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 173 | int main(int argc, char **argv) |
| 174 | { |
| 175 | bool pass = true; |
| 176 | |
| 177 | pass = pass && test_valid_imsi(); |
| 178 | pass = pass && test_valid_msisdn(); |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 179 | pass = pass && test_mnc_from_str(); |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 180 | |
| 181 | OSMO_ASSERT(pass); |
| 182 | |
| 183 | return EXIT_SUCCESS; |
| 184 | } |