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> |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/gsm48_arfcn_range_encode.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/mncc.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 30 | #include <osmocom/core/backtrace.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 31 | #include <osmocom/core/utils.h> |
| 32 | #include <osmocom/core/msgb.h> |
| 33 | |
| 34 | |
| 35 | static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 }; |
| 36 | |
| 37 | static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = { |
| 38 | .transfer = GSM48_BCAP_ITCAP_UNR_DIG_INF, |
| 39 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 40 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 41 | .radio = GSM48_BCAP_RRQ_FR_ONLY, |
| 42 | .speech_ver[0]= -1, |
| 43 | .data = { |
| 44 | .rate_adaption = GSM48_BCAP_RA_V110_X30, |
| 45 | .sig_access = GSM48_BCAP_SA_I440_I450, |
| 46 | .async = 1, |
| 47 | .nr_stop_bits = 1, |
| 48 | .nr_data_bits = 8, |
| 49 | .user_rate = GSM48_BCAP_UR_9600, |
| 50 | .parity = GSM48_BCAP_PAR_NONE, |
| 51 | .interm_rate = GSM48_BCAP_IR_16k, |
| 52 | .transp = GSM48_BCAP_TR_TRANSP, |
| 53 | .modem_type = GSM48_BCAP_MT_NONE, |
| 54 | }, |
| 55 | }; |
| 56 | |
| 57 | static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 }; |
| 58 | |
| 59 | static const struct gsm_mncc_bearer_cap bcap_speech_all = { |
| 60 | .transfer = GSM48_BCAP_ITCAP_SPEECH, |
| 61 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 62 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 63 | .radio = GSM48_BCAP_RRQ_DUAL_FR, |
| 64 | .speech_ver = { |
| 65 | 4, 2, 0, 5, 1, -1, |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | struct bcap_test { |
| 71 | const uint8_t *lv; |
| 72 | const struct gsm_mncc_bearer_cap *bc; |
| 73 | const char *name; |
| 74 | }; |
| 75 | |
| 76 | static const struct bcap_test bcap_tests[] = { |
| 77 | { csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" }, |
| 78 | { speech_all_lv, &bcap_speech_all, "Speech, all codecs" }, |
| 79 | }; |
| 80 | |
| 81 | static int test_bearer_cap() |
| 82 | { |
| 83 | struct gsm_mncc_bearer_cap bc; |
| 84 | int i, rc; |
| 85 | |
| 86 | for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) { |
| 87 | struct msgb *msg = msgb_alloc(100, "test"); |
| 88 | int lv_len; |
| 89 | |
| 90 | memset(&bc, 0, sizeof(bc)); |
| 91 | |
| 92 | /* test decoding */ |
| 93 | rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv); |
| 94 | if (rc < 0) { |
| 95 | fprintf(stderr, "Error decoding %s\n", |
| 96 | bcap_tests[i].name); |
| 97 | return rc; |
| 98 | } |
| 99 | if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) { |
| 100 | fprintf(stderr, "Incorrect decoded result of %s:\n", |
| 101 | bcap_tests[i].name); |
| 102 | fprintf(stderr, " should: %s\n", |
| 103 | osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc))); |
| 104 | fprintf(stderr, " is: %s\n", |
| 105 | osmo_hexdump((uint8_t *) &bc, sizeof(bc))); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | /* also test re-encode? */ |
| 110 | rc = gsm48_encode_bearer_cap(msg, 1, &bc); |
| 111 | if (rc < 0) { |
| 112 | fprintf(stderr, "Error encoding %s\n", |
| 113 | bcap_tests[i].name); |
| 114 | return rc; |
| 115 | } |
| 116 | lv_len = bcap_tests[i].lv[0]+1; |
| 117 | if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) { |
| 118 | fprintf(stderr, "Incorrect encoded result of %s:\n", |
| 119 | bcap_tests[i].name); |
| 120 | fprintf(stderr, " should: %s\n", |
| 121 | osmo_hexdump(bcap_tests[i].lv, lv_len)); |
| 122 | fprintf(stderr, " is: %s\n", |
| 123 | osmo_hexdump(msg->data, msg->len)); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | printf("Test `%s' passed\n", bcap_tests[i].name); |
| 128 | msgb_free(msg); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 134 | static inline void dump_ra(const struct gprs_ra_id *raid) |
| 135 | { |
Neels Hofmeyr | dbb2513 | 2018-02-20 15:12:23 +0100 | [diff] [blame] | 136 | 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] | 137 | } |
| 138 | |
| 139 | static inline void check_ra(const struct gprs_ra_id *raid) |
| 140 | { |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 141 | struct gsm48_ra_id ra; |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 142 | struct gprs_ra_id raid0 = { |
| 143 | .mnc = 0, |
| 144 | .mcc = 0, |
| 145 | .lac = 0, |
| 146 | .rac = 0, |
| 147 | }; |
| 148 | |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 149 | gsm48_encode_ra(&ra, raid); |
| 150 | printf("Constructed RA:\n"); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 151 | |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 152 | gsm48_parse_ra(&raid0, (const uint8_t *)&ra); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 153 | dump_ra(raid); |
Neels Hofmeyr | c38b32d | 2018-02-20 15:13:18 +0100 | [diff] [blame] | 154 | 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] | 155 | dump_ra(&raid0); |
| 156 | printf("RA test..."); |
Neels Hofmeyr | dbb2513 | 2018-02-20 15:12:23 +0100 | [diff] [blame] | 157 | if (raid->mnc != raid0.mnc || raid->mcc != raid0.mcc || raid->lac != raid0.lac || raid->rac != raid0.rac |
| 158 | || (raid->mnc_3_digits || raid->mnc > 99) != raid0.mnc_3_digits) |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 159 | printf("FAIL\n"); |
| 160 | else |
| 161 | printf("passed\n"); |
| 162 | } |
| 163 | |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 164 | static inline void check_lai(const struct gprs_ra_id *raid) |
| 165 | { |
| 166 | int rc; |
| 167 | struct gsm48_loc_area_id lai = {}; |
| 168 | struct gprs_ra_id decoded = {}; |
| 169 | struct gprs_ra_id _laid = *raid; |
| 170 | struct gprs_ra_id *laid = &_laid; |
| 171 | laid->rac = 0; |
| 172 | |
| 173 | printf("- gsm48_generate_lai() from "); dump_ra(laid); |
| 174 | |
| 175 | gsm48_generate_lai(&lai, laid->mcc, laid->mnc, laid->lac); |
| 176 | printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai))); |
| 177 | rc = gsm48_decode_lai(&lai, &decoded.mcc, &decoded.mnc, &decoded.lac); |
| 178 | if (rc) { |
| 179 | printf(" gsm48_decode_lai() returned %d --> FAIL\n", rc); |
| 180 | return; |
| 181 | } |
| 182 | printf(" gsm48_decode_lai() gives "); dump_ra(&decoded); |
| 183 | if (decoded.mcc == laid->mcc |
| 184 | && decoded.mnc == laid->mnc |
| 185 | && decoded.lac == laid->lac) |
| 186 | printf(" passed\n"); |
| 187 | else |
| 188 | printf(" FAIL\n"); |
| 189 | } |
| 190 | |
Neels Hofmeyr | 4566f4e | 2018-02-20 22:19:56 +0100 | [diff] [blame] | 191 | static inline void dump_lai(const struct osmo_location_area_id *lai) |
| 192 | { |
| 193 | printf("%s%s\n", osmo_lai_name(lai), lai->plmn.mnc_3_digits ? " (3-digit MNC)" : ""); |
| 194 | } |
| 195 | |
| 196 | static inline void check_lai2(const struct gprs_ra_id *raid) |
| 197 | { |
| 198 | struct gsm48_loc_area_id lai = {}; |
| 199 | struct osmo_location_area_id decoded = {}; |
| 200 | struct osmo_location_area_id laid = { |
| 201 | .plmn = { |
| 202 | .mcc = raid->mcc, |
| 203 | .mnc = raid->mnc, |
| 204 | .mnc_3_digits = raid->mnc_3_digits, |
| 205 | }, |
| 206 | .lac = raid->lac, |
| 207 | }; |
| 208 | |
| 209 | printf("- gsm48_generate_lai2() from "); dump_lai(&laid); |
| 210 | |
| 211 | gsm48_generate_lai2(&lai, &laid); |
| 212 | printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai))); |
| 213 | gsm48_decode_lai2(&lai, &decoded); |
| 214 | printf(" gsm48_decode_lai2() gives "); dump_lai(&decoded); |
| 215 | if (decoded.plmn.mcc == laid.plmn.mcc |
| 216 | && decoded.plmn.mnc == laid.plmn.mnc |
| 217 | && decoded.lac == laid.lac |
| 218 | && decoded.plmn.mnc_3_digits == (laid.plmn.mnc_3_digits || laid.plmn.mnc > 99)) |
| 219 | printf(" passed\n"); |
| 220 | else |
| 221 | printf(" FAIL\n"); |
| 222 | } |
| 223 | |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 224 | static struct gprs_ra_id test_ra_cap_items[] = { |
| 225 | { |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 226 | .mcc = 77, |
Neels Hofmeyr | 0bf93a6 | 2018-02-20 22:06:56 +0100 | [diff] [blame] | 227 | .mnc = 121, |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 228 | .lac = 666, |
| 229 | .rac = 5, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 230 | }, |
| 231 | { |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 232 | .mcc = 84, |
Neels Hofmeyr | 0bf93a6 | 2018-02-20 22:06:56 +0100 | [diff] [blame] | 233 | .mnc = 98, |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 234 | .lac = 11, |
| 235 | .rac = 89, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 236 | }, |
Neels Hofmeyr | b9fd7eb | 2018-02-20 15:14:03 +0100 | [diff] [blame] | 237 | { |
| 238 | .mcc = 0, |
| 239 | .mnc = 0, |
| 240 | .lac = 0, |
| 241 | .rac = 0, |
Neels Hofmeyr | 6c7b3e2 | 2018-02-20 22:20:42 +0100 | [diff] [blame] | 242 | .mnc_3_digits = false, |
| 243 | /* expecting 000-00, BCD = 00 f0 00 */ |
| 244 | }, |
| 245 | { |
| 246 | .mcc = 0, |
| 247 | .mnc = 0, |
| 248 | .lac = 0, |
| 249 | .rac = 0, |
| 250 | .mnc_3_digits = true, |
| 251 | /* expecting 000-000, BCD = 00 00 00 */ |
Neels Hofmeyr | b9fd7eb | 2018-02-20 15:14:03 +0100 | [diff] [blame] | 252 | }, |
| 253 | { |
| 254 | .mcc = 999, |
| 255 | .mnc = 999, |
| 256 | .lac = 65535, |
| 257 | .rac = 255, |
| 258 | }, |
Neels Hofmeyr | 6c7b3e2 | 2018-02-20 22:20:42 +0100 | [diff] [blame] | 259 | { |
| 260 | .mcc = 1, |
| 261 | .mnc = 2, |
| 262 | .lac = 23, |
| 263 | .rac = 42, |
| 264 | .mnc_3_digits = false, |
| 265 | /* expecting 001-02, BCD = 00 f1 20 */ |
| 266 | }, |
| 267 | { |
| 268 | .mcc = 1, |
| 269 | .mnc = 2, |
| 270 | .lac = 23, |
| 271 | .rac = 42, |
| 272 | .mnc_3_digits = true, |
| 273 | /* expecting 001-002, BCD = 00 21 00 */ |
| 274 | }, |
| 275 | { |
| 276 | .mcc = 12, |
| 277 | .mnc = 34, |
| 278 | .lac = 56, |
| 279 | .rac = 78, |
| 280 | .mnc_3_digits = false, |
| 281 | /* expecting 012-34, BCD = 10 f2 43 */ |
| 282 | }, |
| 283 | { |
| 284 | .mcc = 12, |
| 285 | .mnc = 34, |
| 286 | .lac = 23, |
| 287 | .rac = 42, |
| 288 | .mnc_3_digits = true, |
| 289 | /* expecting 012-034, BCD = 10 42 30 */ |
| 290 | }, |
| 291 | { |
| 292 | .mcc = 123, |
| 293 | .mnc = 456, |
| 294 | .lac = 23, |
| 295 | .rac = 42, |
| 296 | .mnc_3_digits = false, |
| 297 | /* expecting 123-456, BCD = 21 63 54 (false flag has no effect) */ |
| 298 | }, |
| 299 | { |
| 300 | .mcc = 123, |
| 301 | .mnc = 456, |
| 302 | .lac = 23, |
| 303 | .rac = 42, |
| 304 | .mnc_3_digits = true, |
| 305 | /* expecting 123-456, BCD = 21 63 54 (same) */ |
| 306 | }, |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 307 | }; |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 308 | |
Neels Hofmeyr | 22da145 | 2018-02-20 15:11:25 +0100 | [diff] [blame] | 309 | static void test_ra_cap(void) |
| 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) |
| 314 | check_ra(&test_ra_cap_items[i]); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 317 | static void test_lai_encode_decode(void) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) { |
| 322 | check_lai(&test_ra_cap_items[i]); |
Neels Hofmeyr | 4566f4e | 2018-02-20 22:19:56 +0100 | [diff] [blame] | 323 | check_lai2(&test_ra_cap_items[i]); |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 327 | static void test_mid_from_tmsi(void) |
| 328 | { |
| 329 | static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd }; |
| 330 | |
| 331 | |
| 332 | uint32_t tmsi = 0xAABBCCDD; |
| 333 | uint8_t buf[3 + sizeof(uint32_t)]; |
| 334 | |
| 335 | printf("Simple TMSI encoding test...."); |
| 336 | |
| 337 | memset(&buf, 0xFE, sizeof(buf)); |
| 338 | gsm48_generate_mid_from_tmsi(buf, tmsi); |
| 339 | |
| 340 | OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0); |
| 341 | printf("passed\n"); |
| 342 | } |
| 343 | |
Max | d55d7d4 | 2018-02-15 11:27:18 +0100 | [diff] [blame] | 344 | static void test_mid_from_imsi(void) |
| 345 | { |
| 346 | char *imsi = "901700000004620"; |
| 347 | uint8_t buf[10], len; |
| 348 | |
| 349 | printf("Simple IMSI encoding test...."); |
| 350 | |
| 351 | len = gsm48_generate_mid_from_imsi(buf, imsi); |
| 352 | |
| 353 | printf("passed: [%u] %s\n", len, osmo_hexdump(buf, len)); |
| 354 | } |
| 355 | |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 356 | struct test_mid_encode_decode_test { |
| 357 | uint8_t mi_type; |
| 358 | const char *mi_str; |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 359 | const char *mi_name; |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 360 | size_t str_size; |
| 361 | const char *expect_mi_tlv_hex; |
| 362 | const char *expect_str; |
| 363 | int expect_rc; |
| 364 | }; |
| 365 | |
| 366 | static const struct test_mid_encode_decode_test test_mid_encode_decode_tests[] = { |
| 367 | { |
| 368 | .mi_type = GSM_MI_TYPE_IMSI, |
| 369 | .mi_str = "123456789012345", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 370 | .mi_name = "IMSI-123456789012345", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 371 | .expect_mi_tlv_hex = "17081932547698103254", |
| 372 | }, |
| 373 | { |
| 374 | .mi_type = GSM_MI_TYPE_IMSI, |
| 375 | .mi_str = "12345678901234", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 376 | .mi_name = "IMSI-12345678901234", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 377 | .expect_mi_tlv_hex = "170811325476981032f4", |
| 378 | }, |
| 379 | { |
| 380 | .mi_type = GSM_MI_TYPE_IMSI, |
| 381 | .mi_str = "423423", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 382 | .mi_name = "IMSI-423423", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 383 | .expect_mi_tlv_hex = "1704413224f3", |
| 384 | }, |
| 385 | { |
| 386 | .mi_type = GSM_MI_TYPE_IMSI | GSM_MI_ODD, |
| 387 | .mi_str = "423423", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 388 | .mi_name = "IMSI-423423", |
Neels Hofmeyr | 23187fa | 2018-12-05 23:24:50 +0100 | [diff] [blame] | 389 | .expect_mi_tlv_hex = "1704413224f3", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 390 | }, |
| 391 | { |
| 392 | .mi_type = GSM_MI_TYPE_IMSI, |
| 393 | .mi_str = "4234235", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 394 | .mi_name = "IMSI-4234235", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 395 | .expect_mi_tlv_hex = "170449322453", |
| 396 | }, |
| 397 | { |
| 398 | .mi_type = GSM_MI_TYPE_IMSI, |
| 399 | .mi_str = "4234235", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 400 | .mi_name = "IMSI-4234235", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 401 | .expect_mi_tlv_hex = "170449322453", |
| 402 | .str_size = 4, |
| 403 | .expect_str = "423", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 404 | }, |
| 405 | { |
| 406 | .mi_type = GSM_MI_TYPE_IMEI, |
| 407 | .mi_str = "123456789012345", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 408 | .mi_name = "IMEI-123456789012345", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 409 | .expect_mi_tlv_hex = "17081a32547698103254", |
| 410 | }, |
| 411 | { |
| 412 | .mi_type = GSM_MI_TYPE_IMEI, |
| 413 | .mi_str = "98765432109876", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 414 | .mi_name = "IMEI-98765432109876", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 415 | .expect_mi_tlv_hex = "170892785634129078f6", |
| 416 | }, |
| 417 | { |
| 418 | .mi_type = GSM_MI_TYPE_IMEI, |
| 419 | .mi_str = "987654321098765", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 420 | .mi_name = "IMEI-987654321098765", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 421 | .expect_mi_tlv_hex = "17089a78563412907856", |
| 422 | }, |
| 423 | { |
| 424 | .mi_type = GSM_MI_TYPE_IMEISV, |
Harald Welte | 1317771 | 2019-01-20 13:41:26 +0100 | [diff] [blame] | 425 | .mi_str = "9876543210987654", |
| 426 | .mi_name = "IMEI-SV-9876543210987654", |
| 427 | .expect_mi_tlv_hex = "17099378563412907856f4", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 428 | }, |
| 429 | { |
| 430 | .mi_type = GSM_MI_TYPE_IMEISV, |
Harald Welte | 1317771 | 2019-01-20 13:41:26 +0100 | [diff] [blame] | 431 | .mi_str = "9876543210987654", |
| 432 | .mi_name = "IMEI-SV-9876543210987654", |
| 433 | .expect_mi_tlv_hex = "17099378563412907856f4", |
| 434 | .str_size = 17, |
| 435 | .expect_str = "9876543210987654", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 436 | }, |
| 437 | { |
| 438 | /* gsm48 treats TMSI as decimal string */ |
| 439 | .mi_type = GSM_MI_TYPE_TMSI, |
| 440 | .mi_str = "305419896", /* 0x12345678 as decimal */ |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 441 | .mi_name = "TMSI-0x12345678", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 442 | .expect_mi_tlv_hex = "1705f412345678", |
| 443 | .expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 444 | }, |
| 445 | { |
| 446 | .mi_type = GSM_MI_TYPE_TMSI, |
| 447 | .mi_str = "12648430", /* 0xc0ffee as decimal */ |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 448 | .mi_name = "TMSI-0x00C0FFEE", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 449 | .expect_mi_tlv_hex = "1705f400c0ffee", |
| 450 | .expect_rc = 8, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 451 | }, |
| 452 | { |
| 453 | .mi_type = GSM_MI_TYPE_TMSI, |
| 454 | .mi_str = "0", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 455 | .mi_name = "TMSI-0x00000000", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 456 | .expect_mi_tlv_hex = "1705f400000000", |
| 457 | .expect_rc = 1, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */ |
| 458 | }, |
| 459 | { |
| 460 | /* gsm48 treats TMSI as decimal string */ |
| 461 | .mi_type = GSM_MI_TYPE_TMSI, |
| 462 | .mi_str = "305419896", /* 0x12345678 as decimal */ |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 463 | .mi_name = "TMSI-0x12345678", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 464 | .expect_mi_tlv_hex = "1705f412345678", |
| 465 | .str_size = 5, |
| 466 | .expect_str = "3054", |
| 467 | .expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns would-be strlen() like snprintf()! */ |
| 468 | }, |
| 469 | { |
| 470 | .mi_type = GSM_MI_TYPE_NONE, |
| 471 | .mi_str = "123", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 472 | .mi_name = "unknown", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 473 | .expect_mi_tlv_hex = "17021832", /* encoding invalid MI type */ |
| 474 | .expect_str = "", |
| 475 | }, |
| 476 | { |
| 477 | .mi_type = GSM_MI_TYPE_NONE, |
| 478 | .mi_str = "1234", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 479 | .mi_name = "unknown", |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 480 | .expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */ |
| 481 | .expect_str = "", |
| 482 | }, |
| 483 | { |
| 484 | .mi_type = GSM_MI_ODD, |
| 485 | .mi_str = "1234", |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 486 | .mi_name = "unknown", |
Neels Hofmeyr | 23187fa | 2018-12-05 23:24:50 +0100 | [diff] [blame] | 487 | .expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */ |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 488 | .expect_str = "", |
| 489 | }, |
| 490 | }; |
| 491 | |
| 492 | static void test_mid_encode_decode(void) |
| 493 | { |
| 494 | int i; |
| 495 | |
| 496 | printf("\nTesting Mobile Identity conversions\n"); |
| 497 | |
| 498 | for (i = 0; i < ARRAY_SIZE(test_mid_encode_decode_tests); i++) { |
| 499 | const struct test_mid_encode_decode_test *t = &test_mid_encode_decode_tests[i]; |
| 500 | uint8_t tlv_buf[64]; |
| 501 | uint8_t *mi_buf; |
| 502 | int tlv_len; |
| 503 | int mi_len; |
| 504 | const char *tlv_hex; |
| 505 | char str[64] = {}; |
| 506 | size_t str_size = t->str_size ? : sizeof(str); |
| 507 | const char *expect_str = t->expect_str ? : t->mi_str; |
| 508 | int expect_rc = t->expect_rc ? : strlen(expect_str)+1; |
| 509 | int rc; |
| 510 | int str_len; |
| 511 | |
| 512 | printf("- %s %s\n", gsm48_mi_type_name(t->mi_type), t->mi_str); |
| 513 | if (t->mi_type == GSM_MI_TYPE_TMSI) |
| 514 | tlv_len = gsm48_generate_mid_from_tmsi(tlv_buf, (uint32_t)atoll(t->mi_str)); |
| 515 | else |
| 516 | tlv_len = gsm48_generate_mid(tlv_buf, t->mi_str, t->mi_type); |
| 517 | tlv_hex = osmo_hexdump_nospc(tlv_buf, tlv_len); |
| 518 | |
| 519 | printf(" -> MI-TLV-hex='%s'\n", tlv_hex); |
| 520 | if (t->expect_mi_tlv_hex && strcmp(tlv_hex, t->expect_mi_tlv_hex)) { |
| 521 | printf(" ERROR: expected '%s'\n", t->expect_mi_tlv_hex); |
| 522 | } |
| 523 | |
| 524 | /* skip the GSM48_IE_MOBILE_ID tag and length */ |
| 525 | mi_buf = tlv_buf + 2; |
| 526 | mi_len = tlv_len - 2; |
| 527 | |
| 528 | rc = gsm48_mi_to_string(str, str_size, mi_buf, mi_len); |
| 529 | printf(" -> MI-str=%s rc=%d\n", osmo_quote_str(str, -1), rc); |
| 530 | if (strcmp(str, expect_str)) |
| 531 | printf(" ERROR: expected MI-str=%s\n", osmo_quote_str(expect_str, -1)); |
| 532 | if (rc != expect_rc) |
| 533 | printf(" ERROR: expected rc=%d\n", expect_rc); |
| 534 | |
Neels Hofmeyr | 02fd83d | 2019-01-05 00:38:54 +0100 | [diff] [blame] | 535 | if (t->mi_name) { |
| 536 | const char *mi_name = osmo_mi_name(mi_buf, mi_len); |
| 537 | printf(" -> MI-name=%s\n", osmo_quote_str(mi_name, -1)); |
| 538 | if (strcmp(mi_name, t->mi_name)) |
| 539 | printf(" ERROR: expected MI-name=%s\n", osmo_quote_str(t->mi_name, -1)); |
| 540 | } |
| 541 | |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 542 | /* Now make sure the resulting string is always '\0' terminated. |
| 543 | * The above started out with a zeroed buffer, now repeat with a tainted one. */ |
| 544 | str_len = strlen(str); |
| 545 | str[str_len] = '!'; |
| 546 | gsm48_mi_to_string(str, str_size, mi_buf, mi_len); |
| 547 | if (strlen(str) != str_len) |
| 548 | printf(" ERROR: resulting string is not explicitly nul terminated\n"); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static const uint8_t test_mid_decode_zero_length_types[] = { GSM_MI_TYPE_IMSI, GSM_MI_TYPE_TMSI, GSM_MI_TYPE_NONE }; |
| 553 | |
| 554 | static void test_mid_decode_zero_length(void) |
| 555 | { |
| 556 | int odd; |
| 557 | uint8_t valid_mi[64]; |
| 558 | int valid_mi_len; |
| 559 | |
| 560 | printf("\nDecoding zero length Mobile Identities\n"); |
| 561 | |
| 562 | /* IMSI = 123456789012345 */ |
| 563 | valid_mi_len = osmo_hexparse("1932547698103254", valid_mi, sizeof(valid_mi)); |
| 564 | |
| 565 | for (odd = 0; odd <= 1; odd++) { |
| 566 | int i; |
| 567 | for (i = 0; i < ARRAY_SIZE(test_mid_decode_zero_length_types); i++) { |
| 568 | uint8_t mi_type = test_mid_decode_zero_length_types[i] | (odd ? GSM_MI_ODD : 0); |
| 569 | char str[8] = {}; |
| 570 | int rc; |
| 571 | |
| 572 | printf("- MI type: %s%s\n", gsm48_mi_type_name(mi_type & GSM_MI_TYPE_MASK), |
| 573 | odd ? " | GSM_MI_ODD":""); |
| 574 | valid_mi[0] = (valid_mi[0] & 0xf0) | mi_type; |
| 575 | |
| 576 | printf(" - writing to zero-length string:\n"); |
| 577 | memset(str, '!', sizeof(str) - 1); |
| 578 | rc = gsm48_mi_to_string(str, 0, valid_mi, valid_mi_len); |
| 579 | printf(" rc=%d\n", rc); |
| 580 | if (str[0] == '!') |
| 581 | printf(" nothing written\n"); |
| 582 | else |
| 583 | printf(" ERROR: Wrote to invalid memory!\n"); |
| 584 | |
| 585 | printf(" - writing to 1-byte-length string:\n"); |
| 586 | memset(str, '!', sizeof(str) - 1); |
| 587 | rc = gsm48_mi_to_string(str, 1, valid_mi, valid_mi_len); |
| 588 | printf(" rc=%d\n", rc); |
| 589 | if (str[0] == '\0') |
| 590 | printf(" returned empty string\n"); |
| 591 | else if (str[0] == '!') |
| 592 | printf(" ERROR: nothing written, expected nul-terminated empty string\n"); |
| 593 | else |
| 594 | printf(" ERROR: Wrote unexpected string %s\n", osmo_quote_str(str, 5)); |
| 595 | if (str[1] != '!') |
| 596 | printf(" ERROR: Wrote to invalid memory!\n"); |
| 597 | |
| 598 | printf(" - decode zero-length mi:\n"); |
| 599 | memset(str, '!', sizeof(str) - 1); |
| 600 | rc = gsm48_mi_to_string(str, sizeof(str), valid_mi, 0); |
| 601 | printf(" rc=%d\n", rc); |
| 602 | if (str[0] == '\0') |
| 603 | printf(" returned empty string\n"); |
| 604 | else if (str[0] == '!') |
| 605 | printf(" ERROR: nothing written, expected nul-terminated empty string\n"); |
| 606 | else |
| 607 | printf(" ERROR: expected empty string, got output string: %s\n", osmo_quote_str(str, -1)); |
| 608 | } |
| 609 | } |
| 610 | printf("\n"); |
| 611 | } |
| 612 | |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 613 | static const struct bcd_number_test { |
| 614 | /* Human-readable test name */ |
| 615 | const char *test_name; |
| 616 | |
| 617 | /* To be encoded number in ASCII */ |
| 618 | const char *enc_ascii; |
| 619 | /* Expected encoding result in HEX */ |
| 620 | const char *enc_hex; |
| 621 | /* Optional header length (LHV) */ |
| 622 | uint8_t enc_h_len; |
| 623 | /* Expected return code */ |
| 624 | int enc_rc; |
| 625 | |
| 626 | /* To be decoded buffer in HEX */ |
| 627 | const char *dec_hex; |
| 628 | /* Expected decoding result in ASCII */ |
| 629 | const char *dec_ascii; |
| 630 | /* Optional header length (LHV) */ |
| 631 | uint8_t dec_h_len; |
| 632 | /* Expected return code */ |
| 633 | int dec_rc; |
| 634 | |
| 635 | /* Encoding buffer limit (0 means unlimited) */ |
| 636 | size_t enc_buf_lim; |
| 637 | /* Decoding buffer limit (0 means unlimited) */ |
| 638 | size_t dec_buf_lim; |
| 639 | } bcd_number_test_set[] = { |
| 640 | { |
| 641 | .test_name = "regular 9-digit MSISDN", |
| 642 | |
| 643 | /* Encoding test */ |
| 644 | .enc_ascii = "123456789", |
| 645 | .enc_hex = "0521436587f9", |
| 646 | .enc_rc = 6, |
| 647 | |
| 648 | /* Decoding test */ |
| 649 | .dec_hex = "0521436587f9", |
| 650 | .dec_ascii = "123456789", |
| 651 | }, |
| 652 | { |
| 653 | .test_name = "regular 6-digit MSISDN with optional header (LHV)", |
| 654 | |
| 655 | /* Encoding test */ |
| 656 | .enc_ascii = "123456", |
| 657 | .enc_hex = "0700000000214365", |
| 658 | .enc_h_len = 4, /* LHV */ |
| 659 | .enc_rc = 4 + 4, |
| 660 | |
| 661 | /* Decoding test */ |
| 662 | .dec_hex = "07deadbeef214365", |
| 663 | .dec_ascii = "123456", |
| 664 | .dec_h_len = 4, /* LHV */ |
| 665 | }, |
| 666 | { |
| 667 | .test_name = "long 15-digit (maximum) MSISDN", |
| 668 | |
| 669 | /* Encoding test */ |
| 670 | .enc_ascii = "123456789012345", |
| 671 | .enc_hex = "0821436587092143f5", |
| 672 | .enc_rc = 9, |
| 673 | |
| 674 | /* Decoding test */ |
| 675 | .dec_hex = "0821436587092143f5", |
| 676 | .dec_ascii = "123456789012345", |
| 677 | }, |
| 678 | { |
| 679 | .test_name = "long 15-digit (maximum) MSISDN, limited buffer", |
| 680 | |
| 681 | /* Encoding test */ |
| 682 | .enc_ascii = "123456789012345", |
| 683 | .enc_hex = "0821436587092143f5", |
| 684 | .enc_rc = 9, |
| 685 | |
| 686 | /* Decoding test */ |
| 687 | .dec_hex = "0821436587092143f5", |
| 688 | .dec_ascii = "123456789012345", |
| 689 | |
| 690 | /* Buffer length limitations */ |
| 691 | .dec_buf_lim = 15 + 1, |
| 692 | .enc_buf_lim = 9, |
| 693 | }, |
| 694 | { |
| 695 | .test_name = "to be truncated 20-digit MSISDN", |
| 696 | |
| 697 | /* Encoding test (not enough room in buffer) */ |
| 698 | .enc_ascii = "12345678901234567890", |
| 699 | .enc_hex = "", /* nothing */ |
| 700 | .enc_rc = -EIO, |
| 701 | |
| 702 | /* Decoding test (one 5 digits do not fit) */ |
| 703 | .dec_hex = "0a21436587092143658709", |
| 704 | .dec_ascii = "123456789012345", |
Vadim Yanitskiy | 7194087 | 2019-05-26 00:49:57 +0700 | [diff] [blame] | 705 | .dec_rc = -ENOSPC, |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 706 | |
| 707 | /* Buffer length limitations */ |
| 708 | .dec_buf_lim = 15 + 1, /* 5 digits less */ |
| 709 | .enc_buf_lim = 9, |
| 710 | }, |
| 711 | { |
| 712 | .test_name = "LV incorrect length", |
| 713 | .dec_hex = "05214365", /* should be 0x03 */ |
| 714 | .dec_ascii = "(none)", |
Vadim Yanitskiy | e4799f5 | 2019-05-26 00:55:20 +0700 | [diff] [blame] | 715 | .dec_rc = -EINVAL, |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 716 | }, |
| 717 | { |
| 718 | .test_name = "empty input buffer", |
| 719 | |
| 720 | /* Encoding test */ |
| 721 | .enc_ascii = "", |
| 722 | .enc_hex = "00", |
| 723 | .enc_rc = 1, |
| 724 | |
| 725 | /* Decoding test */ |
| 726 | .dec_hex = "", |
| 727 | .dec_ascii = "(none)", |
| 728 | .dec_rc = -EIO, |
| 729 | }, |
| 730 | }; |
| 731 | |
| 732 | static void test_bcd_number_encode_decode() |
| 733 | { |
| 734 | const struct bcd_number_test *test; |
| 735 | uint8_t buf_enc[0xff] = { 0 }; |
| 736 | char buf_dec[0xff] = { 0 }; |
| 737 | size_t buf_len, i; |
| 738 | int rc; |
| 739 | |
| 740 | printf("BSD number encoding / decoding test\n"); |
| 741 | |
| 742 | for (i = 0; i < ARRAY_SIZE(bcd_number_test_set); i++) { |
| 743 | test = &bcd_number_test_set[i]; |
| 744 | printf("- Running test: %s\n", test->test_name); |
| 745 | |
| 746 | if (test->enc_ascii) { |
| 747 | if (test->enc_buf_lim) |
| 748 | buf_len = test->enc_buf_lim; |
| 749 | else |
| 750 | buf_len = sizeof(buf_enc); |
| 751 | |
| 752 | printf(" - Encoding ASCII (buffer limit=%zu) '%s'...\n", |
| 753 | test->enc_buf_lim, test->enc_ascii); |
| 754 | |
| 755 | rc = gsm48_encode_bcd_number(buf_enc, buf_len, |
| 756 | test->enc_h_len, test->enc_ascii); |
| 757 | printf(" - Expected: (rc=%d) '%s'\n", |
| 758 | test->enc_rc, test->enc_hex); |
| 759 | printf(" - Actual: (rc=%d) '%s'\n", |
| 760 | rc, osmo_hexdump_nospc(buf_enc, rc >= 0 ? rc : 0)); |
| 761 | } |
| 762 | |
| 763 | if (test->dec_hex) { |
| 764 | /* Parse a HEX string */ |
| 765 | rc = osmo_hexparse(test->dec_hex, buf_enc, sizeof(buf_enc)); |
| 766 | OSMO_ASSERT(rc >= 0); |
| 767 | |
| 768 | if (test->dec_buf_lim) |
| 769 | buf_len = test->dec_buf_lim; |
| 770 | else |
| 771 | buf_len = sizeof(buf_dec); |
| 772 | |
| 773 | printf(" - Decoding HEX (buffer limit=%zu) '%s'...\n", |
| 774 | test->dec_buf_lim, test->dec_hex); |
| 775 | |
| 776 | rc = gsm48_decode_bcd_number2(buf_dec, buf_len, |
| 777 | buf_enc, rc, test->dec_h_len); |
| 778 | printf(" - Expected: (rc=%d) '%s'\n", |
| 779 | test->dec_rc, test->dec_ascii); |
| 780 | printf(" - Actual: (rc=%d) '%s'\n", |
Vadim Yanitskiy | 7194087 | 2019-05-26 00:49:57 +0700 | [diff] [blame] | 781 | rc, (rc == 0 || rc == -ENOSPC) ? buf_dec : "(none)"); |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | printf("\n"); |
| 786 | } |
| 787 | |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 788 | struct { |
| 789 | int range; |
| 790 | int arfcns_num; |
| 791 | int arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS]; |
| 792 | } arfcn_test_ranges[] = { |
| 793 | {OSMO_GSM48_ARFCN_RANGE_512, 12, |
| 794 | { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }}, |
| 795 | {OSMO_GSM48_ARFCN_RANGE_512, 17, |
| 796 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }}, |
| 797 | {OSMO_GSM48_ARFCN_RANGE_512, 18, |
| 798 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }}, |
| 799 | {OSMO_GSM48_ARFCN_RANGE_512, 18, |
| 800 | { 1, 17, 31, 45, 58, 79, 81, 97, |
| 801 | 113, 127, 213, 277, 287, 311, 331, 391, |
| 802 | 417, 511 }}, |
| 803 | {OSMO_GSM48_ARFCN_RANGE_512, 6, |
| 804 | { 1, 17, 31, 45, 58, 79 }}, |
| 805 | {OSMO_GSM48_ARFCN_RANGE_512, 6, |
| 806 | { 10, 17, 31, 45, 58, 79 }}, |
| 807 | {OSMO_GSM48_ARFCN_RANGE_1024, 17, |
| 808 | { 0, 17, 31, 45, 58, 79, 81, 97, |
| 809 | 113, 127, 213, 277, 287, 311, 331, 391, |
| 810 | 1023 }}, |
| 811 | {OSMO_GSM48_ARFCN_RANGE_1024, 16, |
| 812 | { 17, 31, 45, 58, 79, 81, 97, 113, |
| 813 | 127, 213, 277, 287, 311, 331, 391, 1023 }}, |
| 814 | {-1} |
| 815 | }; |
| 816 | |
| 817 | static int test_single_range_encoding(int range, const int *orig_arfcns, int arfcns_num, int silent) |
| 818 | { |
| 819 | int arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS]; |
| 820 | int w[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS]; |
| 821 | int f0_included = 0; |
| 822 | int rc, f0; |
| 823 | uint8_t chan_list[16] = {0}; |
| 824 | struct gsm_sysinfo_freq dec_freq[1024] = {{0}}; |
| 825 | int dec_arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS] = {0}; |
| 826 | int dec_arfcns_count = 0; |
| 827 | int arfcns_used = 0; |
| 828 | int i; |
| 829 | |
| 830 | arfcns_used = arfcns_num; |
| 831 | memmove(arfcns, orig_arfcns, sizeof(arfcns)); |
| 832 | |
| 833 | f0 = range == OSMO_GSM48_ARFCN_RANGE_1024 ? 0 : arfcns[0]; |
| 834 | /* |
| 835 | * Manipulate the ARFCN list according to the rules in J4 depending |
| 836 | * on the selected range. |
| 837 | */ |
| 838 | arfcns_used = osmo_gsm48_range_enc_filter_arfcns(arfcns, arfcns_used, f0, &f0_included); |
| 839 | |
| 840 | memset(w, 0, sizeof(w)); |
| 841 | osmo_gsm48_range_enc_arfcns(range, arfcns, arfcns_used, w, 0); |
| 842 | |
| 843 | if (!silent) |
| 844 | printf("range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n", range, arfcns_used, f0, f0_included); |
| 845 | |
| 846 | /* Select the range and the amount of bits needed */ |
| 847 | switch (range) { |
| 848 | case OSMO_GSM48_ARFCN_RANGE_128: |
| 849 | osmo_gsm48_range_enc_128(chan_list, f0, w); |
| 850 | break; |
| 851 | case OSMO_GSM48_ARFCN_RANGE_256: |
| 852 | osmo_gsm48_range_enc_256(chan_list, f0, w); |
| 853 | break; |
| 854 | case OSMO_GSM48_ARFCN_RANGE_512: |
| 855 | osmo_gsm48_range_enc_512(chan_list, f0, w); |
| 856 | break; |
| 857 | case OSMO_GSM48_ARFCN_RANGE_1024: |
| 858 | osmo_gsm48_range_enc_1024(chan_list, f0, f0_included, w); |
| 859 | break; |
| 860 | default: |
| 861 | return 1; |
| 862 | }; |
| 863 | |
| 864 | if (!silent) |
| 865 | printf("chan_list = %s\n", |
| 866 | osmo_hexdump(chan_list, sizeof(chan_list))); |
| 867 | |
| 868 | rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list), |
| 869 | 0xfe, 1); |
| 870 | if (rc != 0) { |
| 871 | printf("Cannot decode freq list, rc = %d\n", rc); |
| 872 | return 1; |
| 873 | } |
| 874 | |
| 875 | for (i = 0; i < ARRAY_SIZE(dec_freq); i++) { |
| 876 | if (dec_freq[i].mask && |
| 877 | dec_arfcns_count < ARRAY_SIZE(dec_arfcns)) |
| 878 | dec_arfcns[dec_arfcns_count++] = i; |
| 879 | } |
| 880 | |
| 881 | if (!silent) { |
| 882 | printf("Decoded freqs %d (expected %d)\n", |
| 883 | dec_arfcns_count, arfcns_num); |
| 884 | printf("Decoded: "); |
| 885 | for (i = 0; i < dec_arfcns_count; i++) { |
| 886 | printf("%d ", dec_arfcns[i]); |
| 887 | if (dec_arfcns[i] != orig_arfcns[i]) |
| 888 | printf("(!= %d) ", orig_arfcns[i]); |
| 889 | } |
| 890 | printf("\n"); |
| 891 | } |
| 892 | |
| 893 | if (dec_arfcns_count != arfcns_num) { |
| 894 | printf("Wrong number of arfcns\n"); |
| 895 | return 1; |
| 896 | } |
| 897 | |
| 898 | if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) { |
| 899 | printf("Decoding error, got wrong freqs\n"); |
| 900 | printf(" w = "); |
| 901 | for (i = 0; i < ARRAY_SIZE(w); i++) |
| 902 | printf("%d ", w[i]); |
| 903 | printf("\n"); |
| 904 | return 1; |
| 905 | } |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static void test_random_range_encoding(int range, int max_arfcn_num) |
| 911 | { |
| 912 | int arfcns_num = 0; |
| 913 | int test_idx; |
| 914 | int rc, max_count; |
| 915 | int num_tests = 1024; |
| 916 | |
| 917 | printf("Random range test: range %d, max num ARFCNs %d\n", |
| 918 | range, max_arfcn_num); |
| 919 | |
| 920 | srandom(1); |
| 921 | |
| 922 | for (max_count = 1; max_count < max_arfcn_num; max_count++) { |
| 923 | for (test_idx = 0; test_idx < num_tests; test_idx++) { |
| 924 | int count; |
| 925 | int i; |
| 926 | int min_freq = 0; |
| 927 | |
| 928 | int rnd_arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS] = {0}; |
| 929 | char rnd_arfcns_set[1024] = {0}; |
| 930 | |
| 931 | if (range < OSMO_GSM48_ARFCN_RANGE_1024) |
| 932 | min_freq = random() % (1023 - range); |
| 933 | |
| 934 | for (count = max_count; count; ) { |
| 935 | int arfcn = min_freq + random() % (range + 1); |
| 936 | OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set)); |
| 937 | |
| 938 | if (!rnd_arfcns_set[arfcn]) { |
| 939 | rnd_arfcns_set[arfcn] = 1; |
| 940 | count -= 1; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | arfcns_num = 0; |
| 945 | for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++) |
| 946 | if (rnd_arfcns_set[i]) |
| 947 | rnd_arfcns[arfcns_num++] = i; |
| 948 | |
| 949 | rc = test_single_range_encoding(range, rnd_arfcns, |
| 950 | arfcns_num, 1); |
| 951 | if (rc != 0) { |
| 952 | printf("Failed on test %d, range %d, num ARFCNs %d\n", |
| 953 | test_idx, range, max_count); |
| 954 | test_single_range_encoding(range, rnd_arfcns, |
| 955 | arfcns_num, 0); |
| 956 | return; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static void test_range_encoding() |
| 963 | { |
| 964 | int *arfcns; |
| 965 | int arfcns_num = 0; |
| 966 | int test_idx; |
| 967 | int range; |
| 968 | |
| 969 | for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++) |
| 970 | { |
| 971 | arfcns_num = arfcn_test_ranges[test_idx].arfcns_num; |
| 972 | arfcns = &arfcn_test_ranges[test_idx].arfcns[0]; |
| 973 | range = arfcn_test_ranges[test_idx].range; |
| 974 | |
| 975 | printf("Range test %d: range %d, num ARFCNs %d\n", |
| 976 | test_idx, range, arfcns_num); |
| 977 | |
| 978 | test_single_range_encoding(range, arfcns, arfcns_num, 0); |
| 979 | } |
| 980 | |
| 981 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_128, 29); |
| 982 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_256, 22); |
| 983 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_512, 18); |
| 984 | test_random_range_encoding(OSMO_GSM48_ARFCN_RANGE_1024, 16); |
| 985 | } |
| 986 | |
| 987 | static int freqs1[] = { |
| 988 | 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980 |
| 989 | }; |
| 990 | |
| 991 | static int freqs2[] = { |
| 992 | 402, 460, 1, 67, 131, 197, 272, 347, |
| 993 | }; |
| 994 | |
| 995 | static int freqs3[] = { |
| 996 | 68, 128, 198, 279, 353, 398, 452, |
| 997 | |
| 998 | }; |
| 999 | |
| 1000 | static int w_out[] = { |
| 1001 | 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9, |
| 1002 | }; |
| 1003 | |
| 1004 | static int range128[] = { |
| 1005 | 1, 1 + 127, |
| 1006 | }; |
| 1007 | |
| 1008 | static int range256[] = { |
| 1009 | 1, 1 + 128, |
| 1010 | }; |
| 1011 | |
| 1012 | static int range512[] = { |
| 1013 | 1, 1+ 511, |
| 1014 | }; |
| 1015 | |
| 1016 | |
| 1017 | #define VERIFY(res, cmp, wanted) \ |
| 1018 | if (!(res cmp wanted)) { \ |
| 1019 | printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \ |
| 1020 | __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \ |
| 1021 | } |
| 1022 | |
| 1023 | static void test_arfcn_filter() |
| 1024 | { |
| 1025 | int arfcns[50], i, res, f0_included; |
| 1026 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 1027 | arfcns[i] = (i + 1) * 2; |
| 1028 | |
| 1029 | /* check that the arfcn is taken out. f0_included is only set for Range1024 */ |
| 1030 | f0_included = 24; |
| 1031 | res = osmo_gsm48_range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), arfcns[0], &f0_included); |
| 1032 | VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1); |
| 1033 | VERIFY(f0_included, ==, 1); |
| 1034 | for (i = 0; i < res; ++i) |
| 1035 | VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1)); |
| 1036 | |
| 1037 | /* check with range1024, ARFCN 0 is included */ |
| 1038 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 1039 | arfcns[i] = i * 2; |
| 1040 | res = osmo_gsm48_range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), 0, &f0_included); |
| 1041 | VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1); |
| 1042 | VERIFY(f0_included, ==, 1); |
| 1043 | for (i = 0; i < res; ++i) |
| 1044 | VERIFY(arfcns[i], ==, (i + 1) * 2 - 1); |
| 1045 | |
| 1046 | /* check with range1024, ARFCN 0 not included */ |
| 1047 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 1048 | arfcns[i] = (i + 1) * 2; |
| 1049 | res = osmo_gsm48_range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), 0, &f0_included); |
| 1050 | VERIFY(res, ==, ARRAY_SIZE(arfcns)); |
| 1051 | VERIFY(f0_included, ==, 0); |
| 1052 | for (i = 0; i < res; ++i) |
| 1053 | VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1); |
| 1054 | } |
| 1055 | |
| 1056 | static void test_print_encoding() |
| 1057 | { |
| 1058 | int rc; |
| 1059 | int w[17]; |
| 1060 | uint8_t chan_list[16]; |
| 1061 | memset(chan_list, 0x23, sizeof(chan_list)); |
| 1062 | |
| 1063 | for (rc = 0; rc < ARRAY_SIZE(w); ++rc) |
| 1064 | switch (rc % 3) { |
| 1065 | case 0: |
| 1066 | w[rc] = 0xAAAA; |
| 1067 | break; |
| 1068 | case 1: |
| 1069 | w[rc] = 0x5555; |
| 1070 | break; |
| 1071 | case 2: |
| 1072 | w[rc] = 0x9696; |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | osmo_gsm48_range_enc_512(chan_list, (1 << 9) | 0x96, w); |
| 1077 | |
| 1078 | printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list))); |
| 1079 | } |
| 1080 | |
| 1081 | static void test_si_range_helpers() |
| 1082 | { |
| 1083 | int ws[(sizeof(freqs1)/sizeof(freqs1[0]))]; |
| 1084 | int i, f0 = 0xFFFFFF; |
| 1085 | |
| 1086 | memset(&ws[0], 0x23, sizeof(ws)); |
| 1087 | |
| 1088 | i = osmo_gsm48_range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1)); |
| 1089 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1); |
| 1090 | VERIFY(i, ==, 2); |
| 1091 | |
| 1092 | i = osmo_gsm48_range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2)); |
| 1093 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1); |
| 1094 | VERIFY(i, ==, 2); |
| 1095 | |
| 1096 | i = osmo_gsm48_range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3)); |
| 1097 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1); |
| 1098 | VERIFY(i, ==, 0); |
| 1099 | |
| 1100 | osmo_gsm48_range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0); |
| 1101 | |
| 1102 | for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) { |
| 1103 | printf("w[%d]=%d\n", i, ws[i]); |
| 1104 | VERIFY(ws[i], ==, w_out[i]); |
| 1105 | } |
| 1106 | |
| 1107 | i = osmo_gsm48_range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0); |
| 1108 | VERIFY(i, ==, OSMO_GSM48_ARFCN_RANGE_128); |
| 1109 | VERIFY(f0, ==, 1); |
| 1110 | |
| 1111 | i = osmo_gsm48_range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0); |
| 1112 | VERIFY(i, ==, OSMO_GSM48_ARFCN_RANGE_256); |
| 1113 | VERIFY(f0, ==, 1); |
| 1114 | |
| 1115 | i = osmo_gsm48_range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0); |
| 1116 | VERIFY(i, ==, OSMO_GSM48_ARFCN_RANGE_512); |
| 1117 | VERIFY(f0, ==, 1); |
| 1118 | } |
| 1119 | |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 1120 | int main(int argc, char **argv) |
| 1121 | { |
| 1122 | test_bearer_cap(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 1123 | test_mid_from_tmsi(); |
Max | d55d7d4 | 2018-02-15 11:27:18 +0100 | [diff] [blame] | 1124 | test_mid_from_imsi(); |
Neels Hofmeyr | 4968628 | 2018-12-05 21:32:21 +0100 | [diff] [blame] | 1125 | test_mid_encode_decode(); |
| 1126 | test_mid_decode_zero_length(); |
Vadim Yanitskiy | aa0683d | 2019-05-25 23:14:00 +0700 | [diff] [blame] | 1127 | test_bcd_number_encode_decode(); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 1128 | test_ra_cap(); |
Neels Hofmeyr | d5a577b | 2018-02-20 21:48:07 +0100 | [diff] [blame] | 1129 | test_lai_encode_decode(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 1130 | |
Stefan Sperling | fdf8b7b | 2018-07-27 12:19:15 +0200 | [diff] [blame] | 1131 | test_si_range_helpers(); |
| 1132 | test_arfcn_filter(); |
| 1133 | test_print_encoding(); |
| 1134 | test_range_encoding(); |
| 1135 | |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 1136 | return EXIT_SUCCESS; |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 1137 | } |