blob: 38c68bf096de3f0a3ad21f886ff8fd21b3b722fb [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 *
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010015 */
16
17#include <osmocom/gsm/gsm0808.h>
Philipp Maier22401432017-03-24 17:59:26 +010018#include <osmocom/gsm/gsm0808_utils.h>
19#include <osmocom/gsm/protocol/gsm_08_08.h>
Philipp Maier3d48ec02017-03-29 17:37:55 +020020#include <osmocom/gsm/protocol/gsm_08_58.h>
Max969fb2e2018-12-10 11:01:10 +010021#include <osmocom/core/logging.h>
22#include <osmocom/core/application.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010023
24#include <stdio.h>
25#include <stdlib.h>
Philipp Maier22401432017-03-24 17:59:26 +010026#include <sys/socket.h>
27#include <netinet/in.h>
28#include <arpa/inet.h>
Neels Hofmeyr74663d92018-03-23 01:46:42 +010029#include <errno.h>
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010030
Philipp Maier4f4905f2018-11-30 13:36:12 +010031#define EXPECT_ENCODED(hexstr) do { \
32 const char *enc_str = msgb_hexdump(msg); \
33 printf("%s: encoded: %s(rc = %u)\n", __func__, enc_str, rc_enc); \
34 OSMO_ASSERT(strcmp(enc_str, hexstr " ") == 0); \
35 OSMO_ASSERT(rc_enc == msg->len); \
36 } while(0)
37
Vadim Yanitskiycf6ee642018-12-20 05:23:00 +070038#define VERIFY(msg, data, data_len) do { \
39 if (!msgb_eq_l3_data_print(msg, data, data_len)) \
40 abort(); \
41 } while(0)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010042
Philipp Maierfa896ab2017-03-27 16:55:32 +020043/* Setup a fake codec list for testing */
44static void setup_codec_list(struct gsm0808_speech_codec_list *scl)
45{
46 memset(scl, 0, sizeof(*scl));
47
48 scl->codec[0].pi = true;
49 scl->codec[0].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +020050 scl->codec[0].type = GSM0808_SCT_FR3;
Philipp Maierfa896ab2017-03-27 16:55:32 +020051 scl->codec[0].cfg = 0xcdef;
52
53 scl->codec[1].fi = true;
54 scl->codec[1].pt = true;
Philipp Maierbb839662017-06-01 17:11:19 +020055 scl->codec[1].type = GSM0808_SCT_FR2;
Philipp Maierfa896ab2017-03-27 16:55:32 +020056
57 scl->codec[2].fi = true;
58 scl->codec[2].tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +020059 scl->codec[2].type = GSM0808_SCT_CSD;
60 scl->codec[2].cfg = 0xc0;
Philipp Maierfa896ab2017-03-27 16:55:32 +020061
62 scl->len = 3;
63}
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010064
Philipp Maier4f4905f2018-11-30 13:36:12 +010065void test_gsm0808_enc_cause(void)
66{
67 /* NOTE: This must be tested early because many of the following tests
68 * rely on the generation of a proper cause code. */
69
70 uint8_t rc_enc;
71 struct msgb *msg;
72
73 /* Test with a single byte cause code */
74 msg = msgb_alloc(1024, "output buffer");
75 rc_enc = gsm0808_enc_cause(msg, 0x41);
76 EXPECT_ENCODED("04 01 41");
77 msgb_free(msg);
78
79 /* Test with an extended (two byte) cause code */
80 msg = msgb_alloc(1024, "output buffer");
81 rc_enc = gsm0808_enc_cause(msg, 0x8041);
82 EXPECT_ENCODED("04 02 80 41");
83 msgb_free(msg);
84}
85
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +010086static void test_create_layer3(void)
87{
88 static const uint8_t res[] = {
89 0x00, 0x0e, 0x57, 0x05, 0x08, 0x00, 0x77, 0x62,
90 0x83, 0x33, 0x66, 0x44, 0x88, 0x17, 0x01, 0x23 };
91 struct msgb *msg, *in_msg;
Neels Hofmeyr178bf7a2018-04-20 12:23:45 +020092 struct osmo_cell_global_id cgi = {
93 .lai = {
94 .plmn = {
95 .mcc = 0x2244,
96 .mnc = 0x1122,
97 },
98 .lac = 0x3366,
99 },
100 .cell_identity = 0x4488,
101 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100102 printf("Testing creating Layer3\n");
103
104 in_msg = msgb_alloc_headroom(512, 128, "foo");
105 in_msg->l3h = in_msg->data;
106 msgb_v_put(in_msg, 0x23);
107
Neels Hofmeyr178bf7a2018-04-20 12:23:45 +0200108 msg = gsm0808_create_layer3_2(in_msg, &cgi, NULL);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100109 VERIFY(msg, res, ARRAY_SIZE(res));
110 msgb_free(msg);
111 msgb_free(in_msg);
112}
113
Philipp Maierfa896ab2017-03-27 16:55:32 +0200114static void test_create_layer3_aoip()
115{
116 static const uint8_t res[] = {
117 0x00, 0x17, 0x57, 0x05, 0x08, 0x00, 0x77, 0x62,
118 0x83, 0x33, 0x66, 0x44, 0x88, 0x17, 0x01, 0x23,
Philipp Maierbb839662017-06-01 17:11:19 +0200119 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50,
Philipp Maier7e27b142018-03-22 17:26:46 +0100120 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f,
Philipp Maierbb839662017-06-01 17:11:19 +0200121 GSM0808_SCT_CSD | 0x90, 0xc0
Philipp Maierfa896ab2017-03-27 16:55:32 +0200122 };
Maxfa3b4822018-11-05 14:59:54 +0100123 struct osmo_cell_global_id cgi = {
124 .lai = {
125 .plmn = {
126 .mcc = 0x2244,
127 .mnc = 0x1122,
128 },
129 .lac = 0x3366,
130 },
131 .cell_identity = 0x4488,
132 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200133 struct msgb *msg, *in_msg;
134 struct gsm0808_speech_codec_list sc_list;
135 printf("Testing creating Layer3 (AoIP)\n");
136
137 setup_codec_list(&sc_list);
138
139 in_msg = msgb_alloc_headroom(512, 128, "foo");
140 in_msg->l3h = in_msg->data;
141 msgb_v_put(in_msg, 0x23);
142
Maxfa3b4822018-11-05 14:59:54 +0100143 msg = gsm0808_create_layer3_2(in_msg, &cgi, &sc_list);
144
Philipp Maierfa896ab2017-03-27 16:55:32 +0200145 VERIFY(msg, res, ARRAY_SIZE(res));
146
147 msgb_free(msg);
148 msgb_free(in_msg);
149}
150
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100151static void test_create_reset()
152{
153 static const uint8_t res[] = { 0x00, 0x04, 0x30, 0x04, 0x01, 0x20 };
154 struct msgb *msg;
155
156 printf("Testing creating Reset\n");
157 msg = gsm0808_create_reset();
158 VERIFY(msg, res, ARRAY_SIZE(res));
159 msgb_free(msg);
160}
161
Philipp Maier15596e22017-04-05 17:55:27 +0200162static void test_create_reset_ack()
163{
164 static const uint8_t res[] = { 0x00, 0x01, 0x31 };
165 struct msgb *msg;
166
167 printf("Testing creating Reset Ack\n");
168 msg = gsm0808_create_reset_ack();
169 VERIFY(msg, res, ARRAY_SIZE(res));
170 msgb_free(msg);
171}
172
173
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100174static void test_create_clear_command()
175{
176 static const uint8_t res[] = { 0x20, 0x04, 0x01, 0x23 };
177 struct msgb *msg;
178
179 printf("Testing creating Clear Command\n");
180 msg = gsm0808_create_clear_command(0x23);
181 VERIFY(msg, res, ARRAY_SIZE(res));
182 msgb_free(msg);
183}
184
Harald Weltecf665fc2019-02-18 13:45:36 +0100185static void test_create_clear_command2()
186{
187 static const uint8_t res[] = { 0x00, 0x04, 0x20, 0x04, 0x01, 0x23 };
188 struct msgb *msg;
189
190 printf("Testing creating Clear Command 2\n");
191 msg = gsm0808_create_clear_command2(0x23, false);
192 VERIFY(msg, res, ARRAY_SIZE(res));
193 msgb_free(msg);
194}
195
196static void test_create_clear_command2_csfb()
197{
198 static const uint8_t res[] = { 0x00, 0x05, 0x20, 0x04, 0x01, 0x23, 0x8F };
199 struct msgb *msg;
200
201 printf("Testing creating Clear Command 2 (CSFB)\n");
202 msg = gsm0808_create_clear_command2(0x23, true);
203 VERIFY(msg, res, ARRAY_SIZE(res));
204 msgb_free(msg);
205}
206
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100207static void test_create_clear_complete()
208{
209 static const uint8_t res[] = { 0x00, 0x01, 0x21 };
210 struct msgb *msg;
211
212 printf("Testing creating Clear Complete\n");
213 msg = gsm0808_create_clear_complete();
214 VERIFY(msg, res, ARRAY_SIZE(res));
215 msgb_free(msg);
216}
217
Philipp Maierb478dd32017-03-29 15:50:05 +0200218static void test_create_cipher()
219{
220 static const uint8_t res[] =
221 { 0x00, 0x0c, 0x53, 0x0a, 0x09, 0x03, 0xaa,
222 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 };
223 static const uint8_t res2[] =
224 { 0x00, 0x0e, 0x53, 0x0a, 0x09, 0x03, 0xaa,
225 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42,
226 GSM0808_IE_CIPHER_RESPONSE_MODE, 0x01 };
227 struct msgb *msg;
228 struct gsm0808_encrypt_info ei;
229 uint8_t include_imeisv;
230
231 memset(&ei, 0, sizeof(ei));
232 ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
233 ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
234 ei.perm_algo_len = 2;
235 ei.key[0] = 0xaa;
236 ei.key[1] = 0xbb;
237 ei.key[2] = 0xcc;
238 ei.key[3] = 0xdd;
239 ei.key[4] = 0xee;
240 ei.key[5] = 0xff;
241 ei.key[6] = 0x23;
242 ei.key[7] = 0x42;
243 ei.key_len = 8;
244 include_imeisv = 1;
245
246 printf("Testing creating Chipher Mode Command\n");
247 msg = gsm0808_create_cipher(&ei, NULL);
248 OSMO_ASSERT(msg);
249 VERIFY(msg, res, ARRAY_SIZE(res));
250 msgb_free(msg);
251
252 msg = gsm0808_create_cipher(&ei, &include_imeisv);
253 OSMO_ASSERT(msg);
254 VERIFY(msg, res2, ARRAY_SIZE(res2));
255 msgb_free(msg);
256}
257
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100258static void test_create_cipher_complete()
259{
260 static const uint8_t res1[] = {
261 0x00, 0x08, 0x55, 0x20, 0x03, 0x23, 0x42, 0x21, 0x2c, 0x04 };
262 static const uint8_t res2[] = { 0x00, 0x03, 0x55, 0x2c, 0x04};
263 struct msgb *l3, *msg;
264
265 printf("Testing creating Cipher Complete\n");
266 l3 = msgb_alloc_headroom(512, 128, "l3h");
267 l3->l3h = l3->data;
268 msgb_v_put(l3, 0x23);
269 msgb_v_put(l3, 0x42);
270 msgb_v_put(l3, 0x21);
271
272 /* with l3 data */
273 msg = gsm0808_create_cipher_complete(l3, 4);
274 VERIFY(msg, res1, ARRAY_SIZE(res1));
275 msgb_free(msg);
276
277 /* with l3 data but short */
278 l3->len -= 1;
279 l3->tail -= 1;
280 msg = gsm0808_create_cipher_complete(l3, 4);
281 VERIFY(msg, res2, ARRAY_SIZE(res2));
282 msgb_free(msg);
283
284 /* without l3 data */
285 msg = gsm0808_create_cipher_complete(NULL, 4);
286 VERIFY(msg, res2, ARRAY_SIZE(res2));
287 msgb_free(msg);
288
289
290 msgb_free(l3);
291}
292
Maxed651d22018-11-07 15:25:05 +0100293static inline void parse_cipher_reject(struct msgb *msg, uint8_t exp)
294{
295 struct tlv_parsed tp;
296 int rc;
297
298 /* skip header and message type so we can parse Cause IE directly */
299 msg->l2h = msgb_data(msg) + sizeof(struct bssmap_header) + 1;
300
301 rc = osmo_bssap_tlv_parse(&tp, msg->l2h, msgb_l2len(msg));
302 if (rc < 0)
303 printf("FIXME: failed (%d) to parse created message %s\n", rc, msgb_hexdump(msg));
304
Alexander Chemerisfdfe25b2020-05-12 23:21:56 +0300305 rc = gsm0808_get_cause(&tp);
Maxed651d22018-11-07 15:25:05 +0100306 if (rc < 0)
307 printf("FIXME: failed (%s) to extract Cause from created message %s\n",
308 strerror(-rc), msgb_hexdump(msg));
309
310 if (exp != (enum gsm0808_cause)rc)
311 printf("FIXME: wrong Cause %d != %u (" OSMO_BIN_SPEC ") extracted from created message %s\n",
312 rc, exp, OSMO_BIT_PRINT(exp), msgb_hexdump(msg));
313}
314
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100315static void test_create_cipher_reject()
316{
Harald Welte62e40852017-12-17 20:50:34 +0100317 static const uint8_t res[] = { 0x00, 0x04, 0x59, 0x04, 0x01, 0x23 };
Maxed651d22018-11-07 15:25:05 +0100318 enum gsm0808_cause cause = GSM0808_CAUSE_CCCH_OVERLOAD;
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100319 struct msgb *msg;
320
321 printf("Testing creating Cipher Reject\n");
Maxed651d22018-11-07 15:25:05 +0100322 msg = gsm0808_create_cipher_reject(cause);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100323 VERIFY(msg, res, ARRAY_SIZE(res));
Maxed651d22018-11-07 15:25:05 +0100324
325 parse_cipher_reject(msg, cause);
326
327 msgb_free(msg);
328}
329
330static void test_create_cipher_reject_ext()
331{
332 static const uint8_t res[] = { 0x00, 0x05, 0x59, 0x04, 0x02, 0xd0, 0xFA };
333 uint8_t cause = 0xFA;
334 struct msgb *msg;
335
336 printf("Testing creating Cipher Reject (extended)\n");
337 msg = gsm0808_create_cipher_reject_ext(GSM0808_CAUSE_CLASS_INVAL, cause);
338 VERIFY(msg, res, ARRAY_SIZE(res));
339
340 parse_cipher_reject(msg, cause);
341
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100342 msgb_free(msg);
343}
344
345static void test_create_cm_u()
346{
Harald Welte07b625d2012-01-23 10:02:58 +0100347 static const uint8_t res[] = {
348 0x00, 0x07, 0x54, 0x12, 0x01, 0x23, 0x13, 0x01, 0x42 };
349 static const uint8_t res2o[] = {
350 0x00, 0x04, 0x54, 0x12, 0x01, 0x23 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100351 struct msgb *msg;
Harald Welte07b625d2012-01-23 10:02:58 +0100352 const uint8_t cm2 = 0x23;
353 const uint8_t cm3 = 0x42;
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100354
355 printf("Testing creating CM U\n");
Harald Welte07b625d2012-01-23 10:02:58 +0100356 msg = gsm0808_create_classmark_update(&cm2, 1, &cm3, 1);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100357 VERIFY(msg, res, ARRAY_SIZE(res));
Harald Welte07b625d2012-01-23 10:02:58 +0100358
Neels Hofmeyr9a938ae2017-11-16 17:34:07 +0100359 msgb_free(msg);
360
Harald Welte07b625d2012-01-23 10:02:58 +0100361 msg = gsm0808_create_classmark_update(&cm2, 1, NULL, 0);
362 VERIFY(msg, res2o, ARRAY_SIZE(res2o));
363
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100364 msgb_free(msg);
365}
366
367static void test_create_sapi_reject()
368{
Alexander Chemerisa5b1b862020-05-12 01:03:08 +0300369 static const uint8_t res[] = { 0x00, 0x06, 0x25, 0x18, 0x03, 0x04, 0x01, 0x25 };
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100370 struct msgb *msg;
371
372 printf("Testing creating SAPI Reject\n");
Vadim Yanitskiyfa6cd882020-08-26 18:03:59 +0700373 msg = gsm0808_create_sapi_reject_cause(3, GSM0808_CAUSE_BSS_NOT_EQUIPPED);
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100374 VERIFY(msg, res, ARRAY_SIZE(res));
375 msgb_free(msg);
376}
377
Alexander Chemeris22630e62020-05-13 00:44:04 +0300378static void test_dec_confusion()
379{
380 static const uint8_t hex[] =
381 { 0x26, 0x04, 0x01, 0x52, 0x1f, 0x07, 0x00, 0xff, 0x00, 0x03, 0x25, 0x03, 0x25 };
382 struct tlv_parsed tp;
383 int diag_len;
384 enum gsm0808_cause cause;
385 enum gsm0808_cause_class cause_class;
386 struct gsm0808_diagnostics *diag;
387
388 printf("Testing decoding CONFUSION\n");
389
390 tlv_parse(&tp, gsm0808_att_tlvdef(), hex+1, sizeof(hex)-1, 0, 0);
391
392 /* Check for the Cause and Diagnostic mandatory elements */
393 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE) || !TLVP_PRESENT(&tp, GSM0808_IE_DIAGNOSTIC)) {
394 printf("Either Cause or Diagnostic mandatory IE are not detected\n");
395 return;
396 }
397
398 diag_len = TLVP_LEN(&tp, GSM0808_IE_DIAGNOSTIC);
399 if (diag_len < 5) {
400 printf("Diagnostic length is too short: %d (expected > 5)\n",
401 diag_len);
402 return;
403 }
404
405 cause = gsm0808_get_cause(&tp);
406 if ((int)cause < 0) {
407 printf("ERROR: failed (%s) to extract Cause, aborting\n", strerror(-(int)cause));
408 return;
409 }
410 cause_class = gsm0808_cause_class(cause);
411 printf(" Cause class %d/0x%x (%s)\n",
412 cause_class, cause_class, gsm0808_cause_class_name(cause_class));
413 printf(" Cause %d/0x%x (%s)\n",
414 cause, cause, gsm0808_cause_name(cause));
415
416 diag = (struct gsm0808_diagnostics *)TLVP_VAL(&tp, GSM0808_IE_DIAGNOSTIC);
417 printf(" Diagnostics error octet location %d (%s)\n",
418 diag->error_pointer_octet,
419 gsm0808_diagnostics_octet_location_str(diag->error_pointer_octet));
420 printf(" Diagnostics error bit location %d (%s)\n",
421 diag->error_pointer_bit,
422 gsm0808_diagnostics_bit_location_str(diag->error_pointer_bit));
423 printf(" Diagnostics message that provoked the error: %s\n",
424 osmo_hexdump(diag->msg, diag_len-2));
425}
426
Pau Espin Pedrola3987fc2022-03-18 13:17:28 +0100427/* Test Perform Location Report SYS#5891 */
428static void test_dec_perform_location_report_sys5891()
429{
430/* Message Type Perform Location Request
431 Location Type
432 Element ID: 0x44
433 Length: 1
434 Location Information: current geographic location (0x00)
435 Cell Identifier/CI (25911)
436 Element ID: 0x05
437 Length: 8
438 0000 .... = Spare bit(s): 0x00
439 .... 0000 = Cell identification discriminator: The whole Cell Global Identification, CGI, is used to identify the cells. (0)
440 Mobile Country Code (MCC): (removed))
441 Mobile Network Code (MNC): (removed))
442 Cell LAC: 0x001e (30)
443 Cell CI: 0x6537 (25911)
444 LCS Client Type
445 Element ID: 0x48
446 Length: 1
447 0011 .... = Client Category: Emergency Services (0x03)
448 .... 0000 = Client Subtype: unspecified (0x00)
449 LCS Priority
450 Element ID: 0x43
451 Length: 1
452 Periodicity: highest (0)
453 LCS QoS
454 Element ID: 0x3e
455 Length: 4
456 0000 00.. = Spare: 0x00
457 .... ..0. = Velocity Requested: do not report velocity (0x00)
458 .... ...0 = Vertical Coordinate Indicator: vertical coordinate not requested (0x00)
459 1... .... = Horizontal Accuracy Indicator: horizontal accuracy is specified (0x01)
460 .001 0010 = Horizontal Accuracy: 0x12
461 0... .... = Vertical Accuracy Indicator: vertical accuracy is not specified (0x00)
462 .000 0000 = Spare: 0x00
463 00.. .... = Response Time Category: Response Time is not specified (0x00)
464*/
465 const uint8_t hex[] = {
466 0x2b, 0x44, 0x01, 0x00, 0x05, 0x08, 0x00, 0xab, 0xbc, 0xcd, 0x00, 0x1e,
467 0x65, 0x37, 0x48, 0x01, 0x30, 0x43, 0x01, 0x00, 0x3e, 0x04, 0x00, 0x92,
468 0x00, 0x00
469 };
470
471 struct tlv_parsed tp;
472 int rc;
473
474 printf("Testing decoding Perform Location Report SYS#5891\n");
475
476 rc = tlv_parse(&tp, gsm0808_att_tlvdef(), hex+1, sizeof(hex)-1, 0, 0);
477 OSMO_ASSERT(rc < 0);
478}
479
Philipp Maierc6144a22017-03-29 17:53:43 +0200480static void test_create_ass()
481{
482 static const uint8_t res1[] =
483 { 0x00, 0x0a, 0x01, 0x0b, 0x04, 0x01, 0x0b, 0xa1, 0x25, 0x01, 0x00,
484 0x04 };
485 static const uint8_t res2[] =
486 { 0x00, 0x20, 0x01, 0x0b, 0x04, 0x01, 0x0b, 0xa1, 0x25, 0x01, 0x00,
487 0x04, GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17,
Philipp Maierbb839662017-06-01 17:11:19 +0200488 0x04, 0xd2, GSM0808_IE_SPEECH_CODEC_LIST, 0x07,
Philipp Maier7e27b142018-03-22 17:26:46 +0100489 GSM0808_SCT_FR3 | 0x50, 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f,
Philipp Maier2908dbf2020-06-05 14:16:11 +0200490 GSM0808_SCT_CSD | 0x90, 0xc0, GSM0808_IE_CALL_ID, 0xdd, 0xcc,
491 0xbb, 0xaa };
Philipp Maierc6144a22017-03-29 17:53:43 +0200492
493 struct msgb *msg;
494 struct gsm0808_channel_type ct;
495 uint16_t cic = 0004;
496 struct sockaddr_storage ss;
497 struct sockaddr_in sin;
498 struct gsm0808_speech_codec_list sc_list;
499 uint32_t call_id = 0xAABBCCDD;
500
501 memset(&ct, 0, sizeof(ct));
502 ct.ch_indctr = GSM0808_CHAN_SPEECH;
503 ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
504 ct.perm_spch[0] = GSM0808_PERM_FR3;
505 ct.perm_spch[1] = GSM0808_PERM_HR3;
506 ct.perm_spch_len = 2;
507
508 memset(&sin, 0, sizeof(sin));
509 sin.sin_family = AF_INET;
510 sin.sin_port = htons(1234);
511 inet_aton("192.168.100.23", &sin.sin_addr);
512
513 memset(&ss, 0, sizeof(ss));
514 memcpy(&ss, &sin, sizeof(sin));
515
516 setup_codec_list(&sc_list);
517
518 printf("Testing creating Assignment Request\n");
519 msg = gsm0808_create_ass(&ct, &cic, NULL, NULL, NULL);
520 OSMO_ASSERT(msg);
521 VERIFY(msg, res1, ARRAY_SIZE(res1));
522 msgb_free(msg);
523
524 msg = gsm0808_create_ass(&ct, &cic, &ss, &sc_list, &call_id);
525 OSMO_ASSERT(msg);
526 VERIFY(msg, res2, ARRAY_SIZE(res2));
527 msgb_free(msg);
528}
529
Max52074322018-11-30 10:44:07 +0100530static void test_create_ass2()
531{
532 static const uint8_t res[] = {
533 BSSAP_MSG_BSS_MANAGEMENT,
534 0x45,
535 BSS_MAP_MSG_ASSIGMENT_RQST,
536 GSM0808_IE_CHANNEL_TYPE,
537 0x04, 0x01, 0x0b, 0x91, 0x15, 0x01, 0x00, 0x04,
538 GSM0808_IE_AOIP_TRASP_ADDR,
539 0x06,
540 0xac, 0x0c, 0x65, 0x0d, /* IPv4 */
541 0x02, 0x9a,
542 GSM0808_IE_SPEECH_CODEC_LIST,
543 0x07,
544 GSM0808_SCT_FR3 | 0x50,
545 0xef, 0xcd,
546 GSM0808_SCT_FR2 | 0xa0,
547 0x9f,
548 GSM0808_SCT_CSD | 0x90,
549 0xc0,
550 GSM0808_IE_CALL_ID,
Pau Espin Pedrol1bd495a2021-02-15 16:15:59 +0100551 0xce, 0xfa, 0xad, 0xde, /* CallID */
Max52074322018-11-30 10:44:07 +0100552 0x83, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, 0x45, /* Kc */
553 GSM0808_IE_GLOBAL_CALL_REF, 0x0d, /* GCR, length */
554 0x03, 0x44, 0x44, 0x44, /* GCR, Net ID */
555 0x02, 0xfe, 0xed, /* GCR, Node ID */
556 0x05, 0x41, 0x41, 0x41, 0x41, 0x41, /* GCR, Call ref. ID */
557 GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_BOTH_WAY,
558 GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_CONNECT,
559 GSM0808_IE_LCLS_CORR_NOT_NEEDED,
560 };
561 struct msgb *msg;
562 struct gsm0808_channel_type ct;
563 uint16_t cic = 4;
564 struct sockaddr_storage ss;
565 struct sockaddr_in sin;
566 struct gsm0808_speech_codec_list sc_list;
567 uint32_t call_id = 0xDEADFACE;
Max52074322018-11-30 10:44:07 +0100568 uint8_t Kc[16];
569 struct osmo_lcls lcls = {
570 .config = GSM0808_LCLS_CFG_BOTH_WAY,
571 .control = GSM0808_LCLS_CSC_CONNECT,
Max3b901252019-01-15 14:15:11 +0100572 .gcr = { .net_len = 3, .node = 0xFEED },
573 .gcr_available = true,
Max52074322018-11-30 10:44:07 +0100574 .corr_needed = false
575 };
576
Max3b901252019-01-15 14:15:11 +0100577 memset(lcls.gcr.cr, 'A', 5);
578 memset(lcls.gcr.net, 'D', lcls.gcr.net_len);
Max52074322018-11-30 10:44:07 +0100579 memset(Kc, 'E', 16);
580
581 memset(&ct, 0, sizeof(ct));
582 ct.ch_indctr = GSM0808_CHAN_SPEECH;
583 ct.ch_rate_type = GSM0808_SPEECH_HALF_PREF;
584 ct.perm_spch[0] = GSM0808_PERM_FR2;
585 ct.perm_spch[1] = GSM0808_PERM_HR2;
586 ct.perm_spch_len = 2;
587
588 memset(&sin, 0, sizeof(sin));
589 sin.sin_family = AF_INET;
590 sin.sin_port = htons(666);
591 inet_aton("172.12.101.13", &sin.sin_addr); /* IPv4 */
592
593 memset(&ss, 0, sizeof(ss));
594 memcpy(&ss, &sin, sizeof(sin));
595
596 setup_codec_list(&sc_list);
597
598 printf("Testing creating Assignment Request with Kc and LCLS\n");
599
600 msg = gsm0808_create_ass2(&ct, &cic, &ss, &sc_list, &call_id, Kc, &lcls);
601 if (!msgb_eq_l3_data_print(msg, res, ARRAY_SIZE(res)))
602 abort();
603
604 msgb_free(msg);
605}
606
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100607static void test_create_ass_compl()
608{
609 static const uint8_t res1[] = {
610 0x00, 0x09, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c,
611 0x11, 0x40, 0x22 };
612 static const uint8_t res2[] = {
613 0x00, 0x07, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11};
614 struct msgb *msg;
615
616 printf("Testing creating Assignment Complete\n");
617 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0x22);
618 VERIFY(msg, res1, ARRAY_SIZE(res1));
619 msgb_free(msg);
620
621 msg = gsm0808_create_assignment_completed(0x23, 0x42, 0x11, 0);
622 VERIFY(msg, res2, ARRAY_SIZE(res2));
623 msgb_free(msg);
624}
625
Philipp Maierfa896ab2017-03-27 16:55:32 +0200626static void test_create_ass_compl_aoip()
627{
628 struct sockaddr_storage ss;
629 struct sockaddr_in sin;
630 struct gsm0808_speech_codec sc;
631 struct gsm0808_speech_codec_list sc_list;
632 static const uint8_t res[] =
Max414c8f52019-01-08 14:44:24 +0100633 { 0x00, 0x1f, 0x02, 0x15, 0x23, 0x21, 0x42, 0x2c, 0x11, 0x40, 0x22,
Philipp Maierfa896ab2017-03-27 16:55:32 +0200634 GSM0808_IE_AOIP_TRASP_ADDR, 0x06, 0xc0, 0xa8, 0x64, 0x17, 0x04,
Philipp Maierbb839662017-06-01 17:11:19 +0200635 0xd2, GSM0808_IE_SPEECH_CODEC, 0x01, GSM0808_SCT_HR1 | 0x90,
Philipp Maier7e27b142018-03-22 17:26:46 +0100636 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50, 0xef,
Max414c8f52019-01-08 14:44:24 +0100637 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0,
638 GSM0808_IE_LCLS_BSS_STATUS, GSM0808_LCLS_STS_LOCALLY_SWITCHED };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200639 struct msgb *msg;
640
641 memset(&sin, 0, sizeof(sin));
642 sin.sin_family = AF_INET;
643 sin.sin_port = htons(1234);
644 inet_aton("192.168.100.23", &sin.sin_addr);
645
646 memset(&ss, 0, sizeof(ss));
647 memcpy(&ss, &sin, sizeof(sin));
648
649 memset(&sc, 0, sizeof(sc));
650 sc.fi = true;
651 sc.tf = true;
Philipp Maierbb839662017-06-01 17:11:19 +0200652 sc.type = GSM0808_SCT_HR1;
Philipp Maierfa896ab2017-03-27 16:55:32 +0200653
654 setup_codec_list(&sc_list);
655
656 printf("Testing creating Assignment Complete (AoIP)\n");
Max414c8f52019-01-08 14:44:24 +0100657 msg = gsm0808_create_ass_compl2(0x23, 0x42, 0x11, 0x22,
658 &ss, &sc, &sc_list, GSM0808_LCLS_STS_LOCALLY_SWITCHED);
Philipp Maierfa896ab2017-03-27 16:55:32 +0200659 VERIFY(msg, res, ARRAY_SIZE(res));
660 msgb_free(msg);
661}
662
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100663static void test_create_ass_fail()
664{
665 static const uint8_t res1[] = { 0x00, 0x04, 0x03, 0x04, 0x01, 0x23 };
666 static const uint8_t res2[] = {
667 0x00, 0x06, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02};
668 uint8_t rr_res = 2;
669 struct msgb *msg;
670
671 printf("Testing creating Assignment Failure\n");
672 msg = gsm0808_create_assignment_failure(0x23, NULL);
673 VERIFY(msg, res1, ARRAY_SIZE(res1));
674 msgb_free(msg);
675
676 msg = gsm0808_create_assignment_failure(0x23, &rr_res);
677 VERIFY(msg, res2, ARRAY_SIZE(res2));
678 msgb_free(msg);
679}
680
Philipp Maierfa896ab2017-03-27 16:55:32 +0200681static void test_create_ass_fail_aoip()
682{
683 static const uint8_t res1[] =
684 { 0x00, 0x0d, 0x03, 0x04, 0x01, 0x23, GSM0808_IE_SPEECH_CODEC_LIST,
Philipp Maier7e27b142018-03-22 17:26:46 +0100685 0x07, GSM0808_SCT_FR3 | 0x50, 0xef, 0xcd, GSM0808_SCT_FR2 | 0xa0,
Philipp Maierbb839662017-06-01 17:11:19 +0200686 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200687 static const uint8_t res2[] =
688 { 0x00, 0x0f, 0x03, 0x04, 0x01, 0x23, 0x15, 0x02,
Philipp Maier7e27b142018-03-22 17:26:46 +0100689 GSM0808_IE_SPEECH_CODEC_LIST, 0x07, GSM0808_SCT_FR3 | 0x50, 0xef,
690 0xcd, GSM0808_SCT_FR2 | 0xa0, 0x9f, GSM0808_SCT_CSD | 0x90, 0xc0 };
Philipp Maierfa896ab2017-03-27 16:55:32 +0200691 uint8_t rr_res = 2;
692 struct msgb *msg;
693 struct gsm0808_speech_codec_list sc_list;
694
695 setup_codec_list(&sc_list);
696
697 printf("Testing creating Assignment Failure (AoIP)\n");
698 msg = gsm0808_create_ass_fail(0x23, NULL, &sc_list);
699 VERIFY(msg, res1, ARRAY_SIZE(res1));
700 msgb_free(msg);
701
702 msg = gsm0808_create_ass_fail(0x23, &rr_res, &sc_list);
703 VERIFY(msg, res2, ARRAY_SIZE(res2));
704 msgb_free(msg);
705}
706
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100707static void test_create_clear_rqst()
708{
709 static const uint8_t res[] = { 0x00, 0x04, 0x22, 0x04, 0x01, 0x23 };
710 struct msgb *msg;
711
712 printf("Testing creating Clear Request\n");
713 msg = gsm0808_create_clear_rqst(0x23);
714 VERIFY(msg, res, ARRAY_SIZE(res));
715 msgb_free(msg);
716}
717
Philipp Maier3d48ec02017-03-29 17:37:55 +0200718static void test_create_paging()
719{
720 static const uint8_t res[] =
721 { 0x00, 0x10, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
722 0x21, 0x43, 0x1a, 0x03, 0x05, 0x23, 0x42 };
723 static const uint8_t res2[] =
724 { 0x00, 0x16, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
725 0x21, 0x43, GSM0808_IE_TMSI, 0x04, 0x12, 0x34, 0x56, 0x78, 0x1a,
726 0x03, 0x05, 0x23, 0x42 };
727 static const uint8_t res3[] =
728 { 0x00, 0x18, 0x52, 0x08, 0x08, 0x09, 0x10, 0x10, 0x00, 0x00, 0x00,
729 0x21, 0x43, GSM0808_IE_TMSI, 0x04, 0x12, 0x34, 0x56, 0x78, 0x1a,
730 0x03, 0x05, 0x23, 0x42, GSM0808_IE_CHANNEL_NEEDED,
731 RSL_CHANNEED_TCH_ForH };
732
733 struct msgb *msg;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100734 struct gsm0808_cell_id_list2 cil;
Philipp Maier3d48ec02017-03-29 17:37:55 +0200735 uint32_t tmsi = 0x12345678;
736 uint8_t chan_needed = RSL_CHANNEED_TCH_ForH;
737
738 char imsi[] = "001010000001234";
739
740 cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100741 cil.id_list[0].lac = 0x2342;
Philipp Maier3d48ec02017-03-29 17:37:55 +0200742 cil.id_list_len = 1;
743
744 printf("Testing creating Paging Request\n");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100745 msg = gsm0808_create_paging2(imsi, NULL, &cil, NULL);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200746 VERIFY(msg, res, ARRAY_SIZE(res));
747 msgb_free(msg);
748
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100749 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, NULL);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200750 VERIFY(msg, res2, ARRAY_SIZE(res2));
751 msgb_free(msg);
752
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100753 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, &chan_needed);
Philipp Maier3d48ec02017-03-29 17:37:55 +0200754 VERIFY(msg, res3, ARRAY_SIZE(res3));
755 msgb_free(msg);
756}
757
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100758static void test_create_dtap()
759{
760 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
761 struct msgb *msg, *l3;
762
763 printf("Testing creating DTAP\n");
764 l3 = msgb_alloc_headroom(512, 128, "test");
765 l3->l3h = l3->data;
766 msgb_v_put(l3, 0x23);
767 msgb_v_put(l3, 0x42);
768
769 msg = gsm0808_create_dtap(l3, 0x3);
770 VERIFY(msg, res, ARRAY_SIZE(res));
771 msgb_free(msg);
772 msgb_free(l3);
773}
774
775static void test_prepend_dtap()
776{
777 static const uint8_t res[] = { 0x01, 0x03, 0x02, 0x23, 0x42 };
778 struct msgb *in_msg;
779
780 printf("Testing prepend DTAP\n");
781
782 in_msg = msgb_alloc_headroom(512, 128, "test");
783 msgb_v_put(in_msg, 0x23);
784 msgb_v_put(in_msg, 0x42);
785
786 gsm0808_prepend_dtap_header(in_msg, 0x3);
787 in_msg->l3h = in_msg->data;
788 VERIFY(in_msg, res, ARRAY_SIZE(res));
789 msgb_free(in_msg);
790}
791
Max47022152018-12-19 18:51:00 +0100792static void test_enc_dec_lcls()
Max969fb2e2018-12-10 11:01:10 +0100793{
794 static const uint8_t res[] = {
795 GSM0808_IE_GLOBAL_CALL_REF,
796 0x0d, /* GCR length */
797 0x03, /* .net_len */
798 0xf1, 0xf2, 0xf3, /* .net */
799 0x02, /* .node length */
800 0xde, 0xad, /* .node */
801 0x05, /* length of Call. Ref. */
802 0x41, 0x42, 0x43, 0x44, 0x45 /* .cr - Call. Ref. */
803 };
804 uint8_t len;
805 struct msgb *msg;
Max969fb2e2018-12-10 11:01:10 +0100806 int rc;
807 struct tlv_parsed tp;
Max3b901252019-01-15 14:15:11 +0100808 struct osmo_lcls *lcls_out, lcls_in = {
809 .gcr = {
810 .net_len = 3,
811 .net = { 0xf1, 0xf2, 0xf3 },
812 .node = 0xDEAD,
813 .cr = { 0x41, 0x42, 0x43, 0x44, 0x45 },
814 },
815 .gcr_available = true,
Max47022152018-12-19 18:51:00 +0100816 .config = GSM0808_LCLS_CFG_NA,
817 .control = GSM0808_LCLS_CSC_NA,
818 .corr_needed = true,
819 };
820
821 msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, "LCLS IE");
Max969fb2e2018-12-10 11:01:10 +0100822 if (!msg)
823 return;
824
Max3b901252019-01-15 14:15:11 +0100825 lcls_out = talloc_zero(msg, struct osmo_lcls);
826 if (!lcls_out)
827 return;
828
Max47022152018-12-19 18:51:00 +0100829 len = gsm0808_enc_lcls(msg, &lcls_in);
Max969fb2e2018-12-10 11:01:10 +0100830 printf("Testing Global Call Reference IE encoder...\n\t%d bytes added: %s\n",
831 len, len == ARRAY_SIZE(res) ? "OK" : "FAIL");
832
833 if (!msgb_eq_data_print(msg, res, ARRAY_SIZE(res)))
834 abort();
835
836 rc = osmo_bssap_tlv_parse(&tp, msgb_data(msg), msgb_length(msg));
837 if (rc < 0) {
838 printf("parsing failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg));
839 abort();
840 }
841
Max3b901252019-01-15 14:15:11 +0100842 rc = gsm0808_dec_lcls(lcls_out, &tp);
Max969fb2e2018-12-10 11:01:10 +0100843 if (rc < 0) {
844 printf("decoding failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg));
845 abort();
846 }
847
Max3b901252019-01-15 14:15:11 +0100848 if (lcls_out->config != lcls_in.config) {
Max4fd64e52019-01-14 19:27:44 +0100849 printf("LCLS Config parsed wrong: %s != %s\n",
Max3b901252019-01-15 14:15:11 +0100850 gsm0808_lcls_config_name(lcls_out->config), gsm0808_lcls_config_name(lcls_in.config));
Max4fd64e52019-01-14 19:27:44 +0100851 abort();
852 }
853
Max3b901252019-01-15 14:15:11 +0100854 if (lcls_out->control != lcls_in.control) {
Max4fd64e52019-01-14 19:27:44 +0100855 printf("LCLS Control parsed wrong: %s != %s\n",
Max3b901252019-01-15 14:15:11 +0100856 gsm0808_lcls_control_name(lcls_out->control), gsm0808_lcls_control_name(lcls_in.control));
Max4fd64e52019-01-14 19:27:44 +0100857 abort();
858 }
859
Max3b901252019-01-15 14:15:11 +0100860 if (!osmo_gcr_eq(&lcls_out->gcr, &lcls_in.gcr)) {
Max5ec0cf52019-01-15 16:37:09 +0100861 printf("GCR parsed wrong:\n\t%s\n\t%s\n", osmo_gcr_dump(lcls_out), osmo_gcr_dump(&lcls_in));
Max1bec3902019-01-14 19:31:42 +0100862 abort();
863 }
Max969fb2e2018-12-10 11:01:10 +0100864
Max5ec0cf52019-01-15 16:37:09 +0100865 printf("\tdecoded %d bytes: %s:\n%s\n", rc, rc == len ? "OK" : "FAIL", osmo_lcls_dump(lcls_out));
866 printf("\t%s\n", osmo_gcr_dump(lcls_out));
Max969fb2e2018-12-10 11:01:10 +0100867 msgb_free(msg);
868}
869
Philipp Maier22401432017-03-24 17:59:26 +0100870static void test_enc_dec_aoip_trasp_addr_v4()
871{
872 struct sockaddr_storage enc_addr;
873 struct sockaddr_storage dec_addr;
874 struct sockaddr_in enc_addr_in;
875 struct msgb *msg;
876 uint8_t rc_enc;
877 int rc_dec;
878
879 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
880 enc_addr_in.sin_family = AF_INET;
881 enc_addr_in.sin_port = htons(1234);
882 inet_aton("255.0.255.255", &enc_addr_in.sin_addr);
883
884 memset(&enc_addr, 0, sizeof(enc_addr));
885 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
886
887 msg = msgb_alloc(1024, "output buffer");
888 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
889 OSMO_ASSERT(rc_enc == 8);
890 rc_dec =
891 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
892 OSMO_ASSERT(rc_dec == 6);
893 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
894
895 msgb_free(msg);
896}
897
898static void test_enc_dec_aoip_trasp_addr_v6()
899{
900 struct sockaddr_storage enc_addr;
901 struct sockaddr_storage dec_addr;
902 struct sockaddr_in6 enc_addr_in;
903 struct msgb *msg;
904 uint8_t rc_enc;
905 int rc_dec;
906
907 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
908 enc_addr_in.sin6_family = AF_INET6;
909 enc_addr_in.sin6_port = htons(4567);
910 inet_pton(AF_INET6, "2001:0db8:85a3:08d3:1319:8a2e:0370:7344",
911 &enc_addr_in.sin6_addr);
912
913 memset(&enc_addr, 0, sizeof(enc_addr));
914 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
915
916 msg = msgb_alloc(1024, "output buffer");
917 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
918 OSMO_ASSERT(rc_enc == 20);
919 rc_dec =
920 gsm0808_dec_aoip_trasp_addr(&dec_addr, msg->data + 2, msg->len - 2);
921 OSMO_ASSERT(rc_dec == 18);
922 OSMO_ASSERT(memcmp(&enc_addr, &dec_addr, sizeof(enc_addr)) == 0);
923
924 msgb_free(msg);
925}
926
Philipp Maier6f725d62017-03-24 18:03:17 +0100927static void test_gsm0808_enc_dec_speech_codec()
928{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200929 struct gsm0808_speech_codec enc_sc = {
930 .pi = true,
931 .tf = true,
932 .type = GSM0808_SCT_FR2,
933 };
934 struct gsm0808_speech_codec dec_sc = {};
Philipp Maier6f725d62017-03-24 18:03:17 +0100935 struct msgb *msg;
936 uint8_t rc_enc;
937 int rc_dec;
938
Philipp Maier6f725d62017-03-24 18:03:17 +0100939 msg = msgb_alloc(1024, "output buffer");
940 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
941 OSMO_ASSERT(rc_enc == 3);
942
943 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
944 OSMO_ASSERT(rc_dec == 1);
945
946 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
947
948 msgb_free(msg);
949}
950
951
Philipp Maierbb839662017-06-01 17:11:19 +0200952static void test_gsm0808_enc_dec_speech_codec_with_cfg()
953{
Neels Hofmeyrc62c9342018-04-15 23:31:47 +0200954 struct gsm0808_speech_codec enc_sc = {
955 .pi = true,
956 .tf = true,
957 .type = GSM0808_SCT_FR3,
958 .cfg = 0xabcd,
959 };
960 struct gsm0808_speech_codec dec_sc = {};
Philipp Maierbb839662017-06-01 17:11:19 +0200961 struct msgb *msg;
962 uint8_t rc_enc;
963 int rc_dec;
964
Philipp Maierbb839662017-06-01 17:11:19 +0200965 msg = msgb_alloc(1024, "output buffer");
966 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
967 OSMO_ASSERT(rc_enc == 5);
968
969 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
970 OSMO_ASSERT(rc_dec == 3);
971
972 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
973
974 msgb_free(msg);
975}
976
Philipp Maier6f725d62017-03-24 18:03:17 +0100977static void test_gsm0808_enc_dec_speech_codec_ext_with_cfg()
978{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200979 struct gsm0808_speech_codec enc_sc = {
980 .pi = true,
981 .tf = true,
982 .type = GSM0808_SCT_CSD,
983 .cfg = 0xc0,
984 };
985 struct gsm0808_speech_codec dec_sc = {};
Philipp Maier6f725d62017-03-24 18:03:17 +0100986 struct msgb *msg;
987 uint8_t rc_enc;
988 int rc_dec;
989
Philipp Maier6f725d62017-03-24 18:03:17 +0100990 msg = msgb_alloc(1024, "output buffer");
991 rc_enc = gsm0808_enc_speech_codec(msg, &enc_sc);
Philipp Maierbb839662017-06-01 17:11:19 +0200992 OSMO_ASSERT(rc_enc == 5);
Philipp Maier6f725d62017-03-24 18:03:17 +0100993
994 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
Philipp Maierbb839662017-06-01 17:11:19 +0200995 OSMO_ASSERT(rc_dec == 3);
Philipp Maier6f725d62017-03-24 18:03:17 +0100996
997 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
998
999 msgb_free(msg);
1000}
1001
1002static void test_gsm0808_enc_dec_speech_codec_list()
1003{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001004 struct gsm0808_speech_codec_list enc_scl = {
1005 .codec = {
1006 {
1007 .pi = true,
1008 .tf = true,
1009 .type = GSM0808_SCT_FR3,
1010 .cfg = 0xcdef,
1011 },
1012
1013 {
1014 .fi = true,
1015 .pt = true,
1016 .type = GSM0808_SCT_FR2,
1017 },
1018
1019 {
1020 .fi = true,
1021 .tf = true,
1022 .type = GSM0808_SCT_CSD,
1023 .cfg = 0xc0,
1024 },
1025 },
1026 .len = 3,
1027 };
1028 struct gsm0808_speech_codec_list dec_scl = {};
Philipp Maier6f725d62017-03-24 18:03:17 +01001029 struct msgb *msg;
1030 uint8_t rc_enc;
1031 int rc_dec;
1032
Philipp Maier6f725d62017-03-24 18:03:17 +01001033 msg = msgb_alloc(1024, "output buffer");
1034 rc_enc = gsm0808_enc_speech_codec_list(msg, &enc_scl);
1035 OSMO_ASSERT(rc_enc == 9);
1036
1037 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
1038 OSMO_ASSERT(rc_dec == 7);
1039
1040 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
1041
1042 msgb_free(msg);
1043}
1044
Philipp Maierf6c369f2018-10-16 15:24:47 +02001045static void test_gsm0808_enc_dec_empty_speech_codec_list()
1046{
1047 struct gsm0808_speech_codec_list enc_scl = {
1048 .len = 0,
1049 };
1050 struct gsm0808_speech_codec_list dec_scl = {};
1051 struct msgb *msg;
1052 uint8_t rc_enc;
1053 int rc_dec;
1054
1055 msg = msgb_alloc(1024, "output buffer");
1056 rc_enc = gsm0808_enc_speech_codec_list(msg, &enc_scl);
1057 OSMO_ASSERT(rc_enc == 2);
1058
1059 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
1060 OSMO_ASSERT(rc_dec == 0);
1061
1062 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
1063
1064 msgb_free(msg);
1065}
1066
Philipp Maiere0c65302017-03-28 17:05:40 +02001067static void test_gsm0808_enc_dec_channel_type()
1068{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001069 struct gsm0808_channel_type enc_ct = {
1070 .ch_indctr = GSM0808_CHAN_SPEECH,
1071 .ch_rate_type = GSM0808_SPEECH_HALF_PREF,
1072 .perm_spch = { GSM0808_PERM_FR3, GSM0808_PERM_HR3 },
1073 .perm_spch_len = 2,
1074 };
1075 struct gsm0808_channel_type dec_ct = {};
Philipp Maiere0c65302017-03-28 17:05:40 +02001076 struct msgb *msg;
1077 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
1078 0x04, 0x01, 0x0b, 0xa1, 0x25
1079 };
1080 uint8_t rc_enc;
1081 int rc_dec;
1082
Philipp Maiere0c65302017-03-28 17:05:40 +02001083 msg = msgb_alloc(1024, "output buffer");
1084 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
1085 OSMO_ASSERT(rc_enc == 6);
1086 OSMO_ASSERT(memcmp(ct_enc_expected, msg->data, msg->len) == 0);
1087
1088 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
1089 OSMO_ASSERT(rc_dec == 4);
Pau Espin Pedrol2605ffc2019-07-29 16:44:22 +02001090 OSMO_ASSERT(enc_ct.ch_indctr == dec_ct.ch_indctr);
1091 OSMO_ASSERT(enc_ct.ch_rate_type == dec_ct.ch_rate_type);
1092 OSMO_ASSERT(enc_ct.perm_spch_len == dec_ct.perm_spch_len);
1093 OSMO_ASSERT(memcmp(&enc_ct.perm_spch[0], &dec_ct.perm_spch[0], enc_ct.perm_spch_len) == 0);
Philipp Maiere0c65302017-03-28 17:05:40 +02001094
1095 msgb_free(msg);
1096}
1097
Philipp Maier14e76b92017-03-28 18:36:52 +02001098static void test_gsm0808_enc_dec_encrypt_info()
1099{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001100 struct gsm0808_encrypt_info enc_ei = {
1101 .perm_algo = { GSM0808_ALG_ID_A5_0, GSM0808_ALG_ID_A5_1 },
1102 .perm_algo_len = 2,
1103 .key = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42, },
1104 .key_len = 8,
1105 };
1106 struct gsm0808_encrypt_info dec_ei = {};
Philipp Maier14e76b92017-03-28 18:36:52 +02001107 struct msgb *msg;
1108 uint8_t ei_enc_expected[] =
1109 { GSM0808_IE_ENCRYPTION_INFORMATION, 0x09, 0x03, 0xaa, 0xbb,
1110 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42
1111 };
1112 uint8_t rc_enc;
1113 int rc_dec;
1114
Philipp Maier14e76b92017-03-28 18:36:52 +02001115 msg = msgb_alloc(1024, "output buffer");
1116 rc_enc = gsm0808_enc_encrypt_info(msg, &enc_ei);
1117 OSMO_ASSERT(rc_enc == 11);
1118 OSMO_ASSERT(memcmp(ei_enc_expected, msg->data, msg->len) == 0);
1119
1120 rc_dec = gsm0808_dec_encrypt_info(&dec_ei, msg->data + 2, msg->len - 2);
1121 OSMO_ASSERT(rc_dec == 9);
1122
1123 OSMO_ASSERT(memcmp(&enc_ei, &dec_ei, sizeof(enc_ei)) == 0);
1124
1125 msgb_free(msg);
1126}
1127
Harald Weltea87526d2022-02-15 20:06:16 +01001128static void test_gsm0808_dec_cell_id_list_srvcc()
1129{
1130 /* taken from a pcap file of a real-world 3rd party MSC (SYS#5838) */
1131 const uint8_t enc_cil[] = { 0x0b, 0x2, 0xf2, 0x10, 0x4e, 0x20, 0x15, 0xbe};
1132 struct gsm0808_cell_id_list2 dec_cil;
1133 int rc;
1134
1135 rc = gsm0808_dec_cell_id_list2(&dec_cil, enc_cil, sizeof(enc_cil));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001136 OSMO_ASSERT(rc == sizeof(enc_cil));
1137 OSMO_ASSERT(dec_cil.id_discr = CELL_IDENT_SAI);
1138 OSMO_ASSERT(dec_cil.id_list_len = 1);
Harald Weltea87526d2022-02-15 20:06:16 +01001139}
1140
Philipp Maier783047e2017-03-29 11:35:50 +02001141static void test_gsm0808_enc_dec_cell_id_list_lac()
1142{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001143 struct gsm0808_cell_id_list2 enc_cil;
1144 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001145 struct msgb *msg;
1146 uint8_t rc_enc;
1147 int rc_dec;
1148
1149 memset(&enc_cil, 0, sizeof(enc_cil));
1150 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001151 enc_cil.id_list[0].lac = 0x0124;
Neels Hofmeyrdb2fa4e2018-04-13 04:11:20 +02001152 enc_cil.id_list[1].lac = 0xABCD;
1153 enc_cil.id_list[2].lac = 0x5678;
Philipp Maier783047e2017-03-29 11:35:50 +02001154 enc_cil.id_list_len = 3;
1155
1156 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001157 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Neels Hofmeyrdb2fa4e2018-04-13 04:11:20 +02001158 EXPECT_ENCODED("1a 07 05 01 24 ab cd 56 78");
Philipp Maier783047e2017-03-29 11:35:50 +02001159
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001160 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001161 OSMO_ASSERT(rc_dec == 7);
1162
1163 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1164
1165 msgb_free(msg);
1166}
1167
1168static void test_gsm0808_enc_dec_cell_id_list_single_lac()
1169{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001170 struct gsm0808_cell_id_list2 enc_cil;
1171 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001172 struct msgb *msg;
1173 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x03,
1174 0x05, 0x23, 0x42
1175 };
1176 uint8_t rc_enc;
1177 int rc_dec;
1178
1179 memset(&enc_cil, 0, sizeof(enc_cil));
1180 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001181 enc_cil.id_list[0].lac = 0x2342;
Philipp Maier783047e2017-03-29 11:35:50 +02001182 enc_cil.id_list_len = 1;
1183
1184 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001185 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +02001186 OSMO_ASSERT(rc_enc == 5);
1187 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1188
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001189 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001190 OSMO_ASSERT(rc_dec == 3);
1191
1192 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1193
1194 msgb_free(msg);
1195}
1196
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001197static void test_gsm0808_enc_dec_cell_id_list_multi_lac()
1198{
1199 struct gsm0808_cell_id_list2 enc_cil;
1200 struct gsm0808_cell_id_list2 dec_cil;
1201 struct msgb *msg;
1202 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x0b, 0x05,
1203 0x23, 0x42,
1204 0x24, 0x43,
1205 0x25, 0x44,
1206 0x26, 0x45,
1207 0x27, 0x46
1208 };
1209 uint8_t rc_enc;
1210 int rc_dec;
1211
1212 memset(&enc_cil, 0, sizeof(enc_cil));
1213 enc_cil.id_discr = CELL_IDENT_LAC;
1214 enc_cil.id_list[0].lac = 0x2342;
1215 enc_cil.id_list[1].lac = 0x2443;
1216 enc_cil.id_list[2].lac = 0x2544;
1217 enc_cil.id_list[3].lac = 0x2645;
1218 enc_cil.id_list[4].lac = 0x2746;
1219 enc_cil.id_list_len = 5;
1220
1221 msg = msgb_alloc(1024, "output buffer");
1222 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1223 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1224 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1225
1226 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1227 OSMO_ASSERT(rc_dec == msg->len - 2);
1228 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1229
1230 msgb_free(msg);
1231}
1232
Philipp Maier783047e2017-03-29 11:35:50 +02001233static void test_gsm0808_enc_dec_cell_id_list_bss()
1234{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001235 struct gsm0808_cell_id_list2 enc_cil;
1236 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001237 struct msgb *msg;
1238 uint8_t rc_enc;
1239 int rc_dec;
1240
1241 memset(&enc_cil, 0, sizeof(enc_cil));
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001242 enc_cil.id_discr = CELL_IDENT_BSS;
Philipp Maier783047e2017-03-29 11:35:50 +02001243
1244 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001245 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +02001246 OSMO_ASSERT(rc_enc == 3);
1247
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001248 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001249 OSMO_ASSERT(rc_dec == 1);
1250
1251 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1252
1253 msgb_free(msg);
1254}
1255
Stefan Sperling23381452018-03-15 19:38:15 +01001256static void test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac()
1257{
1258 struct gsm0808_cell_id_list2 enc_cil;
1259 struct gsm0808_cell_id_list2 dec_cil;
1260 struct osmo_location_area_id id;
1261 struct msgb *msg;
1262 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x10, 0x04,
1263 0x92, 0x61, 0x54, 0x23, 0x42,
1264 0x92, 0x72, 0x54, 0x24, 0x43,
1265 0x92, 0x83, 0x54, 0x25, 0x44
1266 };
1267 uint8_t rc_enc;
1268 int rc_dec, i;
1269
1270 memset(&enc_cil, 0, sizeof(enc_cil));
1271 enc_cil.id_discr = CELL_IDENT_LAI_AND_LAC;
1272
1273 id.plmn.mcc = 0x123;
1274 osmo_mnc_from_str("456", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1275 id.lac = 0x2342;
1276 memcpy(&enc_cil.id_list[0].lai_and_lac, &id, sizeof(id));
1277
1278 id.plmn.mcc = 0x124;
1279 osmo_mnc_from_str("457", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1280 id.lac = 0x2443;
1281 memcpy(&enc_cil.id_list[1].lai_and_lac, &id, sizeof(id));
1282
1283 id.plmn.mcc = 0x125;
1284 osmo_mnc_from_str("458", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1285 id.lac = 0x2544;
1286 memcpy(&enc_cil.id_list[2].lai_and_lac, &id, sizeof(id));
1287
1288 enc_cil.id_list_len = 3;
1289
1290 msg = msgb_alloc(1024, "output buffer");
1291 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1292 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1293 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1294
1295 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1296 OSMO_ASSERT(rc_dec == msg->len - 2);
1297
1298 OSMO_ASSERT(dec_cil.id_list_len == 3);
1299 /* Check MAXLEN elements to ensure everything has been initialized. */
1300 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1301 struct osmo_location_area_id *enc_id;
1302 struct osmo_location_area_id *dec_id;
1303 enc_id = &enc_cil.id_list[i].lai_and_lac;
1304 dec_id = &dec_cil.id_list[i].lai_and_lac;
1305 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->plmn, &dec_id->plmn) == 0);
1306 OSMO_ASSERT(enc_id->lac == dec_id->lac);
1307 }
1308
1309 msgb_free(msg);
1310}
1311
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001312static void test_gsm0808_enc_dec_cell_id_list_multi_ci()
1313{
1314 struct gsm0808_cell_id_list2 enc_cil;
1315 struct gsm0808_cell_id_list2 dec_cil;
1316 struct msgb *msg;
1317 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x09, 0x02,
1318 0x00, 0x01,
1319 0x00, 0x02,
1320 0x00, 0x77,
1321 0x01, 0xff,
1322 };
1323 uint8_t rc_enc;
1324 int rc_dec;
1325
1326 memset(&enc_cil, 0, sizeof(enc_cil));
1327 enc_cil.id_discr = CELL_IDENT_CI;
1328 enc_cil.id_list[0].ci = 1;
1329 enc_cil.id_list[1].ci = 2;
1330 enc_cil.id_list[2].ci = 119;
1331 enc_cil.id_list[3].ci = 511;
1332 enc_cil.id_list_len = 4;
1333
1334 msg = msgb_alloc(1024, "output buffer");
1335 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1336 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1337 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1338
1339 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1340 OSMO_ASSERT(rc_dec == msg->len - 2);
1341 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1342
1343 msgb_free(msg);
1344}
1345
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001346static void test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci()
1347{
1348 struct gsm0808_cell_id_list2 enc_cil;
1349 struct gsm0808_cell_id_list2 dec_cil;
1350 struct msgb *msg;
1351 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x15, 0x01,
1352 0x23, 0x42, 0x00, 0x01,
1353 0x24, 0x43, 0x00, 0x02,
1354 0x25, 0x44, 0x00, 0x77,
1355 0x26, 0x45, 0x01, 0xff,
1356 0x27, 0x46, 0x02, 0xfe,
1357 };
1358 uint8_t rc_enc;
1359 int rc_dec;
1360
1361 memset(&enc_cil, 0, sizeof(enc_cil));
1362 enc_cil.id_discr = CELL_IDENT_LAC_AND_CI;
1363 enc_cil.id_list[0].lac_and_ci.lac = 0x2342;
1364 enc_cil.id_list[0].lac_and_ci.ci = 1;
1365 enc_cil.id_list[1].lac_and_ci.lac = 0x2443;
1366 enc_cil.id_list[1].lac_and_ci.ci = 2;
1367 enc_cil.id_list[2].lac_and_ci.lac = 0x2544;
1368 enc_cil.id_list[2].lac_and_ci.ci = 119;
1369 enc_cil.id_list[3].lac_and_ci.lac = 0x2645;
1370 enc_cil.id_list[3].lac_and_ci.ci = 511;
1371 enc_cil.id_list[4].lac_and_ci.lac = 0x2746;
1372 enc_cil.id_list[4].lac_and_ci.ci = 766;
1373 enc_cil.id_list_len = 5;
1374
1375 msg = msgb_alloc(1024, "output buffer");
1376 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1377 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1378 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1379
1380 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1381 OSMO_ASSERT(rc_dec == msg->len - 2);
1382 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1383
1384 msgb_free(msg);
1385}
1386
Stefan Sperling483f3862018-03-16 12:21:26 +01001387static void test_gsm0808_enc_dec_cell_id_list_multi_global()
1388{
1389 struct gsm0808_cell_id_list2 enc_cil;
1390 struct gsm0808_cell_id_list2 dec_cil;
1391 struct msgb *msg;
1392 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x16, 0x00,
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001393 0x21, 0x63, 0x54, 0x23, 0x42, 0x00, 0x1,
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001394 0x21, 0xf4, 0x75, 0x24, 0x43, 0x00, 0x2,
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +01001395 0x21, 0x75, 0x00, 0x25, 0x44, 0x00, 0x77
Stefan Sperling483f3862018-03-16 12:21:26 +01001396 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001397 uint8_t rc_enc;
1398 int rc_dec, i;
1399
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001400 enc_cil = (struct gsm0808_cell_id_list2){
1401 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1402 .id_list_len = 3,
1403 .id_list = {
1404 {
1405 .global = {
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001406 .lai = { .plmn = { .mcc = 123, .mnc = 456 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001407 .lac = 0x2342 },
1408 .cell_identity = 1,
1409 }
1410 },
1411 {
1412 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001413 .lai = { .plmn = { .mcc = 124, .mnc = 57 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001414 .lac = 0x2443 },
1415 .cell_identity = 2,
1416 }
1417 },
1418 {
1419 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001420 .lai = { .plmn = { .mcc = 125, .mnc = 7,
1421 .mnc_3_digits = true },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001422 .lac = 0x2544 },
1423 .cell_identity = 119,
1424 }
1425 },
1426 }
1427 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001428
1429 msg = msgb_alloc(1024, "output buffer");
1430 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1431 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001432 if (memcmp(cil_enc_expected, msg->data, msg->len)) {
1433 printf(" got: %s\n", osmo_hexdump(msg->data, msg->len));
1434 printf("expect: %s\n", osmo_hexdump(cil_enc_expected, sizeof(cil_enc_expected)));
1435 OSMO_ASSERT(false);
1436 }
Stefan Sperling483f3862018-03-16 12:21:26 +01001437
1438 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1439 OSMO_ASSERT(rc_dec == msg->len - 2);
1440
1441 /* Check MAXLEN elements to ensure everything has been initialized. */
1442 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1443 struct osmo_cell_global_id *enc_id;
1444 struct osmo_cell_global_id *dec_id;
1445 enc_id = &enc_cil.id_list[i].global;
1446 dec_id = &dec_cil.id_list[i].global;
1447 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->lai.plmn, &dec_id->lai.plmn) == 0);
1448 OSMO_ASSERT(enc_id->lai.lac == dec_id->lai.lac);
1449 OSMO_ASSERT(enc_id->cell_identity == dec_id->cell_identity);
1450 }
1451
1452 msgb_free(msg);
1453}
1454
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001455static void print_cil(const struct gsm0808_cell_id_list2 *cil)
1456{
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001457 printf(" cell_id_list == %s\n", gsm0808_cell_id_list_name(cil));
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001458}
1459
1460void test_cell_id_list_add() {
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001461 size_t zu;
1462
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001463 const struct gsm0808_cell_id_list2 cgi1 = {
1464 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1465 .id_list_len = 1,
1466 .id_list = {
1467 {
1468 .global = {
1469 .lai = {
1470 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = false },
1471 .lac = 3,
1472 },
1473 .cell_identity = 4,
1474 }
1475 },
1476 },
1477 };
1478
1479 const struct gsm0808_cell_id_list2 cgi2 = {
1480 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1481 .id_list_len = 2,
1482 .id_list = {
1483 {
1484 .global = {
1485 .lai = {
1486 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = true },
1487 .lac = 3,
1488 },
1489 .cell_identity = 4,
1490 }
1491 },
1492 {
1493 .global = {
1494 .lai = {
1495 .plmn = { .mcc = 5, .mnc = 6, .mnc_3_digits = true },
1496 .lac = 7,
1497 },
1498 .cell_identity = 8,
1499 }
1500 },
1501 },
1502 };
1503
1504 const struct gsm0808_cell_id_list2 cgi2a = {
1505 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1506 .id_list_len = 2,
1507 .id_list = {
1508 {
1509 .global = cgi2.id_list[0].global
1510 },
1511 {
1512 .global = {
1513 .lai = {
1514 .plmn = { .mcc = 9, .mnc = 10, .mnc_3_digits = true },
1515 .lac = 11,
1516 },
1517 .cell_identity = 12,
1518 }
1519 },
1520 },
1521 };
1522
1523 const struct gsm0808_cell_id_list2 cgi3 = {
1524 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1525 .id_list_len = 2,
1526 .id_list = {
1527 {
1528 .global = {
1529 .lai = {
1530 .plmn = { .mcc = 13, .mnc = 14, .mnc_3_digits = true },
1531 .lac = 15,
1532 },
1533 .cell_identity = 16,
1534 }
1535 },
1536 {
1537 .global = {
1538 .lai = {
1539 .plmn = { .mcc = 16, .mnc = 17, .mnc_3_digits = true },
1540 .lac = 18,
1541 },
1542 .cell_identity = 19,
1543 }
1544 },
1545 },
1546 };
1547
1548
1549 const struct gsm0808_cell_id_list2 lac1 = {
1550 .id_discr = CELL_IDENT_LAC,
1551 .id_list_len = 1,
1552 .id_list = {
1553 {
1554 .lac = 123
1555 },
1556 },
1557 };
1558
1559 const struct gsm0808_cell_id_list2 lac2 = {
1560 .id_discr = CELL_IDENT_LAC,
1561 .id_list_len = 2,
1562 .id_list = {
1563 {
1564 .lac = 456
1565 },
1566 {
1567 .lac = 789
1568 },
1569 },
1570 };
1571
1572 struct gsm0808_cell_id_list2 cil = {};
1573
1574 printf("------- %s\n", __func__);
1575
1576 print_cil(&cil);
1577
1578#define ADD_QUIET(other_cil, expect_rc) do { \
1579 int rc = gsm0808_cell_id_list_add(&cil, &other_cil); \
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001580 printf("gsm0808_cell_id_list_add(&cil, &" #other_cil ") --> rc = %d\n", rc); \
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001581 OSMO_ASSERT(rc == expect_rc); \
1582 } while(0)
1583
1584#define ADD(other_cil, expect_rc) ADD_QUIET(other_cil, expect_rc); print_cil(&cil)
1585
1586 ADD(lac1, 1);
1587 ADD(lac1, 0);
1588 ADD(lac2, 2);
1589 ADD(lac2, 0);
1590 ADD(cil, 0);
1591 ADD(cgi1, -EINVAL);
1592
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001593 printf("* can't add to BSS list\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001594 cil.id_list_len = 0;
1595 cil.id_discr = CELL_IDENT_BSS;
1596 print_cil(&cil);
1597 ADD(lac1, -EINVAL);
1598
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001599 printf("* other types (including NO_CELL) take on new type iff empty\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001600 cil.id_list_len = 0;
1601 cil.id_discr = CELL_IDENT_NO_CELL;
1602 print_cil(&cil);
1603 ADD(cgi1, 1);
1604 ADD(cgi1, 0);
1605 ADD(cgi2, 2);
1606 ADD(cgi2, 0);
1607
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001608 printf("* test gsm0808_cell_id_list_name_buf()'s return val\n");
1609 zu = strlen(gsm0808_cell_id_list_name(&cil));
1610 printf(" strlen(gsm0808_cell_id_list_name(cil)) == %zu\n", zu);
1611 zu ++;
1612 while (1) {
1613 char buf[128] = "?";
1614 int rc;
1615 OSMO_ASSERT(zu < sizeof(buf));
1616 buf[zu] = '#';
1617 rc = gsm0808_cell_id_list_name_buf(buf, zu, &cil);
1618 printf(" gsm0808_cell_id_list_name_buf(buf, %zu, cil)) == %d \"%s\"\n",
1619 zu, rc, buf);
1620 OSMO_ASSERT(buf[zu] == '#');
1621 if (!zu)
1622 break;
1623 zu /= 2;
1624 }
1625
1626 printf("* list-full behavior\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001627 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001628 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001629 ADD_QUIET(cgi2a, 1);
1630 printf("cil.id_list_len = %u\n", cil.id_list_len);
1631
1632 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001633 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001634 ADD_QUIET(cgi3, -ENOSPC);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001635 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001636 ADD_QUIET(cgi2a, -ENOSPC);
1637 printf("cil.id_list_len = %u\n", cil.id_list_len);
1638
1639 printf("------- %s done\n", __func__);
1640}
1641
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001642static void test_gsm0808_enc_dec_cell_id_lac()
1643{
1644 struct gsm0808_cell_id enc_ci = {
1645 .id_discr = CELL_IDENT_LAC,
1646 .id.lac = 0x0124,
1647 };
1648 struct gsm0808_cell_id dec_ci;
1649 struct msgb *msg;
1650 uint8_t rc_enc;
1651 int rc_dec;
1652
1653 memset(&dec_ci, 0xa5, sizeof(dec_ci));
1654
1655 msg = msgb_alloc(1024, "output buffer");
1656 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1657 EXPECT_ENCODED("05 03 05 01 24");
1658
1659 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1660 OSMO_ASSERT(rc_dec == 3);
1661
1662 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1663 && enc_ci.id.lac == dec_ci.id.lac);
1664
1665 msgb_free(msg);
1666}
1667
1668static void test_gsm0808_enc_dec_cell_id_bss()
1669{
1670 struct gsm0808_cell_id enc_ci = {
1671 .id_discr = CELL_IDENT_BSS,
1672 };
1673 struct gsm0808_cell_id dec_ci;
1674 struct msgb *msg;
1675 uint8_t rc_enc;
1676 int rc_dec;
1677
1678 msg = msgb_alloc(1024, "output buffer");
1679 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1680 EXPECT_ENCODED("05 01 06");
1681
1682 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1683 OSMO_ASSERT(rc_dec == 1);
1684
1685 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr);
1686
1687 msgb_free(msg);
1688}
1689
1690static void test_gsm0808_enc_dec_cell_id_no_cell()
1691{
1692 struct gsm0808_cell_id enc_ci = {
1693 .id_discr = CELL_IDENT_NO_CELL,
1694 };
1695 struct gsm0808_cell_id dec_ci;
1696 struct msgb *msg;
1697 uint8_t rc_enc;
1698 int rc_dec;
1699
1700 msg = msgb_alloc(1024, "output buffer");
1701 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1702 EXPECT_ENCODED("05 01 03");
1703
1704 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1705 OSMO_ASSERT(rc_dec == 1);
1706
1707 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr);
1708
1709 msgb_free(msg);
1710}
1711
1712static void test_gsm0808_enc_dec_cell_id_lai_and_lac()
1713{
1714 struct gsm0808_cell_id enc_ci = {
1715 .id_discr = CELL_IDENT_LAI_AND_LAC,
1716 .id.lai_and_lac = {
1717 .plmn = {
1718 .mcc = 123,
1719 .mnc = 456,
1720 },
1721 .lac = 0x2342,
1722 },
1723 };
1724 struct gsm0808_cell_id dec_ci;
1725 struct msgb *msg;
1726 uint8_t rc_enc;
1727 int rc_dec;
1728
1729 msg = msgb_alloc(1024, "output buffer");
1730 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1731 EXPECT_ENCODED("05 06 04 21 63 54 23 42");
1732
1733 memset(&dec_ci, 0xa5, sizeof(dec_ci));
1734 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1735 OSMO_ASSERT(rc_dec == msg->len - 2);
1736
1737 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1738 && osmo_plmn_cmp(&enc_ci.id.lai_and_lac.plmn, &dec_ci.id.lai_and_lac.plmn) == 0
1739 && enc_ci.id.lai_and_lac.lac == dec_ci.id.lai_and_lac.lac);
1740 msgb_free(msg);
1741}
1742
1743static void test_gsm0808_enc_dec_cell_id_ci()
1744{
1745 struct gsm0808_cell_id enc_ci = {
1746 .id_discr = CELL_IDENT_CI,
1747 .id.ci = 0x423,
1748 };
1749 struct gsm0808_cell_id dec_ci;
1750 struct msgb *msg;
1751 uint8_t rc_enc;
1752 int rc_dec;
1753
1754 msg = msgb_alloc(1024, "output buffer");
1755 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1756 EXPECT_ENCODED("05 03 02 04 23");
1757
1758 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1759 OSMO_ASSERT(rc_dec == msg->len - 2);
1760 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1761 && enc_ci.id.ci == dec_ci.id.ci);
1762
1763 msgb_free(msg);
1764}
1765
1766static void test_gsm0808_enc_dec_cell_id_lac_and_ci()
1767{
1768 struct gsm0808_cell_id enc_ci = {
1769 .id_discr = CELL_IDENT_LAC_AND_CI,
1770 .id.lac_and_ci = {
1771 .lac = 0x423,
1772 .ci = 0x235,
1773 },
1774 };
1775 struct gsm0808_cell_id dec_ci;
1776 struct msgb *msg;
1777 uint8_t rc_enc;
1778 int rc_dec;
1779
1780 msg = msgb_alloc(1024, "output buffer");
1781 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1782 EXPECT_ENCODED("05 05 01 04 23 02 35");
1783
1784 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1785 OSMO_ASSERT(rc_dec == msg->len - 2);
1786 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1787 && enc_ci.id.lac_and_ci.lac == dec_ci.id.lac_and_ci.lac
1788 && enc_ci.id.lac_and_ci.ci == dec_ci.id.lac_and_ci.ci);
1789
1790 msgb_free(msg);
1791}
1792
1793static void test_gsm0808_enc_dec_cell_id_global()
1794{
1795 struct gsm0808_cell_id enc_ci = {
1796 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1797 .id.global = {
1798 .lai = {
1799 .plmn = { .mcc = 123, .mnc = 456 },
1800 .lac = 0x2342
1801 },
1802 .cell_identity = 0x423,
1803 }
1804 };
1805 struct gsm0808_cell_id dec_ci;
1806 struct msgb *msg;
1807 uint8_t rc_enc;
1808 int rc_dec;
1809
1810 msg = msgb_alloc(1024, "output buffer");
1811 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1812 EXPECT_ENCODED("05 08 00 21 63 54 23 42 04 23");
1813
1814 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1815 OSMO_ASSERT(rc_dec == msg->len - 2);
1816
1817 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1818 && osmo_plmn_cmp(&enc_ci.id.global.lai.plmn,
1819 &dec_ci.id.global.lai.plmn) == 0
1820 && enc_ci.id.global.lai.lac == dec_ci.id.global.lai.lac
1821 && enc_ci.id.global.cell_identity == dec_ci.id.global.cell_identity);
1822 msgb_free(msg);
1823}
1824
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001825static void test_gsm0808_enc_dec_cell_id_global_ps()
1826{
1827 struct gsm0808_cell_id enc_cgi = {
1828 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1829 .id.global = {
1830 .lai = {
1831 .plmn = { .mcc = 123, .mnc = 456 },
1832 .lac = 0x2342
1833 },
1834 .cell_identity = 0x423,
1835 }
1836 };
1837 struct gsm0808_cell_id enc_cgi_ps = {
1838 .id_discr = CELL_IDENT_WHOLE_GLOBAL_PS,
1839 .id.global_ps = {
1840 .rai = {
1841 .lac = {
1842 .plmn = { .mcc = 123, .mnc = 456 },
1843 .lac = 0x2342
1844 },
1845 .rac = 0xcc,
1846 },
1847 .cell_identity = 0x423,
1848 }
1849 };
1850 struct msgb *msg_cgi, *msg_cgi_ps;
1851 uint8_t rc_enc;
1852
1853 msg_cgi = msgb_alloc(1024, "output buffer (CGI)");
1854 rc_enc = gsm0808_enc_cell_id(msg_cgi, &enc_cgi);
1855 OSMO_ASSERT(rc_enc > 0);
1856
1857 msg_cgi_ps = msgb_alloc(1024, "output buffer (CGI-PS)");
1858 rc_enc = gsm0808_enc_cell_id(msg_cgi_ps, &enc_cgi_ps);
1859 OSMO_ASSERT(rc_enc > 0);
1860
1861 OSMO_ASSERT(msgb_eq(msg_cgi, msg_cgi_ps));
1862
1863 msgb_free(msg_cgi);
1864 msgb_free(msg_cgi_ps);
1865}
1866
Philipp Maier5f2eb152018-09-19 13:40:21 +02001867static void test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(struct gsm48_multi_rate_conf *cfg)
1868{
1869 uint16_t s15_s0;
1870
1871 printf("Input:\n");
1872 printf(" m4_75= %u smod= %u\n", cfg->m4_75, cfg->smod);
1873 printf(" m5_15= %u spare= %u\n", cfg->m5_15, cfg->spare);
1874 printf(" m5_90= %u icmi= %u\n", cfg->m5_90, cfg->icmi);
1875 printf(" m6_70= %u nscb= %u\n", cfg->m6_70, cfg->nscb);
1876 printf(" m7_40= %u ver= %u\n", cfg->m7_40, cfg->ver);
1877 printf(" m7_95= %u\n", cfg->m7_95);
1878 printf(" m10_2= %u\n", cfg->m10_2);
1879 printf(" m12_2= %u\n", cfg->m12_2);
1880
1881 s15_s0 = gsm0808_sc_cfg_from_gsm48_mr_cfg(cfg, true);
1882 printf("Result (fr):\n");
1883 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
1884 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
1885
1886 s15_s0 = gsm0808_sc_cfg_from_gsm48_mr_cfg(cfg, false);
1887 printf("Result (hr):\n");
1888 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
1889 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
1890
1891 printf("\n");
1892}
1893
1894static void test_gsm0808_sc_cfg_from_gsm48_mr_cfg(void)
1895{
1896 struct gsm48_multi_rate_conf cfg;
1897
1898 printf("Testing gsm0808_sc_cfg_from_gsm48_mr_cfg():\n");
1899
1900 memset(&cfg, 0, sizeof(cfg));
1901
1902 cfg.m4_75 = 0;
1903 cfg.m5_15 = 0;
1904 cfg.m5_90 = 0;
1905 cfg.m6_70 = 0;
1906 cfg.m7_40 = 0;
1907 cfg.m7_95 = 0;
1908 cfg.m10_2 = 0;
1909 cfg.m12_2 = 0;
1910 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1911
1912 cfg.m4_75 = 1;
1913 cfg.m5_15 = 0;
1914 cfg.m5_90 = 0;
1915 cfg.m6_70 = 0;
1916 cfg.m7_40 = 0;
1917 cfg.m7_95 = 0;
1918 cfg.m10_2 = 0;
1919 cfg.m12_2 = 0;
1920 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1921
1922 cfg.m4_75 = 0;
1923 cfg.m5_15 = 1;
1924 cfg.m5_90 = 0;
1925 cfg.m6_70 = 0;
1926 cfg.m7_40 = 0;
1927 cfg.m7_95 = 0;
1928 cfg.m10_2 = 0;
1929 cfg.m12_2 = 0;
1930 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1931
1932 cfg.m4_75 = 0;
1933 cfg.m5_15 = 0;
1934 cfg.m5_90 = 1;
1935 cfg.m6_70 = 0;
1936 cfg.m7_40 = 0;
1937 cfg.m7_95 = 0;
1938 cfg.m10_2 = 0;
1939 cfg.m12_2 = 0;
1940 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1941
1942 cfg.m4_75 = 0;
1943 cfg.m5_15 = 0;
1944 cfg.m5_90 = 0;
1945 cfg.m6_70 = 1;
1946 cfg.m7_40 = 0;
1947 cfg.m7_95 = 0;
1948 cfg.m10_2 = 0;
1949 cfg.m12_2 = 0;
1950 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1951
1952 cfg.m4_75 = 0;
1953 cfg.m5_15 = 0;
1954 cfg.m5_90 = 0;
1955 cfg.m6_70 = 0;
1956 cfg.m7_40 = 1;
1957 cfg.m7_95 = 0;
1958 cfg.m10_2 = 0;
1959 cfg.m12_2 = 0;
1960 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1961
1962 cfg.m4_75 = 0;
1963 cfg.m5_15 = 0;
1964 cfg.m5_90 = 0;
1965 cfg.m6_70 = 0;
1966 cfg.m7_40 = 0;
1967 cfg.m7_95 = 1;
1968 cfg.m10_2 = 0;
1969 cfg.m12_2 = 0;
1970 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1971
1972 cfg.m4_75 = 0;
1973 cfg.m5_15 = 0;
1974 cfg.m5_90 = 0;
1975 cfg.m6_70 = 0;
1976 cfg.m7_40 = 0;
1977 cfg.m7_95 = 0;
1978 cfg.m10_2 = 1;
1979 cfg.m12_2 = 0;
1980 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1981
1982 cfg.m4_75 = 0;
1983 cfg.m5_15 = 0;
1984 cfg.m5_90 = 0;
1985 cfg.m6_70 = 0;
1986 cfg.m7_40 = 0;
1987 cfg.m7_95 = 0;
1988 cfg.m10_2 = 0;
1989 cfg.m12_2 = 1;
1990 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
1991
1992 cfg.m4_75 = 1;
1993 cfg.m5_15 = 1;
1994 cfg.m5_90 = 1;
1995 cfg.m6_70 = 1;
1996 cfg.m7_40 = 0;
1997 cfg.m7_95 = 0;
1998 cfg.m10_2 = 0;
1999 cfg.m12_2 = 0;
2000 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2001
2002 cfg.m4_75 = 0;
2003 cfg.m5_15 = 0;
2004 cfg.m5_90 = 0;
2005 cfg.m6_70 = 0;
2006 cfg.m7_40 = 1;
2007 cfg.m7_95 = 1;
2008 cfg.m10_2 = 1;
2009 cfg.m12_2 = 1;
2010 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2011
2012 cfg.m4_75 = 0;
2013 cfg.m5_15 = 0;
2014 cfg.m5_90 = 1;
2015 cfg.m6_70 = 1;
2016 cfg.m7_40 = 0;
2017 cfg.m7_95 = 0;
2018 cfg.m10_2 = 1;
2019 cfg.m12_2 = 1;
2020 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2021
2022 cfg.m4_75 = 1;
2023 cfg.m5_15 = 1;
2024 cfg.m5_90 = 0;
2025 cfg.m6_70 = 0;
2026 cfg.m7_40 = 1;
2027 cfg.m7_95 = 1;
2028 cfg.m10_2 = 0;
2029 cfg.m12_2 = 0;
2030 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2031
2032 cfg.m4_75 = 0;
2033 cfg.m5_15 = 1;
2034 cfg.m5_90 = 0;
2035 cfg.m6_70 = 1;
2036 cfg.m7_40 = 0;
2037 cfg.m7_95 = 1;
2038 cfg.m10_2 = 0;
2039 cfg.m12_2 = 1;
2040 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2041
2042 cfg.m4_75 = 1;
2043 cfg.m5_15 = 0;
2044 cfg.m5_90 = 1;
2045 cfg.m6_70 = 0;
2046 cfg.m7_40 = 1;
2047 cfg.m7_95 = 0;
2048 cfg.m10_2 = 1;
2049 cfg.m12_2 = 0;
2050 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2051
2052 cfg.m4_75 = 1;
2053 cfg.m5_15 = 1;
2054 cfg.m5_90 = 1;
2055 cfg.m6_70 = 1;
2056 cfg.m7_40 = 1;
2057 cfg.m7_95 = 1;
2058 cfg.m10_2 = 1;
2059 cfg.m12_2 = 1;
2060 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
Philipp Maier94d79fd2019-03-01 10:40:48 +01002061
2062 cfg.m4_75 = 1;
2063 cfg.m5_15 = 0;
2064 cfg.m5_90 = 1;
2065 cfg.m6_70 = 0;
2066 cfg.m7_40 = 1;
2067 cfg.m7_95 = 0;
2068 cfg.m10_2 = 0;
2069 cfg.m12_2 = 1;
2070 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2071
2072 cfg.m4_75 = 1;
2073 cfg.m5_15 = 0;
2074 cfg.m5_90 = 1;
2075 cfg.m6_70 = 0;
2076 cfg.m7_40 = 1;
2077 cfg.m7_95 = 0;
2078 cfg.m10_2 = 0;
2079 cfg.m12_2 = 0;
2080 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
Pau Espin Pedrol1bd495a2021-02-15 16:15:59 +01002081
Philipp Maier5f2eb152018-09-19 13:40:21 +02002082}
2083
Philipp Maier8515d032018-09-25 15:57:49 +02002084static void test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(uint16_t s15_s0)
2085{
2086 struct gsm48_multi_rate_conf cfg;
Philipp Maier3713af82019-02-27 16:48:25 +01002087 int rc;
Philipp Maier8515d032018-09-25 15:57:49 +02002088
2089 printf("Input:\n");
2090 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
2091 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
2092
Philipp Maier3713af82019-02-27 16:48:25 +01002093 rc = gsm48_mr_cfg_from_gsm0808_sc_cfg(&cfg, s15_s0);
Philipp Maier8515d032018-09-25 15:57:49 +02002094
2095 printf("Output:\n");
2096 printf(" m4_75= %u smod= %u\n", cfg.m4_75, cfg.smod);
2097 printf(" m5_15= %u spare= %u\n", cfg.m5_15, cfg.spare);
2098 printf(" m5_90= %u icmi= %u\n", cfg.m5_90, cfg.icmi);
2099 printf(" m6_70= %u nscb= %u\n", cfg.m6_70, cfg.nscb);
2100 printf(" m7_40= %u ver= %u\n", cfg.m7_40, cfg.ver);
2101 printf(" m7_95= %u\n", cfg.m7_95);
2102 printf(" m10_2= %u\n", cfg.m10_2);
2103 printf(" m12_2= %u\n", cfg.m12_2);
2104
Philipp Maier3713af82019-02-27 16:48:25 +01002105 if (rc != 0)
2106 printf(" Result invalid!\n");
2107
Philipp Maier8515d032018-09-25 15:57:49 +02002108 printf("\n");
2109}
2110
2111void test_gsm48_mr_cfg_from_gsm0808_sc_cfg()
2112{
2113 printf("Testing gsm48_mr_cfg_from_gsm0808_sc_cfg():\n");
2114
Philipp Maier3713af82019-02-27 16:48:25 +01002115 /* Test with settings as defined in 3GPP TS 28.062, Table 7.11.3.1.3-2,
2116 * (up to four codecs may become selected) */
Philipp Maier8515d032018-09-25 15:57:49 +02002117 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2118 (GSM0808_SC_CFG_DEFAULT_AMR_4_75);
2119 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2120 (GSM0808_SC_CFG_DEFAULT_AMR_5_15);
2121 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2122 (GSM0808_SC_CFG_DEFAULT_AMR_5_90);
2123 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2124 (GSM0808_SC_CFG_DEFAULT_AMR_6_70);
2125 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2126 (GSM0808_SC_CFG_DEFAULT_AMR_7_40);
2127 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2128 (GSM0808_SC_CFG_DEFAULT_AMR_7_95);
2129 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2130 (GSM0808_SC_CFG_DEFAULT_AMR_10_2);
2131 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2132 (GSM0808_SC_CFG_DEFAULT_AMR_12_2);
2133
Philipp Maier3713af82019-02-27 16:48:25 +01002134 /* Test with settings as defined in 3GPP TS 28.062, Table 7.11.3.1.3-2,
2135 * but pick only one distinctive setting at a time */
2136 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_4_75);
Philipp Maier8515d032018-09-25 15:57:49 +02002137 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
Philipp Maier3713af82019-02-27 16:48:25 +01002138 (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20);
2139 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_5_90);
2140 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_6_70);
2141 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40);
2142 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_95);
2143 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_10_2);
2144 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_12_2);
2145
2146 /* Arbitrary, but valid combinations */
2147 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40 |
2148 GSM0808_SC_CFG_AMR_6_70 |
2149 GSM0808_SC_CFG_AMR_10_2);
2150 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_95 |
2151 GSM0808_SC_CFG_AMR_4_75);
2152 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_5_90 |
2153 GSM0808_SC_CFG_AMR_12_2);
Philipp Maier8515d032018-09-25 15:57:49 +02002154 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
Philipp Maier3713af82019-02-27 16:48:25 +01002155 (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 | GSM0808_SC_CFG_AMR_5_90 |
2156 GSM0808_SC_CFG_AMR_12_2);
2157
2158 /* Invalid combinations */
Philipp Maier8515d032018-09-25 15:57:49 +02002159 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
Philipp Maier3713af82019-02-27 16:48:25 +01002160 (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 | GSM0808_SC_CFG_AMR_6_70);
2161 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40 |
2162 GSM0808_SC_CFG_AMR_6_70 |
2163 GSM0808_SC_CFG_AMR_10_2 |
2164 GSM0808_SC_CFG_AMR_7_95 |
2165 GSM0808_SC_CFG_AMR_4_75);
2166 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(0x0000);
2167 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(0xffff);
Philipp Maier8515d032018-09-25 15:57:49 +02002168}
2169
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002170struct test_cell_id_matching_data {
2171 struct gsm0808_cell_id id;
2172 struct gsm0808_cell_id match_id;
2173 bool expect_match;
2174 bool expect_exact_match;
2175};
2176
2177#define lac_23 { .id_discr = CELL_IDENT_LAC, .id.lac = 23, }
2178#define lac_42 { .id_discr = CELL_IDENT_LAC, .id.lac = 42, }
2179#define ci_5 { .id_discr = CELL_IDENT_CI, .id.ci = 5, }
2180#define ci_6 { .id_discr = CELL_IDENT_CI, .id.ci = 6, }
2181#define lac_ci_23_5 { \
2182 .id_discr = CELL_IDENT_LAC_AND_CI, \
2183 .id.lac_and_ci = { .lac = 23, .ci = 5, }, \
2184 }
2185#define lac_ci_42_6 { \
2186 .id_discr = CELL_IDENT_LAC_AND_CI, \
2187 .id.lac_and_ci = { .lac = 42, .ci = 6, }, \
2188 }
2189#define lai_23_042_23 { \
2190 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2191 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 23, }, \
2192 }
2193#define lai_23_042_42 { \
2194 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2195 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 42, }, \
2196 }
2197#define lai_23_99_23 { \
2198 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2199 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 99, .mnc_3_digits = false }, .lac = 23, }, \
2200 }
2201#define lai_23_42_23 { \
2202 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2203 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = false }, .lac = 23, }, \
2204 }
2205#define cgi_23_042_23_5 { \
2206 .id_discr = CELL_IDENT_WHOLE_GLOBAL, \
2207 .id.global = { \
2208 .lai = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 23, }, \
2209 .cell_identity = 5, \
2210 }, \
2211 }
2212#define cgi_23_042_42_6 { \
2213 .id_discr = CELL_IDENT_WHOLE_GLOBAL, \
2214 .id.global = { \
2215 .lai = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 42, }, \
2216 .cell_identity = 6, \
2217 }, \
2218 }
2219#define cgi_23_99_23_5 { \
2220 .id_discr = CELL_IDENT_WHOLE_GLOBAL, \
2221 .id.global = { \
2222 .lai = { .plmn = { .mcc = 23, .mnc = 99, .mnc_3_digits = false }, .lac = 23, }, \
2223 .cell_identity = 5, \
2224 }, \
2225 }
2226
2227
2228static const struct test_cell_id_matching_data test_cell_id_matching_tests[] = {
2229 { .id = lac_23, .match_id = lac_23, .expect_match = true, .expect_exact_match = true },
2230 { .id = lac_23, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2231 { .id = lac_23, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2232 { .id = lac_23, .match_id = ci_6, .expect_match = true, .expect_exact_match = false },
2233 { .id = lac_23, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2234 { .id = lac_23, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2235 { .id = lac_23, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2236 { .id = lac_23, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2237 { .id = lac_23, .match_id = lai_23_99_23, .expect_match = true, .expect_exact_match = false },
2238 { .id = lac_23, .match_id = lai_23_42_23, .expect_match = true, .expect_exact_match = false },
2239 { .id = lac_23, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2240 { .id = lac_23, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2241 { .id = lac_23, .match_id = cgi_23_99_23_5, .expect_match = true, .expect_exact_match = false },
2242 { .id = ci_5, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2243 { .id = ci_5, .match_id = lac_42, .expect_match = true, .expect_exact_match = false },
2244 { .id = ci_5, .match_id = ci_5, .expect_match = true, .expect_exact_match = true },
2245 { .id = ci_5, .match_id = ci_6, .expect_match = false, .expect_exact_match = false },
2246 { .id = ci_5, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2247 { .id = ci_5, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2248 { .id = ci_5, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2249 { .id = ci_5, .match_id = lai_23_042_42, .expect_match = true, .expect_exact_match = false },
2250 { .id = ci_5, .match_id = lai_23_99_23, .expect_match = true, .expect_exact_match = false },
2251 { .id = ci_5, .match_id = lai_23_42_23, .expect_match = true, .expect_exact_match = false },
2252 { .id = ci_5, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2253 { .id = ci_5, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2254 { .id = ci_5, .match_id = cgi_23_99_23_5, .expect_match = true, .expect_exact_match = false },
2255 { .id = lac_ci_23_5, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2256 { .id = lac_ci_23_5, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2257 { .id = lac_ci_23_5, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2258 { .id = lac_ci_23_5, .match_id = ci_6, .expect_match = false, .expect_exact_match = false },
2259 { .id = lac_ci_23_5, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = true },
2260 { .id = lac_ci_23_5, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2261 { .id = lac_ci_23_5, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2262 { .id = lac_ci_23_5, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2263 { .id = lac_ci_23_5, .match_id = lai_23_99_23, .expect_match = true, .expect_exact_match = false },
2264 { .id = lac_ci_23_5, .match_id = lai_23_42_23, .expect_match = true, .expect_exact_match = false },
2265 { .id = lac_ci_23_5, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2266 { .id = lac_ci_23_5, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2267 { .id = lac_ci_23_5, .match_id = cgi_23_99_23_5, .expect_match = true, .expect_exact_match = false },
2268 { .id = lai_23_042_23, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2269 { .id = lai_23_042_23, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2270 { .id = lai_23_042_23, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2271 { .id = lai_23_042_23, .match_id = ci_6, .expect_match = true, .expect_exact_match = false },
2272 { .id = lai_23_042_23, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2273 { .id = lai_23_042_23, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2274 { .id = lai_23_042_23, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = true },
2275 { .id = lai_23_042_23, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2276 { .id = lai_23_042_23, .match_id = lai_23_99_23, .expect_match = false, .expect_exact_match = false },
2277 { .id = lai_23_042_23, .match_id = lai_23_42_23, .expect_match = false, .expect_exact_match = false },
2278 { .id = lai_23_042_23, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2279 { .id = lai_23_042_23, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2280 { .id = lai_23_042_23, .match_id = cgi_23_99_23_5, .expect_match = false, .expect_exact_match = false },
2281 { .id = cgi_23_042_23_5, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2282 { .id = cgi_23_042_23_5, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2283 { .id = cgi_23_042_23_5, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2284 { .id = cgi_23_042_23_5, .match_id = ci_6, .expect_match = false, .expect_exact_match = false },
2285 { .id = cgi_23_042_23_5, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2286 { .id = cgi_23_042_23_5, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2287 { .id = cgi_23_042_23_5, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2288 { .id = cgi_23_042_23_5, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2289 { .id = cgi_23_042_23_5, .match_id = lai_23_99_23, .expect_match = false, .expect_exact_match = false },
2290 { .id = cgi_23_042_23_5, .match_id = lai_23_42_23, .expect_match = false, .expect_exact_match = false },
2291 { .id = cgi_23_042_23_5, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = true },
2292 { .id = cgi_23_042_23_5, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2293 { .id = cgi_23_042_23_5, .match_id = cgi_23_99_23_5, .expect_match = false, .expect_exact_match = false },
2294};
2295
2296static void test_cell_id_matching()
2297{
2298 int i;
2299 bool ok = true;
2300 printf("\n%s\n", __func__);
2301
2302 for (i = 0; i < ARRAY_SIZE(test_cell_id_matching_tests); i++) {
2303 const struct test_cell_id_matching_data *d = &test_cell_id_matching_tests[i];
2304 int exact_match;
2305
2306 for (exact_match = 0; exact_match < 2; exact_match++) {
2307 bool result;
2308 bool expect_result = exact_match ? d->expect_exact_match : d->expect_match;
2309
2310 result = gsm0808_cell_ids_match(&d->id, &d->match_id, (bool)exact_match);
2311
2312 printf("[%d] %s %s %s%s\n",
2313 i,
2314 gsm0808_cell_id_name(&d->id),
2315 gsm0808_cell_id_name2(&d->match_id),
2316 result ? "MATCH" : "don't match",
2317 exact_match ? " exactly" : "");
2318 if (result != expect_result) {
2319 printf(" ERROR: expected %s\n", d->expect_match ? "MATCH" : "no match");
2320 ok = false;
2321 }
2322 }
2323 }
2324
2325 OSMO_ASSERT(ok);
2326}
2327
2328static bool test_cell_id_list_matching_discrs(bool test_match,
2329 enum CELL_IDENT id_discr,
2330 enum CELL_IDENT list_discr)
2331{
2332 int i, j;
2333 const struct gsm0808_cell_id *id = NULL;
2334 struct gsm0808_cell_id_list2 list = {};
2335 int match_idx = -1;
2336 int result;
2337
2338 for (i = 0; i < ARRAY_SIZE(test_cell_id_matching_tests); i++) {
2339 const struct test_cell_id_matching_data *d = &test_cell_id_matching_tests[i];
2340 if (id_discr != d->id.id_discr)
2341 continue;
2342 id = &d->id;
2343 break;
2344 }
2345
2346 if (!id) {
2347 printf("Did not find any entry for %s\n", gsm0808_cell_id_discr_name(id_discr));
2348 return true;
2349 }
2350
2351 /* Collect those entries with exactly this id on the left, of type list_discr on the right.
2352 * Collect the mismatches first, for more interesting match indexes in the results. */
2353 for (j = 0; j < 2; j++) {
2354 bool collect_matches = (bool)j;
2355
2356 /* If we want to have a mismatching list, don't add any entries that match. */
2357 if (!test_match && collect_matches)
2358 continue;
2359
2360 for (i = 0; i < ARRAY_SIZE(test_cell_id_matching_tests); i++) {
2361 const struct test_cell_id_matching_data *d = &test_cell_id_matching_tests[i];
2362 struct gsm0808_cell_id_list2 add;
2363
2364 /* Ignore those with a different d->id */
2365 if (!gsm0808_cell_ids_match(&d->id, id, true))
2366 continue;
2367
2368 /* Ignore those with a different d->match_id discr */
2369 if (d->match_id.id_discr != list_discr)
2370 continue;
2371
2372 if (collect_matches != d->expect_match)
2373 continue;
2374
2375 if (match_idx < 0 && d->expect_match) {
2376 match_idx = list.id_list_len;
2377 }
2378
2379 gsm0808_cell_id_to_list(&add, &d->match_id);
2380 gsm0808_cell_id_list_add(&list, &add);
2381 }
2382 }
2383
2384 if (!list.id_list_len) {
2385 printf("%s vs. %s: No match_id entries to test %s\n",
2386 gsm0808_cell_id_name(id),
2387 gsm0808_cell_id_discr_name(list_discr),
2388 test_match ? "MATCH" : "mismatch");
2389 return true;
2390 }
2391
2392 result = gsm0808_cell_id_matches_list(id, &list, 0, false);
2393
2394 printf("%s and %s: ",
2395 gsm0808_cell_id_name(id),
2396 gsm0808_cell_id_list_name(&list));
2397 if (result >= 0)
2398 printf("MATCH at [%d]\n", result);
2399 else
2400 printf("mismatch\n");
2401
2402 if (test_match
2403 && (result < 0 || result != match_idx)) {
2404 printf(" ERROR: expected MATCH at %d\n", match_idx);
2405 return false;
2406 }
2407
2408 if (!test_match && result >= 0) {
2409 printf(" ERROR: expected mismatch\n");
2410 return false;
2411 }
2412
2413 return true;
2414}
2415
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002416const enum CELL_IDENT cell_ident_discrs[] = {
2417 CELL_IDENT_LAC, CELL_IDENT_CI, CELL_IDENT_LAC_AND_CI, CELL_IDENT_LAI_AND_LAC,
2418 CELL_IDENT_WHOLE_GLOBAL,
2419};
2420
2421
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002422static void test_cell_id_list_matching(bool test_match)
2423{
2424 int i, j;
2425 bool ok = true;
2426
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002427 printf("\n%s(%s)\n", __func__, test_match ? "test match" : "test mismatch");
2428
2429 /* Autogenerate Cell ID lists from above dataset, which should match / not match. */
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002430 for (i = 0; i < ARRAY_SIZE(cell_ident_discrs); i++) {
2431 for (j = 0; j < ARRAY_SIZE(cell_ident_discrs); j++)
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002432 if (!test_cell_id_list_matching_discrs(test_match,
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002433 cell_ident_discrs[i], cell_ident_discrs[j]))
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002434 ok = false;
2435 }
2436
2437 OSMO_ASSERT(ok);
2438}
2439
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002440
2441static const struct gsm0808_cell_id test_gsm0808_cell_id_to_from_cgi_data[] = {
2442 lac_23,
2443 lac_42,
2444 ci_5,
2445 ci_6,
2446 lac_ci_23_5,
2447 lac_ci_42_6,
2448 lai_23_042_23,
2449 lai_23_042_42,
2450 lai_23_99_23,
2451 lai_23_42_23,
2452 cgi_23_042_23_5,
2453 cgi_23_042_42_6,
2454 cgi_23_99_23_5,
2455 { .id_discr = CELL_IDENT_NO_CELL },
2456 { .id_discr = 423 },
2457};
2458
2459static void test_gsm0808_cell_id_to_from_cgi()
2460{
2461 int i;
2462 int j;
2463
2464 printf("\n%s()\n", __func__);
2465
2466 for (i = 0; i < ARRAY_SIZE(test_gsm0808_cell_id_to_from_cgi_data); i++) {
2467 const struct gsm0808_cell_id *from_cid = &test_gsm0808_cell_id_to_from_cgi_data[i];
2468 struct osmo_cell_global_id cgi = {
2469 .lai = {
2470 .plmn = {
2471 .mcc = 777,
2472 .mnc = 7,
2473 .mnc_3_digits = true,
2474 },
2475 .lac = 7777,
2476 },
2477 .cell_identity = 7777,
2478 };
2479 struct gsm0808_cell_id cid = {};
2480 int rc;
2481
2482 rc = gsm0808_cell_id_to_cgi(&cgi, from_cid);
2483 printf("cid %s -> cgi %s", gsm0808_cell_id_name(from_cid), osmo_cgi_name(&cgi));
2484
2485 if (rc & OSMO_CGI_PART_PLMN)
2486 printf(" PLMN");
2487 if (rc & OSMO_CGI_PART_LAC)
2488 printf(" LAC");
2489 if (rc & OSMO_CGI_PART_CI)
2490 printf(" CI");
2491
2492 gsm0808_cell_id_from_cgi(&cid, from_cid->id_discr, &cgi);
2493 printf(" -> cid %s\n", gsm0808_cell_id_name(&cid));
2494 if (!gsm0808_cell_ids_match(from_cid, &cid, true))
2495 printf(" MISMATCH!\n");
2496
2497 for (j = 0; j < ARRAY_SIZE(cell_ident_discrs); j++) {
2498 enum CELL_IDENT discr = cell_ident_discrs[j];
2499
2500 gsm0808_cell_id_from_cgi(&cid, discr, &cgi);
2501 printf(" --> gsm0808_cell_id{%s} = %s\n", gsm0808_cell_id_discr_name(discr), gsm0808_cell_id_name(&cid));
2502 }
2503 }
2504}
2505
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002506int main(int argc, char **argv)
2507{
Max969fb2e2018-12-10 11:01:10 +01002508 void *ctx = talloc_named_const(NULL, 0, "gsm0808 test");
2509 msgb_talloc_ctx_init(ctx, 0);
2510 osmo_init_logging2(ctx, NULL);
2511
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002512 printf("Testing generation of GSM0808 messages\n");
Philipp Maier4f4905f2018-11-30 13:36:12 +01002513 test_gsm0808_enc_cause();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002514 test_create_layer3();
Philipp Maierfa896ab2017-03-27 16:55:32 +02002515 test_create_layer3_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002516 test_create_reset();
Philipp Maier15596e22017-04-05 17:55:27 +02002517 test_create_reset_ack();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002518 test_create_clear_command();
Harald Weltecf665fc2019-02-18 13:45:36 +01002519 test_create_clear_command2();
2520 test_create_clear_command2_csfb();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002521 test_create_clear_complete();
Philipp Maierb478dd32017-03-29 15:50:05 +02002522 test_create_cipher();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002523 test_create_cipher_complete();
2524 test_create_cipher_reject();
Maxed651d22018-11-07 15:25:05 +01002525 test_create_cipher_reject_ext();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002526 test_create_cm_u();
2527 test_create_sapi_reject();
Philipp Maierc6144a22017-03-29 17:53:43 +02002528 test_create_ass();
Max52074322018-11-30 10:44:07 +01002529 test_create_ass2();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002530 test_create_ass_compl();
Philipp Maierfa896ab2017-03-27 16:55:32 +02002531 test_create_ass_compl_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002532 test_create_ass_fail();
Philipp Maierfa896ab2017-03-27 16:55:32 +02002533 test_create_ass_fail_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002534 test_create_clear_rqst();
Philipp Maier3d48ec02017-03-29 17:37:55 +02002535 test_create_paging();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002536 test_create_dtap();
2537 test_prepend_dtap();
Max969fb2e2018-12-10 11:01:10 +01002538
Max47022152018-12-19 18:51:00 +01002539 test_enc_dec_lcls();
Max969fb2e2018-12-10 11:01:10 +01002540
Philipp Maier22401432017-03-24 17:59:26 +01002541 test_enc_dec_aoip_trasp_addr_v4();
2542 test_enc_dec_aoip_trasp_addr_v6();
Philipp Maier6f725d62017-03-24 18:03:17 +01002543 test_gsm0808_enc_dec_speech_codec();
Philipp Maier6f725d62017-03-24 18:03:17 +01002544 test_gsm0808_enc_dec_speech_codec_ext_with_cfg();
Philipp Maierbb839662017-06-01 17:11:19 +02002545 test_gsm0808_enc_dec_speech_codec_with_cfg();
Philipp Maier6f725d62017-03-24 18:03:17 +01002546 test_gsm0808_enc_dec_speech_codec_list();
Philipp Maierf6c369f2018-10-16 15:24:47 +02002547 test_gsm0808_enc_dec_empty_speech_codec_list();
Philipp Maiere0c65302017-03-28 17:05:40 +02002548 test_gsm0808_enc_dec_channel_type();
Philipp Maier14e76b92017-03-28 18:36:52 +02002549 test_gsm0808_enc_dec_encrypt_info();
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02002550
Philipp Maier783047e2017-03-29 11:35:50 +02002551 test_gsm0808_enc_dec_cell_id_list_lac();
2552 test_gsm0808_enc_dec_cell_id_list_single_lac();
Stefan Sperlinge1a86742018-03-15 18:05:02 +01002553 test_gsm0808_enc_dec_cell_id_list_multi_lac();
Philipp Maier783047e2017-03-29 11:35:50 +02002554 test_gsm0808_enc_dec_cell_id_list_bss();
Stefan Sperling23381452018-03-15 19:38:15 +01002555 test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac();
Stefan Sperling9c62fc62018-03-16 10:23:34 +01002556 test_gsm0808_enc_dec_cell_id_list_multi_ci();
Stefan Sperlinged4327c2018-03-16 11:02:59 +01002557 test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci();
Stefan Sperling483f3862018-03-16 12:21:26 +01002558 test_gsm0808_enc_dec_cell_id_list_multi_global();
Harald Weltea87526d2022-02-15 20:06:16 +01002559 test_gsm0808_dec_cell_id_list_srvcc();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002560
Neels Hofmeyr74663d92018-03-23 01:46:42 +01002561 test_cell_id_list_add();
2562
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02002563 test_gsm0808_enc_dec_cell_id_lac();
2564 test_gsm0808_enc_dec_cell_id_bss();
2565 test_gsm0808_enc_dec_cell_id_no_cell();
2566 test_gsm0808_enc_dec_cell_id_lai_and_lac();
2567 test_gsm0808_enc_dec_cell_id_ci();
2568 test_gsm0808_enc_dec_cell_id_lac_and_ci();
2569 test_gsm0808_enc_dec_cell_id_global();
Pau Espin Pedrol52489852021-02-15 16:26:37 +01002570 test_gsm0808_enc_dec_cell_id_global_ps();
Philipp Maier5f2eb152018-09-19 13:40:21 +02002571 test_gsm0808_sc_cfg_from_gsm48_mr_cfg();
Philipp Maier8515d032018-09-25 15:57:49 +02002572 test_gsm48_mr_cfg_from_gsm0808_sc_cfg();
Philipp Maier5f2eb152018-09-19 13:40:21 +02002573
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002574 test_cell_id_matching();
2575 test_cell_id_list_matching(true);
2576 test_cell_id_list_matching(false);
2577
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002578 test_gsm0808_cell_id_to_from_cgi();
2579
Alexander Chemeris22630e62020-05-13 00:44:04 +03002580 test_dec_confusion();
Pau Espin Pedrola3987fc2022-03-18 13:17:28 +01002581 test_dec_perform_location_report_sys5891();
Alexander Chemeris22630e62020-05-13 00:44:04 +03002582
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002583 printf("Done\n");
2584 return EXIT_SUCCESS;
2585}