blob: 093689333850c9594fcec65e71b34a085a6909ac [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
Vadim Yanitskiyc2628312018-09-25 23:03:13 +0700220 static const uint8_t send_mo_forward_sm_req[] = {
221 0x24, /* OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST */
222 TEST_IMSI_IE,
223
224 /* SM related IEs */
225 0x40, 0x01, /* SM-RP-MR (Message Reference) */
226 0xfa,
227 0x41, 0x08, /* SM-RP-DA (Destination Address) */
228 0x03, /* SMSC address */
229 0x91, 0x52, 0x75, 0x47, 0x99, 0x09, 0x82,
230 0x42, 0x01, /* SM-RP-OA (Originating Address) */
231 0xff, /* Special case: noSM-RP-OA */
232 0x43, 0x04, /* SM-RP-UI (TPDU) */
233 0xde, 0xad, 0xbe, 0xef,
234 };
235
236 static const uint8_t send_mt_forward_sm_req[] = {
237 0x28, /* OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST */
238 TEST_IMSI_IE,
239
240 /* SM related IEs */
241 0x40, 0x01, /* SM-RP-MR (Message Reference) */
242 0xfa,
243 0x41, 0x09, /* SM-RP-DA (Destination Address) */
244 0x01, /* IMSI */
245 0x21, 0x43, 0x65, 0x87, 0x09, 0x21, 0x43, 0xf5,
246 0x42, 0x08, /* SM-RP-OA (Originating Address) */
247 0x03, /* SMSC address */
248 0x91, 0x52, 0x75, 0x47, 0x99, 0x09, 0x82,
249 0x43, 0x04, /* SM-RP-UI (TPDU) */
250 0xde, 0xad, 0xbe, 0xef,
251 0x45, 0x01, /* SM-RP-MMS (More Messages to Send) */
252 0x01,
253 };
254
255 static const uint8_t send_mo_mt_forward_sm_err[] = {
256 0x25, /* OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR */
257 TEST_IMSI_IE,
258
259 /* Session related IEs */
260 0x30, 0x04, /* Session ID */
261 0xde, 0xad, 0xbe, 0xef,
262 0x31, 0x01, /* Session state (END) */
263 0x03,
264
265 /* SM related IEs */
266 0x40, 0x01, /* SM-RP-MR (Message Reference) */
267 0xfa,
268 0x44, 0x01, /* SM-RP-Cause value */
269 0xaf,
270 };
271
272 static const uint8_t send_mo_mt_forward_sm_rsp[] = {
273 0x2a, /* OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT */
274 TEST_IMSI_IE,
275
276 /* SM related IEs */
277 0x40, 0x01, /* SM-RP-MR (Message Reference) */
278 0xfa,
279 0x43, 0x04, /* SM-RP-UI (TPDU) */
280 0xde, 0xad, 0xbe, 0xef,
281 };
282
Harald Welte3b6fb082016-04-25 18:46:22 +0200283 static const struct test {
284 char *name;
285 const uint8_t *data;
286 size_t data_len;
287 } test_messages[] = {
288 {"Send Authentication Info Request",
289 send_auth_info_req, sizeof(send_auth_info_req)},
290 {"Send Authentication Info Error",
291 send_auth_info_err, sizeof(send_auth_info_err)},
292 {"Send Authentication Info Result",
293 send_auth_info_res, sizeof(send_auth_info_res)},
294 {"Update Location Request",
295 update_location_req, sizeof(update_location_req)},
296 {"Update Location Error",
297 update_location_err, sizeof(update_location_err)},
298 {"Update Location Result",
299 update_location_res, sizeof(update_location_res)},
300 {"Location Cancellation Request",
301 location_cancellation_req, sizeof(location_cancellation_req)},
302 {"Location Cancellation Error",
303 location_cancellation_err, sizeof(location_cancellation_err)},
304 {"Location Cancellation Result",
305 location_cancellation_res, sizeof(location_cancellation_res)},
306 {"Purge MS Request",
307 purge_ms_req, sizeof(purge_ms_req)},
308 {"Purge MS Error",
309 purge_ms_err, sizeof(purge_ms_err)},
310 {"Purge MS Result",
311 purge_ms_res, sizeof(purge_ms_res)},
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100312 {"Send Authentication Info Result with IK, CK, AUTN and RES (UMTS)",
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100313 send_auth_info_res_umts, sizeof(send_auth_info_res_umts)},
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100314 {"Send Authentication Info Request with AUTS and RAND (UMTS)",
315 send_auth_info_req_auts, sizeof(send_auth_info_req_auts)},
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700316 {"Dummy message with session IEs",
317 dummy_session_ies, sizeof(dummy_session_ies)},
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700318 {"SS/USSD processUnstructuredSS-Request / Invoke",
319 send_ussd_req, sizeof(send_ussd_req)},
320 {"SS/USSD processUnstructuredSS-Request / ReturnResult",
321 send_ussd_res, sizeof(send_ussd_res)},
Vadim Yanitskiyc2628312018-09-25 23:03:13 +0700322 {"MO-ForwardSM (MSC -> SMSC) Request",
323 send_mo_forward_sm_req, sizeof(send_mo_forward_sm_req)},
324 {"MT-ForwardSM (MSC -> SMSC) Request",
325 send_mt_forward_sm_req, sizeof(send_mt_forward_sm_req)},
326 {"MO-/MT-ForwardSM Response",
327 send_mo_mt_forward_sm_rsp, sizeof(send_mo_mt_forward_sm_rsp)},
328 {"MO-/MT-ForwardSM Error",
329 send_mo_mt_forward_sm_err, sizeof(send_mo_mt_forward_sm_err)},
Harald Welte3b6fb082016-04-25 18:46:22 +0200330 };
331
332 printf("Test GSUP message decoding/encoding\n");
333
334 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
335 const struct test *t = &test_messages[test_idx];
336 struct osmo_gsup_message gm = {0};
337 struct msgb *msg = msgb_alloc(4096, "gsup_test");
Max49382722018-01-22 15:15:06 +0100338 bool passed = true;
Harald Welte3b6fb082016-04-25 18:46:22 +0200339
340 printf(" Testing %s\n", t->name);
341
342 rc = osmo_gsup_decode(t->data, t->data_len, &gm);
Max49382722018-01-22 15:15:06 +0100343 if (rc < 0)
344 passed = false;
Harald Welte3b6fb082016-04-25 18:46:22 +0200345
Max80f4c4e2018-01-24 12:33:05 +0100346 rc = osmo_gsup_encode(msg, &gm);
347 if (rc < 0)
348 passed = false;
Harald Welte3b6fb082016-04-25 18:46:22 +0200349
350 fprintf(stderr, " generated message: %s\n", msgb_hexdump(msg));
351 fprintf(stderr, " original message: %s\n", osmo_hexdump(t->data, t->data_len));
352 fprintf(stderr, " IMSI: %s\n", gm.imsi);
Harald Welte3b6fb082016-04-25 18:46:22 +0200353
Max49382722018-01-22 15:15:06 +0100354 if (strcmp(gm.imsi, TEST_IMSI_STR) != 0 ||
355 msgb_length(msg) != t->data_len ||
356 memcmp(msgb_data(msg), t->data, t->data_len) != 0)
357 passed = false;
358
359 if (passed)
360 printf(" %s OK\n", t->name);
361 else
362 printf(" %s FAILED: %d<%s> [%u,%u,%zu,%u]\n",
363 t->name, rc, strerror(-rc),
364 strcmp(gm.imsi, TEST_IMSI_STR),
365 msgb_length(msg), t->data_len,
366 memcmp(msgb_data(msg), t->data, t->data_len));
Harald Welte3b6fb082016-04-25 18:46:22 +0200367 msgb_free(msg);
368 }
369
370 /* simple truncation test */
371 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
372 int j;
373 const struct test *t = &test_messages[test_idx];
374 int ie_end = t->data_len;
375 struct osmo_gsup_message gm = {0};
376 int counter = 0;
377 int parse_err = 0;
378
379 for (j = t->data_len - 1; j >= 0; --j) {
380 rc = osmo_gsup_decode(t->data, j, &gm);
381 counter += 1;
382
383 VERBOSE_FPRINTF(stderr,
384 " partial message decoding: "
385 "orig_len = %d, trunc = %d, rc = %d, ie_end = %d\n",
386 t->data_len, j, rc, ie_end);
387 if (rc >= 0) {
388 VERBOSE_FPRINTF(stderr,
389 " remaing partial message: %s\n",
390 osmo_hexdump(t->data + j, ie_end - j));
391
392 OSMO_ASSERT(j <= ie_end - 2);
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700393 /**
394 * FIXME: share the maximal IE value somehow
395 * in order to avoid manual updating of this
396 */
Vadim Yanitskiyc2628312018-09-25 23:03:13 +0700397 OSMO_ASSERT(t->data[j+0] <= OSMO_GSUP_SM_RP_MMS_IE);
Harald Welte3b6fb082016-04-25 18:46:22 +0200398 OSMO_ASSERT(t->data[j+1] <= ie_end - j - 2);
399
400 ie_end = j;
401 } else {
402 parse_err += 1;
403 }
404 }
405
406 fprintf(stderr,
407 " message %d: tested %d truncations, %d parse failures\n",
408 test_idx, counter, parse_err);
409 }
410
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100411 /* Don't log thousands of message modification errors */
412 LOGP(DLGSUP, LOGL_NOTICE, "Stopping DLGSUP logging\n");
413 log_set_category_filter(osmo_stderr_target, DLGSUP, 0, 0);
414
Harald Welte3b6fb082016-04-25 18:46:22 +0200415 /* message modification test (relies on ASAN or valgrind being used) */
416 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
417 int j;
418 const struct test *t = &test_messages[test_idx];
419 struct osmo_gsup_message gm = {0};
420 uint8_t val;
421 int counter = 0;
422 int parse_err = 0;
423
424 OSMO_ASSERT(sizeof(buf) >= t->data_len);
425
426 for (j = t->data_len - 1; j >= 0; --j) {
427 memcpy(buf, t->data, t->data_len);
428 val = 0;
429 do {
430 VERBOSE_FPRINTF(stderr,
431 "t = %d, len = %d, val = %d\n",
432 test_idx, j, val);
433 buf[j] = val;
434 rc = osmo_gsup_decode(buf, t->data_len, &gm);
435 counter += 1;
436 if (rc < 0)
437 parse_err += 1;
438
439 val += 1;
440 } while (val != (uint8_t)256);
441 }
442
443 fprintf(stderr,
444 " message %d: tested %d modifications, %d parse failures\n",
445 test_idx, counter, parse_err);
446 }
447}
448
449const struct log_info_cat default_categories[] = {
Harald Welte3b6fb082016-04-25 18:46:22 +0200450};
451
452static struct log_info info = {
453 .cat = default_categories,
454 .num_cat = ARRAY_SIZE(default_categories),
455};
456
457int main(int argc, char **argv)
458{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200459 void *ctx = talloc_named_const(NULL, 0, "gsup_test");
460 osmo_init_logging2(ctx, &info);
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100461 log_set_print_filename(osmo_stderr_target, 0);
462 log_set_print_timestamp(osmo_stderr_target, 0);
463 log_set_use_color(osmo_stderr_target, 0);
464 log_set_print_category(osmo_stderr_target, 1);
Harald Welte3b6fb082016-04-25 18:46:22 +0200465
466 test_gsup_messages_dec_enc();
467
468 printf("Done.\n");
469 return EXIT_SUCCESS;
470}