blob: f46709964f3398e5836053bd281231836921ae5d [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>
Philipp Maier3d48ec02017-03-29 17:37:55 +020024#include <osmocom/gsm/protocol/gsm_08_58.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010025
26#include <stdio.h>
27#include <stdlib.h>
Philipp Maier22401432017-03-24 17:59:26 +010028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
Neels Hofmeyr74663d92018-03-23 01:46:42 +010031#include <errno.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010032
33#define VERIFY(msg, data, len) \
34 if (msgb_l3len(msg) != len) { \
35 printf("%s:%d Length don't match: %d vs. %d. %s\n", \
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000036 __func__, __LINE__, msgb_l3len(msg), (int) len, \
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010037 osmo_hexdump(msg->l3h, msgb_l3len(msg))); \
38 abort(); \
39 } else if (memcmp(msg->l3h, data, len) != 0) { \
40 printf("%s:%d didn't match: got: %s\n", \
41 __func__, __LINE__, \
42 osmo_hexdump(msg->l3h, msgb_l3len(msg))); \
43 abort(); \
44 }
45
Philipp Maierfa896ab2017-03-27 16:55:32 +020046/* Setup a fake codec list for testing */
47static void setup_codec_list(struct gsm0808_speech_codec_list *scl)
48{
49 memset(scl, 0, sizeof(*scl));
50
51 scl->codec[0].pi = true;
52 scl->codec[0].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +020053 scl->codec[0].type = GSM0808_SCT_FR3;
Philipp Maierfa896ab2017-03-27 16:55:32 +020054 scl->codec[0].cfg = 0xcdef;
55
56 scl->codec[1].fi = true;
57 scl->codec[1].pt = true;
Philipp Maierbb839662017-06-01 17:11:19 +020058 scl->codec[1].type = GSM0808_SCT_FR2;
Philipp Maierfa896ab2017-03-27 16:55:32 +020059
60 scl->codec[2].fi = true;
61 scl->codec[2].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +020062 scl->codec[2].type = GSM0808_SCT_CSD;
63 scl->codec[2].cfg = 0xc0;
Philipp Maierfa896ab2017-03-27 16:55:32 +020064
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,
Philipp Maierbb839662017-06-01 17:11:19 +020091 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50,
Philipp Maier7e27b142018-03-22 17:26:46 +010092 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f,
Philipp Maierbb839662017-06-01 17:11:19 +020093 GSM0808_SCT_CSD | 0x90, 0xc0
Philipp Maierfa896ab2017-03-27 16:55:32 +020094 };
95
96 struct msgb *msg, *in_msg;
97 struct gsm0808_speech_codec_list sc_list;
98 printf("Testing creating Layer3 (AoIP)\n");
99
100 setup_codec_list(&sc_list);
101
102 in_msg = msgb_alloc_headroom(512, 128, "foo");
103 in_msg->l3h = in_msg->data;
104 msgb_v_put(in_msg, 0x23);
105
106 msg =
107 gsm0808_create_layer3_aoip(in_msg, 0x1122, 0x2244, 0x3366, 0x4488,
108 &sc_list);
109 VERIFY(msg, res, ARRAY_SIZE(res));
110
111 msgb_free(msg);
112 msgb_free(in_msg);
113}
114
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100115static void test_create_reset()
116{
117 static const uint8_t res[] = { 0x00, 0x04, 0x30, 0x04, 0x01, 0x20 };
118 struct msgb *msg;
119
120 printf("Testing creating Reset\n");
121 msg = gsm0808_create_reset();
122 VERIFY(msg, res, ARRAY_SIZE(res));
123 msgb_free(msg);
124}
125
Philipp Maier15596e22017-04-05 17:55:27 +0200126static void test_create_reset_ack()
127{
128 static const uint8_t res[] = { 0x00, 0x01, 0x31 };
129 struct msgb *msg;
130
131 printf("Testing creating Reset Ack\n");
132 msg = gsm0808_create_reset_ack();
133 VERIFY(msg, res, ARRAY_SIZE(res));
134 msgb_free(msg);
135}
136
137
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100138static void test_create_clear_command()
139{
140 static const uint8_t res[] = { 0x20, 0x04, 0x01, 0x23 };
141 struct msgb *msg;
142
143 printf("Testing creating Clear Command\n");
144 msg = gsm0808_create_clear_command(0x23);
145 VERIFY(msg, res, ARRAY_SIZE(res));
146 msgb_free(msg);
147}
148
149static void test_create_clear_complete()
150{
151 static const uint8_t res[] = { 0x00, 0x01, 0x21 };
152 struct msgb *msg;
153
154 printf("Testing creating Clear Complete\n");
155 msg = gsm0808_create_clear_complete();
156 VERIFY(msg, res, ARRAY_SIZE(res));
157 msgb_free(msg);
158}
159
Philipp Maierb478dd32017-03-29 15:50:05 +0200160static void test_create_cipher()
161{
162 static const uint8_t res[] =
163 { 0x00, 0x0c, 0x53, 0x0a, 0x09, 0x03, 0xaa,
164 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 };
165 static const uint8_t res2[] =
166 { 0x00, 0x0e, 0x53, 0x0a, 0x09, 0x03, 0xaa,
167 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42,
168 GSM0808_IE_CIPHER_RESPONSE_MODE, 0x01 };
169 struct msgb *msg;
170 struct gsm0808_encrypt_info ei;
171 uint8_t include_imeisv;
172
173 memset(&ei, 0, sizeof(ei));
174 ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
175 ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
176 ei.perm_algo_len = 2;
177 ei.key[0] = 0xaa;
178 ei.key[1] = 0xbb;
179 ei.key[2] = 0xcc;
180 ei.key[3] = 0xdd;
181 ei.key[4] = 0xee;
182 ei.key[5] = 0xff;
183 ei.key[6] = 0x23;
184 ei.key[7] = 0x42;
185 ei.key_len = 8;
186 include_imeisv = 1;
187
188 printf("Testing creating Chipher Mode Command\n");
189 msg = gsm0808_create_cipher(&ei, NULL);
190 OSMO_ASSERT(msg);
191 VERIFY(msg, res, ARRAY_SIZE(res));
192 msgb_free(msg);
193
194 msg = gsm0808_create_cipher(&ei, &include_imeisv);
195 OSMO_ASSERT(msg);
196 VERIFY(msg, res2, ARRAY_SIZE(res2));
197 msgb_free(msg);
198}
199
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100200static void test_create_cipher_complete()
201{
202 static const uint8_t res1[] = {
203 0x00, 0x08, 0x55, 0x20, 0x03, 0x23, 0x42, 0x21, 0x2c, 0x04 };
204 static const uint8_t res2[] = { 0x00, 0x03, 0x55, 0x2c, 0x04};
205 struct msgb *l3, *msg;
206
207 printf("Testing creating Cipher Complete\n");
208 l3 = msgb_alloc_headroom(512, 128, "l3h");
209 l3->l3h = l3->data;
210 msgb_v_put(l3, 0x23);
211 msgb_v_put(l3, 0x42);
212 msgb_v_put(l3, 0x21);
213
214 /* with l3 data */
215 msg = gsm0808_create_cipher_complete(l3, 4);
216 VERIFY(msg, res1, ARRAY_SIZE(res1));
217 msgb_free(msg);
218
219 /* with l3 data but short */
220 l3->len -= 1;
221 l3->tail -= 1;
222 msg = gsm0808_create_cipher_complete(l3, 4);
223 VERIFY(msg, res2, ARRAY_SIZE(res2));
224 msgb_free(msg);
225
226 /* without l3 data */
227 msg = gsm0808_create_cipher_complete(NULL, 4);
228 VERIFY(msg, res2, ARRAY_SIZE(res2));
229 msgb_free(msg);
230
231
232 msgb_free(l3);
233}
234
235static void test_create_cipher_reject()
236{
Harald Welte62e40852017-12-17 20:50:34 +0100237 static const uint8_t res[] = { 0x00, 0x04, 0x59, 0x04, 0x01, 0x23 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100238 struct msgb *msg;
239
240 printf("Testing creating Cipher Reject\n");
241 msg = gsm0808_create_cipher_reject(0x23);
242 VERIFY(msg, res, ARRAY_SIZE(res));
243 msgb_free(msg);
244}
245
246static void test_create_cm_u()
247{
Harald Welte07b625d2012-01-23 10:02:58 +0100248 static const uint8_t res[] = {
249 0x00, 0x07, 0x54, 0x12, 0x01, 0x23, 0x13, 0x01, 0x42 };
250 static const uint8_t res2o[] = {
251 0x00, 0x04, 0x54, 0x12, 0x01, 0x23 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100252 struct msgb *msg;
Harald Welte07b625d2012-01-23 10:02:58 +0100253 const uint8_t cm2 = 0x23;
254 const uint8_t cm3 = 0x42;
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100255
256 printf("Testing creating CM U\n");
Harald Welte07b625d2012-01-23 10:02:58 +0100257 msg = gsm0808_create_classmark_update(&cm2, 1, &cm3, 1);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100258 VERIFY(msg, res, ARRAY_SIZE(res));
Harald Welte07b625d2012-01-23 10:02:58 +0100259
Neels Hofmeyr9a938ae2017-11-16 17:34:07 +0100260 msgb_free(msg);
261
Harald Welte07b625d2012-01-23 10:02:58 +0100262 msg = gsm0808_create_classmark_update(&cm2, 1, NULL, 0);
263 VERIFY(msg, res2o, ARRAY_SIZE(res2o));
264
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100265 msgb_free(msg);
266}
267
268static void test_create_sapi_reject()
269{
270 static const uint8_t res[] = { 0x00, 0x03, 0x25, 0x03, 0x25 };
271 struct msgb *msg;
272
273 printf("Testing creating SAPI Reject\n");
274 msg = gsm0808_create_sapi_reject(3);
275 VERIFY(msg, res, ARRAY_SIZE(res));
276 msgb_free(msg);
277}
278
Philipp Maierc6144a22017-03-29 17:53:43 +0200279static void test_create_ass()
280{
281 static const uint8_t res1[] =
282 { 0x00, 0x0a, 0x01, 0x0b, 0x04, 0x01, 0x0b, 0xa1, 0x25, 0x01, 0x00,
283 0x04 };
284 static const uint8_t res2[] =
285 { 0x00, 0x20, 0x01, 0x0b, 0x04, 0x01, 0x0b, 0xa1, 0x25, 0x01, 0x00,
286 0x04, GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17,
Philipp Maierbb839662017-06-01 17:11:19 +0200287 0x04, 0xd2, GSM0808_IE_SPEECH_CODEC_LIST, 0x07,
Philipp Maier7e27b142018-03-22 17:26:46 +0100288 GSM0808_SCT_FR3 | 0x50, 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f,
Philipp Maierbb839662017-06-01 17:11:19 +0200289 GSM0808_SCT_CSD | 0x90, 0xc0, GSM0808_IE_CALL_ID, 0xaa, 0xbb,
290 0xcc, 0xdd };
Philipp Maierc6144a22017-03-29 17:53:43 +0200291
292 struct msgb *msg;
293 struct gsm0808_channel_type ct;
294 uint16_t cic = 0004;
295 struct sockaddr_storage ss;
296 struct sockaddr_in sin;
297 struct gsm0808_speech_codec_list sc_list;
298 uint32_t call_id = 0xAABBCCDD;
299
300 memset(&ct, 0, sizeof(ct));
301 ct.ch_indctr = GSM0808_CHAN_SPEECH;
302 ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
303 ct.perm_spch[0] = GSM0808_PERM_FR3;
304 ct.perm_spch[1] = GSM0808_PERM_HR3;
305 ct.perm_spch_len = 2;
306
307 memset(&sin, 0, sizeof(sin));
308 sin.sin_family = AF_INET;
309 sin.sin_port = htons(1234);
310 inet_aton("192.168.100.23", &sin.sin_addr);
311
312 memset(&ss, 0, sizeof(ss));
313 memcpy(&ss, &sin, sizeof(sin));
314
315 setup_codec_list(&sc_list);
316
317 printf("Testing creating Assignment Request\n");
318 msg = gsm0808_create_ass(&ct, &cic, NULL, NULL, NULL);
319 OSMO_ASSERT(msg);
320 VERIFY(msg, res1, ARRAY_SIZE(res1));
321 msgb_free(msg);
322
323 msg = gsm0808_create_ass(&ct, &cic, &ss, &sc_list, &call_id);
324 OSMO_ASSERT(msg);
325 VERIFY(msg, res2, ARRAY_SIZE(res2));
326 msgb_free(msg);
327}
328
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100329static void test_create_ass_compl()
330{
331 static const uint8_t res1[] = {
332 0x00, 0x09, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c,
333 0x11, 0x40, 0x22 };
334 static const uint8_t res2[] = {
335 0x00, 0x07, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11};
336 struct msgb *msg;
337
338 printf("Testing creating Assignment Complete\n");
339 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0x22);
340 VERIFY(msg, res1, ARRAY_SIZE(res1));
341 msgb_free(msg);
342
343 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0);
344 VERIFY(msg, res2, ARRAY_SIZE(res2));
345 msgb_free(msg);
346}
347
Philipp Maierfa896ab2017-03-27 16:55:32 +0200348static void test_create_ass_compl_aoip()
349{
350 struct sockaddr_storage ss;
351 struct sockaddr_in sin;
352 struct gsm0808_speech_codec sc;
353 struct gsm0808_speech_codec_list sc_list;
354 static const uint8_t res[] =
355 { 0x00, 0x1d, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11, 0x40, 0x22,
356 GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17, 0x04,
Philipp Maierbb839662017-06-01 17:11:19 +0200357 0xd2, GSM0808_IE_SPEECH_CODEC, 0x01, GSM0808_SCT_HR1 | 0x90,
Philipp Maier7e27b142018-03-22 17:26:46 +0100358 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50, 0xef,
359 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200360 struct msgb *msg;
361
362 memset(&sin, 0, sizeof(sin));
363 sin.sin_family = AF_INET;
364 sin.sin_port = htons(1234);
365 inet_aton("192.168.100.23", &sin.sin_addr);
366
367 memset(&ss, 0, sizeof(ss));
368 memcpy(&ss, &sin, sizeof(sin));
369
370 memset(&sc, 0, sizeof(sc));
371 sc.fi = true;
372 sc.tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200373 sc.type = GSM0808_SCT_HR1;
Philipp Maierfa896ab2017-03-27 16:55:32 +0200374
375 setup_codec_list(&sc_list);
376
377 printf("Testing creating Assignment Complete (AoIP)\n");
378 msg = gsm0808_create_ass_compl(0x23, 0x42, 0x11, 0x22,
379 &ss, &sc, &sc_list);
380 VERIFY(msg, res, ARRAY_SIZE(res));
381 msgb_free(msg);
382}
383
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100384static void test_create_ass_fail()
385{
386 static const uint8_t res1[] = { 0x00, 0x04, 0x03, 0x04, 0x01, 0x23 };
387 static const uint8_t res2[] = {
388 0x00, 0x06, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02};
389 uint8_t rr_res = 2;
390 struct msgb *msg;
391
392 printf("Testing creating Assignment Failure\n");
393 msg = gsm0808_create_assignment_failure(0x23, NULL);
394 VERIFY(msg, res1, ARRAY_SIZE(res1));
395 msgb_free(msg);
396
397 msg = gsm0808_create_assignment_failure(0x23, &rr_res);
398 VERIFY(msg, res2, ARRAY_SIZE(res2));
399 msgb_free(msg);
400}
401
Philipp Maierfa896ab2017-03-27 16:55:32 +0200402static void test_create_ass_fail_aoip()
403{
404 static const uint8_t res1[] =
405 { 0x00, 0x0d, 0x03, 0x04, 0x01, 0x23, GSM0808_IE_SPEECH_CODEC_LIST,
Philipp Maier7e27b142018-03-22 17:26:46 +0100406 0x07, GSM0808_SCT_FR3 | 0x50, 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0,
Philipp Maierbb839662017-06-01 17:11:19 +0200407 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200408 static const uint8_t res2[] =
409 { 0x00, 0x0f, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02,
Philipp Maier7e27b142018-03-22 17:26:46 +0100410 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50, 0xef,
411 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200412 uint8_t rr_res = 2;
413 struct msgb *msg;
414 struct gsm0808_speech_codec_list sc_list;
415
416 setup_codec_list(&sc_list);
417
418 printf("Testing creating Assignment Failure (AoIP)\n");
419 msg = gsm0808_create_ass_fail(0x23, NULL, &sc_list);
420 VERIFY(msg, res1, ARRAY_SIZE(res1));
421 msgb_free(msg);
422
423 msg = gsm0808_create_ass_fail(0x23, &rr_res, &sc_list);
424 VERIFY(msg, res2, ARRAY_SIZE(res2));
425 msgb_free(msg);
426}
427
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100428static void test_create_clear_rqst()
429{
430 static const uint8_t res[] = { 0x00, 0x04, 0x22, 0x04, 0x01, 0x23 };
431 struct msgb *msg;
432
433 printf("Testing creating Clear Request\n");
434 msg = gsm0808_create_clear_rqst(0x23);
435 VERIFY(msg, res, ARRAY_SIZE(res));
436 msgb_free(msg);
437}
438
Philipp Maier3d48ec02017-03-29 17:37:55 +0200439static void test_create_paging()
440{
441 static const uint8_t res[] =
442 { 0x00, 0x10, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
443 0x21, 0x43, 0x1a, 0x03, 0x05, 0x23, 0x42 };
444 static const uint8_t res2[] =
445 { 0x00, 0x16, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
446 0x21, 0x43, GSM0808_IE_TMSI, 0x04, 0x12, 0x34, 0x56, 0x78, 0x1a,
447 0x03, 0x05, 0x23, 0x42 };
448 static const uint8_t res3[] =
449 { 0x00, 0x18, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
450 0x21, 0x43, GSM0808_IE_TMSI, 0x04, 0x12, 0x34, 0x56, 0x78, 0x1a,
451 0x03, 0x05, 0x23, 0x42, GSM0808_IE_CHANNEL_NEEDED,
452 RSL_CHANNEED_TCH_ForH };
453
454 struct msgb *msg;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100455 struct gsm0808_cell_id_list2 cil;
Philipp Maier3d48ec02017-03-29 17:37:55 +0200456 uint32_t tmsi = 0x12345678;
457 uint8_t chan_needed = RSL_CHANNEED_TCH_ForH;
458
459 char imsi[] = "001010000001234";
460
461 cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100462 cil.id_list[0].lac = 0x2342;
Philipp Maier3d48ec02017-03-29 17:37:55 +0200463 cil.id_list_len = 1;
464
465 printf("Testing creating Paging Request\n");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100466 msg = gsm0808_create_paging2(imsi, NULL, &cil, NULL);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200467 VERIFY(msg, res, ARRAY_SIZE(res));
468 msgb_free(msg);
469
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100470 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, NULL);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200471 VERIFY(msg, res2, ARRAY_SIZE(res2));
472 msgb_free(msg);
473
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100474 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, &chan_needed);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200475 VERIFY(msg, res3, ARRAY_SIZE(res3));
476 msgb_free(msg);
477}
478
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100479static void test_create_dtap()
480{
481 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
482 struct msgb *msg, *l3;
483
484 printf("Testing creating DTAP\n");
485 l3 = msgb_alloc_headroom(512, 128, "test");
486 l3->l3h = l3->data;
487 msgb_v_put(l3, 0x23);
488 msgb_v_put(l3, 0x42);
489
490 msg = gsm0808_create_dtap(l3, 0x3);
491 VERIFY(msg, res, ARRAY_SIZE(res));
492 msgb_free(msg);
493 msgb_free(l3);
494}
495
496static void test_prepend_dtap()
497{
498 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
499 struct msgb *in_msg;
500
501 printf("Testing prepend DTAP\n");
502
503 in_msg = msgb_alloc_headroom(512, 128, "test");
504 msgb_v_put(in_msg, 0x23);
505 msgb_v_put(in_msg, 0x42);
506
507 gsm0808_prepend_dtap_header(in_msg, 0x3);
508 in_msg->l3h = in_msg->data;
509 VERIFY(in_msg, res, ARRAY_SIZE(res));
510 msgb_free(in_msg);
511}
512
Philipp Maier22401432017-03-24 17:59:26 +0100513static void test_enc_dec_aoip_trasp_addr_v4()
514{
515 struct sockaddr_storage enc_addr;
516 struct sockaddr_storage dec_addr;
517 struct sockaddr_in enc_addr_in;
518 struct msgb *msg;
519 uint8_t rc_enc;
520 int rc_dec;
521
522 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
523 enc_addr_in.sin_family = AF_INET;
524 enc_addr_in.sin_port = htons(1234);
525 inet_aton("255.0.255.255", &enc_addr_in.sin_addr);
526
527 memset(&enc_addr, 0, sizeof(enc_addr));
528 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
529
530 msg = msgb_alloc(1024, "output buffer");
531 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
532 OSMO_ASSERT(rc_enc == 8);
533 rc_dec =
534 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
535 OSMO_ASSERT(rc_dec == 6);
536 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
537
538 msgb_free(msg);
539}
540
541static void test_enc_dec_aoip_trasp_addr_v6()
542{
543 struct sockaddr_storage enc_addr;
544 struct sockaddr_storage dec_addr;
545 struct sockaddr_in6 enc_addr_in;
546 struct msgb *msg;
547 uint8_t rc_enc;
548 int rc_dec;
549
550 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
551 enc_addr_in.sin6_family = AF_INET6;
552 enc_addr_in.sin6_port = htons(4567);
553 inet_pton(AF_INET6, "2001:0db8:85a3:08d3:1319:8a2e:0370:7344",
554 &enc_addr_in.sin6_addr);
555
556 memset(&enc_addr, 0, sizeof(enc_addr));
557 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
558
559 msg = msgb_alloc(1024, "output buffer");
560 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
561 OSMO_ASSERT(rc_enc == 20);
562 rc_dec =
563 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
564 OSMO_ASSERT(rc_dec == 18);
565 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
566
567 msgb_free(msg);
568}
569
Philipp Maier6f725d62017-03-24 18:03:17 +0100570static void test_gsm0808_enc_dec_speech_codec()
571{
572 struct gsm0808_speech_codec enc_sc;
573 struct gsm0808_speech_codec dec_sc;
574 struct msgb *msg;
575 uint8_t rc_enc;
576 int rc_dec;
577
578 memset(&enc_sc, 0, sizeof(enc_sc));
579 enc_sc.fi = true;
580 enc_sc.pt = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200581 enc_sc.type = GSM0808_SCT_FR2;
Philipp Maier6f725d62017-03-24 18:03:17 +0100582
583 msg = msgb_alloc(1024, "output buffer");
584 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
585 OSMO_ASSERT(rc_enc == 3);
586
587 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
588 OSMO_ASSERT(rc_dec == 1);
589
590 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
591
592 msgb_free(msg);
593}
594
595
Philipp Maierbb839662017-06-01 17:11:19 +0200596static void test_gsm0808_enc_dec_speech_codec_with_cfg()
597{
598 struct gsm0808_speech_codec enc_sc;
599 struct gsm0808_speech_codec dec_sc;
600 struct msgb *msg;
601 uint8_t rc_enc;
602 int rc_dec;
603
604 enc_sc.pi = true;
605 enc_sc.tf = true;
606 enc_sc.type = GSM0808_SCT_FR3;
607 enc_sc.cfg = 0xabcd;
608
609 msg = msgb_alloc(1024, "output buffer");
610 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
611 OSMO_ASSERT(rc_enc == 5);
612
613 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
614 OSMO_ASSERT(rc_dec == 3);
615
616 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
617
618 msgb_free(msg);
619}
620
Philipp Maier6f725d62017-03-24 18:03:17 +0100621static void test_gsm0808_enc_dec_speech_codec_ext_with_cfg()
622{
623 struct gsm0808_speech_codec enc_sc;
624 struct gsm0808_speech_codec dec_sc;
625 struct msgb *msg;
626 uint8_t rc_enc;
627 int rc_dec;
628
629 enc_sc.pi = true;
630 enc_sc.tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200631 enc_sc.type = GSM0808_SCT_CSD;
632 enc_sc.cfg = 0xc0;
Philipp Maier6f725d62017-03-24 18:03:17 +0100633
634 msg = msgb_alloc(1024, "output buffer");
635 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
Philipp Maierbb839662017-06-01 17:11:19 +0200636 OSMO_ASSERT(rc_enc == 5);
Philipp Maier6f725d62017-03-24 18:03:17 +0100637
638 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
Philipp Maierbb839662017-06-01 17:11:19 +0200639 OSMO_ASSERT(rc_dec == 3);
Philipp Maier6f725d62017-03-24 18:03:17 +0100640
641 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
642
643 msgb_free(msg);
644}
645
646static void test_gsm0808_enc_dec_speech_codec_list()
647{
648 struct gsm0808_speech_codec_list enc_scl;
649 struct gsm0808_speech_codec_list dec_scl;
650 struct msgb *msg;
651 uint8_t rc_enc;
652 int rc_dec;
653
654 memset(&enc_scl, 0, sizeof(enc_scl));
655
656 enc_scl.codec[0].pi = true;
657 enc_scl.codec[0].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200658 enc_scl.codec[0].type = GSM0808_SCT_FR3;
Philipp Maier6f725d62017-03-24 18:03:17 +0100659 enc_scl.codec[0].cfg = 0xcdef;
660
661 enc_scl.codec[1].fi = true;
662 enc_scl.codec[1].pt = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200663 enc_scl.codec[1].type = GSM0808_SCT_FR2;
Philipp Maier6f725d62017-03-24 18:03:17 +0100664
665 enc_scl.codec[2].fi = true;
666 enc_scl.codec[2].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200667 enc_scl.codec[2].type = GSM0808_SCT_CSD;
668 enc_scl.codec[2].cfg = 0xc0;
Philipp Maier6f725d62017-03-24 18:03:17 +0100669
670 enc_scl.len = 3;
671
672 msg = msgb_alloc(1024, "output buffer");
673 rc_enc = gsm0808_enc_speech_codec_list(msg, &enc_scl);
674 OSMO_ASSERT(rc_enc == 9);
675
676 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
677 OSMO_ASSERT(rc_dec == 7);
678
679 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
680
681 msgb_free(msg);
682}
683
Philipp Maiere0c65302017-03-28 17:05:40 +0200684static void test_gsm0808_enc_dec_channel_type()
685{
686 struct gsm0808_channel_type enc_ct;
687 struct gsm0808_channel_type dec_ct;
688 struct msgb *msg;
689 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
690 0x04, 0x01, 0x0b, 0xa1, 0x25
691 };
692 uint8_t rc_enc;
693 int rc_dec;
694
695 memset(&enc_ct, 0, sizeof(enc_ct));
696 enc_ct.ch_indctr = GSM0808_CHAN_SPEECH;
697 enc_ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
698 enc_ct.perm_spch[0] = GSM0808_PERM_FR3;
699 enc_ct.perm_spch[1] = GSM0808_PERM_HR3;
700 enc_ct.perm_spch_len = 2;
701
702 msg = msgb_alloc(1024, "output buffer");
703 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
704 OSMO_ASSERT(rc_enc == 6);
705 OSMO_ASSERT(memcmp(ct_enc_expected, msg->data, msg->len) == 0);
706
707 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
708 OSMO_ASSERT(rc_dec == 4);
709 OSMO_ASSERT(memcmp(&enc_ct, &dec_ct, sizeof(enc_ct)) == 0);
710
711 msgb_free(msg);
712}
713
Philipp Maier14e76b92017-03-28 18:36:52 +0200714static void test_gsm0808_enc_dec_encrypt_info()
715{
716 struct gsm0808_encrypt_info enc_ei;
717 struct gsm0808_encrypt_info dec_ei;
718 struct msgb *msg;
719 uint8_t ei_enc_expected[] =
720 { GSM0808_IE_ENCRYPTION_INFORMATION, 0x09, 0x03, 0xaa, 0xbb,
721 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42
722 };
723 uint8_t rc_enc;
724 int rc_dec;
725
726 memset(&enc_ei, 0, sizeof(enc_ei));
727 enc_ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
728 enc_ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
729 enc_ei.perm_algo_len = 2;
730 enc_ei.key[0] = 0xaa;
731 enc_ei.key[1] = 0xbb;
732 enc_ei.key[2] = 0xcc;
733 enc_ei.key[3] = 0xdd;
734 enc_ei.key[4] = 0xee;
735 enc_ei.key[5] = 0xff;
736 enc_ei.key[6] = 0x23;
737 enc_ei.key[7] = 0x42;
738 enc_ei.key_len = 8;
739
740 msg = msgb_alloc(1024, "output buffer");
741 rc_enc = gsm0808_enc_encrypt_info(msg, &enc_ei);
742 OSMO_ASSERT(rc_enc == 11);
743 OSMO_ASSERT(memcmp(ei_enc_expected, msg->data, msg->len) == 0);
744
745 rc_dec = gsm0808_dec_encrypt_info(&dec_ei, msg->data + 2, msg->len - 2);
746 OSMO_ASSERT(rc_dec == 9);
747
748 OSMO_ASSERT(memcmp(&enc_ei, &dec_ei, sizeof(enc_ei)) == 0);
749
750 msgb_free(msg);
751}
752
Philipp Maier783047e2017-03-29 11:35:50 +0200753static void test_gsm0808_enc_dec_cell_id_list_lac()
754{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100755 struct gsm0808_cell_id_list2 enc_cil;
756 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +0200757 struct msgb *msg;
758 uint8_t rc_enc;
759 int rc_dec;
760
761 memset(&enc_cil, 0, sizeof(enc_cil));
762 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100763 enc_cil.id_list[0].lac = 0x0124;
764 enc_cil.id_list[0].lac = 0xABCD;
765 enc_cil.id_list[0].lac = 0x5678;
Philipp Maier783047e2017-03-29 11:35:50 +0200766 enc_cil.id_list_len = 3;
767
768 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100769 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +0200770 OSMO_ASSERT(rc_enc == 9);
771
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100772 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +0200773 OSMO_ASSERT(rc_dec == 7);
774
775 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
776
777 msgb_free(msg);
778}
779
780static void test_gsm0808_enc_dec_cell_id_list_single_lac()
781{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100782 struct gsm0808_cell_id_list2 enc_cil;
783 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +0200784 struct msgb *msg;
785 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x03,
786 0x05, 0x23, 0x42
787 };
788 uint8_t rc_enc;
789 int rc_dec;
790
791 memset(&enc_cil, 0, sizeof(enc_cil));
792 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100793 enc_cil.id_list[0].lac = 0x2342;
Philipp Maier783047e2017-03-29 11:35:50 +0200794 enc_cil.id_list_len = 1;
795
796 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100797 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +0200798 OSMO_ASSERT(rc_enc == 5);
799 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
800
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100801 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +0200802 OSMO_ASSERT(rc_dec == 3);
803
804 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
805
806 msgb_free(msg);
807}
808
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100809static void test_gsm0808_enc_dec_cell_id_list_multi_lac()
810{
811 struct gsm0808_cell_id_list2 enc_cil;
812 struct gsm0808_cell_id_list2 dec_cil;
813 struct msgb *msg;
814 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x0b, 0x05,
815 0x23, 0x42,
816 0x24, 0x43,
817 0x25, 0x44,
818 0x26, 0x45,
819 0x27, 0x46
820 };
821 uint8_t rc_enc;
822 int rc_dec;
823
824 memset(&enc_cil, 0, sizeof(enc_cil));
825 enc_cil.id_discr = CELL_IDENT_LAC;
826 enc_cil.id_list[0].lac = 0x2342;
827 enc_cil.id_list[1].lac = 0x2443;
828 enc_cil.id_list[2].lac = 0x2544;
829 enc_cil.id_list[3].lac = 0x2645;
830 enc_cil.id_list[4].lac = 0x2746;
831 enc_cil.id_list_len = 5;
832
833 msg = msgb_alloc(1024, "output buffer");
834 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
835 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
836 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
837
838 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
839 OSMO_ASSERT(rc_dec == msg->len - 2);
840 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
841
842 msgb_free(msg);
843}
844
Philipp Maier783047e2017-03-29 11:35:50 +0200845static void test_gsm0808_enc_dec_cell_id_list_bss()
846{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100847 struct gsm0808_cell_id_list2 enc_cil;
848 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +0200849 struct msgb *msg;
850 uint8_t rc_enc;
851 int rc_dec;
852
853 memset(&enc_cil, 0, sizeof(enc_cil));
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100854 enc_cil.id_discr = CELL_IDENT_BSS;
Philipp Maier783047e2017-03-29 11:35:50 +0200855
856 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100857 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +0200858 OSMO_ASSERT(rc_enc == 3);
859
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100860 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +0200861 OSMO_ASSERT(rc_dec == 1);
862
863 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
864
865 msgb_free(msg);
866}
867
Stefan Sperling23381452018-03-15 19:38:15 +0100868static void test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac()
869{
870 struct gsm0808_cell_id_list2 enc_cil;
871 struct gsm0808_cell_id_list2 dec_cil;
872 struct osmo_location_area_id id;
873 struct msgb *msg;
874 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x10, 0x04,
875 0x92, 0x61, 0x54, 0x23, 0x42,
876 0x92, 0x72, 0x54, 0x24, 0x43,
877 0x92, 0x83, 0x54, 0x25, 0x44
878 };
879 uint8_t rc_enc;
880 int rc_dec, i;
881
882 memset(&enc_cil, 0, sizeof(enc_cil));
883 enc_cil.id_discr = CELL_IDENT_LAI_AND_LAC;
884
885 id.plmn.mcc = 0x123;
886 osmo_mnc_from_str("456", &id.plmn.mnc, &id.plmn.mnc_3_digits);
887 id.lac = 0x2342;
888 memcpy(&enc_cil.id_list[0].lai_and_lac, &id, sizeof(id));
889
890 id.plmn.mcc = 0x124;
891 osmo_mnc_from_str("457", &id.plmn.mnc, &id.plmn.mnc_3_digits);
892 id.lac = 0x2443;
893 memcpy(&enc_cil.id_list[1].lai_and_lac, &id, sizeof(id));
894
895 id.plmn.mcc = 0x125;
896 osmo_mnc_from_str("458", &id.plmn.mnc, &id.plmn.mnc_3_digits);
897 id.lac = 0x2544;
898 memcpy(&enc_cil.id_list[2].lai_and_lac, &id, sizeof(id));
899
900 enc_cil.id_list_len = 3;
901
902 msg = msgb_alloc(1024, "output buffer");
903 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
904 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
905 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
906
907 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
908 OSMO_ASSERT(rc_dec == msg->len - 2);
909
910 OSMO_ASSERT(dec_cil.id_list_len == 3);
911 /* Check MAXLEN elements to ensure everything has been initialized. */
912 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
913 struct osmo_location_area_id *enc_id;
914 struct osmo_location_area_id *dec_id;
915 enc_id = &enc_cil.id_list[i].lai_and_lac;
916 dec_id = &dec_cil.id_list[i].lai_and_lac;
917 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->plmn, &dec_id->plmn) == 0);
918 OSMO_ASSERT(enc_id->lac == dec_id->lac);
919 }
920
921 msgb_free(msg);
922}
923
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100924static void test_gsm0808_enc_dec_cell_id_list_multi_ci()
925{
926 struct gsm0808_cell_id_list2 enc_cil;
927 struct gsm0808_cell_id_list2 dec_cil;
928 struct msgb *msg;
929 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x09, 0x02,
930 0x00, 0x01,
931 0x00, 0x02,
932 0x00, 0x77,
933 0x01, 0xff,
934 };
935 uint8_t rc_enc;
936 int rc_dec;
937
938 memset(&enc_cil, 0, sizeof(enc_cil));
939 enc_cil.id_discr = CELL_IDENT_CI;
940 enc_cil.id_list[0].ci = 1;
941 enc_cil.id_list[1].ci = 2;
942 enc_cil.id_list[2].ci = 119;
943 enc_cil.id_list[3].ci = 511;
944 enc_cil.id_list_len = 4;
945
946 msg = msgb_alloc(1024, "output buffer");
947 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
948 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
949 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
950
951 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
952 OSMO_ASSERT(rc_dec == msg->len - 2);
953 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
954
955 msgb_free(msg);
956}
957
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100958static void test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci()
959{
960 struct gsm0808_cell_id_list2 enc_cil;
961 struct gsm0808_cell_id_list2 dec_cil;
962 struct msgb *msg;
963 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x15, 0x01,
964 0x23, 0x42, 0x00, 0x01,
965 0x24, 0x43, 0x00, 0x02,
966 0x25, 0x44, 0x00, 0x77,
967 0x26, 0x45, 0x01, 0xff,
968 0x27, 0x46, 0x02, 0xfe,
969 };
970 uint8_t rc_enc;
971 int rc_dec;
972
973 memset(&enc_cil, 0, sizeof(enc_cil));
974 enc_cil.id_discr = CELL_IDENT_LAC_AND_CI;
975 enc_cil.id_list[0].lac_and_ci.lac = 0x2342;
976 enc_cil.id_list[0].lac_and_ci.ci = 1;
977 enc_cil.id_list[1].lac_and_ci.lac = 0x2443;
978 enc_cil.id_list[1].lac_and_ci.ci = 2;
979 enc_cil.id_list[2].lac_and_ci.lac = 0x2544;
980 enc_cil.id_list[2].lac_and_ci.ci = 119;
981 enc_cil.id_list[3].lac_and_ci.lac = 0x2645;
982 enc_cil.id_list[3].lac_and_ci.ci = 511;
983 enc_cil.id_list[4].lac_and_ci.lac = 0x2746;
984 enc_cil.id_list[4].lac_and_ci.ci = 766;
985 enc_cil.id_list_len = 5;
986
987 msg = msgb_alloc(1024, "output buffer");
988 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
989 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
990 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
991
992 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
993 OSMO_ASSERT(rc_dec == msg->len - 2);
994 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
995
996 msgb_free(msg);
997}
998
Stefan Sperling483f3862018-03-16 12:21:26 +0100999static void test_gsm0808_enc_dec_cell_id_list_multi_global()
1000{
1001 struct gsm0808_cell_id_list2 enc_cil;
1002 struct gsm0808_cell_id_list2 dec_cil;
1003 struct msgb *msg;
1004 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x16, 0x00,
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001005 0x21, 0x63, 0x54, 0x23, 0x42, 0x00, 0x1,
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001006 0x21, 0xf4, 0x75, 0x24, 0x43, 0x00, 0x2,
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +01001007 0x21, 0x75, 0x00, 0x25, 0x44, 0x00, 0x77
Stefan Sperling483f3862018-03-16 12:21:26 +01001008 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001009 uint8_t rc_enc;
1010 int rc_dec, i;
1011
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001012 enc_cil = (struct gsm0808_cell_id_list2){
1013 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1014 .id_list_len = 3,
1015 .id_list = {
1016 {
1017 .global = {
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001018 .lai = { .plmn = { .mcc = 123, .mnc = 456 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001019 .lac = 0x2342 },
1020 .cell_identity = 1,
1021 }
1022 },
1023 {
1024 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001025 .lai = { .plmn = { .mcc = 124, .mnc = 57 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001026 .lac = 0x2443 },
1027 .cell_identity = 2,
1028 }
1029 },
1030 {
1031 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001032 .lai = { .plmn = { .mcc = 125, .mnc = 7,
1033 .mnc_3_digits = true },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001034 .lac = 0x2544 },
1035 .cell_identity = 119,
1036 }
1037 },
1038 }
1039 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001040
1041 msg = msgb_alloc(1024, "output buffer");
1042 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1043 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001044 if (memcmp(cil_enc_expected, msg->data, msg->len)) {
1045 printf(" got: %s\n", osmo_hexdump(msg->data, msg->len));
1046 printf("expect: %s\n", osmo_hexdump(cil_enc_expected, sizeof(cil_enc_expected)));
1047 OSMO_ASSERT(false);
1048 }
Stefan Sperling483f3862018-03-16 12:21:26 +01001049
1050 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1051 OSMO_ASSERT(rc_dec == msg->len - 2);
1052
1053 /* Check MAXLEN elements to ensure everything has been initialized. */
1054 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1055 struct osmo_cell_global_id *enc_id;
1056 struct osmo_cell_global_id *dec_id;
1057 enc_id = &enc_cil.id_list[i].global;
1058 dec_id = &dec_cil.id_list[i].global;
1059 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->lai.plmn, &dec_id->lai.plmn) == 0);
1060 OSMO_ASSERT(enc_id->lai.lac == dec_id->lai.lac);
1061 OSMO_ASSERT(enc_id->cell_identity == dec_id->cell_identity);
1062 }
1063
1064 msgb_free(msg);
1065}
1066
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001067static void print_cil(const struct gsm0808_cell_id_list2 *cil)
1068{
1069 unsigned int i;
1070 if (!cil) {
1071 printf(" cell_id_list == NULL\n");
1072 return;
1073 }
1074 switch (cil->id_discr) {
1075 case CELL_IDENT_WHOLE_GLOBAL:
1076 printf(" cell_id_list cgi[%u] = {\n", cil->id_list_len);
1077 for (i = 0; i < cil->id_list_len; i++)
1078 printf(" %2d: %s\n", i, osmo_cgi_name(&cil->id_list[i].global));
1079 printf(" }\n");
1080 break;
1081 case CELL_IDENT_LAC:
1082 printf(" cell_id_list lac[%u] = {\n", cil->id_list_len);
1083 for (i = 0; i < cil->id_list_len; i++)
1084 printf(" %2d: %u\n", i, cil->id_list[i].lac);
1085 printf(" }\n");
1086 break;
1087 case CELL_IDENT_BSS:
1088 printf(" cell_id_list bss[%u]\n", cil->id_list_len);
1089 break;
1090 case CELL_IDENT_NO_CELL:
1091 printf(" cell_id_list no_cell[%u]\n", cil->id_list_len);
1092 break;
1093 default:
1094 printf(" Unimplemented id_disc\n");
1095 }
1096}
1097
1098void test_cell_id_list_add() {
1099 const struct gsm0808_cell_id_list2 cgi1 = {
1100 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1101 .id_list_len = 1,
1102 .id_list = {
1103 {
1104 .global = {
1105 .lai = {
1106 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = false },
1107 .lac = 3,
1108 },
1109 .cell_identity = 4,
1110 }
1111 },
1112 },
1113 };
1114
1115 const struct gsm0808_cell_id_list2 cgi2 = {
1116 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1117 .id_list_len = 2,
1118 .id_list = {
1119 {
1120 .global = {
1121 .lai = {
1122 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = true },
1123 .lac = 3,
1124 },
1125 .cell_identity = 4,
1126 }
1127 },
1128 {
1129 .global = {
1130 .lai = {
1131 .plmn = { .mcc = 5, .mnc = 6, .mnc_3_digits = true },
1132 .lac = 7,
1133 },
1134 .cell_identity = 8,
1135 }
1136 },
1137 },
1138 };
1139
1140 const struct gsm0808_cell_id_list2 cgi2a = {
1141 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1142 .id_list_len = 2,
1143 .id_list = {
1144 {
1145 .global = cgi2.id_list[0].global
1146 },
1147 {
1148 .global = {
1149 .lai = {
1150 .plmn = { .mcc = 9, .mnc = 10, .mnc_3_digits = true },
1151 .lac = 11,
1152 },
1153 .cell_identity = 12,
1154 }
1155 },
1156 },
1157 };
1158
1159 const struct gsm0808_cell_id_list2 cgi3 = {
1160 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1161 .id_list_len = 2,
1162 .id_list = {
1163 {
1164 .global = {
1165 .lai = {
1166 .plmn = { .mcc = 13, .mnc = 14, .mnc_3_digits = true },
1167 .lac = 15,
1168 },
1169 .cell_identity = 16,
1170 }
1171 },
1172 {
1173 .global = {
1174 .lai = {
1175 .plmn = { .mcc = 16, .mnc = 17, .mnc_3_digits = true },
1176 .lac = 18,
1177 },
1178 .cell_identity = 19,
1179 }
1180 },
1181 },
1182 };
1183
1184
1185 const struct gsm0808_cell_id_list2 lac1 = {
1186 .id_discr = CELL_IDENT_LAC,
1187 .id_list_len = 1,
1188 .id_list = {
1189 {
1190 .lac = 123
1191 },
1192 },
1193 };
1194
1195 const struct gsm0808_cell_id_list2 lac2 = {
1196 .id_discr = CELL_IDENT_LAC,
1197 .id_list_len = 2,
1198 .id_list = {
1199 {
1200 .lac = 456
1201 },
1202 {
1203 .lac = 789
1204 },
1205 },
1206 };
1207
1208 struct gsm0808_cell_id_list2 cil = {};
1209
1210 printf("------- %s\n", __func__);
1211
1212 print_cil(&cil);
1213
1214#define ADD_QUIET(other_cil, expect_rc) do { \
1215 int rc = gsm0808_cell_id_list_add(&cil, &other_cil); \
1216 printf("\ngsm0808_cell_id_list_add(&cil, &" #other_cil ") --> rc = %d\n", rc); \
1217 OSMO_ASSERT(rc == expect_rc); \
1218 } while(0)
1219
1220#define ADD(other_cil, expect_rc) ADD_QUIET(other_cil, expect_rc); print_cil(&cil)
1221
1222 ADD(lac1, 1);
1223 ADD(lac1, 0);
1224 ADD(lac2, 2);
1225 ADD(lac2, 0);
1226 ADD(cil, 0);
1227 ADD(cgi1, -EINVAL);
1228
1229 printf("\ncan't add to BSS list\n");
1230 cil.id_list_len = 0;
1231 cil.id_discr = CELL_IDENT_BSS;
1232 print_cil(&cil);
1233 ADD(lac1, -EINVAL);
1234
1235 printf("\nother types (including NO_CELL) take on new type iff empty\n");
1236 cil.id_list_len = 0;
1237 cil.id_discr = CELL_IDENT_NO_CELL;
1238 print_cil(&cil);
1239 ADD(cgi1, 1);
1240 ADD(cgi1, 0);
1241 ADD(cgi2, 2);
1242 ADD(cgi2, 0);
1243
1244 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
1245 printf("\ncil.id_list_len = %u", cil.id_list_len);
1246 ADD_QUIET(cgi2a, 1);
1247 printf("cil.id_list_len = %u\n", cil.id_list_len);
1248
1249 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
1250 printf("\ncil.id_list_len = %u", cil.id_list_len);
1251 ADD_QUIET(cgi3, -ENOSPC);
1252 printf("cil.id_list_len = %u", cil.id_list_len);
1253 ADD_QUIET(cgi2a, -ENOSPC);
1254 printf("cil.id_list_len = %u\n", cil.id_list_len);
1255
1256 printf("------- %s done\n", __func__);
1257}
1258
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001259int main(int argc, char **argv)
1260{
1261 printf("Testing generation of GSM0808 messages\n");
1262 test_create_layer3();
Philipp Maierfa896ab2017-03-27 16:55:32 +02001263 test_create_layer3_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001264 test_create_reset();
Philipp Maier15596e22017-04-05 17:55:27 +02001265 test_create_reset_ack();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001266 test_create_clear_command();
1267 test_create_clear_complete();
Philipp Maierb478dd32017-03-29 15:50:05 +02001268 test_create_cipher();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001269 test_create_cipher_complete();
1270 test_create_cipher_reject();
1271 test_create_cm_u();
1272 test_create_sapi_reject();
Philipp Maierc6144a22017-03-29 17:53:43 +02001273 test_create_ass();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001274 test_create_ass_compl();
Philipp Maierfa896ab2017-03-27 16:55:32 +02001275 test_create_ass_compl_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001276 test_create_ass_fail();
Philipp Maierfa896ab2017-03-27 16:55:32 +02001277 test_create_ass_fail_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001278 test_create_clear_rqst();
Philipp Maier3d48ec02017-03-29 17:37:55 +02001279 test_create_paging();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001280 test_create_dtap();
1281 test_prepend_dtap();
Philipp Maier22401432017-03-24 17:59:26 +01001282 test_enc_dec_aoip_trasp_addr_v4();
1283 test_enc_dec_aoip_trasp_addr_v6();
Philipp Maier6f725d62017-03-24 18:03:17 +01001284 test_gsm0808_enc_dec_speech_codec();
Philipp Maier6f725d62017-03-24 18:03:17 +01001285 test_gsm0808_enc_dec_speech_codec_ext_with_cfg();
Philipp Maierbb839662017-06-01 17:11:19 +02001286 test_gsm0808_enc_dec_speech_codec_with_cfg();
Philipp Maier6f725d62017-03-24 18:03:17 +01001287 test_gsm0808_enc_dec_speech_codec_list();
Philipp Maiere0c65302017-03-28 17:05:40 +02001288 test_gsm0808_enc_dec_channel_type();
Philipp Maier14e76b92017-03-28 18:36:52 +02001289 test_gsm0808_enc_dec_encrypt_info();
Philipp Maier783047e2017-03-29 11:35:50 +02001290 test_gsm0808_enc_dec_cell_id_list_lac();
1291 test_gsm0808_enc_dec_cell_id_list_single_lac();
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001292 test_gsm0808_enc_dec_cell_id_list_multi_lac();
Philipp Maier783047e2017-03-29 11:35:50 +02001293 test_gsm0808_enc_dec_cell_id_list_bss();
Stefan Sperling23381452018-03-15 19:38:15 +01001294 test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac();
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001295 test_gsm0808_enc_dec_cell_id_list_multi_ci();
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001296 test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci();
Stefan Sperling483f3862018-03-16 12:21:26 +01001297 test_gsm0808_enc_dec_cell_id_list_multi_global();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001298
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001299 test_cell_id_list_add();
1300
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001301 printf("Done\n");
1302 return EXIT_SUCCESS;
1303}