blob: 9712d77baffc54288ef10b23a781fcfe7619d63a [file] [log] [blame]
Harald Welte3b6fb082016-04-25 18:46:22 +02001#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
15static 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 Freythereb55c0d2017-07-07 16:53:30 +020072 0x05, 0x19,
Harald Welte3b6fb082016-04-25 18:46:22 +020073 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 Freythereb55c0d2017-07-07 16:53:30 +020077 0x14, 0x02, 0xFF, 0x23,
Harald Welte3b6fb082016-04-25 18:46:22 +020078 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 Freythereb55c0d2017-07-07 16:53:30 +020082 0x14, 0x02,
83 0xAE, 0xFF
Harald Welte3b6fb082016-04-25 18:46:22 +020084 };
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 Hofmeyrabb23692017-02-21 15:53:20 +0100120 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 Hofmeyrbd9de2f2017-02-21 20:17:35 +0100140 0x27, 0x08, /* RES (UMTS) */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100141 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100142 0x03, 0x62, /* Auth tuple */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100143 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 Hofmeyrbd9de2f2017-02-21 20:17:35 +0100159 0x27, 0x08, /* RES (UMTS) */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100160 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 Hofmeyrbd9de2f2017-02-21 20:17:35 +0100166 0x26, 0x0e, /* AUTS (UMTS) */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100167 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
168 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100169 0x20, 0x10, /* rand */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100170 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
171 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
172 };
173
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700174 static const uint8_t dummy_session_ies[] = {
175 0x2b, /* Dummy value, we only interested in IE coding */
176 TEST_IMSI_IE,
177
178 /* Session ID and state */
179 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
180 0x31, 0x01, 0x01,
181 };
182
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700183 static const uint8_t send_ussd_req[] = {
184 0x20, /* OSMO_GSUP_MSGT_PROC_SS_REQUEST */
185 TEST_IMSI_IE,
186
187 /* Session ID and state */
188 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
189 0x31, 0x01, 0x01,
190
191 /* SS/USSD information IE */
192 0x35, 0x14,
193 /* ASN.1 encoded MAP payload */
194 0xa1, 0x12,
195 0x02, 0x01, /* Component: invoke */
196 0x01, /* invokeID = 1 */
197 /* opCode: processUnstructuredSS-Request */
198 0x02, 0x01, 0x3b, 0x30, 0x0a, 0x04, 0x01, 0x0f,
199 0x04, 0x05, 0xaa, 0x18, 0x0c, 0x36, 0x02,
200 };
201
202 static const uint8_t send_ussd_res[] = {
203 0x22, /* OSMO_GSUP_MSGT_PROC_SS_RESULT */
204 TEST_IMSI_IE,
205
206 /* Session ID and state */
207 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
208 0x31, 0x01, 0x03,
209
210 /* SS/USSD information IE */
211 0x35, 0x08,
212 /* ASN.1 encoded MAP payload */
213 0xa3, 0x06,
214 0x02, 0x01, /* Component: returnError */
215 0x01, /* invokeID = 1 */
216 /* localValue: unknownAlphabet */
217 0x02, 0x01, 0x47,
218 };
219
Harald Welte3b6fb082016-04-25 18:46:22 +0200220 static const struct test {
221 char *name;
222 const uint8_t *data;
223 size_t data_len;
224 } test_messages[] = {
225 {"Send Authentication Info Request",
226 send_auth_info_req, sizeof(send_auth_info_req)},
227 {"Send Authentication Info Error",
228 send_auth_info_err, sizeof(send_auth_info_err)},
229 {"Send Authentication Info Result",
230 send_auth_info_res, sizeof(send_auth_info_res)},
231 {"Update Location Request",
232 update_location_req, sizeof(update_location_req)},
233 {"Update Location Error",
234 update_location_err, sizeof(update_location_err)},
235 {"Update Location Result",
236 update_location_res, sizeof(update_location_res)},
237 {"Location Cancellation Request",
238 location_cancellation_req, sizeof(location_cancellation_req)},
239 {"Location Cancellation Error",
240 location_cancellation_err, sizeof(location_cancellation_err)},
241 {"Location Cancellation Result",
242 location_cancellation_res, sizeof(location_cancellation_res)},
243 {"Purge MS Request",
244 purge_ms_req, sizeof(purge_ms_req)},
245 {"Purge MS Error",
246 purge_ms_err, sizeof(purge_ms_err)},
247 {"Purge MS Result",
248 purge_ms_res, sizeof(purge_ms_res)},
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100249 {"Send Authentication Info Result with IK, CK, AUTN and RES (UMTS)",
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100250 send_auth_info_res_umts, sizeof(send_auth_info_res_umts)},
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100251 {"Send Authentication Info Request with AUTS and RAND (UMTS)",
252 send_auth_info_req_auts, sizeof(send_auth_info_req_auts)},
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700253 {"Dummy message with session IEs",
254 dummy_session_ies, sizeof(dummy_session_ies)},
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700255 {"SS/USSD processUnstructuredSS-Request / Invoke",
256 send_ussd_req, sizeof(send_ussd_req)},
257 {"SS/USSD processUnstructuredSS-Request / ReturnResult",
258 send_ussd_res, sizeof(send_ussd_res)},
Harald Welte3b6fb082016-04-25 18:46:22 +0200259 };
260
261 printf("Test GSUP message decoding/encoding\n");
262
263 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
264 const struct test *t = &test_messages[test_idx];
265 struct osmo_gsup_message gm = {0};
266 struct msgb *msg = msgb_alloc(4096, "gsup_test");
Max49382722018-01-22 15:15:06 +0100267 bool passed = true;
Harald Welte3b6fb082016-04-25 18:46:22 +0200268
269 printf(" Testing %s\n", t->name);
270
271 rc = osmo_gsup_decode(t->data, t->data_len, &gm);
Max49382722018-01-22 15:15:06 +0100272 if (rc < 0)
273 passed = false;
Harald Welte3b6fb082016-04-25 18:46:22 +0200274
Max80f4c4e2018-01-24 12:33:05 +0100275 rc = osmo_gsup_encode(msg, &gm);
276 if (rc < 0)
277 passed = false;
Harald Welte3b6fb082016-04-25 18:46:22 +0200278
279 fprintf(stderr, " generated message: %s\n", msgb_hexdump(msg));
280 fprintf(stderr, " original message: %s\n", osmo_hexdump(t->data, t->data_len));
281 fprintf(stderr, " IMSI: %s\n", gm.imsi);
Harald Welte3b6fb082016-04-25 18:46:22 +0200282
Max49382722018-01-22 15:15:06 +0100283 if (strcmp(gm.imsi, TEST_IMSI_STR) != 0 ||
284 msgb_length(msg) != t->data_len ||
285 memcmp(msgb_data(msg), t->data, t->data_len) != 0)
286 passed = false;
287
288 if (passed)
289 printf(" %s OK\n", t->name);
290 else
291 printf(" %s FAILED: %d<%s> [%u,%u,%zu,%u]\n",
292 t->name, rc, strerror(-rc),
293 strcmp(gm.imsi, TEST_IMSI_STR),
294 msgb_length(msg), t->data_len,
295 memcmp(msgb_data(msg), t->data, t->data_len));
Harald Welte3b6fb082016-04-25 18:46:22 +0200296 msgb_free(msg);
297 }
298
299 /* simple truncation test */
300 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
301 int j;
302 const struct test *t = &test_messages[test_idx];
303 int ie_end = t->data_len;
304 struct osmo_gsup_message gm = {0};
305 int counter = 0;
306 int parse_err = 0;
307
308 for (j = t->data_len - 1; j >= 0; --j) {
309 rc = osmo_gsup_decode(t->data, j, &gm);
310 counter += 1;
311
312 VERBOSE_FPRINTF(stderr,
313 " partial message decoding: "
314 "orig_len = %d, trunc = %d, rc = %d, ie_end = %d\n",
315 t->data_len, j, rc, ie_end);
316 if (rc >= 0) {
317 VERBOSE_FPRINTF(stderr,
318 " remaing partial message: %s\n",
319 osmo_hexdump(t->data + j, ie_end - j));
320
321 OSMO_ASSERT(j <= ie_end - 2);
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700322 /**
323 * FIXME: share the maximal IE value somehow
324 * in order to avoid manual updating of this
325 */
326 OSMO_ASSERT(t->data[j+0] <= OSMO_GSUP_SS_INFO_IE);
Harald Welte3b6fb082016-04-25 18:46:22 +0200327 OSMO_ASSERT(t->data[j+1] <= ie_end - j - 2);
328
329 ie_end = j;
330 } else {
331 parse_err += 1;
332 }
333 }
334
335 fprintf(stderr,
336 " message %d: tested %d truncations, %d parse failures\n",
337 test_idx, counter, parse_err);
338 }
339
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100340 /* Don't log thousands of message modification errors */
341 LOGP(DLGSUP, LOGL_NOTICE, "Stopping DLGSUP logging\n");
342 log_set_category_filter(osmo_stderr_target, DLGSUP, 0, 0);
343
Harald Welte3b6fb082016-04-25 18:46:22 +0200344 /* message modification test (relies on ASAN or valgrind being used) */
345 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
346 int j;
347 const struct test *t = &test_messages[test_idx];
348 struct osmo_gsup_message gm = {0};
349 uint8_t val;
350 int counter = 0;
351 int parse_err = 0;
352
353 OSMO_ASSERT(sizeof(buf) >= t->data_len);
354
355 for (j = t->data_len - 1; j >= 0; --j) {
356 memcpy(buf, t->data, t->data_len);
357 val = 0;
358 do {
359 VERBOSE_FPRINTF(stderr,
360 "t = %d, len = %d, val = %d\n",
361 test_idx, j, val);
362 buf[j] = val;
363 rc = osmo_gsup_decode(buf, t->data_len, &gm);
364 counter += 1;
365 if (rc < 0)
366 parse_err += 1;
367
368 val += 1;
369 } while (val != (uint8_t)256);
370 }
371
372 fprintf(stderr,
373 " message %d: tested %d modifications, %d parse failures\n",
374 test_idx, counter, parse_err);
375 }
376}
377
378const struct log_info_cat default_categories[] = {
Harald Welte3b6fb082016-04-25 18:46:22 +0200379};
380
381static struct log_info info = {
382 .cat = default_categories,
383 .num_cat = ARRAY_SIZE(default_categories),
384};
385
386int main(int argc, char **argv)
387{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200388 void *ctx = talloc_named_const(NULL, 0, "gsup_test");
389 osmo_init_logging2(ctx, &info);
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100390 log_set_print_filename(osmo_stderr_target, 0);
391 log_set_print_timestamp(osmo_stderr_target, 0);
392 log_set_use_color(osmo_stderr_target, 0);
393 log_set_print_category(osmo_stderr_target, 1);
Harald Welte3b6fb082016-04-25 18:46:22 +0200394
395 test_gsup_messages_dec_enc();
396
397 printf("Done.\n");
398 return EXIT_SUCCESS;
399}