Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
Neels Hofmeyr | 30856ca | 2020-06-16 13:17:20 +0200 | [diff] [blame] | 21 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 22 | |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <stdio.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 28 | #include <osmocom/gsm/gsm48_ie.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/gsm48.h> |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm48_arfcn_range_encode.h> |
Pau Espin Pedrol | b99f4ca | 2019-10-31 13:35:22 +0100 | [diff] [blame] | 31 | #include <osmocom/gsm/gsm_utils.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/mncc.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 33 | #include <osmocom/core/backtrace.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
| 35 | #include <osmocom/core/msgb.h> |
| 36 | |
| 37 | |
| 38 | static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 }; |
| 39 | |
| 40 | static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = { |
| 41 | .transfer = GSM48_BCAP_ITCAP_UNR_DIG_INF, |
| 42 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 43 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 44 | .radio = GSM48_BCAP_RRQ_FR_ONLY, |
| 45 | .speech_ver[0]= -1, |
| 46 | .data = { |
| 47 | .rate_adaption = GSM48_BCAP_RA_V110_X30, |
| 48 | .sig_access = GSM48_BCAP_SA_I440_I450, |
| 49 | .async = 1, |
| 50 | .nr_stop_bits = 1, |
| 51 | .nr_data_bits = 8, |
| 52 | .user_rate = GSM48_BCAP_UR_9600, |
| 53 | .parity = GSM48_BCAP_PAR_NONE, |
| 54 | .interm_rate = GSM48_BCAP_IR_16k, |
| 55 | .transp = GSM48_BCAP_TR_TRANSP, |
| 56 | .modem_type = GSM48_BCAP_MT_NONE, |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 }; |
| 61 | |
| 62 | static const struct gsm_mncc_bearer_cap bcap_speech_all = { |
| 63 | .transfer = GSM48_BCAP_ITCAP_SPEECH, |
| 64 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 65 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 66 | .radio = GSM48_BCAP_RRQ_DUAL_FR, |
| 67 | .speech_ver = { |
| 68 | 4, 2, 0, 5, 1, -1, |
| 69 | }, |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | struct bcap_test { |
| 74 | const uint8_t *lv; |
| 75 | const struct gsm_mncc_bearer_cap *bc; |
| 76 | const char *name; |
| 77 | }; |
| 78 | |
| 79 | static const struct bcap_test bcap_tests[] = { |
| 80 | { csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" }, |
| 81 | { speech_all_lv, &bcap_speech_all, "Speech, all codecs" }, |
| 82 | }; |
| 83 | |
| 84 | static int test_bearer_cap() |
| 85 | { |
| 86 | struct gsm_mncc_bearer_cap bc; |
| 87 | int i, rc; |
| 88 | |
| 89 | for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) { |
| 90 | struct msgb *msg = msgb_alloc(100, "test"); |
| 91 | int lv_len; |
| 92 | |
| 93 | memset(&bc, 0, sizeof(bc)); |
| 94 | |
| 95 | /* test decoding */ |
| 96 | rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv); |
| 97 | if (rc < 0) { |
| 98 | fprintf(stderr, "Error decoding %s\n", |
| 99 | bcap_tests[i].name); |
| 100 | return rc; |
| 101 | } |
| 102 | if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) { |
| 103 | fprintf(stderr, "Incorrect decoded result of %s:\n", |
| 104 | bcap_tests[i].name); |
| 105 | fprintf(stderr, " should: %s\n", |
| 106 | osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc))); |
| 107 | fprintf(stderr, " is: %s\n", |
| 108 | osmo_hexdump((uint8_t *) &bc, sizeof(bc))); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | /* also test re-encode? */ |
| 113 | rc = gsm48_encode_bearer_cap(msg, 1, &bc); |
| 114 | if (rc < 0) { |
| 115 | fprintf(stderr, "Error encoding %s\n", |
| 116 | bcap_tests[i].name); |
| 117 | return rc; |
| 118 | } |
| 119 | lv_len = bcap_tests[i].lv[0]+1; |
| 120 | if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) { |
| 121 | fprintf(stderr, "Incorrect encoded result of %s:\n", |
| 122 | bcap_tests[i].name); |
| 123 | fprintf(stderr, " should: %s\n", |
| 124 | osmo_hexdump(bcap_tests[i].lv, lv_len)); |
| 125 | fprintf(stderr, " is: %s\n", |
| 126 | osmo_hexdump(msg->data, msg->len)); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | printf("Test `%s' passed\n", bcap_tests[i].name); |
| 131 | msgb_free(msg); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 137 | static inline void dump_ra(const struct gprs_ra_id *raid) |
| 138 | { |
Neels Hofmeyr | dbb2513 | 2018-02-20 15:12:23 +0100 | [diff] [blame] | 139 | printf("%s%s\n", osmo_rai_name(raid), raid->mnc_3_digits ? " (3-digit MNC)" : ""); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static inline void check_ra(const struct gprs_ra_id *raid) |
| 143 | { |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 144 | struct gsm48_ra_id ra; |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 145 | struct gprs_ra_id raid0 = { |
| 146 | .mnc = 0, |
| 147 | .mcc = 0, |
| 148 | .lac = 0, |
| 149 | .rac = 0, |
| 150 | }; |
| 151 | |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 152 | gsm48_encode_ra(&ra, raid); |
| 153 | printf("Constructed RA:\n"); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 154 | |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 155 | gsm48_parse_ra(&raid0, (const uint8_t *)&ra); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 156 | dump_ra(raid); |
Neels Hofmeyr | c38b32d | 2018-02-20 15:13:18 +0100 | [diff] [blame] | 157 | printf("MCC+MNC in BCD: %s\n", osmo_hexdump(ra.digits, sizeof(ra.digits))); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 158 | dump_ra(&raid0); |
| 159 | printf("RA test..."); |
Neels Hofmeyr | dbb2513 | 2018-02-20 15:12:23 +0100 | [diff] [blame] | 160 | if (raid->mnc != raid0.mnc || raid->mcc != raid0.mcc || raid->lac != raid0.lac || raid->rac != raid0.rac |
| 161 | || (raid->mnc_3_digits || raid->mnc > 99) != raid0.mnc_3_digits) |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 162 | printf("FAIL\n"); |
| 163 | else |
| 164 | printf("passed\n"); |
| 165 | } |
| 166 | |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 167 | static inline void check_lai(const struct gprs_ra_id *raid) |
| 168 | { |
| 169 | int rc; |
| 170 | struct gsm48_loc_area_id lai = {}; |
| 171 | struct gprs_ra_id decoded = {}; |
| 172 | struct gprs_ra_id _laid = *raid; |
| 173 | struct gprs_ra_id *laid = &_laid; |
| 174 | laid->rac = 0; |
| 175 | |
| 176 | printf("- gsm48_generate_lai() from "); dump_ra(laid); |
| 177 | |
| 178 | gsm48_generate_lai(&lai, laid->mcc, laid->mnc, laid->lac); |
| 179 | printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai))); |
| 180 | rc = gsm48_decode_lai(&lai, &decoded.mcc, &decoded.mnc, &decoded.lac); |
| 181 | if (rc) { |
| 182 | printf(" gsm48_decode_lai() returned %d --> FAIL\n", rc); |
| 183 | return; |
| 184 | } |
| 185 | printf(" gsm48_decode_lai() gives "); dump_ra(&decoded); |
| 186 | if (decoded.mcc == laid->mcc |
| 187 | && decoded.mnc == laid->mnc |
| 188 | && decoded.lac == laid->lac) |
| 189 | printf(" passed\n"); |
| 190 | else |
| 191 | printf(" FAIL\n"); |
| 192 | } |
| 193 | |
Neels Hofmeyr | 4566f4e | 2018-02-20 22:19:56 +0100 | [diff] [blame] | 194 | static inline void dump_lai(const struct osmo_location_area_id *lai) |
| 195 | { |
| 196 | printf("%s%s\n", osmo_lai_name(lai), lai->plmn.mnc_3_digits ? " (3-digit MNC)" : ""); |
| 197 | } |
| 198 | |
| 199 | static inline void check_lai2(const struct gprs_ra_id *raid) |
| 200 | { |
| 201 | struct gsm48_loc_area_id lai = {}; |
| 202 | struct osmo_location_area_id decoded = {}; |
| 203 | struct osmo_location_area_id laid = { |
| 204 | .plmn = { |
| 205 | .mcc = raid->mcc, |
| 206 | .mnc = raid->mnc, |
| 207 | .mnc_3_digits = raid->mnc_3_digits, |
| 208 | }, |
| 209 | .lac = raid->lac, |
| 210 | }; |
| 211 | |
| 212 | printf("- gsm48_generate_lai2() from "); dump_lai(&laid); |
| 213 | |
| 214 | gsm48_generate_lai2(&lai, &laid); |
| 215 | printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai))); |
| 216 | gsm48_decode_lai2(&lai, &decoded); |
| 217 | printf(" gsm48_decode_lai2() gives "); dump_lai(&decoded); |
| 218 | if (decoded.plmn.mcc == laid.plmn.mcc |
| 219 | && decoded.plmn.mnc == laid.plmn.mnc |
| 220 | && decoded.lac == laid.lac |
| 221 | && decoded.plmn.mnc_3_digits == (laid.plmn.mnc_3_digits || laid.plmn.mnc > 99)) |
| 222 | printf(" passed\n"); |
| 223 | else |
| 224 | printf(" FAIL\n"); |
| 225 | } |
| 226 | |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 227 | static struct gprs_ra_id test_ra_cap_items[] = { |
| 228 | { |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 229 | .mcc = 77, |
Neels Hofmeyr | 0bf93a6 | 2018-02-20 22:06:56 +0100 | [diff] [blame] | 230 | .mnc = 121, |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 231 | .lac = 666, |
| 232 | .rac = 5, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 233 | }, |
| 234 | { |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 235 | .mcc = 84, |
Neels Hofmeyr | 0bf93a6 | 2018-02-20 22:06:56 +0100 | [diff] [blame] | 236 | .mnc = 98, |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 237 | .lac = 11, |
| 238 | .rac = 89, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 239 | }, |
Neels Hofmeyr | b9fd7eb | 2018-02-20 15:14:03 +0100 | [diff] [blame] | 240 | { |
| 241 | .mcc = 0, |
| 242 | .mnc = 0, |
| 243 | .lac = 0, |
| 244 | .rac = 0, |
Neels Hofmeyr | 6c7b3e2 | 2018-02-20 22:20:42 +0100 | [diff] [blame] | 245 | .mnc_3_digits = false, |
| 246 | /* expecting 000-00, BCD = 00 f0 00 */ |
| 247 | }, |
| 248 | { |
| 249 | .mcc = 0, |
| 250 | .mnc = 0, |
| 251 | .lac = 0, |
| 252 | .rac = 0, |
| 253 | .mnc_3_digits = true, |
| 254 | /* expecting 000-000, BCD = 00 00 00 */ |
Neels Hofmeyr | b9fd7eb | 2018-02-20 15:14:03 +0100 | [diff] [blame] | 255 | }, |
| 256 | { |
| 257 | .mcc = 999, |
| 258 | .mnc = 999, |
| 259 | .lac = 65535, |
| 260 | .rac = 255, |
| 261 | }, |
Neels Hofmeyr | 6c7b3e2 | 2018-02-20 22:20:42 +0100 | [diff] [blame] | 262 | { |
| 263 | .mcc = 1, |
| 264 | .mnc = 2, |
| 265 | .lac = 23, |
| 266 | .rac = 42, |
| 267 | .mnc_3_digits = false, |
| 268 | /* expecting 001-02, BCD = 00 f1 20 */ |
| 269 | }, |
| 270 | { |
| 271 | .mcc = 1, |
| 272 | .mnc = 2, |
| 273 | .lac = 23, |
| 274 | .rac = 42, |
| 275 | .mnc_3_digits = true, |
| 276 | /* expecting 001-002, BCD = 00 21 00 */ |
| 277 | }, |
| 278 | { |
| 279 | .mcc = 12, |
| 280 | .mnc = 34, |
| 281 | .lac = 56, |
| 282 | .rac = 78, |
| 283 | .mnc_3_digits = false, |
| 284 | /* expecting 012-34, BCD = 10 f2 43 */ |
| 285 | }, |
| 286 | { |
| 287 | .mcc = 12, |
| 288 | .mnc = 34, |
| 289 | .lac = 23, |
| 290 | .rac = 42, |
| 291 | .mnc_3_digits = true, |
| 292 | /* expecting 012-034, BCD = 10 42 30 */ |
| 293 | }, |
| 294 | { |
| 295 | .mcc = 123, |
| 296 | .mnc = 456, |
| 297 | .lac = 23, |
| 298 | .rac = 42, |
| 299 | .mnc_3_digits = false, |
| 300 | /* expecting 123-456, BCD = 21 63 54 (false flag has no effect) */ |
| 301 | }, |
| 302 | { |
| 303 | .mcc = 123, |
| 304 | .mnc = 456, |
| 305 | .lac = 23, |
| 306 | .rac = 42, |
| 307 | .mnc_3_digits = true, |
| 308 | /* expecting 123-456, BCD = 21 63 54 (same) */ |
| 309 | }, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 310 | }; |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 311 | |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 312 | static void test_ra_cap(void) |
| 313 | { |
| 314 | int i; |
| 315 | |
| 316 | for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) |
| 317 | check_ra(&test_ra_cap_items[i]); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 320 | static void test_lai_encode_decode(void) |
| 321 | { |
| 322 | int i; |
| 323 | |
| 324 | for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) { |
| 325 | check_lai(&test_ra_cap_items[i]); |
Neels Hofmeyr | 4566f4e | 2018-02-20 22:19:56 +0100 | [diff] [blame] | 326 | check_lai2(&test_ra_cap_items[i]); |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 330 | static void test_mid_from_tmsi(void) |
| 331 | { |
| 332 | static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd }; |
| 333 | |
| 334 | |
| 335 | uint32_t tmsi = 0xAABBCCDD; |
| 336 | uint8_t buf[3 + sizeof(uint32_t)]; |
| 337 | |
| 338 | printf("Simple TMSI encoding test...."); |
| 339 | |
| 340 | memset(&buf, 0xFE, sizeof(buf)); |
| 341 | gsm48_generate_mid_from_tmsi(buf, tmsi); |
| 342 | |
| 343 | OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0); |
| 344 | printf("passed\n"); |
| 345 | } |
| 346 | |
Max | d55d7d4 | 2018-02-15 11:27:18 +0100 | [diff] [blame] | 347 | static void test_mid_from_imsi(void) |
| 348 | { |
| 349 | char *imsi = "901700000004620"; |
| 350 | uint8_t buf[10], len; |
| 351 | |
| 352 | printf("Simple IMSI encoding test...."); |
| 353 | |
| 354 | len = gsm48_generate_mid_from_imsi(buf, imsi); |
| 355 | |
| 356 | printf("passed: [%u] %s\n", len, osmo_hexdump(buf, len)); |
| 357 | } |
| 358 | |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 359 | struct test_mid_encode_decode_test { |
| 360 | uint8_t mi_type; |
| 361 | const char *mi_str; |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 362 | const char *mi_name; |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 363 | size_t str_size; |
| 364 | const char *expect_mi_tlv_hex; |
| 365 | const char *expect_str; |
| 366 | int expect_rc; |
| 367 | }; |
| 368 | |
| 369 | static const struct test_mid_encode_decode_test test_mid_encode_decode_tests[] = { |
| 370 | { |
| 371 | .mi_type = GSM_MI_TYPE_IMSI, |
| 372 | .mi_str = "123456789012345", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 373 | .mi_name = "IMSI-123456789012345", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 374 | .expect_mi_tlv_hex = "17081932547698103254", |
| 375 | }, |
| 376 | { |
| 377 | .mi_type = GSM_MI_TYPE_IMSI, |
| 378 | .mi_str = "12345678901234", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 379 | .mi_name = "IMSI-12345678901234", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 380 | .expect_mi_tlv_hex = "170811325476981032f4", |
| 381 | }, |
| 382 | { |
| 383 | .mi_type = GSM_MI_TYPE_IMSI, |
| 384 | .mi_str = "423423", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 385 | .mi_name = "IMSI-423423", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 386 | .expect_mi_tlv_hex = "1704413224f3", |
| 387 | }, |
| 388 | { |
| 389 | .mi_type = GSM_MI_TYPE_IMSI | GSM_MI_ODD, |
| 390 | .mi_str = "423423", |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 391 | .mi_name = "IMSI-423423", |
| 392 | .expect_mi_tlv_hex = "1704413224f3", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 393 | }, |
| 394 | { |
| 395 | .mi_type = GSM_MI_TYPE_IMSI, |
| 396 | .mi_str = "4234235", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 397 | .mi_name = "IMSI-4234235", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 398 | .expect_mi_tlv_hex = "170449322453", |
| 399 | }, |
| 400 | { |
| 401 | .mi_type = GSM_MI_TYPE_IMSI, |
| 402 | .mi_str = "4234235", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 403 | .mi_name = "IMSI-4234235", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 404 | .expect_mi_tlv_hex = "170449322453", |
| 405 | .str_size = 4, |
| 406 | .expect_str = "423", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 407 | }, |
| 408 | { |
| 409 | .mi_type = GSM_MI_TYPE_IMEI, |
| 410 | .mi_str = "123456789012345", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 411 | .mi_name = "IMEI-123456789012345", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 412 | .expect_mi_tlv_hex = "17081a32547698103254", |
| 413 | }, |
| 414 | { |
| 415 | .mi_type = GSM_MI_TYPE_IMEI, |
| 416 | .mi_str = "98765432109876", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 417 | .mi_name = "IMEI-98765432109876", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 418 | .expect_mi_tlv_hex = "170892785634129078f6", |
| 419 | }, |
| 420 | { |
| 421 | .mi_type = GSM_MI_TYPE_IMEI, |
| 422 | .mi_str = "987654321098765", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 423 | .mi_name = "IMEI-987654321098765", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 424 | .expect_mi_tlv_hex = "17089a78563412907856", |
| 425 | }, |
| 426 | { |
| 427 | .mi_type = GSM_MI_TYPE_IMEISV, |
Harald Welte | 1317771 | 2019-01-20 13:41:26 +0100 | [diff] [blame] | 428 | .mi_str = "9876543210987654", |
| 429 | .mi_name = "IMEI-SV-9876543210987654", |
| 430 | .expect_mi_tlv_hex = "17099378563412907856f4", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 431 | }, |
| 432 | { |
| 433 | .mi_type = GSM_MI_TYPE_IMEISV, |
Harald Welte | 1317771 | 2019-01-20 13:41:26 +0100 | [diff] [blame] | 434 | .mi_str = "9876543210987654", |
| 435 | .mi_name = "IMEI-SV-9876543210987654", |
| 436 | .expect_mi_tlv_hex = "17099378563412907856f4", |
| 437 | .str_size = 17, |
| 438 | .expect_str = "9876543210987654", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 439 | }, |
| 440 | { |
| 441 | /* gsm48 treats TMSI as decimal string */ |
| 442 | .mi_type = GSM_MI_TYPE_TMSI, |
| 443 | .mi_str = "305419896", /* 0x12345678 as decimal */ |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 444 | .mi_name = "TMSI-0x12345678", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 445 | .expect_mi_tlv_hex = "1705f412345678", |
| 446 | .expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 447 | }, |
| 448 | { |
| 449 | .mi_type = GSM_MI_TYPE_TMSI, |
| 450 | .mi_str = "12648430", /* 0xc0ffee as decimal */ |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 451 | .mi_name = "TMSI-0x00C0FFEE", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 452 | .expect_mi_tlv_hex = "1705f400c0ffee", |
| 453 | .expect_rc = 8, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 454 | }, |
| 455 | { |
| 456 | .mi_type = GSM_MI_TYPE_TMSI, |
| 457 | .mi_str = "0", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 458 | .mi_name = "TMSI-0x00000000", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 459 | .expect_mi_tlv_hex = "1705f400000000", |
| 460 | .expect_rc = 1, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 461 | }, |
| 462 | { |
| 463 | /* gsm48 treats TMSI as decimal string */ |
| 464 | .mi_type = GSM_MI_TYPE_TMSI, |
| 465 | .mi_str = "305419896", /* 0x12345678 as decimal */ |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 466 | .mi_name = "TMSI-0x12345678", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 467 | .expect_mi_tlv_hex = "1705f412345678", |
| 468 | .str_size = 5, |
| 469 | .expect_str = "3054", |
| 470 | .expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns would-be strlen() like snprintf()! */ |
| 471 | }, |
| 472 | { |
| 473 | .mi_type = GSM_MI_TYPE_NONE, |
| 474 | .mi_str = "123", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 475 | .mi_name = "unknown", |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 476 | .expect_mi_tlv_hex = "17021832", /* encoding invalid MI type */ |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 477 | .expect_str = "", |
| 478 | }, |
| 479 | { |
| 480 | .mi_type = GSM_MI_TYPE_NONE, |
| 481 | .mi_str = "1234", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 482 | .mi_name = "unknown", |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 483 | .expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */ |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 484 | .expect_str = "", |
| 485 | }, |
| 486 | { |
| 487 | .mi_type = GSM_MI_ODD, |
| 488 | .mi_str = "1234", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 489 | .mi_name = "unknown", |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 490 | .expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */ |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 491 | .expect_str = "", |
| 492 | }, |
| 493 | }; |
| 494 | |
| 495 | static void test_mid_encode_decode(void) |
| 496 | { |
| 497 | int i; |
| 498 | |
| 499 | printf("\nTesting Mobile Identity conversions\n"); |
| 500 | |
| 501 | for (i = 0; i < ARRAY_SIZE(test_mid_encode_decode_tests); i++) { |
| 502 | const struct test_mid_encode_decode_test *t = &test_mid_encode_decode_tests[i]; |
| 503 | uint8_t tlv_buf[64]; |
| 504 | uint8_t *mi_buf; |
| 505 | int tlv_len; |
| 506 | int mi_len; |
| 507 | const char *tlv_hex; |
| 508 | char str[64] = {}; |
| 509 | size_t str_size = t->str_size ? : sizeof(str); |
| 510 | const char *expect_str = t->expect_str ? : t->mi_str; |
| 511 | int expect_rc = t->expect_rc ? : strlen(expect_str)+1; |
| 512 | int rc; |
| 513 | int str_len; |
| 514 | |
| 515 | printf("- %s %s\n", gsm48_mi_type_name(t->mi_type), t->mi_str); |
| 516 | if (t->mi_type == GSM_MI_TYPE_TMSI) |
| 517 | tlv_len = gsm48_generate_mid_from_tmsi(tlv_buf, (uint32_t)atoll(t->mi_str)); |
| 518 | else |
| 519 | tlv_len = gsm48_generate_mid(tlv_buf, t->mi_str, t->mi_type); |
| 520 | tlv_hex = osmo_hexdump_nospc(tlv_buf, tlv_len); |
| 521 | |
| 522 | printf(" -> MI-TLV-hex='%s'\n", tlv_hex); |
| 523 | if (t->expect_mi_tlv_hex && strcmp(tlv_hex, t->expect_mi_tlv_hex)) { |
| 524 | printf(" ERROR: expected '%s'\n", t->expect_mi_tlv_hex); |
| 525 | } |
| 526 | |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 527 | /* skip the GSM48_IE_MOBILE_ID tag and length */ |
| 528 | mi_buf = tlv_buf + 2; |
| 529 | mi_len = tlv_len - 2; |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 530 | |
| 531 | rc = gsm48_mi_to_string(str, str_size, mi_buf, mi_len); |
| 532 | printf(" -> MI-str=%s rc=%d\n", osmo_quote_str(str, -1), rc); |
| 533 | if (strcmp(str, expect_str)) |
| 534 | printf(" ERROR: expected MI-str=%s\n", osmo_quote_str(expect_str, -1)); |
| 535 | if (rc != expect_rc) |
| 536 | printf(" ERROR: expected rc=%d\n", expect_rc); |
| 537 | |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 538 | if (t->mi_name) { |
| 539 | const char *mi_name = osmo_mi_name(mi_buf, mi_len); |
| 540 | printf(" -> MI-name=%s\n", osmo_quote_str(mi_name, -1)); |
| 541 | if (strcmp(mi_name, t->mi_name)) |
| 542 | printf(" ERROR: expected MI-name=%s\n", osmo_quote_str(t->mi_name, -1)); |
| 543 | } |
| 544 | |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 545 | /* Now make sure the resulting string is always '\0' terminated. |
| 546 | * The above started out with a zeroed buffer, now repeat with a tainted one. */ |
| 547 | str_len = strlen(str); |
| 548 | str[str_len] = '!'; |
| 549 | gsm48_mi_to_string(str, str_size, mi_buf, mi_len); |
| 550 | if (strlen(str) != str_len) |
| 551 | printf(" ERROR: resulting string is not explicitly nul terminated\n"); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | static const uint8_t test_mid_decode_zero_length_types[] = { GSM_MI_TYPE_IMSI, GSM_MI_TYPE_TMSI, GSM_MI_TYPE_NONE }; |
| 556 | |
| 557 | static void test_mid_decode_zero_length(void) |
| 558 | { |
| 559 | int odd; |
| 560 | uint8_t valid_mi[64]; |
| 561 | int valid_mi_len; |
| 562 | |
| 563 | printf("\nDecoding zero length Mobile Identities\n"); |
| 564 | |
| 565 | /* IMSI = 123456789012345 */ |
| 566 | valid_mi_len = osmo_hexparse("1932547698103254", valid_mi, sizeof(valid_mi)); |
| 567 | |
| 568 | for (odd = 0; odd <= 1; odd++) { |
| 569 | int i; |
| 570 | for (i = 0; i < ARRAY_SIZE(test_mid_decode_zero_length_types); i++) { |
| 571 | uint8_t mi_type = test_mid_decode_zero_length_types[i] | (odd ? GSM_MI_ODD : 0); |
| 572 | char str[8] = {}; |
| 573 | int rc; |
| 574 | |
| 575 | printf("- MI type: %s%s\n", gsm48_mi_type_name(mi_type & GSM_MI_TYPE_MASK), |
| 576 | odd ? " | GSM_MI_ODD":""); |
| 577 | valid_mi[0] = (valid_mi[0] & 0xf0) | mi_type; |
| 578 | |
| 579 | printf(" - writing to zero-length string:\n"); |
| 580 | memset(str, '!', sizeof(str) - 1); |
| 581 | rc = gsm48_mi_to_string(str, 0, valid_mi, valid_mi_len); |
| 582 | printf(" rc=%d\n", rc); |
| 583 | if (str[0] == '!') |
| 584 | printf(" nothing written\n"); |
| 585 | else |
| 586 | printf(" ERROR: Wrote to invalid memory!\n"); |
| 587 | |
| 588 | printf(" - writing to 1-byte-length string:\n"); |
| 589 | memset(str, '!', sizeof(str) - 1); |
| 590 | rc = gsm48_mi_to_string(str, 1, valid_mi, valid_mi_len); |
| 591 | printf(" rc=%d\n", rc); |
| 592 | if (str[0] == '\0') |
| 593 | printf(" returned empty string\n"); |
| 594 | else if (str[0] == '!') |
| 595 | printf(" ERROR: nothing written, expected nul-terminated empty string\n"); |
| 596 | else |
| 597 | printf(" ERROR: Wrote unexpected string %s\n", osmo_quote_str(str, 5)); |
| 598 | if (str[1] != '!') |
| 599 | printf(" ERROR: Wrote to invalid memory!\n"); |
| 600 | |
| 601 | printf(" - decode zero-length mi:\n"); |
| 602 | memset(str, '!', sizeof(str) - 1); |
| 603 | rc = gsm48_mi_to_string(str, sizeof(str), valid_mi, 0); |
| 604 | printf(" rc=%d\n", rc); |
| 605 | if (str[0] == '\0') |
| 606 | printf(" returned empty string\n"); |
| 607 | else if (str[0] == '!') |
| 608 | printf(" ERROR: nothing written, expected nul-terminated empty string\n"); |
| 609 | else |
| 610 | printf(" ERROR: expected empty string, got output string: %s\n", osmo_quote_str(str, -1)); |
| 611 | } |
| 612 | } |
| 613 | printf("\n"); |
| 614 | } |
| 615 | |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 616 | struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex) |
| 617 | { |
| 618 | struct msgb *msg = msgb_alloc_headroom(size, 4, label); |
| 619 | OSMO_ASSERT(msg); |
| 620 | msg->l3h = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg))); |
| 621 | return msg; |
| 622 | } |
| 623 | |
| 624 | struct mobile_identity_tc { |
| 625 | const char *label; |
| 626 | const char *compl_l3_msg; |
| 627 | int expect_rc; |
| 628 | struct osmo_mobile_identity expect_mi; |
| 629 | }; |
| 630 | |
| 631 | /* Some Complete Layer 3 messages copied from real GSM network traces. */ |
| 632 | struct mobile_identity_tc mobile_identity_tests[] = { |
| 633 | { |
| 634 | .label = "LU with IMSI 901700000004620", |
| 635 | .compl_l3_msg = "050802008168000130" "089910070000006402", |
| 636 | .expect_mi = { |
| 637 | .type = GSM_MI_TYPE_IMSI, |
| 638 | .imsi = "901700000004620", |
| 639 | }, |
| 640 | }, |
| 641 | { |
| 642 | .label = "LU with TMSI 0x0980ad8a", |
| 643 | .compl_l3_msg = "05084262f224002a50" "05f40980ad8a", |
| 644 | .expect_mi = { |
| 645 | .type = GSM_MI_TYPE_TMSI, |
| 646 | .tmsi = 0x0980ad8a, |
| 647 | }, |
| 648 | }, |
| 649 | { |
| 650 | .label = "LU with invalid MI type", |
| 651 | .compl_l3_msg = "050802008168000130" "089d10070000006402", |
| 652 | .expect_rc = -EINVAL, |
| 653 | }, |
| 654 | { |
| 655 | .label = "LU with truncated IMSI MI", |
| 656 | .compl_l3_msg = "050802008168000130" "0899100700000064", |
| 657 | .expect_rc = -EBADMSG, |
| 658 | }, |
| 659 | { |
| 660 | .label = "LU with too short IMSI MI (12345)", |
| 661 | .compl_l3_msg = "050802008168000130" "03193254", |
| 662 | .expect_rc = -EBADMSG, |
| 663 | }, |
| 664 | { |
| 665 | .label = "LU with just long enough IMSI MI 123456", |
| 666 | .compl_l3_msg = "050802008168000130" "04113254f6", |
| 667 | .expect_mi = { |
| 668 | .type = GSM_MI_TYPE_IMSI, |
| 669 | .imsi = "123456", |
| 670 | }, |
| 671 | }, |
| 672 | { |
| 673 | .label = "LU with max length IMSI MI 123456789012345", |
| 674 | .compl_l3_msg = "050802008168000130" "081932547698103254", |
| 675 | .expect_mi = { |
| 676 | .type = GSM_MI_TYPE_IMSI, |
| 677 | .imsi = "123456789012345", |
| 678 | }, |
| 679 | }, |
| 680 | { |
| 681 | .label = "LU with just too long IMSI MI 1234567890123456", |
| 682 | .compl_l3_msg = "050802008168000130" "091132547698103254f6", |
| 683 | .expect_rc = -EBADMSG, |
| 684 | }, |
| 685 | { |
| 686 | .label = "LU with truncated TMSI MI", |
| 687 | .compl_l3_msg = "05084262f224002a50" "05f40980ad", |
| 688 | .expect_rc = -EBADMSG, |
| 689 | }, |
| 690 | { |
| 691 | .label = "LU with odd length TMSI", |
| 692 | .compl_l3_msg = "05084262f224002a50" "05fc0980ad8a", |
| 693 | .expect_rc = -EBADMSG, |
| 694 | }, |
| 695 | { |
| 696 | .label = "LU with too long TMSI MI", |
| 697 | .compl_l3_msg = "05084262f224002a50" "06f40980ad23", |
| 698 | .expect_rc = -EBADMSG, |
| 699 | }, |
| 700 | { |
| 701 | .label = "LU with too short TMSI", |
| 702 | .compl_l3_msg = "05084262f224002a50" "04f480ad8a", |
| 703 | .expect_rc = -EBADMSG, |
| 704 | }, |
| 705 | { |
| 706 | .label = "CM Service Request with IMSI 123456", |
| 707 | .compl_l3_msg = "052401035058a6" "04113254f6", |
| 708 | .expect_mi = { |
| 709 | .type = GSM_MI_TYPE_IMSI, |
| 710 | .imsi = "123456", |
| 711 | }, |
| 712 | }, |
| 713 | { |
| 714 | .label = "CM Service Request with TMSI 0x5a42e404", |
| 715 | .compl_l3_msg = "052401035058a6" "05f45a42e404", |
| 716 | .expect_mi = { |
| 717 | .type = GSM_MI_TYPE_TMSI, |
| 718 | .tmsi = 0x5a42e404, |
| 719 | }, |
| 720 | }, |
| 721 | { |
| 722 | .label = "CM Service Request with shorter CM2, with IMSI 123456", |
| 723 | .compl_l3_msg = "052401025058" "04113254f6", |
| 724 | .expect_mi = { |
| 725 | .type = GSM_MI_TYPE_IMSI, |
| 726 | .imsi = "123456", |
| 727 | }, |
| 728 | }, |
| 729 | { |
| 730 | .label = "CM Service Request with longer CM2, with IMSI 123456", |
| 731 | .compl_l3_msg = "052401055058a62342" "04113254f6", |
| 732 | .expect_mi = { |
| 733 | .type = GSM_MI_TYPE_IMSI, |
| 734 | .imsi = "123456", |
| 735 | }, |
| 736 | }, |
| 737 | { |
| 738 | .label = "CM Service Request with shorter CM2, with TMSI 0x00000000", |
| 739 | .compl_l3_msg = "052401025058" "05f400000000", |
| 740 | .expect_mi = { |
| 741 | .type = GSM_MI_TYPE_TMSI, |
| 742 | .tmsi = 0, |
| 743 | }, |
| 744 | }, |
| 745 | { |
| 746 | .label = "CM Service Request with invalid MI type", |
| 747 | .compl_l3_msg = "052401035058a6" "089d10070000006402", |
| 748 | .expect_rc = -EINVAL, |
| 749 | }, |
| 750 | { |
| 751 | .label = "CM Service Request with truncated IMSI MI", |
| 752 | .compl_l3_msg = "052401035058a6" "0899100700000064", |
| 753 | .expect_rc = -EBADMSG, |
| 754 | }, |
| 755 | { |
| 756 | .label = "CM Service Request with truncated TMSI MI", |
| 757 | .compl_l3_msg = "0524010150" "05f40980ad", |
| 758 | .expect_rc = -EBADMSG, |
| 759 | }, |
| 760 | { |
| 761 | .label = "CM Service Request with odd length TMSI", |
| 762 | .compl_l3_msg = "052401045058a623" "05fc0980ad8a", |
| 763 | .expect_rc = -EBADMSG, |
| 764 | }, |
| 765 | { |
| 766 | .label = "CM Service Request with too long TMSI MI", |
| 767 | .compl_l3_msg = "052401035058a6" "06f40980ad23", |
| 768 | .expect_rc = -EBADMSG, |
| 769 | }, |
| 770 | { |
| 771 | .label = "CM Service Request with too short TMSI", |
| 772 | .compl_l3_msg = "052401035058a6" "04f480ad8a", |
| 773 | .expect_rc = -EBADMSG, |
| 774 | }, |
| 775 | { |
| 776 | .label = "CM Service Reestablish Request with TMSI 0x5a42e404", |
| 777 | .compl_l3_msg = "052801035058a6" "05f45a42e404", |
| 778 | .expect_mi = { |
| 779 | .type = GSM_MI_TYPE_TMSI, |
| 780 | .tmsi = 0x5a42e404, |
| 781 | }, |
| 782 | }, |
| 783 | { |
| 784 | .label = "Paging Response with IMSI 1234567", |
| 785 | .compl_l3_msg = "06270003505886" "0419325476", |
| 786 | .expect_mi = { |
| 787 | .type = GSM_MI_TYPE_IMSI, |
| 788 | .imsi = "1234567", |
| 789 | }, |
| 790 | }, |
| 791 | { |
| 792 | .label = "Paging Response with TMSI 0xb48883de", |
| 793 | .compl_l3_msg = "06270003505886" "05f4b48883de", |
| 794 | .expect_mi = { |
| 795 | .type = GSM_MI_TYPE_TMSI, |
| 796 | .tmsi = 0xb48883de, |
| 797 | }, |
| 798 | }, |
| 799 | { |
| 800 | .label = "Paging Response with TMSI, with unused nibble not 0xf", |
| 801 | .compl_l3_msg = "06270003505886" "0504b48883de", |
| 802 | .expect_rc = -EBADMSG, |
| 803 | }, |
| 804 | { |
| 805 | .label = "Paging Response with too short IMEI (1234567)", |
| 806 | .compl_l3_msg = "06270003505886" "041a325476", |
| 807 | .expect_rc = -EBADMSG, |
| 808 | }, |
| 809 | { |
| 810 | .label = "Paging Response with IMEI 123456789012345", |
| 811 | .compl_l3_msg = "06270003505886" "081a32547698103254", |
| 812 | .expect_mi = { |
| 813 | .type = GSM_MI_TYPE_IMEI, |
| 814 | .imei = "123456789012345", |
| 815 | }, |
| 816 | }, |
| 817 | { |
| 818 | .label = "Paging Response with IMEI 12345678901234 (no Luhn checksum)", |
| 819 | .compl_l3_msg = "06270003505886" "0812325476981032f4", |
| 820 | .expect_mi = { |
| 821 | .type = GSM_MI_TYPE_IMEI, |
| 822 | .imei = "12345678901234", |
| 823 | }, |
| 824 | }, |
| 825 | { |
| 826 | .label = "Paging Response with IMEISV 1234567890123456", |
| 827 | .compl_l3_msg = "06270003505886" "091332547698103254f6", |
| 828 | .expect_mi = { |
| 829 | .type = GSM_MI_TYPE_IMEISV, |
| 830 | .imeisv = "1234567890123456", |
| 831 | }, |
| 832 | }, |
| 833 | { |
| 834 | .label = "Paging Response with too short IMEISV 123456789012345", |
| 835 | .compl_l3_msg = "06270003505886" "081b32547698103254", |
| 836 | .expect_rc = -EBADMSG, |
| 837 | }, |
| 838 | { |
| 839 | .label = "Paging Response with too long IMEISV 12345678901234567", |
| 840 | .compl_l3_msg = "06270003505886" "091b3254769810325476", |
| 841 | .expect_rc = -EBADMSG, |
| 842 | }, |
| 843 | { |
| 844 | .label = "Paging Response with IMSI 123456789012345 and flipped ODD bit", |
| 845 | .compl_l3_msg = "06270003505886" "081132547698103254", |
| 846 | .expect_rc = -EBADMSG, |
| 847 | }, |
| 848 | { |
| 849 | .label = "IMSI-Detach with IMSI 901700000004620", |
| 850 | .compl_l3_msg = "050130" "089910070000006402", |
| 851 | .expect_mi = { |
| 852 | .type = GSM_MI_TYPE_IMSI, |
| 853 | .imsi = "901700000004620", |
| 854 | }, |
| 855 | }, |
| 856 | { |
| 857 | .label = "IMSI-Detach with TMSI 0x0980ad8a", |
| 858 | .compl_l3_msg = "050130" "05f40980ad8a", |
| 859 | .expect_mi = { |
| 860 | .type = GSM_MI_TYPE_TMSI, |
| 861 | .tmsi = 0x0980ad8a, |
| 862 | }, |
| 863 | }, |
| 864 | { |
| 865 | .label = "IMSI-Detach with invalid MI type", |
| 866 | .compl_l3_msg = "050130" "089d10070000006402", |
| 867 | .expect_rc = -EINVAL, |
| 868 | }, |
| 869 | { |
| 870 | .label = "IMSI-Detach with truncated IMSI MI", |
| 871 | .compl_l3_msg = "050130" "0899100700000064", |
| 872 | .expect_rc = -EBADMSG, |
| 873 | }, |
| 874 | { |
| 875 | .label = "IMSI-Detach with too short IMSI MI (12345)", |
| 876 | .compl_l3_msg = "050130" "03193254", |
| 877 | .expect_rc = -EBADMSG, |
| 878 | }, |
| 879 | { |
| 880 | .label = "IMSI-Detach with just long enough IMSI MI 123456", |
| 881 | .compl_l3_msg = "050130" "04113254f6", |
| 882 | .expect_mi = { |
| 883 | .type = GSM_MI_TYPE_IMSI, |
| 884 | .imsi = "123456", |
| 885 | }, |
| 886 | }, |
| 887 | { |
| 888 | .label = "IMSI-Detach with max length IMSI MI 123456789012345", |
| 889 | .compl_l3_msg = "050130" "081932547698103254", |
| 890 | .expect_mi = { |
| 891 | .type = GSM_MI_TYPE_IMSI, |
| 892 | .imsi = "123456789012345", |
| 893 | }, |
| 894 | }, |
| 895 | { |
| 896 | .label = "IMSI-Detach with just too long IMSI MI 1234567890123456", |
| 897 | .compl_l3_msg = "050130" "091132547698103254f6", |
| 898 | .expect_rc = -EBADMSG, |
| 899 | }, |
| 900 | { |
| 901 | .label = "IMSI-Detach with truncated TMSI MI", |
| 902 | .compl_l3_msg = "050130" "05f40980ad", |
| 903 | .expect_rc = -EBADMSG, |
| 904 | }, |
| 905 | { |
| 906 | .label = "IMSI-Detach with odd length TMSI", |
| 907 | .compl_l3_msg = "050130" "05fc0980ad8a", |
| 908 | .expect_rc = -EBADMSG, |
| 909 | }, |
| 910 | { |
| 911 | .label = "IMSI-Detach with too long TMSI MI", |
| 912 | .compl_l3_msg = "050130" "06f40980ad23", |
| 913 | .expect_rc = -EBADMSG, |
| 914 | }, |
| 915 | { |
| 916 | .label = "IMSI-Detach with too short TMSI", |
| 917 | .compl_l3_msg = "050130" "04f480ad8a", |
| 918 | .expect_rc = -EBADMSG, |
| 919 | }, |
| 920 | { |
| 921 | .label = "Identity Response with IMSI 901700000004620", |
| 922 | .compl_l3_msg = "0519" "089910070000006402", |
| 923 | .expect_mi = { |
| 924 | .type = GSM_MI_TYPE_IMSI, |
| 925 | .imsi = "901700000004620", |
| 926 | }, |
| 927 | }, |
| 928 | { |
| 929 | .label = "Identity Response with IMEI 123456789012345", |
| 930 | .compl_l3_msg = "0519" "081a32547698103254", |
| 931 | .expect_mi = { |
| 932 | .type = GSM_MI_TYPE_IMEI, |
| 933 | .imei = "123456789012345", |
| 934 | }, |
| 935 | }, |
| 936 | { |
| 937 | .label = "Identity Response with IMEISV 9876543210987654", |
| 938 | .compl_l3_msg = "0519" "099378563412907856f4", |
| 939 | .expect_mi = { |
| 940 | .type = GSM_MI_TYPE_IMEISV, |
| 941 | .imeisv = "9876543210987654", |
| 942 | }, |
| 943 | }, |
| 944 | }; |
| 945 | |
| 946 | void test_struct_mobile_identity() |
| 947 | { |
| 948 | struct mobile_identity_tc *t; |
| 949 | printf("%s()\n", __func__); |
| 950 | for (t = mobile_identity_tests; (t - mobile_identity_tests) < ARRAY_SIZE(mobile_identity_tests); t++) { |
| 951 | struct osmo_mobile_identity mi; |
| 952 | struct msgb *msg; |
| 953 | int rc; |
| 954 | memset(&mi, 0xff, sizeof(mi)); |
| 955 | |
| 956 | msg = msgb_from_hex(t->label, 1024, t->compl_l3_msg); |
| 957 | rc = osmo_mobile_identity_decode_from_l3(&mi, msg, false); |
| 958 | msgb_free(msg); |
| 959 | |
| 960 | printf("%s: rc = %d", t->label, rc); |
| 961 | if (!rc) { |
| 962 | printf(", mi = %s", osmo_mobile_identity_to_str_c(OTC_SELECT, &mi)); |
| 963 | } |
| 964 | |
| 965 | if (rc == t->expect_rc |
| 966 | && ((rc != 0) || !osmo_mobile_identity_cmp(&mi, &t->expect_mi))) { |
| 967 | printf(" ok"); |
| 968 | } else { |
| 969 | printf(" ERROR: Expected rc = %d", t->expect_rc); |
| 970 | if (!t->expect_rc) |
| 971 | printf(", mi = %s", osmo_mobile_identity_to_str_c(OTC_SELECT, &t->expect_mi)); |
| 972 | } |
| 973 | printf("\n"); |
| 974 | } |
| 975 | printf("\n"); |
| 976 | } |
| 977 | |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 978 | static const struct bcd_number_test { |
| 979 | /* Human-readable test name */ |
| 980 | const char *test_name; |
| 981 | |
| 982 | /* To be encoded number in ASCII */ |
| 983 | const char *enc_ascii; |
| 984 | /* Expected encoding result in HEX */ |
| 985 | const char *enc_hex; |
| 986 | /* Optional header length (LHV) */ |
| 987 | uint8_t enc_h_len; |
| 988 | /* Expected return code */ |
| 989 | int enc_rc; |
| 990 | |
| 991 | /* To be decoded buffer in HEX */ |
| 992 | const char *dec_hex; |
| 993 | /* Expected decoding result in ASCII */ |
| 994 | const char *dec_ascii; |
| 995 | /* Optional header length (LHV) */ |
| 996 | uint8_t dec_h_len; |
| 997 | /* Expected return code */ |
| 998 | int dec_rc; |
| 999 | |
| 1000 | /* Encoding buffer limit (0 means unlimited) */ |
| 1001 | size_t enc_buf_lim; |
| 1002 | /* Decoding buffer limit (0 means unlimited) */ |
| 1003 | size_t dec_buf_lim; |
| 1004 | } bcd_number_test_set[] = { |
| 1005 | { |
| 1006 | .test_name = "regular 9-digit MSISDN", |
| 1007 | |
| 1008 | /* Encoding test */ |
| 1009 | .enc_ascii = "123456789", |
| 1010 | .enc_hex = "0521436587f9", |
| 1011 | .enc_rc = 6, |
| 1012 | |
| 1013 | /* Decoding test */ |
| 1014 | .dec_hex = "0521436587f9", |
| 1015 | .dec_ascii = "123456789", |
| 1016 | }, |
| 1017 | { |
| 1018 | .test_name = "regular 6-digit MSISDN with optional header (LHV)", |
| 1019 | |
| 1020 | /* Encoding test */ |
| 1021 | .enc_ascii = "123456", |
Vadim Yanitskiy | 1dc8264 | 2019-05-27 00:53:54 +0700 | [diff] [blame] | 1022 | .enc_hex = "07ffffffff214365", |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1023 | .enc_h_len = 4, /* LHV */ |
| 1024 | .enc_rc = 4 + 4, |
| 1025 | |
| 1026 | /* Decoding test */ |
| 1027 | .dec_hex = "07deadbeef214365", |
| 1028 | .dec_ascii = "123456", |
| 1029 | .dec_h_len = 4, /* LHV */ |
| 1030 | }, |
| 1031 | { |
| 1032 | .test_name = "long 15-digit (maximum) MSISDN", |
| 1033 | |
| 1034 | /* Encoding test */ |
| 1035 | .enc_ascii = "123456789012345", |
| 1036 | .enc_hex = "0821436587092143f5", |
| 1037 | .enc_rc = 9, |
| 1038 | |
| 1039 | /* Decoding test */ |
| 1040 | .dec_hex = "0821436587092143f5", |
| 1041 | .dec_ascii = "123456789012345", |
| 1042 | }, |
| 1043 | { |
| 1044 | .test_name = "long 15-digit (maximum) MSISDN, limited buffer", |
| 1045 | |
| 1046 | /* Encoding test */ |
| 1047 | .enc_ascii = "123456789012345", |
| 1048 | .enc_hex = "0821436587092143f5", |
| 1049 | .enc_rc = 9, |
| 1050 | |
| 1051 | /* Decoding test */ |
| 1052 | .dec_hex = "0821436587092143f5", |
| 1053 | .dec_ascii = "123456789012345", |
| 1054 | |
| 1055 | /* Buffer length limitations */ |
| 1056 | .dec_buf_lim = 15 + 1, |
| 1057 | .enc_buf_lim = 9, |
| 1058 | }, |
| 1059 | { |
| 1060 | .test_name = "to be truncated 20-digit MSISDN", |
| 1061 | |
| 1062 | /* Encoding test (not enough room in buffer) */ |
| 1063 | .enc_ascii = "12345678901234567890", |
| 1064 | .enc_hex = "", /* nothing */ |
| 1065 | .enc_rc = -EIO, |
| 1066 | |
| 1067 | /* Decoding test (one 5 digits do not fit) */ |
| 1068 | .dec_hex = "0a21436587092143658709", |
| 1069 | .dec_ascii = "123456789012345", |
Vadim Yanitskiy | 7194087 | 2019-05-26 00:49:57 +0700 | [diff] [blame] | 1070 | .dec_rc = -ENOSPC, |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1071 | |
| 1072 | /* Buffer length limitations */ |
| 1073 | .dec_buf_lim = 15 + 1, /* 5 digits less */ |
| 1074 | .enc_buf_lim = 9, |
| 1075 | }, |
| 1076 | { |
| 1077 | .test_name = "LV incorrect length", |
| 1078 | .dec_hex = "05214365", /* should be 0x03 */ |
| 1079 | .dec_ascii = "(none)", |
Vadim Yanitskiy | e4799f5 | 2019-05-26 00:55:20 +0700 | [diff] [blame] | 1080 | .dec_rc = -EINVAL, |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1081 | }, |
| 1082 | { |
| 1083 | .test_name = "empty input buffer", |
| 1084 | |
| 1085 | /* Encoding test */ |
| 1086 | .enc_ascii = "", |
| 1087 | .enc_hex = "00", |
| 1088 | .enc_rc = 1, |
| 1089 | |
| 1090 | /* Decoding test */ |
| 1091 | .dec_hex = "", |
| 1092 | .dec_ascii = "(none)", |
| 1093 | .dec_rc = -EIO, |
| 1094 | }, |
Oliver Smith | 186f878 | 2019-06-06 16:11:32 +0200 | [diff] [blame] | 1095 | { |
| 1096 | .test_name = "decoding buffer is one byte too small (OS#4049)", |
| 1097 | |
| 1098 | /* Decoding test */ |
| 1099 | .dec_hex = "022143", /* "1234" */ |
| 1100 | .dec_ascii = "123", /* '4' was truncated */ |
| 1101 | .dec_rc = -ENOSPC, |
| 1102 | |
| 1103 | /* Buffer length limitations */ |
| 1104 | .dec_buf_lim = 4, |
| 1105 | }, |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1106 | }; |
| 1107 | |
| 1108 | static void test_bcd_number_encode_decode() |
| 1109 | { |
| 1110 | const struct bcd_number_test *test; |
Vadim Yanitskiy | 1dc8264 | 2019-05-27 00:53:54 +0700 | [diff] [blame] | 1111 | uint8_t buf_enc[0xff] = { 0xff }; |
| 1112 | char buf_dec[0xff] = { 0xff }; |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1113 | size_t buf_len, i; |
| 1114 | int rc; |
| 1115 | |
| 1116 | printf("BSD number encoding / decoding test\n"); |
| 1117 | |
| 1118 | for (i = 0; i < ARRAY_SIZE(bcd_number_test_set); i++) { |
| 1119 | test = &bcd_number_test_set[i]; |
| 1120 | printf("- Running test: %s\n", test->test_name); |
| 1121 | |
| 1122 | if (test->enc_ascii) { |
| 1123 | if (test->enc_buf_lim) |
| 1124 | buf_len = test->enc_buf_lim; |
| 1125 | else |
| 1126 | buf_len = sizeof(buf_enc); |
| 1127 | |
| 1128 | printf(" - Encoding ASCII (buffer limit=%zu) '%s'...\n", |
| 1129 | test->enc_buf_lim, test->enc_ascii); |
| 1130 | |
| 1131 | rc = gsm48_encode_bcd_number(buf_enc, buf_len, |
| 1132 | test->enc_h_len, test->enc_ascii); |
| 1133 | printf(" - Expected: (rc=%d) '%s'\n", |
| 1134 | test->enc_rc, test->enc_hex); |
| 1135 | printf(" - Actual: (rc=%d) '%s'\n", |
| 1136 | rc, osmo_hexdump_nospc(buf_enc, rc >= 0 ? rc : 0)); |
| 1137 | } |
| 1138 | |
| 1139 | if (test->dec_hex) { |
| 1140 | /* Parse a HEX string */ |
| 1141 | rc = osmo_hexparse(test->dec_hex, buf_enc, sizeof(buf_enc)); |
| 1142 | OSMO_ASSERT(rc >= 0); |
| 1143 | |
| 1144 | if (test->dec_buf_lim) |
| 1145 | buf_len = test->dec_buf_lim; |
| 1146 | else |
| 1147 | buf_len = sizeof(buf_dec); |
| 1148 | |
| 1149 | printf(" - Decoding HEX (buffer limit=%zu) '%s'...\n", |
| 1150 | test->dec_buf_lim, test->dec_hex); |
| 1151 | |
| 1152 | rc = gsm48_decode_bcd_number2(buf_dec, buf_len, |
| 1153 | buf_enc, rc, test->dec_h_len); |
| 1154 | printf(" - Expected: (rc=%d) '%s'\n", |
| 1155 | test->dec_rc, test->dec_ascii); |
| 1156 | printf(" - Actual: (rc=%d) '%s'\n", |
Vadim Yanitskiy | 7194087 | 2019-05-26 00:49:57 +0700 | [diff] [blame] | 1157 | rc, (rc == 0 || rc == -ENOSPC) ? buf_dec : "(none)"); |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1158 | } |
Vadim Yanitskiy | 1dc8264 | 2019-05-27 00:53:54 +0700 | [diff] [blame] | 1159 | |
| 1160 | /* Poison buffers between the test iterations */ |
| 1161 | memset(buf_enc, 0xff, sizeof(buf_enc)); |
| 1162 | memset(buf_dec, 0xff, sizeof(buf_dec)); |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | printf("\n"); |
| 1166 | } |
| 1167 | |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 1168 | struct { |
| 1169 | int range; |
| 1170 | int arfcns_num; |
| 1171 | int arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS]; |
| 1172 | } arfcn_test_ranges[] = { |
| 1173 | {OSMO_GSM48_ARFCN_RANGE_512, 12, |
| 1174 | { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }}, |
| 1175 | {OSMO_GSM48_ARFCN_RANGE_512, 17, |
| 1176 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }}, |
| 1177 | {OSMO_GSM48_ARFCN_RANGE_512, 18, |
| 1178 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }}, |
| 1179 | {OSMO_GSM48_ARFCN_RANGE_512, 18, |
| 1180 | { 1, 17, 31, 45, 58, 79, 81, 97, |
| 1181 | 113, 127, 213, 277, 287, 311, 331, 391, |
| 1182 | 417, 511 }}, |
| 1183 | {OSMO_GSM48_ARFCN_RANGE_512, 6, |
| 1184 | { 1, 17, 31, 45, 58, 79 }}, |
| 1185 | {OSMO_GSM48_ARFCN_RANGE_512, 6, |
| 1186 | { 10, 17, 31, 45, 58, 79 }}, |
| 1187 | {OSMO_GSM48_ARFCN_RANGE_1024, 17, |
| 1188 | { 0, 17, 31, 45, 58, 79, 81, 97, |
| 1189 | 113, 127, 213, 277, 287, 311, 331, 391, |
| 1190 | 1023 }}, |
| 1191 | {OSMO_GSM48_ARFCN_RANGE_1024, 16, |
| 1192 | { 17, 31, 45, 58, 79, 81, 97, 113, |
| 1193 | 127, 213, 277, 287, 311, 331, 391, 1023 }}, |
| 1194 | {-1} |
| 1195 | }; |
| 1196 | |
| 1197 | static int test_single_range_encoding(int range, const int *orig_arfcns, int arfcns_num, int silent) |
| 1198 | { |
| 1199 | int arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS]; |
| 1200 | int w[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS]; |
| 1201 | int f0_included = 0; |
| 1202 | int rc, f0; |
| 1203 | uint8_t chan_list[16] = {0}; |
| 1204 | struct gsm_sysinfo_freq dec_freq[1024] = {{0}}; |
| 1205 | int dec_arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS] = {0}; |
| 1206 | int dec_arfcns_count = 0; |
| 1207 | int arfcns_used = 0; |
| 1208 | int i; |
| 1209 | |
| 1210 | arfcns_used = arfcns_num; |
| 1211 | memmove(arfcns, orig_arfcns, sizeof(arfcns)); |
| 1212 | |
| 1213 | f0 = range == OSMO_GSM48_ARFCN_RANGE_1024 ? 0 : arfcns[0]; |
| 1214 | /* |
| 1215 | * Manipulate the ARFCN list according to the rules in J4 depending |
| 1216 | * on the selected range. |
| 1217 | */ |
| 1218 | arfcns_used = osmo_gsm48_range_enc_filter_arfcns(arfcns, arfcns_used, f0, &f0_included); |
| 1219 | |
| 1220 | memset(w, 0, sizeof(w)); |
| 1221 | osmo_gsm48_range_enc_arfcns(range, arfcns, arfcns_used, w, 0); |
| 1222 | |
| 1223 | if (!silent) |
| 1224 | printf("range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n", range, arfcns_used, f0, f0_included); |
| 1225 | |
| 1226 | /* Select the range and the amount of bits needed */ |
| 1227 | switch (range) { |
| 1228 | case OSMO_GSM48_ARFCN_RANGE_128: |
| 1229 | osmo_gsm48_range_enc_128(chan_list, f0, w); |
| 1230 | break; |
| 1231 | case OSMO_GSM48_ARFCN_RANGE_256: |
| 1232 | osmo_gsm48_range_enc_256(chan_list, f0, w); |
| 1233 | break; |
| 1234 | case OSMO_GSM48_ARFCN_RANGE_512: |
| 1235 | osmo_gsm48_range_enc_512(chan_list, f0, w); |
| 1236 | break; |
| 1237 | case OSMO_GSM48_ARFCN_RANGE_1024: |
| 1238 | osmo_gsm48_range_enc_1024(chan_list, f0, f0_included, w); |
| 1239 | break; |
| 1240 | default: |
| 1241 | return 1; |
| 1242 | }; |
| 1243 | |
| 1244 | if (!silent) |
| 1245 | printf("chan_list = %s\n", |
| 1246 | osmo_hexdump(chan_list, sizeof(chan_list))); |
| 1247 | |
| 1248 | rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list), |
| 1249 | 0xfe, 1); |
| 1250 | if (rc != 0) { |
| 1251 | printf("Cannot decode freq list, rc = %d\n", rc); |
| 1252 | return 1; |
| 1253 | } |
| 1254 | |
| 1255 | for (i = 0; i < ARRAY_SIZE(dec_freq); i++) { |
| 1256 | if (dec_freq[i].mask && |
| 1257 | dec_arfcns_count < ARRAY_SIZE(dec_arfcns)) |
| 1258 | dec_arfcns[dec_arfcns_count++] = i; |
| 1259 | } |
| 1260 | |
| 1261 | if (!silent) { |
| 1262 | printf("Decoded freqs %d (expected %d)\n", |
| 1263 | dec_arfcns_count, arfcns_num); |
| 1264 | printf("Decoded: "); |
| 1265 | for (i = 0; i < dec_arfcns_count; i++) { |
| 1266 | printf("%d ", dec_arfcns[i]); |
| 1267 | if (dec_arfcns[i] != orig_arfcns[i]) |
| 1268 | printf("(!= %d) ", orig_arfcns[i]); |
| 1269 | } |
| 1270 | printf("\n"); |
| 1271 | } |
| 1272 | |
| 1273 | if (dec_arfcns_count != arfcns_num) { |
| 1274 | printf("Wrong number of arfcns\n"); |
| 1275 | return 1; |
| 1276 | } |
| 1277 | |
| 1278 | if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) { |
| 1279 | printf("Decoding error, got wrong freqs\n"); |
| 1280 | printf(" w = "); |
| 1281 | for (i = 0; i < ARRAY_SIZE(w); i++) |
| 1282 | printf("%d ", w[i]); |
| 1283 | printf("\n"); |
| 1284 | return 1; |
| 1285 | } |
| 1286 | |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | static void test_random_range_encoding(int range, int max_arfcn_num) |
| 1291 | { |
| 1292 | int arfcns_num = 0; |
| 1293 | int test_idx; |
| 1294 | int rc, max_count; |
| 1295 | int num_tests = 1024; |
| 1296 | |
| 1297 | printf("Random range test: range %d, max num ARFCNs %d\n", |
| 1298 | range, max_arfcn_num); |
| 1299 | |
| 1300 | srandom(1); |
| 1301 | |
| 1302 | for (max_count = 1; max_count < max_arfcn_num; max_count++) { |
| 1303 | for (test_idx = 0; test_idx < num_tests; test_idx++) { |
| 1304 | int count; |
| 1305 | int i; |
| 1306 | int min_freq = 0; |
| 1307 | |
| 1308 | int rnd_arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS] = {0}; |
| 1309 | char rnd_arfcns_set[1024] = {0}; |
| 1310 | |
| 1311 | if (range < OSMO_GSM48_ARFCN_RANGE_1024) |
| 1312 | min_freq = random() % (1023 - range); |
| 1313 | |
| 1314 | for (count = max_count; count; ) { |
| 1315 | int arfcn = min_freq + random() % (range + 1); |
| 1316 | OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set)); |
| 1317 | |
| 1318 | if (!rnd_arfcns_set[arfcn]) { |
| 1319 | rnd_arfcns_set[arfcn] = 1; |
| 1320 | count -= 1; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | arfcns_num = 0; |
| 1325 | for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++) |
| 1326 | if (rnd_arfcns_set[i]) |
| 1327 | rnd_arfcns[arfcns_num++] = i; |
| 1328 | |
| 1329 | rc = test_single_range_encoding(range, rnd_arfcns, |
| 1330 | arfcns_num, 1); |
| 1331 | if (rc != 0) { |
| 1332 | printf("Failed on test %d, range %d, num ARFCNs %d\n", |
| 1333 | test_idx, range, max_count); |
| 1334 | test_single_range_encoding(range, rnd_arfcns, |
| 1335 | arfcns_num, 0); |
| 1336 | return; |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | static void test_range_encoding() |
| 1343 | { |
| 1344 | int *arfcns; |
| 1345 | int arfcns_num = 0; |
| 1346 | int test_idx; |
| 1347 | int range; |
| 1348 | |
| 1349 | for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++) |
| 1350 | { |
| 1351 | arfcns_num = arfcn_test_ranges[test_idx].arfcns_num; |
| 1352 | arfcns = &arfcn_test_ranges[test_idx].arfcns[0]; |
| 1353 | range = arfcn_test_ranges[test_idx].range; |
| 1354 | |
| 1355 | printf("Range test %d: range %d, num ARFCNs %d\n", |
| 1356 | test_idx, range, arfcns_num); |
| 1357 | |
| 1358 | test_single_range_encoding(range, arfcns, arfcns_num, 0); |
| 1359 | } |
| 1360 | |
| 1361 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_128, 29); |
| 1362 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_256, 22); |
| 1363 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_512, 18); |
| 1364 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_1024, 16); |
| 1365 | } |
| 1366 | |
| 1367 | static int freqs1[] = { |
| 1368 | 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980 |
| 1369 | }; |
| 1370 | |
| 1371 | static int freqs2[] = { |
| 1372 | 402, 460, 1, 67, 131, 197, 272, 347, |
| 1373 | }; |
| 1374 | |
| 1375 | static int freqs3[] = { |
| 1376 | 68, 128, 198, 279, 353, 398, 452, |
| 1377 | |
| 1378 | }; |
| 1379 | |
| 1380 | static int w_out[] = { |
| 1381 | 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9, |
| 1382 | }; |
| 1383 | |
| 1384 | static int range128[] = { |
| 1385 | 1, 1 + 127, |
| 1386 | }; |
| 1387 | |
| 1388 | static int range256[] = { |
| 1389 | 1, 1 + 128, |
| 1390 | }; |
| 1391 | |
| 1392 | static int range512[] = { |
| 1393 | 1, 1+ 511, |
| 1394 | }; |
| 1395 | |
| 1396 | |
| 1397 | #define VERIFY(res, cmp, wanted) \ |
| 1398 | if (!(res cmp wanted)) { \ |
| 1399 | printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \ |
| 1400 | __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \ |
| 1401 | } |
| 1402 | |
| 1403 | static void test_arfcn_filter() |
| 1404 | { |
| 1405 | int arfcns[50], i, res, f0_included; |
| 1406 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 1407 | arfcns[i] = (i + 1) * 2; |
| 1408 | |
| 1409 | /* check that the arfcn is taken out. f0_included is only set for Range1024 */ |
| 1410 | f0_included = 24; |
| 1411 | res = osmo_gsm48_range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), arfcns[0], &f0_included); |
| 1412 | VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1); |
| 1413 | VERIFY(f0_included, ==, 1); |
| 1414 | for (i = 0; i < res; ++i) |
| 1415 | VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1)); |
| 1416 | |
| 1417 | /* check with range1024, ARFCN 0 is included */ |
| 1418 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 1419 | arfcns[i] = i * 2; |
| 1420 | res = osmo_gsm48_range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), 0, &f0_included); |
| 1421 | VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1); |
| 1422 | VERIFY(f0_included, ==, 1); |
| 1423 | for (i = 0; i < res; ++i) |
| 1424 | VERIFY(arfcns[i], ==, (i + 1) * 2 - 1); |
| 1425 | |
| 1426 | /* check with range1024, ARFCN 0 not included */ |
| 1427 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 1428 | arfcns[i] = (i + 1) * 2; |
| 1429 | res = osmo_gsm48_range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), 0, &f0_included); |
| 1430 | VERIFY(res, ==, ARRAY_SIZE(arfcns)); |
| 1431 | VERIFY(f0_included, ==, 0); |
| 1432 | for (i = 0; i < res; ++i) |
| 1433 | VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1); |
| 1434 | } |
| 1435 | |
| 1436 | static void test_print_encoding() |
| 1437 | { |
| 1438 | int rc; |
| 1439 | int w[17]; |
| 1440 | uint8_t chan_list[16]; |
| 1441 | memset(chan_list, 0x23, sizeof(chan_list)); |
| 1442 | |
| 1443 | for (rc = 0; rc < ARRAY_SIZE(w); ++rc) |
| 1444 | switch (rc % 3) { |
| 1445 | case 0: |
| 1446 | w[rc] = 0xAAAA; |
| 1447 | break; |
| 1448 | case 1: |
| 1449 | w[rc] = 0x5555; |
| 1450 | break; |
| 1451 | case 2: |
| 1452 | w[rc] = 0x9696; |
| 1453 | break; |
| 1454 | } |
| 1455 | |
| 1456 | osmo_gsm48_range_enc_512(chan_list, (1 << 9) | 0x96, w); |
| 1457 | |
| 1458 | printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list))); |
| 1459 | } |
| 1460 | |
| 1461 | static void test_si_range_helpers() |
| 1462 | { |
| 1463 | int ws[(sizeof(freqs1)/sizeof(freqs1[0]))]; |
| 1464 | int i, f0 = 0xFFFFFF; |
| 1465 | |
| 1466 | memset(&ws[0], 0x23, sizeof(ws)); |
| 1467 | |
| 1468 | i = osmo_gsm48_range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1)); |
| 1469 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1); |
| 1470 | VERIFY(i, ==, 2); |
| 1471 | |
| 1472 | i = osmo_gsm48_range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2)); |
| 1473 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1); |
| 1474 | VERIFY(i, ==, 2); |
| 1475 | |
| 1476 | i = osmo_gsm48_range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3)); |
| 1477 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1); |
| 1478 | VERIFY(i, ==, 0); |
| 1479 | |
| 1480 | osmo_gsm48_range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0); |
| 1481 | |
| 1482 | for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) { |
| 1483 | printf("w[%d]=%d\n", i, ws[i]); |
| 1484 | VERIFY(ws[i], ==, w_out[i]); |
| 1485 | } |
| 1486 | |
| 1487 | i = osmo_gsm48_range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0); |
| 1488 | VERIFY(i, ==, OSMO_GSM48_ARFCN_RANGE_128); |
| 1489 | VERIFY(f0, ==, 1); |
| 1490 | |
| 1491 | i = osmo_gsm48_range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0); |
| 1492 | VERIFY(i, ==, OSMO_GSM48_ARFCN_RANGE_256); |
| 1493 | VERIFY(f0, ==, 1); |
| 1494 | |
| 1495 | i = osmo_gsm48_range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0); |
| 1496 | VERIFY(i, ==, OSMO_GSM48_ARFCN_RANGE_512); |
| 1497 | VERIFY(f0, ==, 1); |
| 1498 | } |
| 1499 | |
Pau Espin Pedrol | b99f4ca | 2019-10-31 13:35:22 +0100 | [diff] [blame] | 1500 | static void test_power_ctrl() |
| 1501 | { |
| 1502 | int8_t rc8; |
Pau Espin Pedrol | e40b963 | 2019-10-31 15:38:30 +0100 | [diff] [blame] | 1503 | int rc; |
Pau Espin Pedrol | b99f4ca | 2019-10-31 13:35:22 +0100 | [diff] [blame] | 1504 | |
| 1505 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_850, 0x00); |
| 1506 | VERIFY(rc8, ==, 1); |
| 1507 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_900, 0x02); |
| 1508 | VERIFY(rc8, ==, 3); |
| 1509 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_1800, 0x02); |
| 1510 | VERIFY(rc8, ==, 3); |
| 1511 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_1900, 0x02); |
| 1512 | VERIFY(rc8, ==, 3); |
| 1513 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_1900, 0x04); |
| 1514 | VERIFY(rc8, <, 0); |
| 1515 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_900, 0x04); |
| 1516 | VERIFY(rc8, ==, 5); |
| 1517 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_900, 0x05); |
| 1518 | VERIFY(rc8, <, 0); |
| 1519 | rc8 = osmo_gsm48_rfpowercap2powerclass(GSM_BAND_900, 0xf2); |
| 1520 | VERIFY(rc8, <, 0); |
Pau Espin Pedrol | e40b963 | 2019-10-31 15:38:30 +0100 | [diff] [blame] | 1521 | |
| 1522 | rc = ms_class_gmsk_dbm(GSM_BAND_850, 0); |
| 1523 | VERIFY(rc, <, 0); |
| 1524 | rc = ms_class_gmsk_dbm(GSM_BAND_850, 1); |
| 1525 | VERIFY(rc, ==, 43); |
| 1526 | rc = ms_class_gmsk_dbm(GSM_BAND_900, 3); |
| 1527 | VERIFY(rc, ==, 37); |
| 1528 | rc = ms_class_gmsk_dbm(GSM_BAND_1800, 2); |
| 1529 | VERIFY(rc, ==, 24); |
| 1530 | rc = ms_class_gmsk_dbm(GSM_BAND_1800, 3); |
| 1531 | VERIFY(rc, ==, 36); |
| 1532 | rc = ms_class_gmsk_dbm(GSM_BAND_1900, 3); |
| 1533 | VERIFY(rc, ==, 33); |
| 1534 | rc = ms_class_gmsk_dbm(GSM_BAND_1900, 4); |
| 1535 | VERIFY(rc, <, 0); |
| 1536 | rc = ms_class_gmsk_dbm(GSM_BAND_900, 5); |
| 1537 | VERIFY(rc, ==, 29); |
| 1538 | rc = ms_class_gmsk_dbm(GSM_BAND_900, 6); |
| 1539 | VERIFY(rc, <, 0); |
Pau Espin Pedrol | b99f4ca | 2019-10-31 13:35:22 +0100 | [diff] [blame] | 1540 | } |
| 1541 | |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 1542 | int main(int argc, char **argv) |
| 1543 | { |
| 1544 | test_bearer_cap(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 1545 | test_mid_from_tmsi(); |
Max | d55d7d4 | 2018-02-15 11:27:18 +0100 | [diff] [blame] | 1546 | test_mid_from_imsi(); |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 1547 | test_mid_encode_decode(); |
| 1548 | test_mid_decode_zero_length(); |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 1549 | test_struct_mobile_identity(); |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1550 | test_bcd_number_encode_decode(); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 1551 | test_ra_cap(); |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 1552 | test_lai_encode_decode(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 1553 | |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 1554 | test_si_range_helpers(); |
| 1555 | test_arfcn_filter(); |
| 1556 | test_print_encoding(); |
| 1557 | test_range_encoding(); |
Pau Espin Pedrol | b99f4ca | 2019-10-31 13:35:22 +0100 | [diff] [blame] | 1558 | test_power_ctrl(); |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 1559 | |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 1560 | return EXIT_SUCCESS; |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 1561 | } |