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