blob: b84c88f0d671246bd11bc6a97d7efacb5bd8417e [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
Oliver Smithaffbb202019-02-01 11:35:10 +010012/* Complete IEs used multiple times (sorted alphabetically)
13 * 1st byte: IEI from osmo_gsup_iei, 2nd byte: length */
Harald Welte3b6fb082016-04-25 18:46:22 +020014#define TEST_IMSI_IE 0x01, 0x08, 0x21, 0x43, 0x65, 0x87, 0x09, 0x21, 0x43, 0xf5
15#define TEST_IMSI_STR "123456789012345"
Neels Hofmeyr9838c902019-04-01 22:24:33 +020016#define TEST_CLASS_SUBSCR_IE 0xa, 0x1, 0x1
Oliver Smithaffbb202019-02-01 11:35:10 +010017#define TEST_CLASS_INTER_MSC_IE 0xa, 0x1, 0x4
18#define TEST_MSISDN_IE 0x08, 0x07, 0x91, 0x94, 0x61, 0x46, 0x32, 0x24, 0x43
19#define TEST_AN_APDU_IE 0x62, 0x05, 0x01, 0x42, 0x42, 0x42, 0x42
20#define TEST_SOURCE_NAME_IE 0x60, 0x05, 'M', 'S', 'C', '-', 'A'
21#define TEST_DESTINATION_NAME_IE 0x61, 0x05, 'M', 'S', 'C', '-', 'B'
Harald Welte3b6fb082016-04-25 18:46:22 +020022
23static void test_gsup_messages_dec_enc(void)
24{
25 int test_idx;
26 int rc;
27 uint8_t buf[1024];
28
29 static const uint8_t send_auth_info_req[] = {
30 0x08,
Neels Hofmeyr9838c902019-04-01 22:24:33 +020031 TEST_IMSI_IE,
32 TEST_CLASS_SUBSCR_IE
Harald Welte3b6fb082016-04-25 18:46:22 +020033 };
34
35 static const uint8_t send_auth_info_err[] = {
36 0x09,
37 TEST_IMSI_IE,
38 0x02, 0x01, 0x07 /* GPRS no allowed */
39 };
40
41 static const uint8_t send_auth_info_res[] = {
42 0x0a,
43 TEST_IMSI_IE,
44 0x03, 0x22, /* Auth tuple */
45 0x20, 0x10,
46 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
47 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
48 0x21, 0x04,
49 0x21, 0x22, 0x23, 0x24,
50 0x22, 0x08,
51 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
52 0x03, 0x22, /* Auth tuple */
53 0x20, 0x10,
54 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
55 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90,
56 0x21, 0x04,
57 0xa1, 0xa2, 0xa3, 0xa4,
58 0x22, 0x08,
59 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8,
60 };
61
62 static const uint8_t update_location_req[] = {
63 0x04,
64 TEST_IMSI_IE,
65 };
66
67 static const uint8_t update_location_err[] = {
68 0x05,
69 TEST_IMSI_IE,
70 0x02, 0x01, 0x07 /* GPRS no allowed */
71 };
72
73 static const uint8_t update_location_res[] = {
74 0x06,
75 TEST_IMSI_IE,
Oliver Smithaffbb202019-02-01 11:35:10 +010076 TEST_MSISDN_IE,
Harald Welte3b6fb082016-04-25 18:46:22 +020077 0x09, 0x07, /* HLR-Number of the subscriber */
78 0x91, 0x83, 0x52, 0x38, 0x48, 0x83, 0x93,
79 0x04, 0x00, /* PDP info complete */
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +020080 0x05, 0x19,
Harald Welte3b6fb082016-04-25 18:46:22 +020081 0x10, 0x01, 0x01,
82 0x11, 0x02, 0xf1, 0x21, /* IPv4 */
83 0x12, 0x09, 0x04, 't', 'e', 's', 't', 0x03, 'a', 'p', 'n',
84 0x13, 0x01, 0x02,
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +020085 0x14, 0x02, 0xFF, 0x23,
Harald Welte3b6fb082016-04-25 18:46:22 +020086 0x05, 0x11,
87 0x10, 0x01, 0x02,
88 0x11, 0x02, 0xf1, 0x21, /* IPv4 */
89 0x12, 0x08, 0x03, 'f', 'o', 'o', 0x03, 'a', 'p', 'n',
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +020090 0x14, 0x02,
91 0xAE, 0xFF
Harald Welte3b6fb082016-04-25 18:46:22 +020092 };
93
94 static const uint8_t location_cancellation_req[] = {
95 0x1c,
96 TEST_IMSI_IE,
97 0x06, 0x01, 0x00,
98 };
99
100 static const uint8_t location_cancellation_err[] = {
101 0x1d,
102 TEST_IMSI_IE,
103 0x02, 0x01, 0x03 /* Illegal MS */
104 };
105
106 static const uint8_t location_cancellation_res[] = {
107 0x1e,
108 TEST_IMSI_IE,
109 };
110
111 static const uint8_t purge_ms_req[] = {
112 0x0c,
113 TEST_IMSI_IE,
114 };
115
116 static const uint8_t purge_ms_err[] = {
117 0x0d,
118 TEST_IMSI_IE,
119 0x02, 0x01, 0x03, /* Illegal MS */
120 };
121
122 static const uint8_t purge_ms_res[] = {
123 0x0e,
124 TEST_IMSI_IE,
125 0x07, 0x00,
126 };
127
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100128 static const uint8_t send_auth_info_res_umts[] = {
129 0x0a,
130 TEST_IMSI_IE,
131 0x03, 0x62, /* Auth tuple */
132 0x20, 0x10, /* rand */
133 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
134 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
135 0x21, 0x04, /* sres */
136 0x21, 0x22, 0x23, 0x24,
137 0x22, 0x08, /* kc */
138 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
139 0x23, 0x10, /* IK (UMTS) */
140 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
141 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
142 0x24, 0x10, /* CK (UMTS) */
143 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
144 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
145 0x25, 0x10, /* AUTN (UMTS) */
146 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
147 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100148 0x27, 0x08, /* RES (UMTS) */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100149 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100150 0x03, 0x62, /* Auth tuple */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100151 0x20, 0x10, /* rand */
152 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
153 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0x10,
154 0x21, 0x04, /* sres */
155 0xb1, 0xb2, 0xb3, 0xb4,
156 0x22, 0x08, /* kc */
157 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
158 0x23, 0x10, /* IK (UMTS) */
159 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
160 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xd0,
161 0x24, 0x10, /* CK (UMTS) */
162 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
163 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe0,
164 0x25, 0x10, /* AUTN (UMTS) */
165 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
166 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf0,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100167 0x27, 0x08, /* RES (UMTS) */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100168 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
169 };
170
171 static const uint8_t send_auth_info_req_auts[] = {
172 0x08,
173 TEST_IMSI_IE,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100174 0x26, 0x0e, /* AUTS (UMTS) */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100175 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
176 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100177 0x20, 0x10, /* rand */
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100178 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
179 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
180 };
181
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700182 static const uint8_t dummy_session_ies[] = {
183 0x2b, /* Dummy value, we only interested in IE coding */
184 TEST_IMSI_IE,
185
186 /* Session ID and state */
187 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
188 0x31, 0x01, 0x01,
189 };
190
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700191 static const uint8_t send_ussd_req[] = {
192 0x20, /* OSMO_GSUP_MSGT_PROC_SS_REQUEST */
193 TEST_IMSI_IE,
194
195 /* Session ID and state */
196 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
197 0x31, 0x01, 0x01,
198
199 /* SS/USSD information IE */
200 0x35, 0x14,
201 /* ASN.1 encoded MAP payload */
202 0xa1, 0x12,
203 0x02, 0x01, /* Component: invoke */
204 0x01, /* invokeID = 1 */
205 /* opCode: processUnstructuredSS-Request */
206 0x02, 0x01, 0x3b, 0x30, 0x0a, 0x04, 0x01, 0x0f,
207 0x04, 0x05, 0xaa, 0x18, 0x0c, 0x36, 0x02,
208 };
209
210 static const uint8_t send_ussd_res[] = {
211 0x22, /* OSMO_GSUP_MSGT_PROC_SS_RESULT */
212 TEST_IMSI_IE,
213
214 /* Session ID and state */
215 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
216 0x31, 0x01, 0x03,
217
218 /* SS/USSD information IE */
219 0x35, 0x08,
220 /* ASN.1 encoded MAP payload */
221 0xa3, 0x06,
222 0x02, 0x01, /* Component: returnError */
223 0x01, /* invokeID = 1 */
224 /* localValue: unknownAlphabet */
225 0x02, 0x01, 0x47,
226 };
227
Vadim Yanitskiyc2628312018-09-25 23:03:13 +0700228 static const uint8_t send_mo_forward_sm_req[] = {
229 0x24, /* OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST */
230 TEST_IMSI_IE,
231
232 /* SM related IEs */
233 0x40, 0x01, /* SM-RP-MR (Message Reference) */
234 0xfa,
235 0x41, 0x08, /* SM-RP-DA (Destination Address) */
236 0x03, /* SMSC address */
237 0x91, 0x52, 0x75, 0x47, 0x99, 0x09, 0x82,
238 0x42, 0x01, /* SM-RP-OA (Originating Address) */
239 0xff, /* Special case: noSM-RP-OA */
240 0x43, 0x04, /* SM-RP-UI (TPDU) */
241 0xde, 0xad, 0xbe, 0xef,
242 };
243
244 static const uint8_t send_mt_forward_sm_req[] = {
245 0x28, /* OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST */
246 TEST_IMSI_IE,
247
248 /* SM related IEs */
249 0x40, 0x01, /* SM-RP-MR (Message Reference) */
250 0xfa,
251 0x41, 0x09, /* SM-RP-DA (Destination Address) */
252 0x01, /* IMSI */
253 0x21, 0x43, 0x65, 0x87, 0x09, 0x21, 0x43, 0xf5,
254 0x42, 0x08, /* SM-RP-OA (Originating Address) */
255 0x03, /* SMSC address */
256 0x91, 0x52, 0x75, 0x47, 0x99, 0x09, 0x82,
257 0x43, 0x04, /* SM-RP-UI (TPDU) */
258 0xde, 0xad, 0xbe, 0xef,
259 0x45, 0x01, /* SM-RP-MMS (More Messages to Send) */
260 0x01,
261 };
262
263 static const uint8_t send_mo_mt_forward_sm_err[] = {
264 0x25, /* OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR */
265 TEST_IMSI_IE,
266
Vadim Yanitskiyc2628312018-09-25 23:03:13 +0700267 /* SM related IEs */
268 0x40, 0x01, /* SM-RP-MR (Message Reference) */
269 0xfa,
270 0x44, 0x01, /* SM-RP-Cause value */
271 0xaf,
272 };
273
274 static const uint8_t send_mo_mt_forward_sm_rsp[] = {
275 0x2a, /* OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT */
276 TEST_IMSI_IE,
277
278 /* SM related IEs */
279 0x40, 0x01, /* SM-RP-MR (Message Reference) */
280 0xfa,
281 0x43, 0x04, /* SM-RP-UI (TPDU) */
282 0xde, 0xad, 0xbe, 0xef,
283 };
284
Vadim Yanitskiyf9ee8da2018-11-13 02:06:15 +0700285 static const uint8_t send_ready_for_sm_ind[] = {
286 0x2c, /* OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST */
287 TEST_IMSI_IE,
288
289 /* SM related IEs */
290 0x46, 0x01, /* Alert reason */
291 0x02, /* Memory Available (SMMA) */
292 };
293
Oliver Smith10db2812018-12-14 16:14:34 +0100294 static const uint8_t send_check_imei_req[] = {
295 0x30, /* OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST */
296 TEST_IMSI_IE,
297
298 /* imei */
299 0x50, 0x09,
300 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
301 };
302
303 static const uint8_t send_check_imei_err[] = {
304 0x31, /* OSMO_GSUP_MSGT_CHECK_IMEI_ERROR */
305 TEST_IMSI_IE,
306
307 /* cause */
308 0x02, 0x01,
309 0x60, /* GMM_CAUSE_INV_MAND_INFO */
310 };
311
312 static const uint8_t send_check_imei_res[] = {
313 0x32, /* OSMO_GSUP_MSGT_CHECK_IMEI_RESULT */
314 TEST_IMSI_IE,
315
316 /* imei_result */
317 0x51, 0x01,
318 0x00, /* OSMO_GSUP_IMEI_RESULT_ACK */
319 };
320
Oliver Smithaffbb202019-02-01 11:35:10 +0100321 /* Handover related test messages. Oftentimes they only differ in the
322 * AN_APDU_IE, which is mostly a blob in GSUP. To give a better example
323 * of how the messages can be used, I've added the information an_apdu
324 * holds in brackets (see osmo-msc.git's doc/interMSC_HO_GSUP_msgs.txt).
325 * The session states are from the ASCII art in this e-mail:
326 * https://lists.osmocom.org/pipermail/openbsc/2019-January/012653.html */
327 static const uint8_t send_e_prepare_handover_req[] = {
328 0x34, /* OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_REQUEST */
329 TEST_IMSI_IE,
330
331 /* Session ID and state (begin) */
332 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
333 0x31, 0x01, 0x01,
334
335 TEST_CLASS_INTER_MSC_IE,
336 TEST_SOURCE_NAME_IE,
337 TEST_DESTINATION_NAME_IE,
338 TEST_AN_APDU_IE, /* (Handover Request) */
339 };
340
341 static const uint8_t send_e_prepare_handover_err[] = {
342 0x35, /* OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_ERROR */
343 TEST_IMSI_IE,
344
345 /* Session ID and state (continue) */
346 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
347 0x31, 0x01, 0x02,
348
349 TEST_CLASS_INTER_MSC_IE,
350 TEST_SOURCE_NAME_IE,
351 TEST_DESTINATION_NAME_IE,
352
353 /* cause_bssap */
354 0x64, 0x01,
355 0x51, /* GSM0808_CAUSE_INVALID_MESSAGE_CONTENTS */
356 };
357
358 static const uint8_t send_e_prepare_handover_res[] = {
359 0x36, /* OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_RESULT */
360 TEST_IMSI_IE,
361 TEST_MSISDN_IE, /* (Handover Number) */
362
363 /* Session ID and state (continue) */
364 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
365 0x31, 0x01, 0x02,
366
367 TEST_CLASS_INTER_MSC_IE,
368 TEST_SOURCE_NAME_IE,
369 TEST_DESTINATION_NAME_IE,
370 TEST_AN_APDU_IE, /* (Handover Request Ack) */
371 };
372
373 static const uint8_t send_e_prepare_subsequent_handover_req[] = {
374 0x38, /* OSMO_GSUP_MSGT_E_PREPARE_SUBSEQUENT_HANDOVER_REQUEST */
375 TEST_IMSI_IE,
376
377 /* Session ID and state (begin) */
378 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
379 0x31, 0x01, 0x01,
380
381 TEST_CLASS_INTER_MSC_IE,
382 TEST_SOURCE_NAME_IE,
383 TEST_DESTINATION_NAME_IE,
384 TEST_AN_APDU_IE, /* (Handover Required) */
385 };
386
387 static const uint8_t send_e_prepare_subsequent_handover_err[] = {
388 0x39, /* OSMO_GSUP_MSGT_E_PREPARE_SUBSEQUENT_HANDOVER_ERROR */
389 TEST_IMSI_IE,
390
391 /* Session ID and state (continue) */
392 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
393 0x31, 0x01, 0x02,
394
395 TEST_CLASS_INTER_MSC_IE,
396 TEST_SOURCE_NAME_IE,
397 TEST_DESTINATION_NAME_IE,
398
399 /* cause_bssap */
400 0x64, 0x01,
401 0x51, /* GSM0808_CAUSE_INVALID_MESSAGE_CONTENTS */
402 };
403
404 static const uint8_t send_e_prepare_subsequent_handover_res[] = {
405 0x3A, /* OSMO_GSUP_MSGT_E_PREPARE_SUBSEQUENT_HANDOVER_RESULT */
406 TEST_IMSI_IE,
407
408 /* Session ID and state (continue) */
409 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
410 0x31, 0x01, 0x02,
411
412 TEST_CLASS_INTER_MSC_IE,
413 TEST_SOURCE_NAME_IE,
414 TEST_DESTINATION_NAME_IE,
415 TEST_AN_APDU_IE, /* (Handover Request Ack) */
416 };
417
418 static const uint8_t send_e_send_end_signal_req[] = {
419 0x3C, /* OSMO_GSUP_MSGT_E_SEND_END_SIGNAL_REQUEST */
420 TEST_IMSI_IE,
421
422 /* Session ID and state (end) */
423 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
424 0x31, 0x01, 0x03,
425
426 TEST_CLASS_INTER_MSC_IE,
427 TEST_SOURCE_NAME_IE,
428 TEST_DESTINATION_NAME_IE,
429 TEST_AN_APDU_IE, /* (Handover Complete) */
430 };
431
432 static const uint8_t send_e_send_end_signal_err[] = {
433 0x3D, /* OSMO_GSUP_MSGT_E_SEND_END_SIGNAL_ERROR */
434 TEST_IMSI_IE,
435
436 /* Session ID and state (continue) */
437 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
438 0x31, 0x01, 0x02,
439
440 TEST_CLASS_INTER_MSC_IE,
441 TEST_SOURCE_NAME_IE,
442 TEST_DESTINATION_NAME_IE,
443
444 /* cause_bssap */
445 0x64, 0x01,
446 0x51, /* GSM0808_CAUSE_INVALID_MESSAGE_CONTENTS */
447 };
448
449 static const uint8_t send_e_process_access_signalling_req[] = {
450 0x40, /* OSMO_GSUP_MSGT_E_PROCESS_ACCESS_SIGNALLING_REQUEST */
451 TEST_IMSI_IE,
452
453 /* Session ID and state (continue) */
454 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
455 0x31, 0x01, 0x02,
456
457 TEST_CLASS_INTER_MSC_IE,
458 TEST_SOURCE_NAME_IE,
459 TEST_DESTINATION_NAME_IE,
460 TEST_AN_APDU_IE, /* (Handover Detect) */
461 };
462
463 static const uint8_t send_e_send_end_signal_res[] = {
464 0x3E, /* OSMO_GSUP_MSGT_E_SEND_END_SIGNAL_RESULT */
465 TEST_IMSI_IE,
466
467 /* Session ID and state (end) */
468 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
469 0x31, 0x01, 0x03,
470
471 TEST_CLASS_INTER_MSC_IE,
472 TEST_SOURCE_NAME_IE,
473 TEST_DESTINATION_NAME_IE,
474 TEST_AN_APDU_IE, /* (Handover Complete) */
475 };
476
477 static const uint8_t send_e_forward_access_signalling_req [] = {
478 0x44, /* OSMO_GSUP_MSGT_E_FORWARD_ACCESS_SIGNALLING_REQUEST */
479 TEST_IMSI_IE,
480
481 /* Session ID and state (continue) */
482 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
483 0x31, 0x01, 0x02,
484
485 TEST_CLASS_INTER_MSC_IE,
486 TEST_SOURCE_NAME_IE,
487 TEST_DESTINATION_NAME_IE,
488 TEST_AN_APDU_IE, /* (DTAP, e.g. CC, SMS, ...) */
489 };
490
491 static const uint8_t send_e_close[] = {
492 0x47, /* OSMO_GSUP_MSGT_E_CLOSE */
493 TEST_IMSI_IE,
494
495 /* Session ID and state (end) */
496 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
497 0x31, 0x01, 0x03,
498
499 TEST_CLASS_INTER_MSC_IE,
500 TEST_SOURCE_NAME_IE,
501 TEST_DESTINATION_NAME_IE,
502 };
503
504 static const uint8_t send_e_abort[] = {
505 0x4B, /* OSMO_GSUP_MSGT_E_ABORT */
506 TEST_IMSI_IE,
507
508 /* Session ID and state (end) */
509 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
510 0x31, 0x01, 0x03,
511
512 TEST_CLASS_INTER_MSC_IE,
513
514 /* cause_bssap */
515 0x64, 0x01,
516 0x51, /* GSM0808_CAUSE_INVALID_MESSAGE_CONTENTS */
517 };
518
519 static const uint8_t send_e_routing_error[] = {
520 0x4E, /* OSMO_GSUP_MSGT_E_ROUTING_ERROR */
521 TEST_IMSI_IE,
522
523 /* Session ID and state (end) */
524 0x30, 0x04, 0xde, 0xad, 0xbe, 0xef,
525 0x31, 0x01, 0x03,
526
527 TEST_CLASS_INTER_MSC_IE,
528 TEST_SOURCE_NAME_IE,
529 TEST_DESTINATION_NAME_IE,
530 };
531
Harald Welte3b6fb082016-04-25 18:46:22 +0200532 static const struct test {
533 char *name;
534 const uint8_t *data;
535 size_t data_len;
536 } test_messages[] = {
537 {"Send Authentication Info Request",
538 send_auth_info_req, sizeof(send_auth_info_req)},
539 {"Send Authentication Info Error",
540 send_auth_info_err, sizeof(send_auth_info_err)},
541 {"Send Authentication Info Result",
542 send_auth_info_res, sizeof(send_auth_info_res)},
543 {"Update Location Request",
544 update_location_req, sizeof(update_location_req)},
545 {"Update Location Error",
546 update_location_err, sizeof(update_location_err)},
547 {"Update Location Result",
548 update_location_res, sizeof(update_location_res)},
549 {"Location Cancellation Request",
550 location_cancellation_req, sizeof(location_cancellation_req)},
551 {"Location Cancellation Error",
552 location_cancellation_err, sizeof(location_cancellation_err)},
553 {"Location Cancellation Result",
554 location_cancellation_res, sizeof(location_cancellation_res)},
555 {"Purge MS Request",
556 purge_ms_req, sizeof(purge_ms_req)},
557 {"Purge MS Error",
558 purge_ms_err, sizeof(purge_ms_err)},
559 {"Purge MS Result",
560 purge_ms_res, sizeof(purge_ms_res)},
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100561 {"Send Authentication Info Result with IK, CK, AUTN and RES (UMTS)",
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100562 send_auth_info_res_umts, sizeof(send_auth_info_res_umts)},
Neels Hofmeyrabb23692017-02-21 15:53:20 +0100563 {"Send Authentication Info Request with AUTS and RAND (UMTS)",
564 send_auth_info_req_auts, sizeof(send_auth_info_req_auts)},
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700565 {"Dummy message with session IEs",
566 dummy_session_ies, sizeof(dummy_session_ies)},
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700567 {"SS/USSD processUnstructuredSS-Request / Invoke",
568 send_ussd_req, sizeof(send_ussd_req)},
569 {"SS/USSD processUnstructuredSS-Request / ReturnResult",
570 send_ussd_res, sizeof(send_ussd_res)},
Vadim Yanitskiyc2628312018-09-25 23:03:13 +0700571 {"MO-ForwardSM (MSC -> SMSC) Request",
572 send_mo_forward_sm_req, sizeof(send_mo_forward_sm_req)},
573 {"MT-ForwardSM (MSC -> SMSC) Request",
574 send_mt_forward_sm_req, sizeof(send_mt_forward_sm_req)},
575 {"MO-/MT-ForwardSM Response",
576 send_mo_mt_forward_sm_rsp, sizeof(send_mo_mt_forward_sm_rsp)},
577 {"MO-/MT-ForwardSM Error",
578 send_mo_mt_forward_sm_err, sizeof(send_mo_mt_forward_sm_err)},
Vadim Yanitskiyf9ee8da2018-11-13 02:06:15 +0700579 {"ReadyForSM (MSC -> SMSC) Indication",
580 send_ready_for_sm_ind, sizeof(send_ready_for_sm_ind)},
Oliver Smith10db2812018-12-14 16:14:34 +0100581 {"Check IMEI Request",
582 send_check_imei_req, sizeof(send_check_imei_req)},
583 {"Check IMEI Error",
584 send_check_imei_err, sizeof(send_check_imei_err)},
585 {"Check IMEI Result",
586 send_check_imei_res, sizeof(send_check_imei_res)},
Oliver Smithaffbb202019-02-01 11:35:10 +0100587 {"E Prepare Handover Request",
588 send_e_prepare_handover_req, sizeof(send_e_prepare_handover_req)},
589 {"E Prepare Handover Error",
590 send_e_prepare_handover_err, sizeof(send_e_prepare_handover_err)},
591 {"E Prepare Handover Result",
592 send_e_prepare_handover_res, sizeof(send_e_prepare_handover_res)},
593 {"E Prepare Subsequent Handover Request",
594 send_e_prepare_subsequent_handover_req, sizeof(send_e_prepare_subsequent_handover_req)},
595 {"E Prepare Subsequent Handover Error",
596 send_e_prepare_subsequent_handover_err, sizeof(send_e_prepare_subsequent_handover_err)},
597 {"E Prepare Subsequent Handover Result",
598 send_e_prepare_subsequent_handover_res, sizeof(send_e_prepare_subsequent_handover_res)},
599 {"E Send End Signal Request",
600 send_e_send_end_signal_req, sizeof(send_e_send_end_signal_req)},
601 {"E Send End Signal Error",
602 send_e_send_end_signal_err, sizeof(send_e_send_end_signal_err)},
603 {"E Send End Signal Result",
604 send_e_send_end_signal_res, sizeof(send_e_send_end_signal_res)},
605 {"E Process Access Signalling Request",
606 send_e_process_access_signalling_req, sizeof(send_e_process_access_signalling_req)},
607 {"E Forward Access Signalling Request",
608 send_e_forward_access_signalling_req, sizeof(send_e_forward_access_signalling_req)},
609 {"E Close",
610 send_e_close, sizeof(send_e_close)},
611 {"E Abort",
612 send_e_abort, sizeof(send_e_abort)},
613 {"E Routing Error",
614 send_e_routing_error, sizeof(send_e_routing_error)},
Harald Welte3b6fb082016-04-25 18:46:22 +0200615 };
616
617 printf("Test GSUP message decoding/encoding\n");
618
619 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
620 const struct test *t = &test_messages[test_idx];
621 struct osmo_gsup_message gm = {0};
622 struct msgb *msg = msgb_alloc(4096, "gsup_test");
Max49382722018-01-22 15:15:06 +0100623 bool passed = true;
Harald Welte3b6fb082016-04-25 18:46:22 +0200624
625 printf(" Testing %s\n", t->name);
626
627 rc = osmo_gsup_decode(t->data, t->data_len, &gm);
Max49382722018-01-22 15:15:06 +0100628 if (rc < 0)
629 passed = false;
Harald Welte3b6fb082016-04-25 18:46:22 +0200630
Max80f4c4e2018-01-24 12:33:05 +0100631 rc = osmo_gsup_encode(msg, &gm);
632 if (rc < 0)
633 passed = false;
Harald Welte3b6fb082016-04-25 18:46:22 +0200634
635 fprintf(stderr, " generated message: %s\n", msgb_hexdump(msg));
636 fprintf(stderr, " original message: %s\n", osmo_hexdump(t->data, t->data_len));
637 fprintf(stderr, " IMSI: %s\n", gm.imsi);
Harald Welte3b6fb082016-04-25 18:46:22 +0200638
Max49382722018-01-22 15:15:06 +0100639 if (strcmp(gm.imsi, TEST_IMSI_STR) != 0 ||
640 msgb_length(msg) != t->data_len ||
641 memcmp(msgb_data(msg), t->data, t->data_len) != 0)
642 passed = false;
643
644 if (passed)
645 printf(" %s OK\n", t->name);
646 else
647 printf(" %s FAILED: %d<%s> [%u,%u,%zu,%u]\n",
648 t->name, rc, strerror(-rc),
649 strcmp(gm.imsi, TEST_IMSI_STR),
650 msgb_length(msg), t->data_len,
651 memcmp(msgb_data(msg), t->data, t->data_len));
Harald Welte3b6fb082016-04-25 18:46:22 +0200652 msgb_free(msg);
653 }
654
655 /* simple truncation test */
656 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
657 int j;
658 const struct test *t = &test_messages[test_idx];
659 int ie_end = t->data_len;
660 struct osmo_gsup_message gm = {0};
661 int counter = 0;
662 int parse_err = 0;
663
664 for (j = t->data_len - 1; j >= 0; --j) {
665 rc = osmo_gsup_decode(t->data, j, &gm);
666 counter += 1;
667
668 VERBOSE_FPRINTF(stderr,
669 " partial message decoding: "
670 "orig_len = %d, trunc = %d, rc = %d, ie_end = %d\n",
671 t->data_len, j, rc, ie_end);
672 if (rc >= 0) {
673 VERBOSE_FPRINTF(stderr,
674 " remaing partial message: %s\n",
675 osmo_hexdump(t->data + j, ie_end - j));
676
677 OSMO_ASSERT(j <= ie_end - 2);
Oliver Smith19742402018-12-21 10:18:28 +0100678 OSMO_ASSERT(t->data[j+0] < _OSMO_GSUP_IEI_END_MARKER);
Harald Welte3b6fb082016-04-25 18:46:22 +0200679 OSMO_ASSERT(t->data[j+1] <= ie_end - j - 2);
680
681 ie_end = j;
682 } else {
683 parse_err += 1;
684 }
685 }
686
687 fprintf(stderr,
688 " message %d: tested %d truncations, %d parse failures\n",
689 test_idx, counter, parse_err);
690 }
691
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100692 /* Don't log thousands of message modification errors */
693 LOGP(DLGSUP, LOGL_NOTICE, "Stopping DLGSUP logging\n");
694 log_set_category_filter(osmo_stderr_target, DLGSUP, 0, 0);
695
Harald Welte3b6fb082016-04-25 18:46:22 +0200696 /* message modification test (relies on ASAN or valgrind being used) */
697 for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) {
698 int j;
699 const struct test *t = &test_messages[test_idx];
700 struct osmo_gsup_message gm = {0};
701 uint8_t val;
702 int counter = 0;
703 int parse_err = 0;
704
705 OSMO_ASSERT(sizeof(buf) >= t->data_len);
706
707 for (j = t->data_len - 1; j >= 0; --j) {
708 memcpy(buf, t->data, t->data_len);
709 val = 0;
710 do {
711 VERBOSE_FPRINTF(stderr,
712 "t = %d, len = %d, val = %d\n",
713 test_idx, j, val);
714 buf[j] = val;
715 rc = osmo_gsup_decode(buf, t->data_len, &gm);
716 counter += 1;
717 if (rc < 0)
718 parse_err += 1;
719
720 val += 1;
721 } while (val != (uint8_t)256);
722 }
723
724 fprintf(stderr,
725 " message %d: tested %d modifications, %d parse failures\n",
726 test_idx, counter, parse_err);
727 }
728}
729
730const struct log_info_cat default_categories[] = {
Harald Welte3b6fb082016-04-25 18:46:22 +0200731};
732
733static struct log_info info = {
734 .cat = default_categories,
735 .num_cat = ARRAY_SIZE(default_categories),
736};
737
738int main(int argc, char **argv)
739{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200740 void *ctx = talloc_named_const(NULL, 0, "gsup_test");
741 osmo_init_logging2(ctx, &info);
Neels Hofmeyrbd9de2f2017-02-21 20:17:35 +0100742 log_set_print_filename(osmo_stderr_target, 0);
743 log_set_print_timestamp(osmo_stderr_target, 0);
744 log_set_use_color(osmo_stderr_target, 0);
745 log_set_print_category(osmo_stderr_target, 1);
Harald Welte3b6fb082016-04-25 18:46:22 +0200746
747 test_gsup_messages_dec_enc();
748
749 printf("Done.\n");
750 return EXIT_SUCCESS;
751}