blob: b3cab50276656f0b32ef9019ad09f935439eee39 [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>
Max969fb2e2018-12-10 11:01:10 +010025#include <osmocom/core/logging.h>
26#include <osmocom/core/application.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010027
28#include <stdio.h>
29#include <stdlib.h>
Philipp Maier22401432017-03-24 17:59:26 +010030#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
Neels Hofmeyr74663d92018-03-23 01:46:42 +010033#include <errno.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010034
Philipp Maier4f4905f2018-11-30 13:36:12 +010035#define EXPECT_ENCODED(hexstr) do { \
36 const char *enc_str = msgb_hexdump(msg); \
37 printf("%s: encoded: %s(rc = %u)\n", __func__, enc_str, rc_enc); \
38 OSMO_ASSERT(strcmp(enc_str, hexstr " ") == 0); \
39 OSMO_ASSERT(rc_enc == msg->len); \
40 } while(0)
41
Vadim Yanitskiycf6ee642018-12-20 05:23:00 +070042#define VERIFY(msg, data, data_len) do { \
43 if (!msgb_eq_l3_data_print(msg, data, data_len)) \
44 abort(); \
45 } while(0)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010046
Philipp Maierfa896ab2017-03-27 16:55:32 +020047/* Setup a fake codec list for testing */
48static void setup_codec_list(struct gsm0808_speech_codec_list *scl)
49{
50 memset(scl, 0, sizeof(*scl));
51
52 scl->codec[0].pi = true;
53 scl->codec[0].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +020054 scl->codec[0].type = GSM0808_SCT_FR3;
Philipp Maierfa896ab2017-03-27 16:55:32 +020055 scl->codec[0].cfg = 0xcdef;
56
57 scl->codec[1].fi = true;
58 scl->codec[1].pt = true;
Philipp Maierbb839662017-06-01 17:11:19 +020059 scl->codec[1].type = GSM0808_SCT_FR2;
Philipp Maierfa896ab2017-03-27 16:55:32 +020060
61 scl->codec[2].fi = true;
62 scl->codec[2].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +020063 scl->codec[2].type = GSM0808_SCT_CSD;
64 scl->codec[2].cfg = 0xc0;
Philipp Maierfa896ab2017-03-27 16:55:32 +020065
66 scl->len = 3;
67}
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010068
Philipp Maier4f4905f2018-11-30 13:36:12 +010069void test_gsm0808_enc_cause(void)
70{
71 /* NOTE: This must be tested early because many of the following tests
72 * rely on the generation of a proper cause code. */
73
74 uint8_t rc_enc;
75 struct msgb *msg;
76
77 /* Test with a single byte cause code */
78 msg = msgb_alloc(1024, "output buffer");
79 rc_enc = gsm0808_enc_cause(msg, 0x41);
80 EXPECT_ENCODED("04 01 41");
81 msgb_free(msg);
82
83 /* Test with an extended (two byte) cause code */
84 msg = msgb_alloc(1024, "output buffer");
85 rc_enc = gsm0808_enc_cause(msg, 0x8041);
86 EXPECT_ENCODED("04 02 80 41");
87 msgb_free(msg);
88}
89
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010090static void test_create_layer3(void)
91{
92 static const uint8_t res[] = {
93 0x00, 0x0e, 0x57, 0x05, 0x08, 0x00, 0x77, 0x62,
94 0x83, 0x33, 0x66, 0x44, 0x88, 0x17, 0x01, 0x23 };
95 struct msgb *msg, *in_msg;
Neels Hofmeyr178bf7a2018-04-20 12:23:45 +020096 struct osmo_cell_global_id cgi = {
97 .lai = {
98 .plmn = {
99 .mcc = 0x2244,
100 .mnc = 0x1122,
101 },
102 .lac = 0x3366,
103 },
104 .cell_identity = 0x4488,
105 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100106 printf("Testing creating Layer3\n");
107
108 in_msg = msgb_alloc_headroom(512, 128, "foo");
109 in_msg->l3h = in_msg->data;
110 msgb_v_put(in_msg, 0x23);
111
Neels Hofmeyr178bf7a2018-04-20 12:23:45 +0200112 msg = gsm0808_create_layer3_2(in_msg, &cgi, NULL);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100113 VERIFY(msg, res, ARRAY_SIZE(res));
114 msgb_free(msg);
115 msgb_free(in_msg);
116}
117
Philipp Maierfa896ab2017-03-27 16:55:32 +0200118static void test_create_layer3_aoip()
119{
120 static const uint8_t res[] = {
121 0x00, 0x17, 0x57, 0x05, 0x08, 0x00, 0x77, 0x62,
122 0x83, 0x33, 0x66, 0x44, 0x88, 0x17, 0x01, 0x23,
Philipp Maierbb839662017-06-01 17:11:19 +0200123 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50,
Philipp Maier7e27b142018-03-22 17:26:46 +0100124 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f,
Philipp Maierbb839662017-06-01 17:11:19 +0200125 GSM0808_SCT_CSD | 0x90, 0xc0
Philipp Maierfa896ab2017-03-27 16:55:32 +0200126 };
Maxfa3b4822018-11-05 14:59:54 +0100127 struct osmo_cell_global_id cgi = {
128 .lai = {
129 .plmn = {
130 .mcc = 0x2244,
131 .mnc = 0x1122,
132 },
133 .lac = 0x3366,
134 },
135 .cell_identity = 0x4488,
136 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200137 struct msgb *msg, *in_msg;
138 struct gsm0808_speech_codec_list sc_list;
139 printf("Testing creating Layer3 (AoIP)\n");
140
141 setup_codec_list(&sc_list);
142
143 in_msg = msgb_alloc_headroom(512, 128, "foo");
144 in_msg->l3h = in_msg->data;
145 msgb_v_put(in_msg, 0x23);
146
Maxfa3b4822018-11-05 14:59:54 +0100147 msg = gsm0808_create_layer3_2(in_msg, &cgi, &sc_list);
148
Philipp Maierfa896ab2017-03-27 16:55:32 +0200149 VERIFY(msg, res, ARRAY_SIZE(res));
150
151 msgb_free(msg);
152 msgb_free(in_msg);
153}
154
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100155static void test_create_reset()
156{
157 static const uint8_t res[] = { 0x00, 0x04, 0x30, 0x04, 0x01, 0x20 };
158 struct msgb *msg;
159
160 printf("Testing creating Reset\n");
161 msg = gsm0808_create_reset();
162 VERIFY(msg, res, ARRAY_SIZE(res));
163 msgb_free(msg);
164}
165
Philipp Maier15596e22017-04-05 17:55:27 +0200166static void test_create_reset_ack()
167{
168 static const uint8_t res[] = { 0x00, 0x01, 0x31 };
169 struct msgb *msg;
170
171 printf("Testing creating Reset Ack\n");
172 msg = gsm0808_create_reset_ack();
173 VERIFY(msg, res, ARRAY_SIZE(res));
174 msgb_free(msg);
175}
176
177
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100178static void test_create_clear_command()
179{
180 static const uint8_t res[] = { 0x20, 0x04, 0x01, 0x23 };
181 struct msgb *msg;
182
183 printf("Testing creating Clear Command\n");
184 msg = gsm0808_create_clear_command(0x23);
185 VERIFY(msg, res, ARRAY_SIZE(res));
186 msgb_free(msg);
187}
188
189static void test_create_clear_complete()
190{
191 static const uint8_t res[] = { 0x00, 0x01, 0x21 };
192 struct msgb *msg;
193
194 printf("Testing creating Clear Complete\n");
195 msg = gsm0808_create_clear_complete();
196 VERIFY(msg, res, ARRAY_SIZE(res));
197 msgb_free(msg);
198}
199
Philipp Maierb478dd32017-03-29 15:50:05 +0200200static void test_create_cipher()
201{
202 static const uint8_t res[] =
203 { 0x00, 0x0c, 0x53, 0x0a, 0x09, 0x03, 0xaa,
204 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 };
205 static const uint8_t res2[] =
206 { 0x00, 0x0e, 0x53, 0x0a, 0x09, 0x03, 0xaa,
207 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42,
208 GSM0808_IE_CIPHER_RESPONSE_MODE, 0x01 };
209 struct msgb *msg;
210 struct gsm0808_encrypt_info ei;
211 uint8_t include_imeisv;
212
213 memset(&ei, 0, sizeof(ei));
214 ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
215 ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
216 ei.perm_algo_len = 2;
217 ei.key[0] = 0xaa;
218 ei.key[1] = 0xbb;
219 ei.key[2] = 0xcc;
220 ei.key[3] = 0xdd;
221 ei.key[4] = 0xee;
222 ei.key[5] = 0xff;
223 ei.key[6] = 0x23;
224 ei.key[7] = 0x42;
225 ei.key_len = 8;
226 include_imeisv = 1;
227
228 printf("Testing creating Chipher Mode Command\n");
229 msg = gsm0808_create_cipher(&ei, NULL);
230 OSMO_ASSERT(msg);
231 VERIFY(msg, res, ARRAY_SIZE(res));
232 msgb_free(msg);
233
234 msg = gsm0808_create_cipher(&ei, &include_imeisv);
235 OSMO_ASSERT(msg);
236 VERIFY(msg, res2, ARRAY_SIZE(res2));
237 msgb_free(msg);
238}
239
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100240static void test_create_cipher_complete()
241{
242 static const uint8_t res1[] = {
243 0x00, 0x08, 0x55, 0x20, 0x03, 0x23, 0x42, 0x21, 0x2c, 0x04 };
244 static const uint8_t res2[] = { 0x00, 0x03, 0x55, 0x2c, 0x04};
245 struct msgb *l3, *msg;
246
247 printf("Testing creating Cipher Complete\n");
248 l3 = msgb_alloc_headroom(512, 128, "l3h");
249 l3->l3h = l3->data;
250 msgb_v_put(l3, 0x23);
251 msgb_v_put(l3, 0x42);
252 msgb_v_put(l3, 0x21);
253
254 /* with l3 data */
255 msg = gsm0808_create_cipher_complete(l3, 4);
256 VERIFY(msg, res1, ARRAY_SIZE(res1));
257 msgb_free(msg);
258
259 /* with l3 data but short */
260 l3->len -= 1;
261 l3->tail -= 1;
262 msg = gsm0808_create_cipher_complete(l3, 4);
263 VERIFY(msg, res2, ARRAY_SIZE(res2));
264 msgb_free(msg);
265
266 /* without l3 data */
267 msg = gsm0808_create_cipher_complete(NULL, 4);
268 VERIFY(msg, res2, ARRAY_SIZE(res2));
269 msgb_free(msg);
270
271
272 msgb_free(l3);
273}
274
Maxed651d22018-11-07 15:25:05 +0100275static inline void parse_cipher_reject(struct msgb *msg, uint8_t exp)
276{
277 struct tlv_parsed tp;
278 int rc;
279
280 /* skip header and message type so we can parse Cause IE directly */
281 msg->l2h = msgb_data(msg) + sizeof(struct bssmap_header) + 1;
282
283 rc = osmo_bssap_tlv_parse(&tp, msg->l2h, msgb_l2len(msg));
284 if (rc < 0)
285 printf("FIXME: failed (%d) to parse created message %s\n", rc, msgb_hexdump(msg));
286
287 rc = gsm0808_get_cipher_reject_cause(&tp);
288 if (rc < 0)
289 printf("FIXME: failed (%s) to extract Cause from created message %s\n",
290 strerror(-rc), msgb_hexdump(msg));
291
292 if (exp != (enum gsm0808_cause)rc)
293 printf("FIXME: wrong Cause %d != %u (" OSMO_BIN_SPEC ") extracted from created message %s\n",
294 rc, exp, OSMO_BIT_PRINT(exp), msgb_hexdump(msg));
295}
296
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100297static void test_create_cipher_reject()
298{
Harald Welte62e40852017-12-17 20:50:34 +0100299 static const uint8_t res[] = { 0x00, 0x04, 0x59, 0x04, 0x01, 0x23 };
Maxed651d22018-11-07 15:25:05 +0100300 enum gsm0808_cause cause = GSM0808_CAUSE_CCCH_OVERLOAD;
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100301 struct msgb *msg;
302
303 printf("Testing creating Cipher Reject\n");
Maxed651d22018-11-07 15:25:05 +0100304 msg = gsm0808_create_cipher_reject(cause);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100305 VERIFY(msg, res, ARRAY_SIZE(res));
Maxed651d22018-11-07 15:25:05 +0100306
307 parse_cipher_reject(msg, cause);
308
309 msgb_free(msg);
310}
311
312static void test_create_cipher_reject_ext()
313{
314 static const uint8_t res[] = { 0x00, 0x05, 0x59, 0x04, 0x02, 0xd0, 0xFA };
315 uint8_t cause = 0xFA;
316 struct msgb *msg;
317
318 printf("Testing creating Cipher Reject (extended)\n");
319 msg = gsm0808_create_cipher_reject_ext(GSM0808_CAUSE_CLASS_INVAL, cause);
320 VERIFY(msg, res, ARRAY_SIZE(res));
321
322 parse_cipher_reject(msg, cause);
323
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100324 msgb_free(msg);
325}
326
327static void test_create_cm_u()
328{
Harald Welte07b625d2012-01-23 10:02:58 +0100329 static const uint8_t res[] = {
330 0x00, 0x07, 0x54, 0x12, 0x01, 0x23, 0x13, 0x01, 0x42 };
331 static const uint8_t res2o[] = {
332 0x00, 0x04, 0x54, 0x12, 0x01, 0x23 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100333 struct msgb *msg;
Harald Welte07b625d2012-01-23 10:02:58 +0100334 const uint8_t cm2 = 0x23;
335 const uint8_t cm3 = 0x42;
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100336
337 printf("Testing creating CM U\n");
Harald Welte07b625d2012-01-23 10:02:58 +0100338 msg = gsm0808_create_classmark_update(&cm2, 1, &cm3, 1);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100339 VERIFY(msg, res, ARRAY_SIZE(res));
Harald Welte07b625d2012-01-23 10:02:58 +0100340
Neels Hofmeyr9a938ae2017-11-16 17:34:07 +0100341 msgb_free(msg);
342
Harald Welte07b625d2012-01-23 10:02:58 +0100343 msg = gsm0808_create_classmark_update(&cm2, 1, NULL, 0);
344 VERIFY(msg, res2o, ARRAY_SIZE(res2o));
345
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100346 msgb_free(msg);
347}
348
349static void test_create_sapi_reject()
350{
351 static const uint8_t res[] = { 0x00, 0x03, 0x25, 0x03, 0x25 };
352 struct msgb *msg;
353
354 printf("Testing creating SAPI Reject\n");
355 msg = gsm0808_create_sapi_reject(3);
356 VERIFY(msg, res, ARRAY_SIZE(res));
357 msgb_free(msg);
358}
359
Philipp Maierc6144a22017-03-29 17:53:43 +0200360static void test_create_ass()
361{
362 static const uint8_t res1[] =
363 { 0x00, 0x0a, 0x01, 0x0b, 0x04, 0x01, 0x0b, 0xa1, 0x25, 0x01, 0x00,
364 0x04 };
365 static const uint8_t res2[] =
366 { 0x00, 0x20, 0x01, 0x0b, 0x04, 0x01, 0x0b, 0xa1, 0x25, 0x01, 0x00,
367 0x04, GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17,
Philipp Maierbb839662017-06-01 17:11:19 +0200368 0x04, 0xd2, GSM0808_IE_SPEECH_CODEC_LIST, 0x07,
Philipp Maier7e27b142018-03-22 17:26:46 +0100369 GSM0808_SCT_FR3 | 0x50, 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f,
Philipp Maierbb839662017-06-01 17:11:19 +0200370 GSM0808_SCT_CSD | 0x90, 0xc0, GSM0808_IE_CALL_ID, 0xaa, 0xbb,
371 0xcc, 0xdd };
Philipp Maierc6144a22017-03-29 17:53:43 +0200372
373 struct msgb *msg;
374 struct gsm0808_channel_type ct;
375 uint16_t cic = 0004;
376 struct sockaddr_storage ss;
377 struct sockaddr_in sin;
378 struct gsm0808_speech_codec_list sc_list;
379 uint32_t call_id = 0xAABBCCDD;
380
381 memset(&ct, 0, sizeof(ct));
382 ct.ch_indctr = GSM0808_CHAN_SPEECH;
383 ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
384 ct.perm_spch[0] = GSM0808_PERM_FR3;
385 ct.perm_spch[1] = GSM0808_PERM_HR3;
386 ct.perm_spch_len = 2;
387
388 memset(&sin, 0, sizeof(sin));
389 sin.sin_family = AF_INET;
390 sin.sin_port = htons(1234);
391 inet_aton("192.168.100.23", &sin.sin_addr);
392
393 memset(&ss, 0, sizeof(ss));
394 memcpy(&ss, &sin, sizeof(sin));
395
396 setup_codec_list(&sc_list);
397
398 printf("Testing creating Assignment Request\n");
399 msg = gsm0808_create_ass(&ct, &cic, NULL, NULL, NULL);
400 OSMO_ASSERT(msg);
401 VERIFY(msg, res1, ARRAY_SIZE(res1));
402 msgb_free(msg);
403
404 msg = gsm0808_create_ass(&ct, &cic, &ss, &sc_list, &call_id);
405 OSMO_ASSERT(msg);
406 VERIFY(msg, res2, ARRAY_SIZE(res2));
407 msgb_free(msg);
408}
409
Max52074322018-11-30 10:44:07 +0100410static void test_create_ass2()
411{
412 static const uint8_t res[] = {
413 BSSAP_MSG_BSS_MANAGEMENT,
414 0x45,
415 BSS_MAP_MSG_ASSIGMENT_RQST,
416 GSM0808_IE_CHANNEL_TYPE,
417 0x04, 0x01, 0x0b, 0x91, 0x15, 0x01, 0x00, 0x04,
418 GSM0808_IE_AOIP_TRASP_ADDR,
419 0x06,
420 0xac, 0x0c, 0x65, 0x0d, /* IPv4 */
421 0x02, 0x9a,
422 GSM0808_IE_SPEECH_CODEC_LIST,
423 0x07,
424 GSM0808_SCT_FR3 | 0x50,
425 0xef, 0xcd,
426 GSM0808_SCT_FR2 | 0xa0,
427 0x9f,
428 GSM0808_SCT_CSD | 0x90,
429 0xc0,
430 GSM0808_IE_CALL_ID,
431 0xde, 0xad, 0xfa, 0xce, /* CallID */
432 0x83, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, /* Kc */
433 GSM0808_IE_GLOBAL_CALL_REF, 0x0d, /* GCR, length */
434 0x03, 0x44, 0x44, 0x44, /* GCR, Net ID */
435 0x02, 0xfe, 0xed, /* GCR, Node ID */
436 0x05, 0x41, 0x41, 0x41, 0x41, 0x41, /* GCR, Call ref. ID */
437 GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_BOTH_WAY,
438 GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_CONNECT,
439 GSM0808_IE_LCLS_CORR_NOT_NEEDED,
440 };
441 struct msgb *msg;
442 struct gsm0808_channel_type ct;
443 uint16_t cic = 4;
444 struct sockaddr_storage ss;
445 struct sockaddr_in sin;
446 struct gsm0808_speech_codec_list sc_list;
447 uint32_t call_id = 0xDEADFACE;
448 struct osmo_gcr_parsed gcr = { .net_len = 3, .node = 0xFEED };
449 uint8_t Kc[16];
450 struct osmo_lcls lcls = {
451 .config = GSM0808_LCLS_CFG_BOTH_WAY,
452 .control = GSM0808_LCLS_CSC_CONNECT,
453 .gcr = &gcr,
454 .corr_needed = false
455 };
456
457 memset(gcr.cr, 'A', 5);
458 memset(gcr.net, 'D', gcr.net_len);
459 memset(Kc, 'E', 16);
460
461 memset(&ct, 0, sizeof(ct));
462 ct.ch_indctr = GSM0808_CHAN_SPEECH;
463 ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
464 ct.perm_spch[0] = GSM0808_PERM_FR2;
465 ct.perm_spch[1] = GSM0808_PERM_HR2;
466 ct.perm_spch_len = 2;
467
468 memset(&sin, 0, sizeof(sin));
469 sin.sin_family = AF_INET;
470 sin.sin_port = htons(666);
471 inet_aton("172.12.101.13", &sin.sin_addr); /* IPv4 */
472
473 memset(&ss, 0, sizeof(ss));
474 memcpy(&ss, &sin, sizeof(sin));
475
476 setup_codec_list(&sc_list);
477
478 printf("Testing creating Assignment Request with Kc and LCLS\n");
479
480 msg = gsm0808_create_ass2(&ct, &cic, &ss, &sc_list, &call_id, Kc, &lcls);
481 if (!msgb_eq_l3_data_print(msg, res, ARRAY_SIZE(res)))
482 abort();
483
484 msgb_free(msg);
485}
486
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100487static void test_create_ass_compl()
488{
489 static const uint8_t res1[] = {
490 0x00, 0x09, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c,
491 0x11, 0x40, 0x22 };
492 static const uint8_t res2[] = {
493 0x00, 0x07, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11};
494 struct msgb *msg;
495
496 printf("Testing creating Assignment Complete\n");
497 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0x22);
498 VERIFY(msg, res1, ARRAY_SIZE(res1));
499 msgb_free(msg);
500
501 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0);
502 VERIFY(msg, res2, ARRAY_SIZE(res2));
503 msgb_free(msg);
504}
505
Philipp Maierfa896ab2017-03-27 16:55:32 +0200506static void test_create_ass_compl_aoip()
507{
508 struct sockaddr_storage ss;
509 struct sockaddr_in sin;
510 struct gsm0808_speech_codec sc;
511 struct gsm0808_speech_codec_list sc_list;
512 static const uint8_t res[] =
513 { 0x00, 0x1d, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11, 0x40, 0x22,
514 GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17, 0x04,
Philipp Maierbb839662017-06-01 17:11:19 +0200515 0xd2, GSM0808_IE_SPEECH_CODEC, 0x01, GSM0808_SCT_HR1 | 0x90,
Philipp Maier7e27b142018-03-22 17:26:46 +0100516 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50, 0xef,
517 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200518 struct msgb *msg;
519
520 memset(&sin, 0, sizeof(sin));
521 sin.sin_family = AF_INET;
522 sin.sin_port = htons(1234);
523 inet_aton("192.168.100.23", &sin.sin_addr);
524
525 memset(&ss, 0, sizeof(ss));
526 memcpy(&ss, &sin, sizeof(sin));
527
528 memset(&sc, 0, sizeof(sc));
529 sc.fi = true;
530 sc.tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200531 sc.type = GSM0808_SCT_HR1;
Philipp Maierfa896ab2017-03-27 16:55:32 +0200532
533 setup_codec_list(&sc_list);
534
535 printf("Testing creating Assignment Complete (AoIP)\n");
536 msg = gsm0808_create_ass_compl(0x23, 0x42, 0x11, 0x22,
537 &ss, &sc, &sc_list);
538 VERIFY(msg, res, ARRAY_SIZE(res));
539 msgb_free(msg);
540}
541
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100542static void test_create_ass_fail()
543{
544 static const uint8_t res1[] = { 0x00, 0x04, 0x03, 0x04, 0x01, 0x23 };
545 static const uint8_t res2[] = {
546 0x00, 0x06, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02};
547 uint8_t rr_res = 2;
548 struct msgb *msg;
549
550 printf("Testing creating Assignment Failure\n");
551 msg = gsm0808_create_assignment_failure(0x23, NULL);
552 VERIFY(msg, res1, ARRAY_SIZE(res1));
553 msgb_free(msg);
554
555 msg = gsm0808_create_assignment_failure(0x23, &rr_res);
556 VERIFY(msg, res2, ARRAY_SIZE(res2));
557 msgb_free(msg);
558}
559
Philipp Maierfa896ab2017-03-27 16:55:32 +0200560static void test_create_ass_fail_aoip()
561{
562 static const uint8_t res1[] =
563 { 0x00, 0x0d, 0x03, 0x04, 0x01, 0x23, GSM0808_IE_SPEECH_CODEC_LIST,
Philipp Maier7e27b142018-03-22 17:26:46 +0100564 0x07, GSM0808_SCT_FR3 | 0x50, 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0,
Philipp Maierbb839662017-06-01 17:11:19 +0200565 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200566 static const uint8_t res2[] =
567 { 0x00, 0x0f, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02,
Philipp Maier7e27b142018-03-22 17:26:46 +0100568 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50, 0xef,
569 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200570 uint8_t rr_res = 2;
571 struct msgb *msg;
572 struct gsm0808_speech_codec_list sc_list;
573
574 setup_codec_list(&sc_list);
575
576 printf("Testing creating Assignment Failure (AoIP)\n");
577 msg = gsm0808_create_ass_fail(0x23, NULL, &sc_list);
578 VERIFY(msg, res1, ARRAY_SIZE(res1));
579 msgb_free(msg);
580
581 msg = gsm0808_create_ass_fail(0x23, &rr_res, &sc_list);
582 VERIFY(msg, res2, ARRAY_SIZE(res2));
583 msgb_free(msg);
584}
585
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100586static void test_create_clear_rqst()
587{
588 static const uint8_t res[] = { 0x00, 0x04, 0x22, 0x04, 0x01, 0x23 };
589 struct msgb *msg;
590
591 printf("Testing creating Clear Request\n");
592 msg = gsm0808_create_clear_rqst(0x23);
593 VERIFY(msg, res, ARRAY_SIZE(res));
594 msgb_free(msg);
595}
596
Philipp Maier3d48ec02017-03-29 17:37:55 +0200597static void test_create_paging()
598{
599 static const uint8_t res[] =
600 { 0x00, 0x10, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
601 0x21, 0x43, 0x1a, 0x03, 0x05, 0x23, 0x42 };
602 static const uint8_t res2[] =
603 { 0x00, 0x16, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
604 0x21, 0x43, GSM0808_IE_TMSI, 0x04, 0x12, 0x34, 0x56, 0x78, 0x1a,
605 0x03, 0x05, 0x23, 0x42 };
606 static const uint8_t res3[] =
607 { 0x00, 0x18, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
608 0x21, 0x43, GSM0808_IE_TMSI, 0x04, 0x12, 0x34, 0x56, 0x78, 0x1a,
609 0x03, 0x05, 0x23, 0x42, GSM0808_IE_CHANNEL_NEEDED,
610 RSL_CHANNEED_TCH_ForH };
611
612 struct msgb *msg;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100613 struct gsm0808_cell_id_list2 cil;
Philipp Maier3d48ec02017-03-29 17:37:55 +0200614 uint32_t tmsi = 0x12345678;
615 uint8_t chan_needed = RSL_CHANNEED_TCH_ForH;
616
617 char imsi[] = "001010000001234";
618
619 cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100620 cil.id_list[0].lac = 0x2342;
Philipp Maier3d48ec02017-03-29 17:37:55 +0200621 cil.id_list_len = 1;
622
623 printf("Testing creating Paging Request\n");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100624 msg = gsm0808_create_paging2(imsi, NULL, &cil, NULL);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200625 VERIFY(msg, res, ARRAY_SIZE(res));
626 msgb_free(msg);
627
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100628 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, NULL);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200629 VERIFY(msg, res2, ARRAY_SIZE(res2));
630 msgb_free(msg);
631
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100632 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, &chan_needed);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200633 VERIFY(msg, res3, ARRAY_SIZE(res3));
634 msgb_free(msg);
635}
636
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100637static void test_create_dtap()
638{
639 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
640 struct msgb *msg, *l3;
641
642 printf("Testing creating DTAP\n");
643 l3 = msgb_alloc_headroom(512, 128, "test");
644 l3->l3h = l3->data;
645 msgb_v_put(l3, 0x23);
646 msgb_v_put(l3, 0x42);
647
648 msg = gsm0808_create_dtap(l3, 0x3);
649 VERIFY(msg, res, ARRAY_SIZE(res));
650 msgb_free(msg);
651 msgb_free(l3);
652}
653
654static void test_prepend_dtap()
655{
656 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
657 struct msgb *in_msg;
658
659 printf("Testing prepend DTAP\n");
660
661 in_msg = msgb_alloc_headroom(512, 128, "test");
662 msgb_v_put(in_msg, 0x23);
663 msgb_v_put(in_msg, 0x42);
664
665 gsm0808_prepend_dtap_header(in_msg, 0x3);
666 in_msg->l3h = in_msg->data;
667 VERIFY(in_msg, res, ARRAY_SIZE(res));
668 msgb_free(in_msg);
669}
670
Max47022152018-12-19 18:51:00 +0100671static void test_enc_dec_lcls()
Max969fb2e2018-12-10 11:01:10 +0100672{
673 static const uint8_t res[] = {
674 GSM0808_IE_GLOBAL_CALL_REF,
675 0x0d, /* GCR length */
676 0x03, /* .net_len */
677 0xf1, 0xf2, 0xf3, /* .net */
678 0x02, /* .node length */
679 0xde, 0xad, /* .node */
680 0x05, /* length of Call. Ref. */
681 0x41, 0x42, 0x43, 0x44, 0x45 /* .cr - Call. Ref. */
682 };
683 uint8_t len;
684 struct msgb *msg;
685 struct osmo_gcr_parsed p = { 0 }, g = {
686 .net_len = 3,
687 .net = { 0xf1, 0xf2, 0xf3 },
688 .node = 0xDEAD,
689 .cr = { 0x41, 0x42, 0x43, 0x44, 0x45 },
690 };
691 int rc;
692 struct tlv_parsed tp;
Max47022152018-12-19 18:51:00 +0100693 struct osmo_lcls lcls_out = { .gcr = &p }, lcls_in = {
694 .gcr = &g,
695 .config = GSM0808_LCLS_CFG_NA,
696 .control = GSM0808_LCLS_CSC_NA,
697 .corr_needed = true,
698 };
699
700 msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, "LCLS IE");
Max969fb2e2018-12-10 11:01:10 +0100701 if (!msg)
702 return;
703
Max47022152018-12-19 18:51:00 +0100704 len = gsm0808_enc_lcls(msg, &lcls_in);
Max969fb2e2018-12-10 11:01:10 +0100705 printf("Testing Global Call Reference IE encoder...\n\t%d bytes added: %s\n",
706 len, len == ARRAY_SIZE(res) ? "OK" : "FAIL");
707
708 if (!msgb_eq_data_print(msg, res, ARRAY_SIZE(res)))
709 abort();
710
711 rc = osmo_bssap_tlv_parse(&tp, msgb_data(msg), msgb_length(msg));
712 if (rc < 0) {
713 printf("parsing failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg));
714 abort();
715 }
716
Max47022152018-12-19 18:51:00 +0100717 rc = gsm0808_dec_lcls(&lcls_out, &tp);
Max969fb2e2018-12-10 11:01:10 +0100718 if (rc < 0) {
719 printf("decoding failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg));
720 abort();
721 }
722
Max4fd64e52019-01-14 19:27:44 +0100723 if (lcls_out.config != lcls_in.config) {
724 printf("LCLS Config parsed wrong: %s != %s\n",
725 gsm0808_lcls_config_name(lcls_out.config), gsm0808_lcls_config_name(lcls_in.config));
726 abort();
727 }
728
729 if (lcls_out.control != lcls_in.control) {
730 printf("LCLS Control parsed wrong: %s != %s\n",
731 gsm0808_lcls_control_name(lcls_out.control), gsm0808_lcls_control_name(lcls_in.control));
732 abort();
733 }
734
Max47022152018-12-19 18:51:00 +0100735 if (lcls_out.gcr->net_len != g.net_len) {
736 printf("Network ID length parsed wrong: %u != %u\n", lcls_out.gcr->net_len, g.net_len);
Max969fb2e2018-12-10 11:01:10 +0100737 abort();
738 }
739
Max47022152018-12-19 18:51:00 +0100740 if (lcls_out.gcr->node != g.node) {
741 printf("Node ID parsed wrong: 0x%X != 0x%X\n", lcls_out.gcr->node, g.node);
Max969fb2e2018-12-10 11:01:10 +0100742 abort();
743 }
744
Max47022152018-12-19 18:51:00 +0100745 if (memcmp(lcls_out.gcr->net, g.net, g.net_len) != 0) {
746 printf("Network ID parsed wrong: %s\n", osmo_hexdump(lcls_out.gcr->net, lcls_out.gcr->net_len));
Max969fb2e2018-12-10 11:01:10 +0100747 abort();
748 }
749
Max47022152018-12-19 18:51:00 +0100750 if (memcmp(lcls_out.gcr->cr, g.cr, 5) != 0) {
751 printf("Call ref. ID parsed wrong: %s\n", osmo_hexdump(lcls_out.gcr->cr, 5));
Max969fb2e2018-12-10 11:01:10 +0100752 abort();
753 }
754
755 printf("\tdecoded %d bytes: %s\n", rc, rc == len ? "OK" : "FAIL");
756 msgb_free(msg);
757}
758
Philipp Maier22401432017-03-24 17:59:26 +0100759static void test_enc_dec_aoip_trasp_addr_v4()
760{
761 struct sockaddr_storage enc_addr;
762 struct sockaddr_storage dec_addr;
763 struct sockaddr_in enc_addr_in;
764 struct msgb *msg;
765 uint8_t rc_enc;
766 int rc_dec;
767
768 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
769 enc_addr_in.sin_family = AF_INET;
770 enc_addr_in.sin_port = htons(1234);
771 inet_aton("255.0.255.255", &enc_addr_in.sin_addr);
772
773 memset(&enc_addr, 0, sizeof(enc_addr));
774 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
775
776 msg = msgb_alloc(1024, "output buffer");
777 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
778 OSMO_ASSERT(rc_enc == 8);
779 rc_dec =
780 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
781 OSMO_ASSERT(rc_dec == 6);
782 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
783
784 msgb_free(msg);
785}
786
787static void test_enc_dec_aoip_trasp_addr_v6()
788{
789 struct sockaddr_storage enc_addr;
790 struct sockaddr_storage dec_addr;
791 struct sockaddr_in6 enc_addr_in;
792 struct msgb *msg;
793 uint8_t rc_enc;
794 int rc_dec;
795
796 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
797 enc_addr_in.sin6_family = AF_INET6;
798 enc_addr_in.sin6_port = htons(4567);
799 inet_pton(AF_INET6, "2001:0db8:85a3:08d3:1319:8a2e:0370:7344",
800 &enc_addr_in.sin6_addr);
801
802 memset(&enc_addr, 0, sizeof(enc_addr));
803 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
804
805 msg = msgb_alloc(1024, "output buffer");
806 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
807 OSMO_ASSERT(rc_enc == 20);
808 rc_dec =
809 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
810 OSMO_ASSERT(rc_dec == 18);
811 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
812
813 msgb_free(msg);
814}
815
Philipp Maier6f725d62017-03-24 18:03:17 +0100816static void test_gsm0808_enc_dec_speech_codec()
817{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200818 struct gsm0808_speech_codec enc_sc = {
819 .pi = true,
820 .tf = true,
821 .type = GSM0808_SCT_FR2,
822 };
823 struct gsm0808_speech_codec dec_sc = {};
Philipp Maier6f725d62017-03-24 18:03:17 +0100824 struct msgb *msg;
825 uint8_t rc_enc;
826 int rc_dec;
827
Philipp Maier6f725d62017-03-24 18:03:17 +0100828 msg = msgb_alloc(1024, "output buffer");
829 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
830 OSMO_ASSERT(rc_enc == 3);
831
832 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
833 OSMO_ASSERT(rc_dec == 1);
834
835 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
836
837 msgb_free(msg);
838}
839
840
Philipp Maierbb839662017-06-01 17:11:19 +0200841static void test_gsm0808_enc_dec_speech_codec_with_cfg()
842{
Neels Hofmeyrc62c9342018-04-15 23:31:47 +0200843 struct gsm0808_speech_codec enc_sc = {
844 .pi = true,
845 .tf = true,
846 .type = GSM0808_SCT_FR3,
847 .cfg = 0xabcd,
848 };
849 struct gsm0808_speech_codec dec_sc = {};
Philipp Maierbb839662017-06-01 17:11:19 +0200850 struct msgb *msg;
851 uint8_t rc_enc;
852 int rc_dec;
853
Philipp Maierbb839662017-06-01 17:11:19 +0200854 msg = msgb_alloc(1024, "output buffer");
855 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
856 OSMO_ASSERT(rc_enc == 5);
857
858 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
859 OSMO_ASSERT(rc_dec == 3);
860
861 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
862
863 msgb_free(msg);
864}
865
Philipp Maier6f725d62017-03-24 18:03:17 +0100866static void test_gsm0808_enc_dec_speech_codec_ext_with_cfg()
867{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200868 struct gsm0808_speech_codec enc_sc = {
869 .pi = true,
870 .tf = true,
871 .type = GSM0808_SCT_CSD,
872 .cfg = 0xc0,
873 };
874 struct gsm0808_speech_codec dec_sc = {};
Philipp Maier6f725d62017-03-24 18:03:17 +0100875 struct msgb *msg;
876 uint8_t rc_enc;
877 int rc_dec;
878
Philipp Maier6f725d62017-03-24 18:03:17 +0100879 msg = msgb_alloc(1024, "output buffer");
880 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
Philipp Maierbb839662017-06-01 17:11:19 +0200881 OSMO_ASSERT(rc_enc == 5);
Philipp Maier6f725d62017-03-24 18:03:17 +0100882
883 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
Philipp Maierbb839662017-06-01 17:11:19 +0200884 OSMO_ASSERT(rc_dec == 3);
Philipp Maier6f725d62017-03-24 18:03:17 +0100885
886 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
887
888 msgb_free(msg);
889}
890
891static void test_gsm0808_enc_dec_speech_codec_list()
892{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200893 struct gsm0808_speech_codec_list enc_scl = {
894 .codec = {
895 {
896 .pi = true,
897 .tf = true,
898 .type = GSM0808_SCT_FR3,
899 .cfg = 0xcdef,
900 },
901
902 {
903 .fi = true,
904 .pt = true,
905 .type = GSM0808_SCT_FR2,
906 },
907
908 {
909 .fi = true,
910 .tf = true,
911 .type = GSM0808_SCT_CSD,
912 .cfg = 0xc0,
913 },
914 },
915 .len = 3,
916 };
917 struct gsm0808_speech_codec_list dec_scl = {};
Philipp Maier6f725d62017-03-24 18:03:17 +0100918 struct msgb *msg;
919 uint8_t rc_enc;
920 int rc_dec;
921
Philipp Maier6f725d62017-03-24 18:03:17 +0100922 msg = msgb_alloc(1024, "output buffer");
923 rc_enc = gsm0808_enc_speech_codec_list(msg, &enc_scl);
924 OSMO_ASSERT(rc_enc == 9);
925
926 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
927 OSMO_ASSERT(rc_dec == 7);
928
929 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
930
931 msgb_free(msg);
932}
933
Philipp Maierf6c369f2018-10-16 15:24:47 +0200934static void test_gsm0808_enc_dec_empty_speech_codec_list()
935{
936 struct gsm0808_speech_codec_list enc_scl = {
937 .len = 0,
938 };
939 struct gsm0808_speech_codec_list dec_scl = {};
940 struct msgb *msg;
941 uint8_t rc_enc;
942 int rc_dec;
943
944 msg = msgb_alloc(1024, "output buffer");
945 rc_enc = gsm0808_enc_speech_codec_list(msg, &enc_scl);
946 OSMO_ASSERT(rc_enc == 2);
947
948 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
949 OSMO_ASSERT(rc_dec == 0);
950
951 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
952
953 msgb_free(msg);
954}
955
Philipp Maiere0c65302017-03-28 17:05:40 +0200956static void test_gsm0808_enc_dec_channel_type()
957{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200958 struct gsm0808_channel_type enc_ct = {
959 .ch_indctr = GSM0808_CHAN_SPEECH,
960 .ch_rate_type = GSM0808_SPEECH_HALF_PREF,
961 .perm_spch = { GSM0808_PERM_FR3, GSM0808_PERM_HR3 },
962 .perm_spch_len = 2,
963 };
964 struct gsm0808_channel_type dec_ct = {};
Philipp Maiere0c65302017-03-28 17:05:40 +0200965 struct msgb *msg;
966 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
967 0x04, 0x01, 0x0b, 0xa1, 0x25
968 };
969 uint8_t rc_enc;
970 int rc_dec;
971
Philipp Maiere0c65302017-03-28 17:05:40 +0200972 msg = msgb_alloc(1024, "output buffer");
973 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
974 OSMO_ASSERT(rc_enc == 6);
975 OSMO_ASSERT(memcmp(ct_enc_expected, msg->data, msg->len) == 0);
976
977 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
978 OSMO_ASSERT(rc_dec == 4);
979 OSMO_ASSERT(memcmp(&enc_ct, &dec_ct, sizeof(enc_ct)) == 0);
980
981 msgb_free(msg);
982}
983
Philipp Maier14e76b92017-03-28 18:36:52 +0200984static void test_gsm0808_enc_dec_encrypt_info()
985{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200986 struct gsm0808_encrypt_info enc_ei = {
987 .perm_algo = { GSM0808_ALG_ID_A5_0, GSM0808_ALG_ID_A5_1 },
988 .perm_algo_len = 2,
989 .key = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42, },
990 .key_len = 8,
991 };
992 struct gsm0808_encrypt_info dec_ei = {};
Philipp Maier14e76b92017-03-28 18:36:52 +0200993 struct msgb *msg;
994 uint8_t ei_enc_expected[] =
995 { GSM0808_IE_ENCRYPTION_INFORMATION, 0x09, 0x03, 0xaa, 0xbb,
996 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42
997 };
998 uint8_t rc_enc;
999 int rc_dec;
1000
Philipp Maier14e76b92017-03-28 18:36:52 +02001001 msg = msgb_alloc(1024, "output buffer");
1002 rc_enc = gsm0808_enc_encrypt_info(msg, &enc_ei);
1003 OSMO_ASSERT(rc_enc == 11);
1004 OSMO_ASSERT(memcmp(ei_enc_expected, msg->data, msg->len) == 0);
1005
1006 rc_dec = gsm0808_dec_encrypt_info(&dec_ei, msg->data + 2, msg->len - 2);
1007 OSMO_ASSERT(rc_dec == 9);
1008
1009 OSMO_ASSERT(memcmp(&enc_ei, &dec_ei, sizeof(enc_ei)) == 0);
1010
1011 msgb_free(msg);
1012}
1013
Philipp Maier783047e2017-03-29 11:35:50 +02001014static void test_gsm0808_enc_dec_cell_id_list_lac()
1015{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001016 struct gsm0808_cell_id_list2 enc_cil;
1017 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001018 struct msgb *msg;
1019 uint8_t rc_enc;
1020 int rc_dec;
1021
1022 memset(&enc_cil, 0, sizeof(enc_cil));
1023 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001024 enc_cil.id_list[0].lac = 0x0124;
Neels Hofmeyrdb2fa4e2018-04-13 04:11:20 +02001025 enc_cil.id_list[1].lac = 0xABCD;
1026 enc_cil.id_list[2].lac = 0x5678;
Philipp Maier783047e2017-03-29 11:35:50 +02001027 enc_cil.id_list_len = 3;
1028
1029 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001030 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Neels Hofmeyrdb2fa4e2018-04-13 04:11:20 +02001031 EXPECT_ENCODED("1a 07 05 01 24 ab cd 56 78");
Philipp Maier783047e2017-03-29 11:35:50 +02001032
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001033 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001034 OSMO_ASSERT(rc_dec == 7);
1035
1036 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1037
1038 msgb_free(msg);
1039}
1040
1041static void test_gsm0808_enc_dec_cell_id_list_single_lac()
1042{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001043 struct gsm0808_cell_id_list2 enc_cil;
1044 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001045 struct msgb *msg;
1046 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x03,
1047 0x05, 0x23, 0x42
1048 };
1049 uint8_t rc_enc;
1050 int rc_dec;
1051
1052 memset(&enc_cil, 0, sizeof(enc_cil));
1053 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001054 enc_cil.id_list[0].lac = 0x2342;
Philipp Maier783047e2017-03-29 11:35:50 +02001055 enc_cil.id_list_len = 1;
1056
1057 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001058 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +02001059 OSMO_ASSERT(rc_enc == 5);
1060 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1061
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001062 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001063 OSMO_ASSERT(rc_dec == 3);
1064
1065 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1066
1067 msgb_free(msg);
1068}
1069
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001070static void test_gsm0808_enc_dec_cell_id_list_multi_lac()
1071{
1072 struct gsm0808_cell_id_list2 enc_cil;
1073 struct gsm0808_cell_id_list2 dec_cil;
1074 struct msgb *msg;
1075 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x0b, 0x05,
1076 0x23, 0x42,
1077 0x24, 0x43,
1078 0x25, 0x44,
1079 0x26, 0x45,
1080 0x27, 0x46
1081 };
1082 uint8_t rc_enc;
1083 int rc_dec;
1084
1085 memset(&enc_cil, 0, sizeof(enc_cil));
1086 enc_cil.id_discr = CELL_IDENT_LAC;
1087 enc_cil.id_list[0].lac = 0x2342;
1088 enc_cil.id_list[1].lac = 0x2443;
1089 enc_cil.id_list[2].lac = 0x2544;
1090 enc_cil.id_list[3].lac = 0x2645;
1091 enc_cil.id_list[4].lac = 0x2746;
1092 enc_cil.id_list_len = 5;
1093
1094 msg = msgb_alloc(1024, "output buffer");
1095 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1096 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1097 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1098
1099 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1100 OSMO_ASSERT(rc_dec == msg->len - 2);
1101 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1102
1103 msgb_free(msg);
1104}
1105
Philipp Maier783047e2017-03-29 11:35:50 +02001106static void test_gsm0808_enc_dec_cell_id_list_bss()
1107{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001108 struct gsm0808_cell_id_list2 enc_cil;
1109 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001110 struct msgb *msg;
1111 uint8_t rc_enc;
1112 int rc_dec;
1113
1114 memset(&enc_cil, 0, sizeof(enc_cil));
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001115 enc_cil.id_discr = CELL_IDENT_BSS;
Philipp Maier783047e2017-03-29 11:35:50 +02001116
1117 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001118 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +02001119 OSMO_ASSERT(rc_enc == 3);
1120
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001121 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001122 OSMO_ASSERT(rc_dec == 1);
1123
1124 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1125
1126 msgb_free(msg);
1127}
1128
Stefan Sperling23381452018-03-15 19:38:15 +01001129static void test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac()
1130{
1131 struct gsm0808_cell_id_list2 enc_cil;
1132 struct gsm0808_cell_id_list2 dec_cil;
1133 struct osmo_location_area_id id;
1134 struct msgb *msg;
1135 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x10, 0x04,
1136 0x92, 0x61, 0x54, 0x23, 0x42,
1137 0x92, 0x72, 0x54, 0x24, 0x43,
1138 0x92, 0x83, 0x54, 0x25, 0x44
1139 };
1140 uint8_t rc_enc;
1141 int rc_dec, i;
1142
1143 memset(&enc_cil, 0, sizeof(enc_cil));
1144 enc_cil.id_discr = CELL_IDENT_LAI_AND_LAC;
1145
1146 id.plmn.mcc = 0x123;
1147 osmo_mnc_from_str("456", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1148 id.lac = 0x2342;
1149 memcpy(&enc_cil.id_list[0].lai_and_lac, &id, sizeof(id));
1150
1151 id.plmn.mcc = 0x124;
1152 osmo_mnc_from_str("457", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1153 id.lac = 0x2443;
1154 memcpy(&enc_cil.id_list[1].lai_and_lac, &id, sizeof(id));
1155
1156 id.plmn.mcc = 0x125;
1157 osmo_mnc_from_str("458", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1158 id.lac = 0x2544;
1159 memcpy(&enc_cil.id_list[2].lai_and_lac, &id, sizeof(id));
1160
1161 enc_cil.id_list_len = 3;
1162
1163 msg = msgb_alloc(1024, "output buffer");
1164 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1165 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1166 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1167
1168 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1169 OSMO_ASSERT(rc_dec == msg->len - 2);
1170
1171 OSMO_ASSERT(dec_cil.id_list_len == 3);
1172 /* Check MAXLEN elements to ensure everything has been initialized. */
1173 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1174 struct osmo_location_area_id *enc_id;
1175 struct osmo_location_area_id *dec_id;
1176 enc_id = &enc_cil.id_list[i].lai_and_lac;
1177 dec_id = &dec_cil.id_list[i].lai_and_lac;
1178 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->plmn, &dec_id->plmn) == 0);
1179 OSMO_ASSERT(enc_id->lac == dec_id->lac);
1180 }
1181
1182 msgb_free(msg);
1183}
1184
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001185static void test_gsm0808_enc_dec_cell_id_list_multi_ci()
1186{
1187 struct gsm0808_cell_id_list2 enc_cil;
1188 struct gsm0808_cell_id_list2 dec_cil;
1189 struct msgb *msg;
1190 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x09, 0x02,
1191 0x00, 0x01,
1192 0x00, 0x02,
1193 0x00, 0x77,
1194 0x01, 0xff,
1195 };
1196 uint8_t rc_enc;
1197 int rc_dec;
1198
1199 memset(&enc_cil, 0, sizeof(enc_cil));
1200 enc_cil.id_discr = CELL_IDENT_CI;
1201 enc_cil.id_list[0].ci = 1;
1202 enc_cil.id_list[1].ci = 2;
1203 enc_cil.id_list[2].ci = 119;
1204 enc_cil.id_list[3].ci = 511;
1205 enc_cil.id_list_len = 4;
1206
1207 msg = msgb_alloc(1024, "output buffer");
1208 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1209 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1210 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1211
1212 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1213 OSMO_ASSERT(rc_dec == msg->len - 2);
1214 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1215
1216 msgb_free(msg);
1217}
1218
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001219static void test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci()
1220{
1221 struct gsm0808_cell_id_list2 enc_cil;
1222 struct gsm0808_cell_id_list2 dec_cil;
1223 struct msgb *msg;
1224 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x15, 0x01,
1225 0x23, 0x42, 0x00, 0x01,
1226 0x24, 0x43, 0x00, 0x02,
1227 0x25, 0x44, 0x00, 0x77,
1228 0x26, 0x45, 0x01, 0xff,
1229 0x27, 0x46, 0x02, 0xfe,
1230 };
1231 uint8_t rc_enc;
1232 int rc_dec;
1233
1234 memset(&enc_cil, 0, sizeof(enc_cil));
1235 enc_cil.id_discr = CELL_IDENT_LAC_AND_CI;
1236 enc_cil.id_list[0].lac_and_ci.lac = 0x2342;
1237 enc_cil.id_list[0].lac_and_ci.ci = 1;
1238 enc_cil.id_list[1].lac_and_ci.lac = 0x2443;
1239 enc_cil.id_list[1].lac_and_ci.ci = 2;
1240 enc_cil.id_list[2].lac_and_ci.lac = 0x2544;
1241 enc_cil.id_list[2].lac_and_ci.ci = 119;
1242 enc_cil.id_list[3].lac_and_ci.lac = 0x2645;
1243 enc_cil.id_list[3].lac_and_ci.ci = 511;
1244 enc_cil.id_list[4].lac_and_ci.lac = 0x2746;
1245 enc_cil.id_list[4].lac_and_ci.ci = 766;
1246 enc_cil.id_list_len = 5;
1247
1248 msg = msgb_alloc(1024, "output buffer");
1249 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1250 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1251 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1252
1253 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1254 OSMO_ASSERT(rc_dec == msg->len - 2);
1255 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1256
1257 msgb_free(msg);
1258}
1259
Stefan Sperling483f3862018-03-16 12:21:26 +01001260static void test_gsm0808_enc_dec_cell_id_list_multi_global()
1261{
1262 struct gsm0808_cell_id_list2 enc_cil;
1263 struct gsm0808_cell_id_list2 dec_cil;
1264 struct msgb *msg;
1265 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x16, 0x00,
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001266 0x21, 0x63, 0x54, 0x23, 0x42, 0x00, 0x1,
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001267 0x21, 0xf4, 0x75, 0x24, 0x43, 0x00, 0x2,
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +01001268 0x21, 0x75, 0x00, 0x25, 0x44, 0x00, 0x77
Stefan Sperling483f3862018-03-16 12:21:26 +01001269 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001270 uint8_t rc_enc;
1271 int rc_dec, i;
1272
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001273 enc_cil = (struct gsm0808_cell_id_list2){
1274 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1275 .id_list_len = 3,
1276 .id_list = {
1277 {
1278 .global = {
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001279 .lai = { .plmn = { .mcc = 123, .mnc = 456 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001280 .lac = 0x2342 },
1281 .cell_identity = 1,
1282 }
1283 },
1284 {
1285 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001286 .lai = { .plmn = { .mcc = 124, .mnc = 57 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001287 .lac = 0x2443 },
1288 .cell_identity = 2,
1289 }
1290 },
1291 {
1292 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001293 .lai = { .plmn = { .mcc = 125, .mnc = 7,
1294 .mnc_3_digits = true },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001295 .lac = 0x2544 },
1296 .cell_identity = 119,
1297 }
1298 },
1299 }
1300 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001301
1302 msg = msgb_alloc(1024, "output buffer");
1303 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1304 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001305 if (memcmp(cil_enc_expected, msg->data, msg->len)) {
1306 printf(" got: %s\n", osmo_hexdump(msg->data, msg->len));
1307 printf("expect: %s\n", osmo_hexdump(cil_enc_expected, sizeof(cil_enc_expected)));
1308 OSMO_ASSERT(false);
1309 }
Stefan Sperling483f3862018-03-16 12:21:26 +01001310
1311 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1312 OSMO_ASSERT(rc_dec == msg->len - 2);
1313
1314 /* Check MAXLEN elements to ensure everything has been initialized. */
1315 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1316 struct osmo_cell_global_id *enc_id;
1317 struct osmo_cell_global_id *dec_id;
1318 enc_id = &enc_cil.id_list[i].global;
1319 dec_id = &dec_cil.id_list[i].global;
1320 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->lai.plmn, &dec_id->lai.plmn) == 0);
1321 OSMO_ASSERT(enc_id->lai.lac == dec_id->lai.lac);
1322 OSMO_ASSERT(enc_id->cell_identity == dec_id->cell_identity);
1323 }
1324
1325 msgb_free(msg);
1326}
1327
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001328static void print_cil(const struct gsm0808_cell_id_list2 *cil)
1329{
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001330 printf(" cell_id_list == %s\n", gsm0808_cell_id_list_name(cil));
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001331}
1332
1333void test_cell_id_list_add() {
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001334 size_t zu;
1335
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001336 const struct gsm0808_cell_id_list2 cgi1 = {
1337 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1338 .id_list_len = 1,
1339 .id_list = {
1340 {
1341 .global = {
1342 .lai = {
1343 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = false },
1344 .lac = 3,
1345 },
1346 .cell_identity = 4,
1347 }
1348 },
1349 },
1350 };
1351
1352 const struct gsm0808_cell_id_list2 cgi2 = {
1353 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1354 .id_list_len = 2,
1355 .id_list = {
1356 {
1357 .global = {
1358 .lai = {
1359 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = true },
1360 .lac = 3,
1361 },
1362 .cell_identity = 4,
1363 }
1364 },
1365 {
1366 .global = {
1367 .lai = {
1368 .plmn = { .mcc = 5, .mnc = 6, .mnc_3_digits = true },
1369 .lac = 7,
1370 },
1371 .cell_identity = 8,
1372 }
1373 },
1374 },
1375 };
1376
1377 const struct gsm0808_cell_id_list2 cgi2a = {
1378 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1379 .id_list_len = 2,
1380 .id_list = {
1381 {
1382 .global = cgi2.id_list[0].global
1383 },
1384 {
1385 .global = {
1386 .lai = {
1387 .plmn = { .mcc = 9, .mnc = 10, .mnc_3_digits = true },
1388 .lac = 11,
1389 },
1390 .cell_identity = 12,
1391 }
1392 },
1393 },
1394 };
1395
1396 const struct gsm0808_cell_id_list2 cgi3 = {
1397 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1398 .id_list_len = 2,
1399 .id_list = {
1400 {
1401 .global = {
1402 .lai = {
1403 .plmn = { .mcc = 13, .mnc = 14, .mnc_3_digits = true },
1404 .lac = 15,
1405 },
1406 .cell_identity = 16,
1407 }
1408 },
1409 {
1410 .global = {
1411 .lai = {
1412 .plmn = { .mcc = 16, .mnc = 17, .mnc_3_digits = true },
1413 .lac = 18,
1414 },
1415 .cell_identity = 19,
1416 }
1417 },
1418 },
1419 };
1420
1421
1422 const struct gsm0808_cell_id_list2 lac1 = {
1423 .id_discr = CELL_IDENT_LAC,
1424 .id_list_len = 1,
1425 .id_list = {
1426 {
1427 .lac = 123
1428 },
1429 },
1430 };
1431
1432 const struct gsm0808_cell_id_list2 lac2 = {
1433 .id_discr = CELL_IDENT_LAC,
1434 .id_list_len = 2,
1435 .id_list = {
1436 {
1437 .lac = 456
1438 },
1439 {
1440 .lac = 789
1441 },
1442 },
1443 };
1444
1445 struct gsm0808_cell_id_list2 cil = {};
1446
1447 printf("------- %s\n", __func__);
1448
1449 print_cil(&cil);
1450
1451#define ADD_QUIET(other_cil, expect_rc) do { \
1452 int rc = gsm0808_cell_id_list_add(&cil, &other_cil); \
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001453 printf("gsm0808_cell_id_list_add(&cil, &" #other_cil ") --> rc = %d\n", rc); \
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001454 OSMO_ASSERT(rc == expect_rc); \
1455 } while(0)
1456
1457#define ADD(other_cil, expect_rc) ADD_QUIET(other_cil, expect_rc); print_cil(&cil)
1458
1459 ADD(lac1, 1);
1460 ADD(lac1, 0);
1461 ADD(lac2, 2);
1462 ADD(lac2, 0);
1463 ADD(cil, 0);
1464 ADD(cgi1, -EINVAL);
1465
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001466 printf("* can't add to BSS list\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001467 cil.id_list_len = 0;
1468 cil.id_discr = CELL_IDENT_BSS;
1469 print_cil(&cil);
1470 ADD(lac1, -EINVAL);
1471
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001472 printf("* other types (including NO_CELL) take on new type iff empty\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001473 cil.id_list_len = 0;
1474 cil.id_discr = CELL_IDENT_NO_CELL;
1475 print_cil(&cil);
1476 ADD(cgi1, 1);
1477 ADD(cgi1, 0);
1478 ADD(cgi2, 2);
1479 ADD(cgi2, 0);
1480
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001481 printf("* test gsm0808_cell_id_list_name_buf()'s return val\n");
1482 zu = strlen(gsm0808_cell_id_list_name(&cil));
1483 printf(" strlen(gsm0808_cell_id_list_name(cil)) == %zu\n", zu);
1484 zu ++;
1485 while (1) {
1486 char buf[128] = "?";
1487 int rc;
1488 OSMO_ASSERT(zu < sizeof(buf));
1489 buf[zu] = '#';
1490 rc = gsm0808_cell_id_list_name_buf(buf, zu, &cil);
1491 printf(" gsm0808_cell_id_list_name_buf(buf, %zu, cil)) == %d \"%s\"\n",
1492 zu, rc, buf);
1493 OSMO_ASSERT(buf[zu] == '#');
1494 if (!zu)
1495 break;
1496 zu /= 2;
1497 }
1498
1499 printf("* list-full behavior\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001500 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001501 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001502 ADD_QUIET(cgi2a, 1);
1503 printf("cil.id_list_len = %u\n", cil.id_list_len);
1504
1505 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001506 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001507 ADD_QUIET(cgi3, -ENOSPC);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001508 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001509 ADD_QUIET(cgi2a, -ENOSPC);
1510 printf("cil.id_list_len = %u\n", cil.id_list_len);
1511
1512 printf("------- %s done\n", __func__);
1513}
1514
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001515static void test_gsm0808_enc_dec_cell_id_lac()
1516{
1517 struct gsm0808_cell_id enc_ci = {
1518 .id_discr = CELL_IDENT_LAC,
1519 .id.lac = 0x0124,
1520 };
1521 struct gsm0808_cell_id dec_ci;
1522 struct msgb *msg;
1523 uint8_t rc_enc;
1524 int rc_dec;
1525
1526 memset(&dec_ci, 0xa5, sizeof(dec_ci));
1527
1528 msg = msgb_alloc(1024, "output buffer");
1529 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1530 EXPECT_ENCODED("05 03 05 01 24");
1531
1532 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1533 OSMO_ASSERT(rc_dec == 3);
1534
1535 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1536 && enc_ci.id.lac == dec_ci.id.lac);
1537
1538 msgb_free(msg);
1539}
1540
1541static void test_gsm0808_enc_dec_cell_id_bss()
1542{
1543 struct gsm0808_cell_id enc_ci = {
1544 .id_discr = CELL_IDENT_BSS,
1545 };
1546 struct gsm0808_cell_id dec_ci;
1547 struct msgb *msg;
1548 uint8_t rc_enc;
1549 int rc_dec;
1550
1551 msg = msgb_alloc(1024, "output buffer");
1552 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1553 EXPECT_ENCODED("05 01 06");
1554
1555 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1556 OSMO_ASSERT(rc_dec == 1);
1557
1558 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr);
1559
1560 msgb_free(msg);
1561}
1562
1563static void test_gsm0808_enc_dec_cell_id_no_cell()
1564{
1565 struct gsm0808_cell_id enc_ci = {
1566 .id_discr = CELL_IDENT_NO_CELL,
1567 };
1568 struct gsm0808_cell_id dec_ci;
1569 struct msgb *msg;
1570 uint8_t rc_enc;
1571 int rc_dec;
1572
1573 msg = msgb_alloc(1024, "output buffer");
1574 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1575 EXPECT_ENCODED("05 01 03");
1576
1577 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1578 OSMO_ASSERT(rc_dec == 1);
1579
1580 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr);
1581
1582 msgb_free(msg);
1583}
1584
1585static void test_gsm0808_enc_dec_cell_id_lai_and_lac()
1586{
1587 struct gsm0808_cell_id enc_ci = {
1588 .id_discr = CELL_IDENT_LAI_AND_LAC,
1589 .id.lai_and_lac = {
1590 .plmn = {
1591 .mcc = 123,
1592 .mnc = 456,
1593 },
1594 .lac = 0x2342,
1595 },
1596 };
1597 struct gsm0808_cell_id dec_ci;
1598 struct msgb *msg;
1599 uint8_t rc_enc;
1600 int rc_dec;
1601
1602 msg = msgb_alloc(1024, "output buffer");
1603 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1604 EXPECT_ENCODED("05 06 04 21 63 54 23 42");
1605
1606 memset(&dec_ci, 0xa5, sizeof(dec_ci));
1607 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1608 OSMO_ASSERT(rc_dec == msg->len - 2);
1609
1610 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1611 && osmo_plmn_cmp(&enc_ci.id.lai_and_lac.plmn, &dec_ci.id.lai_and_lac.plmn) == 0
1612 && enc_ci.id.lai_and_lac.lac == dec_ci.id.lai_and_lac.lac);
1613 msgb_free(msg);
1614}
1615
1616static void test_gsm0808_enc_dec_cell_id_ci()
1617{
1618 struct gsm0808_cell_id enc_ci = {
1619 .id_discr = CELL_IDENT_CI,
1620 .id.ci = 0x423,
1621 };
1622 struct gsm0808_cell_id dec_ci;
1623 struct msgb *msg;
1624 uint8_t rc_enc;
1625 int rc_dec;
1626
1627 msg = msgb_alloc(1024, "output buffer");
1628 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1629 EXPECT_ENCODED("05 03 02 04 23");
1630
1631 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1632 OSMO_ASSERT(rc_dec == msg->len - 2);
1633 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1634 && enc_ci.id.ci == dec_ci.id.ci);
1635
1636 msgb_free(msg);
1637}
1638
1639static void test_gsm0808_enc_dec_cell_id_lac_and_ci()
1640{
1641 struct gsm0808_cell_id enc_ci = {
1642 .id_discr = CELL_IDENT_LAC_AND_CI,
1643 .id.lac_and_ci = {
1644 .lac = 0x423,
1645 .ci = 0x235,
1646 },
1647 };
1648 struct gsm0808_cell_id dec_ci;
1649 struct msgb *msg;
1650 uint8_t rc_enc;
1651 int rc_dec;
1652
1653 msg = msgb_alloc(1024, "output buffer");
1654 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1655 EXPECT_ENCODED("05 05 01 04 23 02 35");
1656
1657 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1658 OSMO_ASSERT(rc_dec == msg->len - 2);
1659 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1660 && enc_ci.id.lac_and_ci.lac == dec_ci.id.lac_and_ci.lac
1661 && enc_ci.id.lac_and_ci.ci == dec_ci.id.lac_and_ci.ci);
1662
1663 msgb_free(msg);
1664}
1665
1666static void test_gsm0808_enc_dec_cell_id_global()
1667{
1668 struct gsm0808_cell_id enc_ci = {
1669 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1670 .id.global = {
1671 .lai = {
1672 .plmn = { .mcc = 123, .mnc = 456 },
1673 .lac = 0x2342
1674 },
1675 .cell_identity = 0x423,
1676 }
1677 };
1678 struct gsm0808_cell_id dec_ci;
1679 struct msgb *msg;
1680 uint8_t rc_enc;
1681 int rc_dec;
1682
1683 msg = msgb_alloc(1024, "output buffer");
1684 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1685 EXPECT_ENCODED("05 08 00 21 63 54 23 42 04 23");
1686
1687 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1688 OSMO_ASSERT(rc_dec == msg->len - 2);
1689
1690 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1691 && osmo_plmn_cmp(&enc_ci.id.global.lai.plmn,
1692 &dec_ci.id.global.lai.plmn) == 0
1693 && enc_ci.id.global.lai.lac == dec_ci.id.global.lai.lac
1694 && enc_ci.id.global.cell_identity == dec_ci.id.global.cell_identity);
1695 msgb_free(msg);
1696}
1697
Philipp Maier5f2eb152018-09-19 13:40:21 +02001698static void test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(struct gsm48_multi_rate_conf *cfg)
1699{
1700 uint16_t s15_s0;
1701
1702 printf("Input:\n");
1703 printf(" m4_75= %u smod= %u\n", cfg->m4_75, cfg->smod);
1704 printf(" m5_15= %u spare= %u\n", cfg->m5_15, cfg->spare);
1705 printf(" m5_90= %u icmi= %u\n", cfg->m5_90, cfg->icmi);
1706 printf(" m6_70= %u nscb= %u\n", cfg->m6_70, cfg->nscb);
1707 printf(" m7_40= %u ver= %u\n", cfg->m7_40, cfg->ver);
1708 printf(" m7_95= %u\n", cfg->m7_95);
1709 printf(" m10_2= %u\n", cfg->m10_2);
1710 printf(" m12_2= %u\n", cfg->m12_2);
1711
1712 s15_s0 = gsm0808_sc_cfg_from_gsm48_mr_cfg(cfg, true);
1713 printf("Result (fr):\n");
1714 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
1715 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
1716
1717 s15_s0 = gsm0808_sc_cfg_from_gsm48_mr_cfg(cfg, false);
1718 printf("Result (hr):\n");
1719 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
1720 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
1721
1722 printf("\n");
1723}
1724
1725static void test_gsm0808_sc_cfg_from_gsm48_mr_cfg(void)
1726{
1727 struct gsm48_multi_rate_conf cfg;
1728
1729 printf("Testing gsm0808_sc_cfg_from_gsm48_mr_cfg():\n");
1730
1731 memset(&cfg, 0, sizeof(cfg));
1732
1733 cfg.m4_75 = 0;
1734 cfg.m5_15 = 0;
1735 cfg.m5_90 = 0;
1736 cfg.m6_70 = 0;
1737 cfg.m7_40 = 0;
1738 cfg.m7_95 = 0;
1739 cfg.m10_2 = 0;
1740 cfg.m12_2 = 0;
1741 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1742
1743 cfg.m4_75 = 1;
1744 cfg.m5_15 = 0;
1745 cfg.m5_90 = 0;
1746 cfg.m6_70 = 0;
1747 cfg.m7_40 = 0;
1748 cfg.m7_95 = 0;
1749 cfg.m10_2 = 0;
1750 cfg.m12_2 = 0;
1751 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1752
1753 cfg.m4_75 = 0;
1754 cfg.m5_15 = 1;
1755 cfg.m5_90 = 0;
1756 cfg.m6_70 = 0;
1757 cfg.m7_40 = 0;
1758 cfg.m7_95 = 0;
1759 cfg.m10_2 = 0;
1760 cfg.m12_2 = 0;
1761 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1762
1763 cfg.m4_75 = 0;
1764 cfg.m5_15 = 0;
1765 cfg.m5_90 = 1;
1766 cfg.m6_70 = 0;
1767 cfg.m7_40 = 0;
1768 cfg.m7_95 = 0;
1769 cfg.m10_2 = 0;
1770 cfg.m12_2 = 0;
1771 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1772
1773 cfg.m4_75 = 0;
1774 cfg.m5_15 = 0;
1775 cfg.m5_90 = 0;
1776 cfg.m6_70 = 1;
1777 cfg.m7_40 = 0;
1778 cfg.m7_95 = 0;
1779 cfg.m10_2 = 0;
1780 cfg.m12_2 = 0;
1781 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1782
1783 cfg.m4_75 = 0;
1784 cfg.m5_15 = 0;
1785 cfg.m5_90 = 0;
1786 cfg.m6_70 = 0;
1787 cfg.m7_40 = 1;
1788 cfg.m7_95 = 0;
1789 cfg.m10_2 = 0;
1790 cfg.m12_2 = 0;
1791 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1792
1793 cfg.m4_75 = 0;
1794 cfg.m5_15 = 0;
1795 cfg.m5_90 = 0;
1796 cfg.m6_70 = 0;
1797 cfg.m7_40 = 0;
1798 cfg.m7_95 = 1;
1799 cfg.m10_2 = 0;
1800 cfg.m12_2 = 0;
1801 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1802
1803 cfg.m4_75 = 0;
1804 cfg.m5_15 = 0;
1805 cfg.m5_90 = 0;
1806 cfg.m6_70 = 0;
1807 cfg.m7_40 = 0;
1808 cfg.m7_95 = 0;
1809 cfg.m10_2 = 1;
1810 cfg.m12_2 = 0;
1811 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1812
1813 cfg.m4_75 = 0;
1814 cfg.m5_15 = 0;
1815 cfg.m5_90 = 0;
1816 cfg.m6_70 = 0;
1817 cfg.m7_40 = 0;
1818 cfg.m7_95 = 0;
1819 cfg.m10_2 = 0;
1820 cfg.m12_2 = 1;
1821 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1822
1823 cfg.m4_75 = 1;
1824 cfg.m5_15 = 1;
1825 cfg.m5_90 = 1;
1826 cfg.m6_70 = 1;
1827 cfg.m7_40 = 0;
1828 cfg.m7_95 = 0;
1829 cfg.m10_2 = 0;
1830 cfg.m12_2 = 0;
1831 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1832
1833 cfg.m4_75 = 0;
1834 cfg.m5_15 = 0;
1835 cfg.m5_90 = 0;
1836 cfg.m6_70 = 0;
1837 cfg.m7_40 = 1;
1838 cfg.m7_95 = 1;
1839 cfg.m10_2 = 1;
1840 cfg.m12_2 = 1;
1841 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1842
1843 cfg.m4_75 = 0;
1844 cfg.m5_15 = 0;
1845 cfg.m5_90 = 1;
1846 cfg.m6_70 = 1;
1847 cfg.m7_40 = 0;
1848 cfg.m7_95 = 0;
1849 cfg.m10_2 = 1;
1850 cfg.m12_2 = 1;
1851 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1852
1853 cfg.m4_75 = 1;
1854 cfg.m5_15 = 1;
1855 cfg.m5_90 = 0;
1856 cfg.m6_70 = 0;
1857 cfg.m7_40 = 1;
1858 cfg.m7_95 = 1;
1859 cfg.m10_2 = 0;
1860 cfg.m12_2 = 0;
1861 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1862
1863 cfg.m4_75 = 0;
1864 cfg.m5_15 = 1;
1865 cfg.m5_90 = 0;
1866 cfg.m6_70 = 1;
1867 cfg.m7_40 = 0;
1868 cfg.m7_95 = 1;
1869 cfg.m10_2 = 0;
1870 cfg.m12_2 = 1;
1871 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1872
1873 cfg.m4_75 = 1;
1874 cfg.m5_15 = 0;
1875 cfg.m5_90 = 1;
1876 cfg.m6_70 = 0;
1877 cfg.m7_40 = 1;
1878 cfg.m7_95 = 0;
1879 cfg.m10_2 = 1;
1880 cfg.m12_2 = 0;
1881 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1882
1883 cfg.m4_75 = 1;
1884 cfg.m5_15 = 1;
1885 cfg.m5_90 = 1;
1886 cfg.m6_70 = 1;
1887 cfg.m7_40 = 1;
1888 cfg.m7_95 = 1;
1889 cfg.m10_2 = 1;
1890 cfg.m12_2 = 1;
1891 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1892}
1893
Philipp Maier8515d032018-09-25 15:57:49 +02001894static void test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(uint16_t s15_s0)
1895{
1896 struct gsm48_multi_rate_conf cfg;
1897
1898 printf("Input:\n");
1899 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
1900 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
1901
1902 gsm48_mr_cfg_from_gsm0808_sc_cfg(&cfg, s15_s0);
1903
1904 printf("Output:\n");
1905 printf(" m4_75= %u smod= %u\n", cfg.m4_75, cfg.smod);
1906 printf(" m5_15= %u spare= %u\n", cfg.m5_15, cfg.spare);
1907 printf(" m5_90= %u icmi= %u\n", cfg.m5_90, cfg.icmi);
1908 printf(" m6_70= %u nscb= %u\n", cfg.m6_70, cfg.nscb);
1909 printf(" m7_40= %u ver= %u\n", cfg.m7_40, cfg.ver);
1910 printf(" m7_95= %u\n", cfg.m7_95);
1911 printf(" m10_2= %u\n", cfg.m10_2);
1912 printf(" m12_2= %u\n", cfg.m12_2);
1913
1914 printf("\n");
1915}
1916
1917void test_gsm48_mr_cfg_from_gsm0808_sc_cfg()
1918{
1919 printf("Testing gsm48_mr_cfg_from_gsm0808_sc_cfg():\n");
1920
1921 /* Only one codec per setting */
1922 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1923 (GSM0808_SC_CFG_DEFAULT_AMR_4_75);
1924 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1925 (GSM0808_SC_CFG_DEFAULT_AMR_5_15);
1926 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1927 (GSM0808_SC_CFG_DEFAULT_AMR_5_90);
1928 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1929 (GSM0808_SC_CFG_DEFAULT_AMR_6_70);
1930 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1931 (GSM0808_SC_CFG_DEFAULT_AMR_7_40);
1932 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1933 (GSM0808_SC_CFG_DEFAULT_AMR_7_95);
1934 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1935 (GSM0808_SC_CFG_DEFAULT_AMR_10_2);
1936 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1937 (GSM0808_SC_CFG_DEFAULT_AMR_12_2);
1938
1939 /* Combinations */
1940 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1941 (GSM0808_SC_CFG_DEFAULT_AMR_4_75 | GSM0808_SC_CFG_DEFAULT_AMR_6_70 |
1942 GSM0808_SC_CFG_DEFAULT_AMR_10_2);
1943 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1944 (GSM0808_SC_CFG_DEFAULT_AMR_10_2 | GSM0808_SC_CFG_DEFAULT_AMR_12_2 |
1945 GSM0808_SC_CFG_DEFAULT_AMR_7_40);
1946 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
1947 (GSM0808_SC_CFG_DEFAULT_AMR_7_95 | GSM0808_SC_CFG_DEFAULT_AMR_12_2);
1948}
1949
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001950int main(int argc, char **argv)
1951{
Max969fb2e2018-12-10 11:01:10 +01001952 void *ctx = talloc_named_const(NULL, 0, "gsm0808 test");
1953 msgb_talloc_ctx_init(ctx, 0);
1954 osmo_init_logging2(ctx, NULL);
1955
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001956 printf("Testing generation of GSM0808 messages\n");
Philipp Maier4f4905f2018-11-30 13:36:12 +01001957 test_gsm0808_enc_cause();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001958 test_create_layer3();
Philipp Maierfa896ab2017-03-27 16:55:32 +02001959 test_create_layer3_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001960 test_create_reset();
Philipp Maier15596e22017-04-05 17:55:27 +02001961 test_create_reset_ack();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001962 test_create_clear_command();
1963 test_create_clear_complete();
Philipp Maierb478dd32017-03-29 15:50:05 +02001964 test_create_cipher();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001965 test_create_cipher_complete();
1966 test_create_cipher_reject();
Maxed651d22018-11-07 15:25:05 +01001967 test_create_cipher_reject_ext();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001968 test_create_cm_u();
1969 test_create_sapi_reject();
Philipp Maierc6144a22017-03-29 17:53:43 +02001970 test_create_ass();
Max52074322018-11-30 10:44:07 +01001971 test_create_ass2();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001972 test_create_ass_compl();
Philipp Maierfa896ab2017-03-27 16:55:32 +02001973 test_create_ass_compl_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001974 test_create_ass_fail();
Philipp Maierfa896ab2017-03-27 16:55:32 +02001975 test_create_ass_fail_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001976 test_create_clear_rqst();
Philipp Maier3d48ec02017-03-29 17:37:55 +02001977 test_create_paging();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01001978 test_create_dtap();
1979 test_prepend_dtap();
Max969fb2e2018-12-10 11:01:10 +01001980
Max47022152018-12-19 18:51:00 +01001981 test_enc_dec_lcls();
Max969fb2e2018-12-10 11:01:10 +01001982
Philipp Maier22401432017-03-24 17:59:26 +01001983 test_enc_dec_aoip_trasp_addr_v4();
1984 test_enc_dec_aoip_trasp_addr_v6();
Philipp Maier6f725d62017-03-24 18:03:17 +01001985 test_gsm0808_enc_dec_speech_codec();
Philipp Maier6f725d62017-03-24 18:03:17 +01001986 test_gsm0808_enc_dec_speech_codec_ext_with_cfg();
Philipp Maierbb839662017-06-01 17:11:19 +02001987 test_gsm0808_enc_dec_speech_codec_with_cfg();
Philipp Maier6f725d62017-03-24 18:03:17 +01001988 test_gsm0808_enc_dec_speech_codec_list();
Philipp Maierf6c369f2018-10-16 15:24:47 +02001989 test_gsm0808_enc_dec_empty_speech_codec_list();
Philipp Maiere0c65302017-03-28 17:05:40 +02001990 test_gsm0808_enc_dec_channel_type();
Philipp Maier14e76b92017-03-28 18:36:52 +02001991 test_gsm0808_enc_dec_encrypt_info();
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001992
Philipp Maier783047e2017-03-29 11:35:50 +02001993 test_gsm0808_enc_dec_cell_id_list_lac();
1994 test_gsm0808_enc_dec_cell_id_list_single_lac();
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001995 test_gsm0808_enc_dec_cell_id_list_multi_lac();
Philipp Maier783047e2017-03-29 11:35:50 +02001996 test_gsm0808_enc_dec_cell_id_list_bss();
Stefan Sperling23381452018-03-15 19:38:15 +01001997 test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac();
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001998 test_gsm0808_enc_dec_cell_id_list_multi_ci();
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001999 test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci();
Stefan Sperling483f3862018-03-16 12:21:26 +01002000 test_gsm0808_enc_dec_cell_id_list_multi_global();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002001
Neels Hofmeyr74663d92018-03-23 01:46:42 +01002002 test_cell_id_list_add();
2003
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02002004 test_gsm0808_enc_dec_cell_id_lac();
2005 test_gsm0808_enc_dec_cell_id_bss();
2006 test_gsm0808_enc_dec_cell_id_no_cell();
2007 test_gsm0808_enc_dec_cell_id_lai_and_lac();
2008 test_gsm0808_enc_dec_cell_id_ci();
2009 test_gsm0808_enc_dec_cell_id_lac_and_ci();
2010 test_gsm0808_enc_dec_cell_id_global();
2011
Philipp Maier5f2eb152018-09-19 13:40:21 +02002012 test_gsm0808_sc_cfg_from_gsm48_mr_cfg();
Philipp Maier8515d032018-09-25 15:57:49 +02002013 test_gsm48_mr_cfg_from_gsm0808_sc_cfg();
Philipp Maier5f2eb152018-09-19 13:40:21 +02002014
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002015 printf("Done\n");
2016 return EXIT_SUCCESS;
2017}