blob: ea08d0d96e98d475c002dbc32310300be4acc6a5 [file] [log] [blame]
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02001/*
2 * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
Harald Weltee08da972017-11-13 01:00:26 +09003 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02004 * All Rights Reserved
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02007 *
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 Hofmeyr721aa6d2018-02-20 21:38:00 +010025#include <errno.h>
26#include <strings.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020027
28#include <osmocom/gsm/gsm23003.h>
29#include <osmocom/core/utils.h>
30
31#define BOOL_STR(b) ((b)? "true" : "false")
32
33static 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 Hofmeyr4b7c7912017-10-07 04:45:01 +020056 { NULL, false },
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020057};
58
59bool 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
76static 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 Hofmeyr4b7c7912017-10-07 04:45:01 +020099 { NULL, false },
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +0200100};
101
102bool 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 Hofmeyr721aa6d2018-02-20 21:38:00 +0100119struct test_mnc_from_str_result {
120 int rc;
121 uint16_t mnc;
122 bool mnc_3_digits;
123};
124
125struct test_mnc_from_str {
126 const char *mnc_str;
127 struct test_mnc_from_str_result expect;
128};
129
130static 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 } },
139 { "-1", { -EINVAL, 65535, false } },
140 { "1000", { -EINVAL, 1000, true } },
141 { "0x", { -EINVAL, 0, false } },
142 { " 23", { -EINVAL, 23, true } }, /* technically not a 3-digit MNC, but it's EINVAL anyway */
143 { "23 ", { -EINVAL, 23, true } },
144 { " 023", { -EINVAL, 23, true } },
145 { "023 ", { -EINVAL, 23, true } },
146 { "023 ", { -EINVAL, 23, true } },
147};
148
149static 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];
157 struct test_mnc_from_str_result result;
158 bool ok;
159
160 result.rc = osmo_mnc_from_str(t->mnc_str, &result.mnc,
161 &result.mnc_3_digits);
Neels Hofmeyr5b8b8c62018-03-01 19:32:11 +0100162 ok = (result.rc == t->expect.rc)
163 && (result.mnc == t->expect.mnc)
164 && (result.mnc_3_digits == t->expect.mnc_3_digits);
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100165 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 Hofmeyr9cd1e742017-10-04 03:15:47 +0200173int main(int argc, char **argv)
174{
175 bool pass = true;
176
177 pass = pass && test_valid_imsi();
178 pass = pass && test_valid_msisdn();
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100179 pass = pass && test_mnc_from_str();
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +0200180
181 OSMO_ASSERT(pass);
182
183 return EXIT_SUCCESS;
184}