blob: f33e0bd1544a7d642836f54c59ad068db68e50a9 [file] [log] [blame]
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001/*
2 * (C) 2012 by Holger Hans Peter Freyther
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <osmocom/gsm/gsm0808.h>
Philipp Maier22401432017-03-24 17:59:26 +010022#include <osmocom/gsm/gsm0808_utils.h>
23#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010024
25#include <stdio.h>
26#include <stdlib.h>
Philipp Maier22401432017-03-24 17:59:26 +010027#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010030
31#define VERIFY(msg, data, len) \
32 if (msgb_l3len(msg) != len) { \
33 printf("%s:%d Length don't match: %d vs. %d. %s\n", \
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000034 __func__, __LINE__, msgb_l3len(msg), (int) len, \
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010035 osmo_hexdump(msg->l3h, msgb_l3len(msg))); \
36 abort(); \
37 } else if (memcmp(msg->l3h, data, len) != 0) { \
38 printf("%s:%d didn't match: got: %s\n", \
39 __func__, __LINE__, \
40 osmo_hexdump(msg->l3h, msgb_l3len(msg))); \
41 abort(); \
42 }
43
Philipp Maierfa896ab2017-03-27 16:55:32 +020044/* Setup a fake codec list for testing */
45static void setup_codec_list(struct gsm0808_speech_codec_list *scl)
46{
47 memset(scl, 0, sizeof(*scl));
48
49 scl->codec[0].pi = true;
50 scl->codec[0].tf = true;
51 scl->codec[0].type = 0xab;
52 scl->codec[0].type_extended = true;
53 scl->codec[0].cfg_present = true;
54 scl->codec[0].cfg = 0xcdef;
55
56 scl->codec[1].fi = true;
57 scl->codec[1].pt = true;
58 scl->codec[1].type = 0x05;
59
60 scl->codec[2].fi = true;
61 scl->codec[2].tf = true;
62 scl->codec[2].type = 0xf2;
63 scl->codec[2].type_extended = true;
64
65 scl->len = 3;
66}
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010067
68static void test_create_layer3(void)
69{
70 static const uint8_t res[] = {
71 0x00, 0x0e, 0x57, 0x05, 0x08, 0x00, 0x77, 0x62,
72 0x83, 0x33, 0x66, 0x44, 0x88, 0x17, 0x01, 0x23 };
73 struct msgb *msg, *in_msg;
74 printf("Testing creating Layer3\n");
75
76 in_msg = msgb_alloc_headroom(512, 128, "foo");
77 in_msg->l3h = in_msg->data;
78 msgb_v_put(in_msg, 0x23);
79
80 msg = gsm0808_create_layer3(in_msg, 0x1122, 0x2244, 0x3366, 0x4488);
81 VERIFY(msg, res, ARRAY_SIZE(res));
82 msgb_free(msg);
83 msgb_free(in_msg);
84}
85
Philipp Maierfa896ab2017-03-27 16:55:32 +020086static void test_create_layer3_aoip()
87{
88 static const uint8_t res[] = {
89 0x00, 0x17, 0x57, 0x05, 0x08, 0x00, 0x77, 0x62,
90 0x83, 0x33, 0x66, 0x44, 0x88, 0x17, 0x01, 0x23,
91 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, 0x5f, 0xab, 0xcd, 0xef,
92 0xa5, 0x9f, 0xf2
93 };
94
95 struct msgb *msg, *in_msg;
96 struct gsm0808_speech_codec_list sc_list;
97 printf("Testing creating Layer3 (AoIP)\n");
98
99 setup_codec_list(&sc_list);
100
101 in_msg = msgb_alloc_headroom(512, 128, "foo");
102 in_msg->l3h = in_msg->data;
103 msgb_v_put(in_msg, 0x23);
104
105 msg =
106 gsm0808_create_layer3_aoip(in_msg, 0x1122, 0x2244, 0x3366, 0x4488,
107 &sc_list);
108 VERIFY(msg, res, ARRAY_SIZE(res));
109
110 msgb_free(msg);
111 msgb_free(in_msg);
112}
113
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100114static void test_create_reset()
115{
116 static const uint8_t res[] = { 0x00, 0x04, 0x30, 0x04, 0x01, 0x20 };
117 struct msgb *msg;
118
119 printf("Testing creating Reset\n");
120 msg = gsm0808_create_reset();
121 VERIFY(msg, res, ARRAY_SIZE(res));
122 msgb_free(msg);
123}
124
125static void test_create_clear_command()
126{
127 static const uint8_t res[] = { 0x20, 0x04, 0x01, 0x23 };
128 struct msgb *msg;
129
130 printf("Testing creating Clear Command\n");
131 msg = gsm0808_create_clear_command(0x23);
132 VERIFY(msg, res, ARRAY_SIZE(res));
133 msgb_free(msg);
134}
135
136static void test_create_clear_complete()
137{
138 static const uint8_t res[] = { 0x00, 0x01, 0x21 };
139 struct msgb *msg;
140
141 printf("Testing creating Clear Complete\n");
142 msg = gsm0808_create_clear_complete();
143 VERIFY(msg, res, ARRAY_SIZE(res));
144 msgb_free(msg);
145}
146
Philipp Maierb478dd32017-03-29 15:50:05 +0200147static void test_create_cipher()
148{
149 static const uint8_t res[] =
150 { 0x00, 0x0c, 0x53, 0x0a, 0x09, 0x03, 0xaa,
151 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 };
152 static const uint8_t res2[] =
153 { 0x00, 0x0e, 0x53, 0x0a, 0x09, 0x03, 0xaa,
154 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42,
155 GSM0808_IE_CIPHER_RESPONSE_MODE, 0x01 };
156 struct msgb *msg;
157 struct gsm0808_encrypt_info ei;
158 uint8_t include_imeisv;
159
160 memset(&ei, 0, sizeof(ei));
161 ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
162 ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
163 ei.perm_algo_len = 2;
164 ei.key[0] = 0xaa;
165 ei.key[1] = 0xbb;
166 ei.key[2] = 0xcc;
167 ei.key[3] = 0xdd;
168 ei.key[4] = 0xee;
169 ei.key[5] = 0xff;
170 ei.key[6] = 0x23;
171 ei.key[7] = 0x42;
172 ei.key_len = 8;
173 include_imeisv = 1;
174
175 printf("Testing creating Chipher Mode Command\n");
176 msg = gsm0808_create_cipher(&ei, NULL);
177 OSMO_ASSERT(msg);
178 VERIFY(msg, res, ARRAY_SIZE(res));
179 msgb_free(msg);
180
181 msg = gsm0808_create_cipher(&ei, &include_imeisv);
182 OSMO_ASSERT(msg);
183 VERIFY(msg, res2, ARRAY_SIZE(res2));
184 msgb_free(msg);
185}
186
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100187static void test_create_cipher_complete()
188{
189 static const uint8_t res1[] = {
190 0x00, 0x08, 0x55, 0x20, 0x03, 0x23, 0x42, 0x21, 0x2c, 0x04 };
191 static const uint8_t res2[] = { 0x00, 0x03, 0x55, 0x2c, 0x04};
192 struct msgb *l3, *msg;
193
194 printf("Testing creating Cipher Complete\n");
195 l3 = msgb_alloc_headroom(512, 128, "l3h");
196 l3->l3h = l3->data;
197 msgb_v_put(l3, 0x23);
198 msgb_v_put(l3, 0x42);
199 msgb_v_put(l3, 0x21);
200
201 /* with l3 data */
202 msg = gsm0808_create_cipher_complete(l3, 4);
203 VERIFY(msg, res1, ARRAY_SIZE(res1));
204 msgb_free(msg);
205
206 /* with l3 data but short */
207 l3->len -= 1;
208 l3->tail -= 1;
209 msg = gsm0808_create_cipher_complete(l3, 4);
210 VERIFY(msg, res2, ARRAY_SIZE(res2));
211 msgb_free(msg);
212
213 /* without l3 data */
214 msg = gsm0808_create_cipher_complete(NULL, 4);
215 VERIFY(msg, res2, ARRAY_SIZE(res2));
216 msgb_free(msg);
217
218
219 msgb_free(l3);
220}
221
222static void test_create_cipher_reject()
223{
224 static const uint8_t res[] = { 0x00, 0x02, 0x59, 0x23 };
225 struct msgb *msg;
226
227 printf("Testing creating Cipher Reject\n");
228 msg = gsm0808_create_cipher_reject(0x23);
229 VERIFY(msg, res, ARRAY_SIZE(res));
230 msgb_free(msg);
231}
232
233static void test_create_cm_u()
234{
Harald Welte07b625d2012-01-23 10:02:58 +0100235 static const uint8_t res[] = {
236 0x00, 0x07, 0x54, 0x12, 0x01, 0x23, 0x13, 0x01, 0x42 };
237 static const uint8_t res2o[] = {
238 0x00, 0x04, 0x54, 0x12, 0x01, 0x23 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100239 struct msgb *msg;
Harald Welte07b625d2012-01-23 10:02:58 +0100240 const uint8_t cm2 = 0x23;
241 const uint8_t cm3 = 0x42;
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100242
243 printf("Testing creating CM U\n");
Harald Welte07b625d2012-01-23 10:02:58 +0100244 msg = gsm0808_create_classmark_update(&cm2, 1, &cm3, 1);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100245 VERIFY(msg, res, ARRAY_SIZE(res));
Harald Welte07b625d2012-01-23 10:02:58 +0100246
247 msg = gsm0808_create_classmark_update(&cm2, 1, NULL, 0);
248 VERIFY(msg, res2o, ARRAY_SIZE(res2o));
249
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100250 msgb_free(msg);
251}
252
253static void test_create_sapi_reject()
254{
255 static const uint8_t res[] = { 0x00, 0x03, 0x25, 0x03, 0x25 };
256 struct msgb *msg;
257
258 printf("Testing creating SAPI Reject\n");
259 msg = gsm0808_create_sapi_reject(3);
260 VERIFY(msg, res, ARRAY_SIZE(res));
261 msgb_free(msg);
262}
263
264static void test_create_ass_compl()
265{
266 static const uint8_t res1[] = {
267 0x00, 0x09, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c,
268 0x11, 0x40, 0x22 };
269 static const uint8_t res2[] = {
270 0x00, 0x07, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11};
271 struct msgb *msg;
272
273 printf("Testing creating Assignment Complete\n");
274 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0x22);
275 VERIFY(msg, res1, ARRAY_SIZE(res1));
276 msgb_free(msg);
277
278 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0);
279 VERIFY(msg, res2, ARRAY_SIZE(res2));
280 msgb_free(msg);
281}
282
Philipp Maierfa896ab2017-03-27 16:55:32 +0200283static void test_create_ass_compl_aoip()
284{
285 struct sockaddr_storage ss;
286 struct sockaddr_in sin;
287 struct gsm0808_speech_codec sc;
288 struct gsm0808_speech_codec_list sc_list;
289 static const uint8_t res[] =
290 { 0x00, 0x1d, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11, 0x40, 0x22,
291 GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17, 0x04,
292 0xd2, GSM0808_IE_SPEECH_CODEC, 0x01, 0x9a,
293 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, 0x5f, 0xab, 0xcd, 0xef, 0xa5,
294 0x9f, 0xf2 };
295 struct msgb *msg;
296
297 memset(&sin, 0, sizeof(sin));
298 sin.sin_family = AF_INET;
299 sin.sin_port = htons(1234);
300 inet_aton("192.168.100.23", &sin.sin_addr);
301
302 memset(&ss, 0, sizeof(ss));
303 memcpy(&ss, &sin, sizeof(sin));
304
305 memset(&sc, 0, sizeof(sc));
306 sc.fi = true;
307 sc.tf = true;
308 sc.type = 0x0a;
309
310 setup_codec_list(&sc_list);
311
312 printf("Testing creating Assignment Complete (AoIP)\n");
313 msg = gsm0808_create_ass_compl(0x23, 0x42, 0x11, 0x22,
314 &ss, &sc, &sc_list);
315 VERIFY(msg, res, ARRAY_SIZE(res));
316 msgb_free(msg);
317}
318
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100319static void test_create_ass_fail()
320{
321 static const uint8_t res1[] = { 0x00, 0x04, 0x03, 0x04, 0x01, 0x23 };
322 static const uint8_t res2[] = {
323 0x00, 0x06, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02};
324 uint8_t rr_res = 2;
325 struct msgb *msg;
326
327 printf("Testing creating Assignment Failure\n");
328 msg = gsm0808_create_assignment_failure(0x23, NULL);
329 VERIFY(msg, res1, ARRAY_SIZE(res1));
330 msgb_free(msg);
331
332 msg = gsm0808_create_assignment_failure(0x23, &rr_res);
333 VERIFY(msg, res2, ARRAY_SIZE(res2));
334 msgb_free(msg);
335}
336
Philipp Maierfa896ab2017-03-27 16:55:32 +0200337static void test_create_ass_fail_aoip()
338{
339 static const uint8_t res1[] =
340 { 0x00, 0x0d, 0x03, 0x04, 0x01, 0x23, GSM0808_IE_SPEECH_CODEC_LIST,
341 0x07, 0x5f, 0xab, 0xcd, 0xef, 0xa5, 0x9f, 0xf2 };
342 static const uint8_t res2[] =
343 { 0x00, 0x0f, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02,
344 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, 0x5f, 0xab,
345 0xcd, 0xef, 0xa5, 0x9f, 0xf2 };
346 uint8_t rr_res = 2;
347 struct msgb *msg;
348 struct gsm0808_speech_codec_list sc_list;
349
350 setup_codec_list(&sc_list);
351
352 printf("Testing creating Assignment Failure (AoIP)\n");
353 msg = gsm0808_create_ass_fail(0x23, NULL, &sc_list);
354 VERIFY(msg, res1, ARRAY_SIZE(res1));
355 msgb_free(msg);
356
357 msg = gsm0808_create_ass_fail(0x23, &rr_res, &sc_list);
358 VERIFY(msg, res2, ARRAY_SIZE(res2));
359 msgb_free(msg);
360}
361
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100362static void test_create_clear_rqst()
363{
364 static const uint8_t res[] = { 0x00, 0x04, 0x22, 0x04, 0x01, 0x23 };
365 struct msgb *msg;
366
367 printf("Testing creating Clear Request\n");
368 msg = gsm0808_create_clear_rqst(0x23);
369 VERIFY(msg, res, ARRAY_SIZE(res));
370 msgb_free(msg);
371}
372
373static void test_create_dtap()
374{
375 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
376 struct msgb *msg, *l3;
377
378 printf("Testing creating DTAP\n");
379 l3 = msgb_alloc_headroom(512, 128, "test");
380 l3->l3h = l3->data;
381 msgb_v_put(l3, 0x23);
382 msgb_v_put(l3, 0x42);
383
384 msg = gsm0808_create_dtap(l3, 0x3);
385 VERIFY(msg, res, ARRAY_SIZE(res));
386 msgb_free(msg);
387 msgb_free(l3);
388}
389
390static void test_prepend_dtap()
391{
392 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
393 struct msgb *in_msg;
394
395 printf("Testing prepend DTAP\n");
396
397 in_msg = msgb_alloc_headroom(512, 128, "test");
398 msgb_v_put(in_msg, 0x23);
399 msgb_v_put(in_msg, 0x42);
400
401 gsm0808_prepend_dtap_header(in_msg, 0x3);
402 in_msg->l3h = in_msg->data;
403 VERIFY(in_msg, res, ARRAY_SIZE(res));
404 msgb_free(in_msg);
405}
406
Philipp Maier22401432017-03-24 17:59:26 +0100407static void test_enc_dec_aoip_trasp_addr_v4()
408{
409 struct sockaddr_storage enc_addr;
410 struct sockaddr_storage dec_addr;
411 struct sockaddr_in enc_addr_in;
412 struct msgb *msg;
413 uint8_t rc_enc;
414 int rc_dec;
415
416 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
417 enc_addr_in.sin_family = AF_INET;
418 enc_addr_in.sin_port = htons(1234);
419 inet_aton("255.0.255.255", &enc_addr_in.sin_addr);
420
421 memset(&enc_addr, 0, sizeof(enc_addr));
422 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
423
424 msg = msgb_alloc(1024, "output buffer");
425 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
426 OSMO_ASSERT(rc_enc == 8);
427 rc_dec =
428 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
429 OSMO_ASSERT(rc_dec == 6);
430 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
431
432 msgb_free(msg);
433}
434
435static void test_enc_dec_aoip_trasp_addr_v6()
436{
437 struct sockaddr_storage enc_addr;
438 struct sockaddr_storage dec_addr;
439 struct sockaddr_in6 enc_addr_in;
440 struct msgb *msg;
441 uint8_t rc_enc;
442 int rc_dec;
443
444 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
445 enc_addr_in.sin6_family = AF_INET6;
446 enc_addr_in.sin6_port = htons(4567);
447 inet_pton(AF_INET6, "2001:0db8:85a3:08d3:1319:8a2e:0370:7344",
448 &enc_addr_in.sin6_addr);
449
450 memset(&enc_addr, 0, sizeof(enc_addr));
451 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
452
453 msg = msgb_alloc(1024, "output buffer");
454 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
455 OSMO_ASSERT(rc_enc == 20);
456 rc_dec =
457 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
458 OSMO_ASSERT(rc_dec == 18);
459 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
460
461 msgb_free(msg);
462}
463
Philipp Maier6f725d62017-03-24 18:03:17 +0100464static void test_gsm0808_enc_dec_speech_codec()
465{
466 struct gsm0808_speech_codec enc_sc;
467 struct gsm0808_speech_codec dec_sc;
468 struct msgb *msg;
469 uint8_t rc_enc;
470 int rc_dec;
471
472 memset(&enc_sc, 0, sizeof(enc_sc));
473 enc_sc.fi = true;
474 enc_sc.pt = true;
475 enc_sc.type = 0x05;
476
477 msg = msgb_alloc(1024, "output buffer");
478 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
479 OSMO_ASSERT(rc_enc == 3);
480
481 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
482 OSMO_ASSERT(rc_dec == 1);
483
484 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
485
486 msgb_free(msg);
487}
488
489
490static void test_gsm0808_enc_dec_speech_codec_ext_with_cfg()
491{
492 struct gsm0808_speech_codec enc_sc;
493 struct gsm0808_speech_codec dec_sc;
494 struct msgb *msg;
495 uint8_t rc_enc;
496 int rc_dec;
497
498 enc_sc.pi = true;
499 enc_sc.tf = true;
500 enc_sc.type = 0xab;
501 enc_sc.type_extended = true;
502 enc_sc.cfg_present = true;
503 enc_sc.cfg = 0xcdef;
504
505 msg = msgb_alloc(1024, "output buffer");
506 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
507 OSMO_ASSERT(rc_enc == 6);
508
509 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
510 OSMO_ASSERT(rc_dec == 4);
511
512 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
513
514 msgb_free(msg);
515}
516
517static void test_gsm0808_enc_dec_speech_codec_ext()
518{
519 struct gsm0808_speech_codec enc_sc;
520 struct gsm0808_speech_codec dec_sc;
521 struct msgb *msg;
522 uint8_t rc_enc;
523 int rc_dec;
524
525 enc_sc.fi = true;
526 enc_sc.tf = true;
527 enc_sc.type = 0xf2;
528 enc_sc.type_extended = true;
529 enc_sc.cfg_present = false;
530 enc_sc.cfg = 0x0000;
531
532 msg = msgb_alloc(1024, "output buffer");
533 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
534 OSMO_ASSERT(rc_enc == 4);
535
536 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
537 OSMO_ASSERT(rc_dec == 2);
538
539 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
540
541 msgb_free(msg);
542}
543
544static void test_gsm0808_enc_dec_speech_codec_list()
545{
546 struct gsm0808_speech_codec_list enc_scl;
547 struct gsm0808_speech_codec_list dec_scl;
548 struct msgb *msg;
549 uint8_t rc_enc;
550 int rc_dec;
551
552 memset(&enc_scl, 0, sizeof(enc_scl));
553
554 enc_scl.codec[0].pi = true;
555 enc_scl.codec[0].tf = true;
556 enc_scl.codec[0].type = 0xab;
557 enc_scl.codec[0].type_extended = true;
558 enc_scl.codec[0].cfg_present = true;
559 enc_scl.codec[0].cfg = 0xcdef;
560
561 enc_scl.codec[1].fi = true;
562 enc_scl.codec[1].pt = true;
563 enc_scl.codec[1].type = 0x05;
564
565 enc_scl.codec[2].fi = true;
566 enc_scl.codec[2].tf = true;
567 enc_scl.codec[2].type = 0xf2;
568 enc_scl.codec[2].type_extended = true;
569
570 enc_scl.len = 3;
571
572 msg = msgb_alloc(1024, "output buffer");
573 rc_enc = gsm0808_enc_speech_codec_list(msg, &enc_scl);
574 OSMO_ASSERT(rc_enc == 9);
575
576 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
577 OSMO_ASSERT(rc_dec == 7);
578
579 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
580
581 msgb_free(msg);
582}
583
Philipp Maiere0c65302017-03-28 17:05:40 +0200584static void test_gsm0808_enc_dec_channel_type()
585{
586 struct gsm0808_channel_type enc_ct;
587 struct gsm0808_channel_type dec_ct;
588 struct msgb *msg;
589 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
590 0x04, 0x01, 0x0b, 0xa1, 0x25
591 };
592 uint8_t rc_enc;
593 int rc_dec;
594
595 memset(&enc_ct, 0, sizeof(enc_ct));
596 enc_ct.ch_indctr = GSM0808_CHAN_SPEECH;
597 enc_ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
598 enc_ct.perm_spch[0] = GSM0808_PERM_FR3;
599 enc_ct.perm_spch[1] = GSM0808_PERM_HR3;
600 enc_ct.perm_spch_len = 2;
601
602 msg = msgb_alloc(1024, "output buffer");
603 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
604 OSMO_ASSERT(rc_enc == 6);
605 OSMO_ASSERT(memcmp(ct_enc_expected, msg->data, msg->len) == 0);
606
607 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
608 OSMO_ASSERT(rc_dec == 4);
609 OSMO_ASSERT(memcmp(&enc_ct, &dec_ct, sizeof(enc_ct)) == 0);
610
611 msgb_free(msg);
612}
613
Philipp Maier14e76b92017-03-28 18:36:52 +0200614static void test_gsm0808_enc_dec_encrypt_info()
615{
616 struct gsm0808_encrypt_info enc_ei;
617 struct gsm0808_encrypt_info dec_ei;
618 struct msgb *msg;
619 uint8_t ei_enc_expected[] =
620 { GSM0808_IE_ENCRYPTION_INFORMATION, 0x09, 0x03, 0xaa, 0xbb,
621 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42
622 };
623 uint8_t rc_enc;
624 int rc_dec;
625
626 memset(&enc_ei, 0, sizeof(enc_ei));
627 enc_ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
628 enc_ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
629 enc_ei.perm_algo_len = 2;
630 enc_ei.key[0] = 0xaa;
631 enc_ei.key[1] = 0xbb;
632 enc_ei.key[2] = 0xcc;
633 enc_ei.key[3] = 0xdd;
634 enc_ei.key[4] = 0xee;
635 enc_ei.key[5] = 0xff;
636 enc_ei.key[6] = 0x23;
637 enc_ei.key[7] = 0x42;
638 enc_ei.key_len = 8;
639
640 msg = msgb_alloc(1024, "output buffer");
641 rc_enc = gsm0808_enc_encrypt_info(msg, &enc_ei);
642 OSMO_ASSERT(rc_enc == 11);
643 OSMO_ASSERT(memcmp(ei_enc_expected, msg->data, msg->len) == 0);
644
645 rc_dec = gsm0808_dec_encrypt_info(&dec_ei, msg->data + 2, msg->len - 2);
646 OSMO_ASSERT(rc_dec == 9);
647
648 OSMO_ASSERT(memcmp(&enc_ei, &dec_ei, sizeof(enc_ei)) == 0);
649
650 msgb_free(msg);
651}
652
Philipp Maier783047e2017-03-29 11:35:50 +0200653static void test_gsm0808_enc_dec_cell_id_list_lac()
654{
655 struct gsm0808_cell_id_list enc_cil;
656 struct gsm0808_cell_id_list dec_cil;
657 struct msgb *msg;
658 uint8_t rc_enc;
659 int rc_dec;
660
661 memset(&enc_cil, 0, sizeof(enc_cil));
662 enc_cil.id_discr = CELL_IDENT_LAC;
663 enc_cil.id_list_lac[0] = 0x0124;
664 enc_cil.id_list_lac[1] = 0xABCD;
665 enc_cil.id_list_lac[2] = 0x5678;
666 enc_cil.id_list_len = 3;
667
668 msg = msgb_alloc(1024, "output buffer");
669 rc_enc = gsm0808_enc_cell_id_list(msg, &enc_cil);
670 OSMO_ASSERT(rc_enc == 9);
671
672 rc_dec = gsm0808_dec_cell_id_list(&dec_cil, msg->data + 2,
673 msg->len - 2);
674 OSMO_ASSERT(rc_dec == 7);
675
676 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
677
678 msgb_free(msg);
679}
680
681static void test_gsm0808_enc_dec_cell_id_list_single_lac()
682{
683 struct gsm0808_cell_id_list enc_cil;
684 struct gsm0808_cell_id_list dec_cil;
685 struct msgb *msg;
686 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x03,
687 0x05, 0x23, 0x42
688 };
689 uint8_t rc_enc;
690 int rc_dec;
691
692 memset(&enc_cil, 0, sizeof(enc_cil));
693 enc_cil.id_discr = CELL_IDENT_LAC;
694 enc_cil.id_list_lac[0] = 0x2342;
695 enc_cil.id_list_len = 1;
696
697 msg = msgb_alloc(1024, "output buffer");
698 rc_enc = gsm0808_enc_cell_id_list(msg, &enc_cil);
699 OSMO_ASSERT(rc_enc == 5);
700 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
701
702 rc_dec = gsm0808_dec_cell_id_list(&dec_cil, msg->data + 2,
703 msg->len - 2);
704 OSMO_ASSERT(rc_dec == 3);
705
706 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
707
708 msgb_free(msg);
709}
710
711static void test_gsm0808_enc_dec_cell_id_list_bss()
712{
713 struct gsm0808_cell_id_list enc_cil;
714 struct gsm0808_cell_id_list dec_cil;
715 struct msgb *msg;
716 uint8_t rc_enc;
717 int rc_dec;
718
719 memset(&enc_cil, 0, sizeof(enc_cil));
720 enc_cil.id_discr = CELL_IDENT_LAC;
721
722 msg = msgb_alloc(1024, "output buffer");
723 rc_enc = gsm0808_enc_cell_id_list(msg, &enc_cil);
724 OSMO_ASSERT(rc_enc == 3);
725
726 rc_dec = gsm0808_dec_cell_id_list(&dec_cil, msg->data + 2,
727 msg->len - 2);
728 OSMO_ASSERT(rc_dec == 1);
729
730 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
731
732 msgb_free(msg);
733}
734
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100735int main(int argc, char **argv)
736{
737 printf("Testing generation of GSM0808 messages\n");
738 test_create_layer3();
Philipp Maierfa896ab2017-03-27 16:55:32 +0200739 test_create_layer3_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100740 test_create_reset();
741 test_create_clear_command();
742 test_create_clear_complete();
Philipp Maierb478dd32017-03-29 15:50:05 +0200743 test_create_cipher();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100744 test_create_cipher_complete();
745 test_create_cipher_reject();
746 test_create_cm_u();
747 test_create_sapi_reject();
748 test_create_ass_compl();
Philipp Maierfa896ab2017-03-27 16:55:32 +0200749 test_create_ass_compl_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100750 test_create_ass_fail();
Philipp Maierfa896ab2017-03-27 16:55:32 +0200751 test_create_ass_fail_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100752 test_create_clear_rqst();
753 test_create_dtap();
754 test_prepend_dtap();
Philipp Maier22401432017-03-24 17:59:26 +0100755 test_enc_dec_aoip_trasp_addr_v4();
756 test_enc_dec_aoip_trasp_addr_v6();
Philipp Maier6f725d62017-03-24 18:03:17 +0100757 test_gsm0808_enc_dec_speech_codec();
758 test_gsm0808_enc_dec_speech_codec_ext();
759 test_gsm0808_enc_dec_speech_codec_ext_with_cfg();
760 test_gsm0808_enc_dec_speech_codec_list();
Philipp Maiere0c65302017-03-28 17:05:40 +0200761 test_gsm0808_enc_dec_channel_type();
Philipp Maier14e76b92017-03-28 18:36:52 +0200762 test_gsm0808_enc_dec_encrypt_info();
Philipp Maier783047e2017-03-29 11:35:50 +0200763 test_gsm0808_enc_dec_cell_id_list_lac();
764 test_gsm0808_enc_dec_cell_id_list_single_lac();
765 test_gsm0808_enc_dec_cell_id_list_bss();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100766
767 printf("Done\n");
768 return EXIT_SUCCESS;
769}