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 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 24 | |
| 25 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 26 | #include <osmocom/gsm/gsm48_ie.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 27 | #include <osmocom/gsm/gsm48.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/mncc.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 29 | #include <osmocom/core/backtrace.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/core/msgb.h> |
| 32 | |
| 33 | |
| 34 | static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 }; |
| 35 | |
| 36 | static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = { |
| 37 | .transfer = GSM48_BCAP_ITCAP_UNR_DIG_INF, |
| 38 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 39 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 40 | .radio = GSM48_BCAP_RRQ_FR_ONLY, |
| 41 | .speech_ver[0]= -1, |
| 42 | .data = { |
| 43 | .rate_adaption = GSM48_BCAP_RA_V110_X30, |
| 44 | .sig_access = GSM48_BCAP_SA_I440_I450, |
| 45 | .async = 1, |
| 46 | .nr_stop_bits = 1, |
| 47 | .nr_data_bits = 8, |
| 48 | .user_rate = GSM48_BCAP_UR_9600, |
| 49 | .parity = GSM48_BCAP_PAR_NONE, |
| 50 | .interm_rate = GSM48_BCAP_IR_16k, |
| 51 | .transp = GSM48_BCAP_TR_TRANSP, |
| 52 | .modem_type = GSM48_BCAP_MT_NONE, |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 }; |
| 57 | |
| 58 | static const struct gsm_mncc_bearer_cap bcap_speech_all = { |
| 59 | .transfer = GSM48_BCAP_ITCAP_SPEECH, |
| 60 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 61 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 62 | .radio = GSM48_BCAP_RRQ_DUAL_FR, |
| 63 | .speech_ver = { |
| 64 | 4, 2, 0, 5, 1, -1, |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | struct bcap_test { |
| 70 | const uint8_t *lv; |
| 71 | const struct gsm_mncc_bearer_cap *bc; |
| 72 | const char *name; |
| 73 | }; |
| 74 | |
| 75 | static const struct bcap_test bcap_tests[] = { |
| 76 | { csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" }, |
| 77 | { speech_all_lv, &bcap_speech_all, "Speech, all codecs" }, |
| 78 | }; |
| 79 | |
| 80 | static int test_bearer_cap() |
| 81 | { |
| 82 | struct gsm_mncc_bearer_cap bc; |
| 83 | int i, rc; |
| 84 | |
| 85 | for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) { |
| 86 | struct msgb *msg = msgb_alloc(100, "test"); |
| 87 | int lv_len; |
| 88 | |
| 89 | memset(&bc, 0, sizeof(bc)); |
| 90 | |
| 91 | /* test decoding */ |
| 92 | rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv); |
| 93 | if (rc < 0) { |
| 94 | fprintf(stderr, "Error decoding %s\n", |
| 95 | bcap_tests[i].name); |
| 96 | return rc; |
| 97 | } |
| 98 | if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) { |
| 99 | fprintf(stderr, "Incorrect decoded result of %s:\n", |
| 100 | bcap_tests[i].name); |
| 101 | fprintf(stderr, " should: %s\n", |
| 102 | osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc))); |
| 103 | fprintf(stderr, " is: %s\n", |
| 104 | osmo_hexdump((uint8_t *) &bc, sizeof(bc))); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | /* also test re-encode? */ |
| 109 | rc = gsm48_encode_bearer_cap(msg, 1, &bc); |
| 110 | if (rc < 0) { |
| 111 | fprintf(stderr, "Error encoding %s\n", |
| 112 | bcap_tests[i].name); |
| 113 | return rc; |
| 114 | } |
| 115 | lv_len = bcap_tests[i].lv[0]+1; |
| 116 | if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) { |
| 117 | fprintf(stderr, "Incorrect encoded result of %s:\n", |
| 118 | bcap_tests[i].name); |
| 119 | fprintf(stderr, " should: %s\n", |
| 120 | osmo_hexdump(bcap_tests[i].lv, lv_len)); |
| 121 | fprintf(stderr, " is: %s\n", |
| 122 | osmo_hexdump(msg->data, msg->len)); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | printf("Test `%s' passed\n", bcap_tests[i].name); |
| 127 | msgb_free(msg); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 133 | static inline void dump_ra(const struct gprs_ra_id *raid) |
| 134 | { |
Neels Hofmeyr | dbb2513 | 2018-02-20 15:12:23 +0100 | [diff] [blame] | 135 | 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] | 136 | } |
| 137 | |
| 138 | static inline void check_ra(const struct gprs_ra_id *raid) |
| 139 | { |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 140 | struct gsm48_ra_id ra; |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 141 | struct gprs_ra_id raid0 = { |
| 142 | .mnc = 0, |
| 143 | .mcc = 0, |
| 144 | .lac = 0, |
| 145 | .rac = 0, |
| 146 | }; |
| 147 | |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 148 | gsm48_encode_ra(&ra, raid); |
| 149 | printf("Constructed RA:\n"); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 150 | |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 151 | gsm48_parse_ra(&raid0, (const uint8_t *)&ra); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 152 | dump_ra(raid); |
Neels Hofmeyr | c38b32d | 2018-02-20 15:13:18 +0100 | [diff] [blame] | 153 | 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] | 154 | dump_ra(&raid0); |
| 155 | printf("RA test..."); |
Neels Hofmeyr | dbb2513 | 2018-02-20 15:12:23 +0100 | [diff] [blame] | 156 | if (raid->mnc != raid0.mnc || raid->mcc != raid0.mcc || raid->lac != raid0.lac || raid->rac != raid0.rac |
| 157 | || (raid->mnc_3_digits || raid->mnc > 99) != raid0.mnc_3_digits) |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 158 | printf("FAIL\n"); |
| 159 | else |
| 160 | printf("passed\n"); |
| 161 | } |
| 162 | |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 163 | static inline void check_lai(const struct gprs_ra_id *raid) |
| 164 | { |
| 165 | int rc; |
| 166 | struct gsm48_loc_area_id lai = {}; |
| 167 | struct gprs_ra_id decoded = {}; |
| 168 | struct gprs_ra_id _laid = *raid; |
| 169 | struct gprs_ra_id *laid = &_laid; |
| 170 | laid->rac = 0; |
| 171 | |
| 172 | printf("- gsm48_generate_lai() from "); dump_ra(laid); |
| 173 | |
| 174 | gsm48_generate_lai(&lai, laid->mcc, laid->mnc, laid->lac); |
| 175 | printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai))); |
| 176 | rc = gsm48_decode_lai(&lai, &decoded.mcc, &decoded.mnc, &decoded.lac); |
| 177 | if (rc) { |
| 178 | printf(" gsm48_decode_lai() returned %d --> FAIL\n", rc); |
| 179 | return; |
| 180 | } |
| 181 | printf(" gsm48_decode_lai() gives "); dump_ra(&decoded); |
| 182 | if (decoded.mcc == laid->mcc |
| 183 | && decoded.mnc == laid->mnc |
| 184 | && decoded.lac == laid->lac) |
| 185 | printf(" passed\n"); |
| 186 | else |
| 187 | printf(" FAIL\n"); |
| 188 | } |
| 189 | |
Neels Hofmeyr | 4566f4e | 2018-02-20 22:19:56 +0100 | [diff] [blame] | 190 | static inline void dump_lai(const struct osmo_location_area_id *lai) |
| 191 | { |
| 192 | printf("%s%s\n", osmo_lai_name(lai), lai->plmn.mnc_3_digits ? " (3-digit MNC)" : ""); |
| 193 | } |
| 194 | |
| 195 | static inline void check_lai2(const struct gprs_ra_id *raid) |
| 196 | { |
| 197 | struct gsm48_loc_area_id lai = {}; |
| 198 | struct osmo_location_area_id decoded = {}; |
| 199 | struct osmo_location_area_id laid = { |
| 200 | .plmn = { |
| 201 | .mcc = raid->mcc, |
| 202 | .mnc = raid->mnc, |
| 203 | .mnc_3_digits = raid->mnc_3_digits, |
| 204 | }, |
| 205 | .lac = raid->lac, |
| 206 | }; |
| 207 | |
| 208 | printf("- gsm48_generate_lai2() from "); dump_lai(&laid); |
| 209 | |
| 210 | gsm48_generate_lai2(&lai, &laid); |
| 211 | printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai))); |
| 212 | gsm48_decode_lai2(&lai, &decoded); |
| 213 | printf(" gsm48_decode_lai2() gives "); dump_lai(&decoded); |
| 214 | if (decoded.plmn.mcc == laid.plmn.mcc |
| 215 | && decoded.plmn.mnc == laid.plmn.mnc |
| 216 | && decoded.lac == laid.lac |
| 217 | && decoded.plmn.mnc_3_digits == (laid.plmn.mnc_3_digits || laid.plmn.mnc > 99)) |
| 218 | printf(" passed\n"); |
| 219 | else |
| 220 | printf(" FAIL\n"); |
| 221 | } |
| 222 | |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 223 | static struct gprs_ra_id test_ra_cap_items[] = { |
| 224 | { |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 225 | .mcc = 77, |
Neels Hofmeyr | 0bf93a6 | 2018-02-20 22:06:56 +0100 | [diff] [blame] | 226 | .mnc = 121, |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 227 | .lac = 666, |
| 228 | .rac = 5, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 229 | }, |
| 230 | { |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 231 | .mcc = 84, |
Neels Hofmeyr | 0bf93a6 | 2018-02-20 22:06:56 +0100 | [diff] [blame] | 232 | .mnc = 98, |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 233 | .lac = 11, |
| 234 | .rac = 89, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 235 | }, |
Neels Hofmeyr | b9fd7eb | 2018-02-20 15:14:03 +0100 | [diff] [blame] | 236 | { |
| 237 | .mcc = 0, |
| 238 | .mnc = 0, |
| 239 | .lac = 0, |
| 240 | .rac = 0, |
Neels Hofmeyr | 6c7b3e2 | 2018-02-20 22:20:42 +0100 | [diff] [blame] | 241 | .mnc_3_digits = false, |
| 242 | /* expecting 000-00, BCD = 00 f0 00 */ |
| 243 | }, |
| 244 | { |
| 245 | .mcc = 0, |
| 246 | .mnc = 0, |
| 247 | .lac = 0, |
| 248 | .rac = 0, |
| 249 | .mnc_3_digits = true, |
| 250 | /* expecting 000-000, BCD = 00 00 00 */ |
Neels Hofmeyr | b9fd7eb | 2018-02-20 15:14:03 +0100 | [diff] [blame] | 251 | }, |
| 252 | { |
| 253 | .mcc = 999, |
| 254 | .mnc = 999, |
| 255 | .lac = 65535, |
| 256 | .rac = 255, |
| 257 | }, |
Neels Hofmeyr | 6c7b3e2 | 2018-02-20 22:20:42 +0100 | [diff] [blame] | 258 | { |
| 259 | .mcc = 1, |
| 260 | .mnc = 2, |
| 261 | .lac = 23, |
| 262 | .rac = 42, |
| 263 | .mnc_3_digits = false, |
| 264 | /* expecting 001-02, BCD = 00 f1 20 */ |
| 265 | }, |
| 266 | { |
| 267 | .mcc = 1, |
| 268 | .mnc = 2, |
| 269 | .lac = 23, |
| 270 | .rac = 42, |
| 271 | .mnc_3_digits = true, |
| 272 | /* expecting 001-002, BCD = 00 21 00 */ |
| 273 | }, |
| 274 | { |
| 275 | .mcc = 12, |
| 276 | .mnc = 34, |
| 277 | .lac = 56, |
| 278 | .rac = 78, |
| 279 | .mnc_3_digits = false, |
| 280 | /* expecting 012-34, BCD = 10 f2 43 */ |
| 281 | }, |
| 282 | { |
| 283 | .mcc = 12, |
| 284 | .mnc = 34, |
| 285 | .lac = 23, |
| 286 | .rac = 42, |
| 287 | .mnc_3_digits = true, |
| 288 | /* expecting 012-034, BCD = 10 42 30 */ |
| 289 | }, |
| 290 | { |
| 291 | .mcc = 123, |
| 292 | .mnc = 456, |
| 293 | .lac = 23, |
| 294 | .rac = 42, |
| 295 | .mnc_3_digits = false, |
| 296 | /* expecting 123-456, BCD = 21 63 54 (false flag has no effect) */ |
| 297 | }, |
| 298 | { |
| 299 | .mcc = 123, |
| 300 | .mnc = 456, |
| 301 | .lac = 23, |
| 302 | .rac = 42, |
| 303 | .mnc_3_digits = true, |
| 304 | /* expecting 123-456, BCD = 21 63 54 (same) */ |
| 305 | }, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 306 | }; |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 307 | |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 308 | static void test_ra_cap(void) |
| 309 | { |
| 310 | int i; |
| 311 | |
| 312 | for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) |
| 313 | check_ra(&test_ra_cap_items[i]); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 316 | static void test_lai_encode_decode(void) |
| 317 | { |
| 318 | int i; |
| 319 | |
| 320 | for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) { |
| 321 | check_lai(&test_ra_cap_items[i]); |
Neels Hofmeyr | 4566f4e | 2018-02-20 22:19:56 +0100 | [diff] [blame] | 322 | check_lai2(&test_ra_cap_items[i]); |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 326 | static void test_mid_from_tmsi(void) |
| 327 | { |
| 328 | static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd }; |
| 329 | |
| 330 | |
| 331 | uint32_t tmsi = 0xAABBCCDD; |
| 332 | uint8_t buf[3 + sizeof(uint32_t)]; |
| 333 | |
| 334 | printf("Simple TMSI encoding test...."); |
| 335 | |
| 336 | memset(&buf, 0xFE, sizeof(buf)); |
| 337 | gsm48_generate_mid_from_tmsi(buf, tmsi); |
| 338 | |
| 339 | OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0); |
| 340 | printf("passed\n"); |
| 341 | } |
| 342 | |
Max | d55d7d4 | 2018-02-15 11:27:18 +0100 | [diff] [blame] | 343 | static void test_mid_from_imsi(void) |
| 344 | { |
| 345 | char *imsi = "901700000004620"; |
| 346 | uint8_t buf[10], len; |
| 347 | |
| 348 | printf("Simple IMSI encoding test...."); |
| 349 | |
| 350 | len = gsm48_generate_mid_from_imsi(buf, imsi); |
| 351 | |
| 352 | printf("passed: [%u] %s\n", len, osmo_hexdump(buf, len)); |
| 353 | } |
| 354 | |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 355 | struct test_mid_encode_decode_test { |
| 356 | uint8_t mi_type; |
| 357 | const char *mi_str; |
| 358 | size_t str_size; |
| 359 | const char *expect_mi_tlv_hex; |
| 360 | const char *expect_str; |
| 361 | int expect_rc; |
| 362 | }; |
| 363 | |
| 364 | static const struct test_mid_encode_decode_test test_mid_encode_decode_tests[] = { |
| 365 | { |
| 366 | .mi_type = GSM_MI_TYPE_IMSI, |
| 367 | .mi_str = "123456789012345", |
| 368 | .expect_mi_tlv_hex = "17081932547698103254", |
| 369 | }, |
| 370 | { |
| 371 | .mi_type = GSM_MI_TYPE_IMSI, |
| 372 | .mi_str = "12345678901234", |
| 373 | .expect_mi_tlv_hex = "170811325476981032f4", |
| 374 | }, |
| 375 | { |
| 376 | .mi_type = GSM_MI_TYPE_IMSI, |
| 377 | .mi_str = "423423", |
| 378 | .expect_mi_tlv_hex = "1704413224f3", |
| 379 | }, |
| 380 | { |
| 381 | .mi_type = GSM_MI_TYPE_IMSI | GSM_MI_ODD, |
| 382 | .mi_str = "423423", |
Neels Hofmeyr | 23187fa | 2018-12-05 23:24:50 +0100 | [diff] [blame] | 383 | .expect_mi_tlv_hex = "1704413224f3", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 384 | }, |
| 385 | { |
| 386 | .mi_type = GSM_MI_TYPE_IMSI, |
| 387 | .mi_str = "4234235", |
| 388 | .expect_mi_tlv_hex = "170449322453", |
| 389 | }, |
| 390 | { |
| 391 | .mi_type = GSM_MI_TYPE_IMSI, |
| 392 | .mi_str = "4234235", |
| 393 | .expect_mi_tlv_hex = "170449322453", |
| 394 | .str_size = 4, |
| 395 | .expect_str = "423", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 396 | }, |
| 397 | { |
| 398 | .mi_type = GSM_MI_TYPE_IMEI, |
| 399 | .mi_str = "123456789012345", |
| 400 | .expect_mi_tlv_hex = "17081a32547698103254", |
| 401 | }, |
| 402 | { |
| 403 | .mi_type = GSM_MI_TYPE_IMEI, |
| 404 | .mi_str = "98765432109876", |
| 405 | .expect_mi_tlv_hex = "170892785634129078f6", |
| 406 | }, |
| 407 | { |
| 408 | .mi_type = GSM_MI_TYPE_IMEI, |
| 409 | .mi_str = "987654321098765", |
| 410 | .expect_mi_tlv_hex = "17089a78563412907856", |
| 411 | }, |
| 412 | { |
| 413 | .mi_type = GSM_MI_TYPE_IMEISV, |
| 414 | .mi_str = "987654321098765432", |
| 415 | .expect_mi_tlv_hex = "170a937856341290785634f2", |
| 416 | }, |
| 417 | { |
| 418 | .mi_type = GSM_MI_TYPE_IMEISV, |
| 419 | .mi_str = "987654321098765432", |
| 420 | .expect_mi_tlv_hex = "170a937856341290785634f2", |
| 421 | .str_size = 16, |
| 422 | .expect_str = "987654321098765", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 423 | }, |
| 424 | { |
| 425 | /* gsm48 treats TMSI as decimal string */ |
| 426 | .mi_type = GSM_MI_TYPE_TMSI, |
| 427 | .mi_str = "305419896", /* 0x12345678 as decimal */ |
| 428 | .expect_mi_tlv_hex = "1705f412345678", |
| 429 | .expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 430 | }, |
| 431 | { |
| 432 | .mi_type = GSM_MI_TYPE_TMSI, |
| 433 | .mi_str = "12648430", /* 0xc0ffee as decimal */ |
| 434 | .expect_mi_tlv_hex = "1705f400c0ffee", |
| 435 | .expect_rc = 8, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 436 | }, |
| 437 | { |
| 438 | .mi_type = GSM_MI_TYPE_TMSI, |
| 439 | .mi_str = "0", |
| 440 | .expect_mi_tlv_hex = "1705f400000000", |
| 441 | .expect_rc = 1, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 442 | }, |
| 443 | { |
| 444 | /* gsm48 treats TMSI as decimal string */ |
| 445 | .mi_type = GSM_MI_TYPE_TMSI, |
| 446 | .mi_str = "305419896", /* 0x12345678 as decimal */ |
| 447 | .expect_mi_tlv_hex = "1705f412345678", |
| 448 | .str_size = 5, |
| 449 | .expect_str = "3054", |
| 450 | .expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns would-be strlen() like snprintf()! */ |
| 451 | }, |
| 452 | { |
| 453 | .mi_type = GSM_MI_TYPE_NONE, |
| 454 | .mi_str = "123", |
| 455 | .expect_mi_tlv_hex = "17021832", /* encoding invalid MI type */ |
| 456 | .expect_str = "", |
| 457 | }, |
| 458 | { |
| 459 | .mi_type = GSM_MI_TYPE_NONE, |
| 460 | .mi_str = "1234", |
| 461 | .expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */ |
| 462 | .expect_str = "", |
| 463 | }, |
| 464 | { |
| 465 | .mi_type = GSM_MI_ODD, |
| 466 | .mi_str = "1234", |
Neels Hofmeyr | 23187fa | 2018-12-05 23:24:50 +0100 | [diff] [blame] | 467 | .expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */ |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 468 | .expect_str = "", |
| 469 | }, |
| 470 | }; |
| 471 | |
| 472 | static void test_mid_encode_decode(void) |
| 473 | { |
| 474 | int i; |
| 475 | |
| 476 | printf("\nTesting Mobile Identity conversions\n"); |
| 477 | |
| 478 | for (i = 0; i < ARRAY_SIZE(test_mid_encode_decode_tests); i++) { |
| 479 | const struct test_mid_encode_decode_test *t = &test_mid_encode_decode_tests[i]; |
| 480 | uint8_t tlv_buf[64]; |
| 481 | uint8_t *mi_buf; |
| 482 | int tlv_len; |
| 483 | int mi_len; |
| 484 | const char *tlv_hex; |
| 485 | char str[64] = {}; |
| 486 | size_t str_size = t->str_size ? : sizeof(str); |
| 487 | const char *expect_str = t->expect_str ? : t->mi_str; |
| 488 | int expect_rc = t->expect_rc ? : strlen(expect_str)+1; |
| 489 | int rc; |
| 490 | int str_len; |
| 491 | |
| 492 | printf("- %s %s\n", gsm48_mi_type_name(t->mi_type), t->mi_str); |
| 493 | if (t->mi_type == GSM_MI_TYPE_TMSI) |
| 494 | tlv_len = gsm48_generate_mid_from_tmsi(tlv_buf, (uint32_t)atoll(t->mi_str)); |
| 495 | else |
| 496 | tlv_len = gsm48_generate_mid(tlv_buf, t->mi_str, t->mi_type); |
| 497 | tlv_hex = osmo_hexdump_nospc(tlv_buf, tlv_len); |
| 498 | |
| 499 | printf(" -> MI-TLV-hex='%s'\n", tlv_hex); |
| 500 | if (t->expect_mi_tlv_hex && strcmp(tlv_hex, t->expect_mi_tlv_hex)) { |
| 501 | printf(" ERROR: expected '%s'\n", t->expect_mi_tlv_hex); |
| 502 | } |
| 503 | |
| 504 | /* skip the GSM48_IE_MOBILE_ID tag and length */ |
| 505 | mi_buf = tlv_buf + 2; |
| 506 | mi_len = tlv_len - 2; |
| 507 | |
| 508 | rc = gsm48_mi_to_string(str, str_size, mi_buf, mi_len); |
| 509 | printf(" -> MI-str=%s rc=%d\n", osmo_quote_str(str, -1), rc); |
| 510 | if (strcmp(str, expect_str)) |
| 511 | printf(" ERROR: expected MI-str=%s\n", osmo_quote_str(expect_str, -1)); |
| 512 | if (rc != expect_rc) |
| 513 | printf(" ERROR: expected rc=%d\n", expect_rc); |
| 514 | |
| 515 | /* Now make sure the resulting string is always '\0' terminated. |
| 516 | * The above started out with a zeroed buffer, now repeat with a tainted one. */ |
| 517 | str_len = strlen(str); |
| 518 | str[str_len] = '!'; |
| 519 | gsm48_mi_to_string(str, str_size, mi_buf, mi_len); |
| 520 | if (strlen(str) != str_len) |
| 521 | printf(" ERROR: resulting string is not explicitly nul terminated\n"); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | static const uint8_t test_mid_decode_zero_length_types[] = { GSM_MI_TYPE_IMSI, GSM_MI_TYPE_TMSI, GSM_MI_TYPE_NONE }; |
| 526 | |
| 527 | static void test_mid_decode_zero_length(void) |
| 528 | { |
| 529 | int odd; |
| 530 | uint8_t valid_mi[64]; |
| 531 | int valid_mi_len; |
| 532 | |
| 533 | printf("\nDecoding zero length Mobile Identities\n"); |
| 534 | |
| 535 | /* IMSI = 123456789012345 */ |
| 536 | valid_mi_len = osmo_hexparse("1932547698103254", valid_mi, sizeof(valid_mi)); |
| 537 | |
| 538 | for (odd = 0; odd <= 1; odd++) { |
| 539 | int i; |
| 540 | for (i = 0; i < ARRAY_SIZE(test_mid_decode_zero_length_types); i++) { |
| 541 | uint8_t mi_type = test_mid_decode_zero_length_types[i] | (odd ? GSM_MI_ODD : 0); |
| 542 | char str[8] = {}; |
| 543 | int rc; |
| 544 | |
| 545 | printf("- MI type: %s%s\n", gsm48_mi_type_name(mi_type & GSM_MI_TYPE_MASK), |
| 546 | odd ? " | GSM_MI_ODD":""); |
| 547 | valid_mi[0] = (valid_mi[0] & 0xf0) | mi_type; |
| 548 | |
| 549 | printf(" - writing to zero-length string:\n"); |
| 550 | memset(str, '!', sizeof(str) - 1); |
| 551 | rc = gsm48_mi_to_string(str, 0, valid_mi, valid_mi_len); |
| 552 | printf(" rc=%d\n", rc); |
| 553 | if (str[0] == '!') |
| 554 | printf(" nothing written\n"); |
| 555 | else |
| 556 | printf(" ERROR: Wrote to invalid memory!\n"); |
| 557 | |
| 558 | printf(" - writing to 1-byte-length string:\n"); |
| 559 | memset(str, '!', sizeof(str) - 1); |
| 560 | rc = gsm48_mi_to_string(str, 1, valid_mi, valid_mi_len); |
| 561 | printf(" rc=%d\n", rc); |
| 562 | if (str[0] == '\0') |
| 563 | printf(" returned empty string\n"); |
| 564 | else if (str[0] == '!') |
| 565 | printf(" ERROR: nothing written, expected nul-terminated empty string\n"); |
| 566 | else |
| 567 | printf(" ERROR: Wrote unexpected string %s\n", osmo_quote_str(str, 5)); |
| 568 | if (str[1] != '!') |
| 569 | printf(" ERROR: Wrote to invalid memory!\n"); |
| 570 | |
| 571 | printf(" - decode zero-length mi:\n"); |
| 572 | memset(str, '!', sizeof(str) - 1); |
| 573 | rc = gsm48_mi_to_string(str, sizeof(str), valid_mi, 0); |
| 574 | printf(" rc=%d\n", rc); |
| 575 | if (str[0] == '\0') |
| 576 | printf(" returned empty string\n"); |
| 577 | else if (str[0] == '!') |
| 578 | printf(" ERROR: nothing written, expected nul-terminated empty string\n"); |
| 579 | else |
| 580 | printf(" ERROR: expected empty string, got output string: %s\n", osmo_quote_str(str, -1)); |
| 581 | } |
| 582 | } |
| 583 | printf("\n"); |
| 584 | } |
| 585 | |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 586 | int main(int argc, char **argv) |
| 587 | { |
| 588 | test_bearer_cap(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 589 | test_mid_from_tmsi(); |
Max | d55d7d4 | 2018-02-15 11:27:18 +0100 | [diff] [blame] | 590 | test_mid_from_imsi(); |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 591 | test_mid_encode_decode(); |
| 592 | test_mid_decode_zero_length(); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 593 | test_ra_cap(); |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 594 | test_lai_encode_decode(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 595 | |
| 596 | return EXIT_SUCCESS; |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 597 | } |