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