Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <osmocom/core/logging.h> |
| 4 | #include <osmocom/core/utils.h> |
| 5 | #include <osmocom/core/application.h> |
| 6 | #include <osmocom/gsm/gsup.h> |
| 7 | |
| 8 | #define VERBOSE_FPRINTF(...) |
| 9 | |
| 10 | /* Tests for osmo_gsup_messages.c */ |
| 11 | |
| 12 | #define TEST_IMSI_IE 0x01, 0x08, 0x21, 0x43, 0x65, 0x87, 0x09, 0x21, 0x43, 0xf5 |
| 13 | #define TEST_IMSI_STR "123456789012345" |
| 14 | |
| 15 | static void test_gsup_messages_dec_enc(void) |
| 16 | { |
| 17 | int test_idx; |
| 18 | int rc; |
| 19 | uint8_t buf[1024]; |
| 20 | |
| 21 | static const uint8_t send_auth_info_req[] = { |
| 22 | 0x08, |
| 23 | TEST_IMSI_IE |
| 24 | }; |
| 25 | |
| 26 | static const uint8_t send_auth_info_err[] = { |
| 27 | 0x09, |
| 28 | TEST_IMSI_IE, |
| 29 | 0x02, 0x01, 0x07 /* GPRS no allowed */ |
| 30 | }; |
| 31 | |
| 32 | static const uint8_t send_auth_info_res[] = { |
| 33 | 0x0a, |
| 34 | TEST_IMSI_IE, |
| 35 | 0x03, 0x22, /* Auth tuple */ |
| 36 | 0x20, 0x10, |
| 37 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 38 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 39 | 0x21, 0x04, |
| 40 | 0x21, 0x22, 0x23, 0x24, |
| 41 | 0x22, 0x08, |
| 42 | 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, |
| 43 | 0x03, 0x22, /* Auth tuple */ |
| 44 | 0x20, 0x10, |
| 45 | 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, |
| 46 | 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, |
| 47 | 0x21, 0x04, |
| 48 | 0xa1, 0xa2, 0xa3, 0xa4, |
| 49 | 0x22, 0x08, |
| 50 | 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, |
| 51 | }; |
| 52 | |
| 53 | static const uint8_t update_location_req[] = { |
| 54 | 0x04, |
| 55 | TEST_IMSI_IE, |
| 56 | }; |
| 57 | |
| 58 | static const uint8_t update_location_err[] = { |
| 59 | 0x05, |
| 60 | TEST_IMSI_IE, |
| 61 | 0x02, 0x01, 0x07 /* GPRS no allowed */ |
| 62 | }; |
| 63 | |
| 64 | static const uint8_t update_location_res[] = { |
| 65 | 0x06, |
| 66 | TEST_IMSI_IE, |
| 67 | 0x08, 0x07, /* MSISDN of the subscriber */ |
| 68 | 0x91, 0x94, 0x61, 0x46, 0x32, 0x24, 0x43, |
| 69 | 0x09, 0x07, /* HLR-Number of the subscriber */ |
| 70 | 0x91, 0x83, 0x52, 0x38, 0x48, 0x83, 0x93, |
| 71 | 0x04, 0x00, /* PDP info complete */ |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 72 | 0x05, 0x19, |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 73 | 0x10, 0x01, 0x01, |
| 74 | 0x11, 0x02, 0xf1, 0x21, /* IPv4 */ |
| 75 | 0x12, 0x09, 0x04, 't', 'e', 's', 't', 0x03, 'a', 'p', 'n', |
| 76 | 0x13, 0x01, 0x02, |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 77 | 0x14, 0x02, 0xFF, 0x23, |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 78 | 0x05, 0x11, |
| 79 | 0x10, 0x01, 0x02, |
| 80 | 0x11, 0x02, 0xf1, 0x21, /* IPv4 */ |
| 81 | 0x12, 0x08, 0x03, 'f', 'o', 'o', 0x03, 'a', 'p', 'n', |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 82 | 0x14, 0x02, |
| 83 | 0xAE, 0xFF |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static const uint8_t location_cancellation_req[] = { |
| 87 | 0x1c, |
| 88 | TEST_IMSI_IE, |
| 89 | 0x06, 0x01, 0x00, |
| 90 | }; |
| 91 | |
| 92 | static const uint8_t location_cancellation_err[] = { |
| 93 | 0x1d, |
| 94 | TEST_IMSI_IE, |
| 95 | 0x02, 0x01, 0x03 /* Illegal MS */ |
| 96 | }; |
| 97 | |
| 98 | static const uint8_t location_cancellation_res[] = { |
| 99 | 0x1e, |
| 100 | TEST_IMSI_IE, |
| 101 | }; |
| 102 | |
| 103 | static const uint8_t purge_ms_req[] = { |
| 104 | 0x0c, |
| 105 | TEST_IMSI_IE, |
| 106 | }; |
| 107 | |
| 108 | static const uint8_t purge_ms_err[] = { |
| 109 | 0x0d, |
| 110 | TEST_IMSI_IE, |
| 111 | 0x02, 0x01, 0x03, /* Illegal MS */ |
| 112 | }; |
| 113 | |
| 114 | static const uint8_t purge_ms_res[] = { |
| 115 | 0x0e, |
| 116 | TEST_IMSI_IE, |
| 117 | 0x07, 0x00, |
| 118 | }; |
| 119 | |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 120 | static const uint8_t send_auth_info_res_umts[] = { |
| 121 | 0x0a, |
| 122 | TEST_IMSI_IE, |
| 123 | 0x03, 0x62, /* Auth tuple */ |
| 124 | 0x20, 0x10, /* rand */ |
| 125 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 126 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 127 | 0x21, 0x04, /* sres */ |
| 128 | 0x21, 0x22, 0x23, 0x24, |
| 129 | 0x22, 0x08, /* kc */ |
| 130 | 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, |
| 131 | 0x23, 0x10, /* IK (UMTS) */ |
| 132 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 133 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 134 | 0x24, 0x10, /* CK (UMTS) */ |
| 135 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 136 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 137 | 0x25, 0x10, /* AUTN (UMTS) */ |
| 138 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 139 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 140 | 0x27, 0x08, /* RES (UMTS) */ |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 141 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 142 | 0x03, 0x62, /* Auth tuple */ |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 143 | 0x20, 0x10, /* rand */ |
| 144 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, |
| 145 | 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0x10, |
| 146 | 0x21, 0x04, /* sres */ |
| 147 | 0xb1, 0xb2, 0xb3, 0xb4, |
| 148 | 0x22, 0x08, /* kc */ |
| 149 | 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, |
| 150 | 0x23, 0x10, /* IK (UMTS) */ |
| 151 | 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, |
| 152 | 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xd0, |
| 153 | 0x24, 0x10, /* CK (UMTS) */ |
| 154 | 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, |
| 155 | 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe0, |
| 156 | 0x25, 0x10, /* AUTN (UMTS) */ |
| 157 | 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
| 158 | 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf0, |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 159 | 0x27, 0x08, /* RES (UMTS) */ |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 160 | 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 161 | }; |
| 162 | |
| 163 | static const uint8_t send_auth_info_req_auts[] = { |
| 164 | 0x08, |
| 165 | TEST_IMSI_IE, |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 166 | 0x26, 0x0e, /* AUTS (UMTS) */ |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 167 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 168 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 169 | 0x20, 0x10, /* rand */ |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 170 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 171 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 172 | }; |
| 173 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 174 | static const struct test { |
| 175 | char *name; |
| 176 | const uint8_t *data; |
| 177 | size_t data_len; |
| 178 | } test_messages[] = { |
| 179 | {"Send Authentication Info Request", |
| 180 | send_auth_info_req, sizeof(send_auth_info_req)}, |
| 181 | {"Send Authentication Info Error", |
| 182 | send_auth_info_err, sizeof(send_auth_info_err)}, |
| 183 | {"Send Authentication Info Result", |
| 184 | send_auth_info_res, sizeof(send_auth_info_res)}, |
| 185 | {"Update Location Request", |
| 186 | update_location_req, sizeof(update_location_req)}, |
| 187 | {"Update Location Error", |
| 188 | update_location_err, sizeof(update_location_err)}, |
| 189 | {"Update Location Result", |
| 190 | update_location_res, sizeof(update_location_res)}, |
| 191 | {"Location Cancellation Request", |
| 192 | location_cancellation_req, sizeof(location_cancellation_req)}, |
| 193 | {"Location Cancellation Error", |
| 194 | location_cancellation_err, sizeof(location_cancellation_err)}, |
| 195 | {"Location Cancellation Result", |
| 196 | location_cancellation_res, sizeof(location_cancellation_res)}, |
| 197 | {"Purge MS Request", |
| 198 | purge_ms_req, sizeof(purge_ms_req)}, |
| 199 | {"Purge MS Error", |
| 200 | purge_ms_err, sizeof(purge_ms_err)}, |
| 201 | {"Purge MS Result", |
| 202 | purge_ms_res, sizeof(purge_ms_res)}, |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 203 | {"Send Authentication Info Result with IK, CK, AUTN and RES (UMTS)", |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 204 | send_auth_info_res_umts, sizeof(send_auth_info_res_umts)}, |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 205 | {"Send Authentication Info Request with AUTS and RAND (UMTS)", |
| 206 | send_auth_info_req_auts, sizeof(send_auth_info_req_auts)}, |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | printf("Test GSUP message decoding/encoding\n"); |
| 210 | |
| 211 | for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) { |
| 212 | const struct test *t = &test_messages[test_idx]; |
| 213 | struct osmo_gsup_message gm = {0}; |
| 214 | struct msgb *msg = msgb_alloc(4096, "gsup_test"); |
Max | 4938272 | 2018-01-22 15:15:06 +0100 | [diff] [blame] | 215 | bool passed = true; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 216 | |
| 217 | printf(" Testing %s\n", t->name); |
| 218 | |
| 219 | rc = osmo_gsup_decode(t->data, t->data_len, &gm); |
Max | 4938272 | 2018-01-22 15:15:06 +0100 | [diff] [blame] | 220 | if (rc < 0) |
| 221 | passed = false; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 222 | |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 223 | rc = osmo_gsup_encode(msg, &gm); |
| 224 | if (rc < 0) |
| 225 | passed = false; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 226 | |
| 227 | fprintf(stderr, " generated message: %s\n", msgb_hexdump(msg)); |
| 228 | fprintf(stderr, " original message: %s\n", osmo_hexdump(t->data, t->data_len)); |
| 229 | fprintf(stderr, " IMSI: %s\n", gm.imsi); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 230 | |
Max | 4938272 | 2018-01-22 15:15:06 +0100 | [diff] [blame] | 231 | if (strcmp(gm.imsi, TEST_IMSI_STR) != 0 || |
| 232 | msgb_length(msg) != t->data_len || |
| 233 | memcmp(msgb_data(msg), t->data, t->data_len) != 0) |
| 234 | passed = false; |
| 235 | |
| 236 | if (passed) |
| 237 | printf(" %s OK\n", t->name); |
| 238 | else |
| 239 | printf(" %s FAILED: %d<%s> [%u,%u,%zu,%u]\n", |
| 240 | t->name, rc, strerror(-rc), |
| 241 | strcmp(gm.imsi, TEST_IMSI_STR), |
| 242 | msgb_length(msg), t->data_len, |
| 243 | memcmp(msgb_data(msg), t->data, t->data_len)); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 244 | msgb_free(msg); |
| 245 | } |
| 246 | |
| 247 | /* simple truncation test */ |
| 248 | for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) { |
| 249 | int j; |
| 250 | const struct test *t = &test_messages[test_idx]; |
| 251 | int ie_end = t->data_len; |
| 252 | struct osmo_gsup_message gm = {0}; |
| 253 | int counter = 0; |
| 254 | int parse_err = 0; |
| 255 | |
| 256 | for (j = t->data_len - 1; j >= 0; --j) { |
| 257 | rc = osmo_gsup_decode(t->data, j, &gm); |
| 258 | counter += 1; |
| 259 | |
| 260 | VERBOSE_FPRINTF(stderr, |
| 261 | " partial message decoding: " |
| 262 | "orig_len = %d, trunc = %d, rc = %d, ie_end = %d\n", |
| 263 | t->data_len, j, rc, ie_end); |
| 264 | if (rc >= 0) { |
| 265 | VERBOSE_FPRINTF(stderr, |
| 266 | " remaing partial message: %s\n", |
| 267 | osmo_hexdump(t->data + j, ie_end - j)); |
| 268 | |
| 269 | OSMO_ASSERT(j <= ie_end - 2); |
Neels Hofmeyr | abb2369 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 270 | OSMO_ASSERT(t->data[j+0] <= OSMO_GSUP_CN_DOMAIN_IE); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 271 | OSMO_ASSERT(t->data[j+1] <= ie_end - j - 2); |
| 272 | |
| 273 | ie_end = j; |
| 274 | } else { |
| 275 | parse_err += 1; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | fprintf(stderr, |
| 280 | " message %d: tested %d truncations, %d parse failures\n", |
| 281 | test_idx, counter, parse_err); |
| 282 | } |
| 283 | |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 284 | /* Don't log thousands of message modification errors */ |
| 285 | LOGP(DLGSUP, LOGL_NOTICE, "Stopping DLGSUP logging\n"); |
| 286 | log_set_category_filter(osmo_stderr_target, DLGSUP, 0, 0); |
| 287 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 288 | /* message modification test (relies on ASAN or valgrind being used) */ |
| 289 | for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) { |
| 290 | int j; |
| 291 | const struct test *t = &test_messages[test_idx]; |
| 292 | struct osmo_gsup_message gm = {0}; |
| 293 | uint8_t val; |
| 294 | int counter = 0; |
| 295 | int parse_err = 0; |
| 296 | |
| 297 | OSMO_ASSERT(sizeof(buf) >= t->data_len); |
| 298 | |
| 299 | for (j = t->data_len - 1; j >= 0; --j) { |
| 300 | memcpy(buf, t->data, t->data_len); |
| 301 | val = 0; |
| 302 | do { |
| 303 | VERBOSE_FPRINTF(stderr, |
| 304 | "t = %d, len = %d, val = %d\n", |
| 305 | test_idx, j, val); |
| 306 | buf[j] = val; |
| 307 | rc = osmo_gsup_decode(buf, t->data_len, &gm); |
| 308 | counter += 1; |
| 309 | if (rc < 0) |
| 310 | parse_err += 1; |
| 311 | |
| 312 | val += 1; |
| 313 | } while (val != (uint8_t)256); |
| 314 | } |
| 315 | |
| 316 | fprintf(stderr, |
| 317 | " message %d: tested %d modifications, %d parse failures\n", |
| 318 | test_idx, counter, parse_err); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | const struct log_info_cat default_categories[] = { |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | static struct log_info info = { |
| 326 | .cat = default_categories, |
| 327 | .num_cat = ARRAY_SIZE(default_categories), |
| 328 | }; |
| 329 | |
| 330 | int main(int argc, char **argv) |
| 331 | { |
| 332 | osmo_init_logging(&info); |
Neels Hofmeyr | bd9de2f | 2017-02-21 20:17:35 +0100 | [diff] [blame] | 333 | log_set_print_filename(osmo_stderr_target, 0); |
| 334 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 335 | log_set_use_color(osmo_stderr_target, 0); |
| 336 | log_set_print_category(osmo_stderr_target, 1); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 337 | |
| 338 | test_gsup_messages_dec_enc(); |
| 339 | |
| 340 | printf("Done.\n"); |
| 341 | return EXIT_SUCCESS; |
| 342 | } |