blob: a03f276706233b9920e5d600b1f64431e158ae33 [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
Harald Weltee61d4592022-11-03 11:05:58 +0100114static void test_create_layer3_aoip(void)
Philipp Maierfa896ab2017-03-27 16:55:32 +0200115{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100151static void test_create_reset(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100152{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100162static void test_create_reset_ack(void)
Philipp Maier15596e22017-04-05 17:55:27 +0200163{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100174static void test_create_clear_command(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100175{
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 Weltee61d4592022-11-03 11:05:58 +0100185static void test_create_clear_command2(void)
Harald Weltecf665fc2019-02-18 13:45:36 +0100186{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100196static void test_create_clear_command2_csfb(void)
Harald Weltecf665fc2019-02-18 13:45:36 +0100197{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100207static void test_create_clear_complete(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100208{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100218static void test_create_cipher(void)
Philipp Maierb478dd32017-03-29 15:50:05 +0200219{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100258static void test_create_cipher_complete(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100259{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100315static void test_create_cipher_reject(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100316{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100330static void test_create_cipher_reject_ext(void)
Maxed651d22018-11-07 15:25:05 +0100331{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100345static void test_create_cm_u(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100346{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100367static void test_create_sapi_reject(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100368{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100378static void test_dec_confusion(void)
Alexander Chemeris22630e62020-05-13 00:44:04 +0300379{
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 */
Harald Weltee61d4592022-11-03 11:05:58 +0100428static void test_dec_perform_location_report_sys5891(void)
Pau Espin Pedrola3987fc2022-03-18 13:17:28 +0100429{
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);
Pau Espin Pedrolfffd7c32022-03-18 13:45:42 +0100477 OSMO_ASSERT(rc == 5);
Pau Espin Pedrola3987fc2022-03-18 13:17:28 +0100478}
479
Harald Weltee61d4592022-11-03 11:05:58 +0100480static void test_create_ass(void)
Philipp Maierc6144a22017-03-29 17:53:43 +0200481{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100530static void test_create_ass2(void)
Max52074322018-11-30 10:44:07 +0100531{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100607static void test_create_ass_compl(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100608{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100626static void test_create_ass_compl_aoip(void)
Philipp Maierfa896ab2017-03-27 16:55:32 +0200627{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100663static void test_create_ass_fail(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100664{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100681static void test_create_ass_fail_aoip(void)
Philipp Maierfa896ab2017-03-27 16:55:32 +0200682{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100707static void test_create_clear_rqst(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100708{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100718static void test_create_paging(void)
Philipp Maier3d48ec02017-03-29 17:37:55 +0200719{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100758static void test_create_dtap(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100759{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100775static void test_prepend_dtap(void)
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +0100776{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100792static void test_enc_dec_lcls(void)
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
Harald Weltee61d4592022-11-03 11:05:58 +0100870static void test_enc_dec_aoip_trasp_addr_v4(void)
Philipp Maier22401432017-03-24 17:59:26 +0100871{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100898static void test_enc_dec_aoip_trasp_addr_v6(void)
Philipp Maier22401432017-03-24 17:59:26 +0100899{
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
Harald Weltee61d4592022-11-03 11:05:58 +0100927static void test_enc_aoip_trasp_addr_msg_too_small(void)
Oliver Smithc66b35b2022-08-05 11:27:55 +0200928{
929 struct msgb *msg;
930 struct sockaddr_storage enc_addr;
931 struct sockaddr_in enc_addr_in;
932 uint8_t rc_enc;
933
934 memset(&enc_addr_in, 0, sizeof(enc_addr_in));
935 enc_addr_in.sin_family = AF_INET;
936 enc_addr_in.sin_port = htons(1234);
937 inet_aton("255.0.255.255", &enc_addr_in.sin_addr);
938
939 memset(&enc_addr, 0, sizeof(enc_addr));
940 memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
941
942 msg = msgb_alloc(7, "output buffer");
943 rc_enc = gsm0808_enc_aoip_trasp_addr(msg, &enc_addr);
944 OSMO_ASSERT(rc_enc == 0);
945
946 msgb_free(msg);
947}
948
Harald Weltee61d4592022-11-03 11:05:58 +0100949static void test_gsm0808_enc_dec_speech_codec(void)
Philipp Maier6f725d62017-03-24 18:03:17 +0100950{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +0200951 struct gsm0808_speech_codec enc_sc = {
952 .pi = true,
953 .tf = true,
954 .type = GSM0808_SCT_FR2,
955 };
956 struct gsm0808_speech_codec dec_sc = {};
Philipp Maier6f725d62017-03-24 18:03:17 +0100957 struct msgb *msg;
958 uint8_t rc_enc;
959 int rc_dec;
960
Philipp Maier6f725d62017-03-24 18:03:17 +0100961 msg = msgb_alloc(1024, "output buffer");
Vadim Yanitskiy6db830f2022-12-14 00:45:25 +0700962 rc_enc = gsm0808_enc_speech_codec2(msg, &enc_sc);
Philipp Maier6f725d62017-03-24 18:03:17 +0100963 OSMO_ASSERT(rc_enc == 3);
964
965 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
966 OSMO_ASSERT(rc_dec == 1);
967
968 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
969
970 msgb_free(msg);
971}
972
973
Harald Weltee61d4592022-11-03 11:05:58 +0100974static void test_gsm0808_enc_dec_speech_codec_with_cfg(void)
Philipp Maierbb839662017-06-01 17:11:19 +0200975{
Neels Hofmeyrc62c9342018-04-15 23:31:47 +0200976 struct gsm0808_speech_codec enc_sc = {
977 .pi = true,
978 .tf = true,
979 .type = GSM0808_SCT_FR3,
980 .cfg = 0xabcd,
981 };
982 struct gsm0808_speech_codec dec_sc = {};
Philipp Maierbb839662017-06-01 17:11:19 +0200983 struct msgb *msg;
984 uint8_t rc_enc;
985 int rc_dec;
986
Philipp Maierbb839662017-06-01 17:11:19 +0200987 msg = msgb_alloc(1024, "output buffer");
Vadim Yanitskiy6db830f2022-12-14 00:45:25 +0700988 rc_enc = gsm0808_enc_speech_codec2(msg, &enc_sc);
Philipp Maierbb839662017-06-01 17:11:19 +0200989 OSMO_ASSERT(rc_enc == 5);
990
991 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
992 OSMO_ASSERT(rc_dec == 3);
993
994 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
995
996 msgb_free(msg);
997}
998
Harald Weltee61d4592022-11-03 11:05:58 +0100999static void test_gsm0808_enc_dec_speech_codec_ext_with_cfg(void)
Philipp Maier6f725d62017-03-24 18:03:17 +01001000{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001001 struct gsm0808_speech_codec enc_sc = {
1002 .pi = true,
1003 .tf = true,
1004 .type = GSM0808_SCT_CSD,
1005 .cfg = 0xc0,
1006 };
1007 struct gsm0808_speech_codec dec_sc = {};
Philipp Maier6f725d62017-03-24 18:03:17 +01001008 struct msgb *msg;
1009 uint8_t rc_enc;
1010 int rc_dec;
1011
Philipp Maier6f725d62017-03-24 18:03:17 +01001012 msg = msgb_alloc(1024, "output buffer");
Vadim Yanitskiy6db830f2022-12-14 00:45:25 +07001013 rc_enc = gsm0808_enc_speech_codec2(msg, &enc_sc);
Philipp Maierbb839662017-06-01 17:11:19 +02001014 OSMO_ASSERT(rc_enc == 5);
Philipp Maier6f725d62017-03-24 18:03:17 +01001015
1016 rc_dec = gsm0808_dec_speech_codec(&dec_sc, msg->data + 2, msg->len - 2);
Philipp Maierbb839662017-06-01 17:11:19 +02001017 OSMO_ASSERT(rc_dec == 3);
Philipp Maier6f725d62017-03-24 18:03:17 +01001018
1019 OSMO_ASSERT(memcmp(&enc_sc, &dec_sc, sizeof(enc_sc)) == 0);
1020
1021 msgb_free(msg);
1022}
1023
Harald Weltee61d4592022-11-03 11:05:58 +01001024static void test_gsm0808_enc_dec_speech_codec_list(void)
Philipp Maier6f725d62017-03-24 18:03:17 +01001025{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001026 struct gsm0808_speech_codec_list enc_scl = {
1027 .codec = {
1028 {
1029 .pi = true,
1030 .tf = true,
1031 .type = GSM0808_SCT_FR3,
1032 .cfg = 0xcdef,
1033 },
1034
1035 {
1036 .fi = true,
1037 .pt = true,
1038 .type = GSM0808_SCT_FR2,
1039 },
1040
1041 {
1042 .fi = true,
1043 .tf = true,
1044 .type = GSM0808_SCT_CSD,
1045 .cfg = 0xc0,
1046 },
1047 },
1048 .len = 3,
1049 };
1050 struct gsm0808_speech_codec_list dec_scl = {};
Philipp Maier6f725d62017-03-24 18:03:17 +01001051 struct msgb *msg;
1052 uint8_t rc_enc;
1053 int rc_dec;
1054
Philipp Maier6f725d62017-03-24 18:03:17 +01001055 msg = msgb_alloc(1024, "output buffer");
Vadim Yanitskiy6db830f2022-12-14 00:45:25 +07001056 rc_enc = gsm0808_enc_speech_codec_list2(msg, &enc_scl);
Philipp Maier6f725d62017-03-24 18:03:17 +01001057 OSMO_ASSERT(rc_enc == 9);
1058
1059 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
1060 OSMO_ASSERT(rc_dec == 7);
1061
1062 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
1063
1064 msgb_free(msg);
1065}
1066
Harald Weltee61d4592022-11-03 11:05:58 +01001067static void test_gsm0808_enc_dec_empty_speech_codec_list(void)
Philipp Maierf6c369f2018-10-16 15:24:47 +02001068{
1069 struct gsm0808_speech_codec_list enc_scl = {
1070 .len = 0,
1071 };
1072 struct gsm0808_speech_codec_list dec_scl = {};
1073 struct msgb *msg;
1074 uint8_t rc_enc;
1075 int rc_dec;
1076
1077 msg = msgb_alloc(1024, "output buffer");
Vadim Yanitskiy6db830f2022-12-14 00:45:25 +07001078 rc_enc = gsm0808_enc_speech_codec_list2(msg, &enc_scl);
Philipp Maierf6c369f2018-10-16 15:24:47 +02001079 OSMO_ASSERT(rc_enc == 2);
1080
1081 rc_dec = gsm0808_dec_speech_codec_list(&dec_scl, msg->data + 2, msg->len - 2);
1082 OSMO_ASSERT(rc_dec == 0);
1083
1084 OSMO_ASSERT(memcmp(&enc_scl, &dec_scl, sizeof(enc_scl)) == 0);
1085
1086 msgb_free(msg);
1087}
1088
Oliver Smithf047a4a2023-01-31 13:10:03 +01001089static void test_gsm0808_enc_dec_channel_type_data(void)
1090{
1091 struct gsm0808_channel_type enc_ct = {
1092 .ch_indctr = GSM0808_CHAN_DATA,
1093 .ch_rate_type = GSM0808_DATA_HALF_PREF,
1094
1095 .data_transparent = true,
1096 .data_rate = GSM0808_DATA_RATE_TRANSP_04800,
1097 };
1098 struct gsm0808_channel_type dec_ct = {};
1099 struct msgb *msg;
1100 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
1101 0x03, 0x02, 0x0b, 0x51,
1102 };
1103 uint8_t rc_enc;
1104 int rc_dec;
1105
1106 msg = msgb_alloc(1024, "output buffer");
1107 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
1108 OSMO_ASSERT(rc_enc == 5);
1109 if (memcmp(ct_enc_expected, msg->data, msg->len)) {
1110 printf(" got: %s\n", osmo_hexdump(msg->data, msg->len));
1111 printf("expect: %s\n", osmo_hexdump(ct_enc_expected, sizeof(ct_enc_expected)));
1112 OSMO_ASSERT(false);
1113 }
1114
1115 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
1116 OSMO_ASSERT(rc_dec == 3);
1117 OSMO_ASSERT(dec_ct.ch_indctr == enc_ct.ch_indctr);
1118 OSMO_ASSERT(dec_ct.ch_rate_type == enc_ct.ch_rate_type);
1119 OSMO_ASSERT(dec_ct.data_transparent == enc_ct.data_transparent);
1120 OSMO_ASSERT(dec_ct.data_rate == enc_ct.data_rate);
1121 OSMO_ASSERT(dec_ct.data_rate_allowed_is_set == enc_ct.data_rate_allowed_is_set);
1122 OSMO_ASSERT(dec_ct.data_asym_pref_is_set == enc_ct.data_asym_pref_is_set);
1123
1124 msgb_free(msg);
1125}
1126
1127static void test_gsm0808_enc_dec_channel_type_data_asym_pref(void)
1128{
1129 struct gsm0808_channel_type enc_ct = {
1130 .ch_indctr = GSM0808_CHAN_DATA,
1131 .ch_rate_type = GSM0808_DATA_HALF_PREF,
1132
1133 .data_transparent = false,
1134 .data_rate = GSM0808_DATA_RATE_NON_TRANSP_06000,
1135 .data_rate_allowed_is_set = true,
1136 .data_rate_allowed = GSM0808_DATA_RATE_NON_TRANSP_ALLOWED_06000
1137 | GSM0808_DATA_RATE_NON_TRANSP_ALLOWED_12000
1138 | GSM0808_DATA_RATE_NON_TRANSP_ALLOWED_14500,
1139 .data_asym_pref_is_set = true,
1140 .data_asym_pref = GSM0808_CT_ASYM_PREF_UL,
1141 };
1142 struct gsm0808_channel_type dec_ct = {};
1143 struct msgb *msg;
1144 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
1145 0x05, 0x02, 0x0b, 0x91, 0x8b, 0x20,
1146 };
1147 uint8_t rc_enc;
1148 int rc_dec;
1149
1150 msg = msgb_alloc(1024, "output buffer");
1151 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
1152 OSMO_ASSERT(rc_enc == 7);
1153 if (memcmp(ct_enc_expected, msg->data, msg->len)) {
1154 printf(" got: %s\n", osmo_hexdump(msg->data, msg->len));
1155 printf("expect: %s\n", osmo_hexdump(ct_enc_expected, sizeof(ct_enc_expected)));
1156 OSMO_ASSERT(false);
1157 }
1158
1159 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
1160 OSMO_ASSERT(rc_dec == 5);
1161 OSMO_ASSERT(dec_ct.ch_indctr == enc_ct.ch_indctr);
1162 OSMO_ASSERT(dec_ct.ch_rate_type == enc_ct.ch_rate_type);
1163 OSMO_ASSERT(dec_ct.data_transparent == enc_ct.data_transparent);
1164 OSMO_ASSERT(dec_ct.data_rate == enc_ct.data_rate);
1165 OSMO_ASSERT(dec_ct.data_rate_allowed_is_set == enc_ct.data_rate_allowed_is_set);
1166 OSMO_ASSERT(dec_ct.data_asym_pref_is_set == enc_ct.data_asym_pref_is_set);
1167
1168 msgb_free(msg);
1169}
1170
Oliver Smith02b90952023-01-31 16:42:55 +01001171static void test_gsm0808_enc_dec_channel_type_speech(void)
Philipp Maiere0c65302017-03-28 17:05:40 +02001172{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001173 struct gsm0808_channel_type enc_ct = {
1174 .ch_indctr = GSM0808_CHAN_SPEECH,
1175 .ch_rate_type = GSM0808_SPEECH_HALF_PREF,
1176 .perm_spch = { GSM0808_PERM_FR3, GSM0808_PERM_HR3 },
1177 .perm_spch_len = 2,
1178 };
1179 struct gsm0808_channel_type dec_ct = {};
Philipp Maiere0c65302017-03-28 17:05:40 +02001180 struct msgb *msg;
1181 uint8_t ct_enc_expected[] = { GSM0808_IE_CHANNEL_TYPE,
1182 0x04, 0x01, 0x0b, 0xa1, 0x25
1183 };
1184 uint8_t rc_enc;
1185 int rc_dec;
1186
Philipp Maiere0c65302017-03-28 17:05:40 +02001187 msg = msgb_alloc(1024, "output buffer");
1188 rc_enc = gsm0808_enc_channel_type(msg, &enc_ct);
1189 OSMO_ASSERT(rc_enc == 6);
1190 OSMO_ASSERT(memcmp(ct_enc_expected, msg->data, msg->len) == 0);
1191
1192 rc_dec = gsm0808_dec_channel_type(&dec_ct, msg->data + 2, msg->len - 2);
1193 OSMO_ASSERT(rc_dec == 4);
Pau Espin Pedrol2605ffc2019-07-29 16:44:22 +02001194 OSMO_ASSERT(enc_ct.ch_indctr == dec_ct.ch_indctr);
1195 OSMO_ASSERT(enc_ct.ch_rate_type == dec_ct.ch_rate_type);
1196 OSMO_ASSERT(enc_ct.perm_spch_len == dec_ct.perm_spch_len);
1197 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 +02001198
1199 msgb_free(msg);
1200}
1201
Oliver Smith81e5a6f2023-01-31 12:34:26 +01001202static void test_gsm0808_dec_channel_type_err(void)
1203{
1204 struct gsm0808_channel_type ct;
1205 int rc;
1206
Oliver Smithf047a4a2023-01-31 13:10:03 +01001207 /* Unknown channel indicator */
1208 const uint8_t hex1[] = { 0x05, 0x0b, 0xa1, 0x25 };
1209 rc = gsm0808_dec_channel_type(&ct, hex1, sizeof(hex1));
1210 OSMO_ASSERT(rc == -ENOTSUP);
1211
1212 /* Data: ext in Octet 5 with transparent service */
1213 const uint8_t hex2[] = { 0x02, 0x0b, 0xc0, 0x00 };
1214 rc = gsm0808_dec_channel_type(&ct, hex2, sizeof(hex2));
1215 OSMO_ASSERT(rc == -EINVAL);
1216
1217 /* Data: ext in Octet 5, but too short */
1218 const uint8_t hex3[] = { 0x02, 0x0b, 0x80 };
1219 rc = gsm0808_dec_channel_type(&ct, hex3, sizeof(hex3));
1220 OSMO_ASSERT(rc == -EOVERFLOW);
1221
1222 /* Data: ext in Octet 5a, but too short */
1223 const uint8_t hex4[] = { 0x02, 0x0b, 0x80, 0x80 };
1224 rc = gsm0808_dec_channel_type(&ct, hex4, sizeof(hex4));
1225 OSMO_ASSERT(rc == -EOVERFLOW);
1226
Oliver Smith81e5a6f2023-01-31 12:34:26 +01001227 /* Speech: extension bit is set in last byte */
Oliver Smithf047a4a2023-01-31 13:10:03 +01001228 const uint8_t hex5[] = { 0x01, 0x0b, 0xa1, 0xa5 };
1229 rc = gsm0808_dec_channel_type(&ct, hex5, sizeof(hex5));
Oliver Smith81e5a6f2023-01-31 12:34:26 +01001230 OSMO_ASSERT(rc == -EOVERFLOW);
1231}
1232
Harald Weltee61d4592022-11-03 11:05:58 +01001233static void test_gsm0808_enc_dec_encrypt_info(void)
Philipp Maier14e76b92017-03-28 18:36:52 +02001234{
Neels Hofmeyr9a4286b2018-04-20 12:27:52 +02001235 struct gsm0808_encrypt_info enc_ei = {
1236 .perm_algo = { GSM0808_ALG_ID_A5_0, GSM0808_ALG_ID_A5_1 },
1237 .perm_algo_len = 2,
1238 .key = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42, },
1239 .key_len = 8,
1240 };
1241 struct gsm0808_encrypt_info dec_ei = {};
Philipp Maier14e76b92017-03-28 18:36:52 +02001242 struct msgb *msg;
1243 uint8_t ei_enc_expected[] =
1244 { GSM0808_IE_ENCRYPTION_INFORMATION, 0x09, 0x03, 0xaa, 0xbb,
1245 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42
1246 };
1247 uint8_t rc_enc;
1248 int rc_dec;
1249
Philipp Maier14e76b92017-03-28 18:36:52 +02001250 msg = msgb_alloc(1024, "output buffer");
1251 rc_enc = gsm0808_enc_encrypt_info(msg, &enc_ei);
1252 OSMO_ASSERT(rc_enc == 11);
1253 OSMO_ASSERT(memcmp(ei_enc_expected, msg->data, msg->len) == 0);
1254
1255 rc_dec = gsm0808_dec_encrypt_info(&dec_ei, msg->data + 2, msg->len - 2);
1256 OSMO_ASSERT(rc_dec == 9);
1257
1258 OSMO_ASSERT(memcmp(&enc_ei, &dec_ei, sizeof(enc_ei)) == 0);
1259
1260 msgb_free(msg);
1261}
1262
Harald Weltee61d4592022-11-03 11:05:58 +01001263static void test_gsm0808_dec_cell_id_list_srvcc(void)
Harald Weltea87526d2022-02-15 20:06:16 +01001264{
1265 /* taken from a pcap file of a real-world 3rd party MSC (SYS#5838) */
1266 const uint8_t enc_cil[] = { 0x0b, 0x2, 0xf2, 0x10, 0x4e, 0x20, 0x15, 0xbe};
1267 struct gsm0808_cell_id_list2 dec_cil;
1268 int rc;
1269
1270 rc = gsm0808_dec_cell_id_list2(&dec_cil, enc_cil, sizeof(enc_cil));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001271 OSMO_ASSERT(rc == sizeof(enc_cil));
1272 OSMO_ASSERT(dec_cil.id_discr = CELL_IDENT_SAI);
1273 OSMO_ASSERT(dec_cil.id_list_len = 1);
Harald Weltea87526d2022-02-15 20:06:16 +01001274}
1275
Harald Weltee61d4592022-11-03 11:05:58 +01001276static void test_gsm0808_enc_dec_cell_id_list_lac(void)
Philipp Maier783047e2017-03-29 11:35:50 +02001277{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001278 struct gsm0808_cell_id_list2 enc_cil;
1279 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001280 struct msgb *msg;
1281 uint8_t rc_enc;
1282 int rc_dec;
1283
1284 memset(&enc_cil, 0, sizeof(enc_cil));
1285 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001286 enc_cil.id_list[0].lac = 0x0124;
Neels Hofmeyrdb2fa4e2018-04-13 04:11:20 +02001287 enc_cil.id_list[1].lac = 0xABCD;
1288 enc_cil.id_list[2].lac = 0x5678;
Philipp Maier783047e2017-03-29 11:35:50 +02001289 enc_cil.id_list_len = 3;
1290
1291 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001292 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Neels Hofmeyrdb2fa4e2018-04-13 04:11:20 +02001293 EXPECT_ENCODED("1a 07 05 01 24 ab cd 56 78");
Philipp Maier783047e2017-03-29 11:35:50 +02001294
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001295 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001296 OSMO_ASSERT(rc_dec == 7);
1297
1298 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1299
1300 msgb_free(msg);
1301}
1302
Harald Weltee61d4592022-11-03 11:05:58 +01001303static void test_gsm0808_enc_dec_cell_id_list_single_lac(void)
Philipp Maier783047e2017-03-29 11:35:50 +02001304{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001305 struct gsm0808_cell_id_list2 enc_cil;
1306 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001307 struct msgb *msg;
1308 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x03,
1309 0x05, 0x23, 0x42
1310 };
1311 uint8_t rc_enc;
1312 int rc_dec;
1313
1314 memset(&enc_cil, 0, sizeof(enc_cil));
1315 enc_cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001316 enc_cil.id_list[0].lac = 0x2342;
Philipp Maier783047e2017-03-29 11:35:50 +02001317 enc_cil.id_list_len = 1;
1318
1319 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001320 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +02001321 OSMO_ASSERT(rc_enc == 5);
1322 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1323
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001324 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001325 OSMO_ASSERT(rc_dec == 3);
1326
1327 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1328
1329 msgb_free(msg);
1330}
1331
Harald Weltee61d4592022-11-03 11:05:58 +01001332static void test_gsm0808_enc_dec_cell_id_list_multi_lac(void)
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001333{
1334 struct gsm0808_cell_id_list2 enc_cil;
1335 struct gsm0808_cell_id_list2 dec_cil;
1336 struct msgb *msg;
1337 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x0b, 0x05,
1338 0x23, 0x42,
1339 0x24, 0x43,
1340 0x25, 0x44,
1341 0x26, 0x45,
1342 0x27, 0x46
1343 };
1344 uint8_t rc_enc;
1345 int rc_dec;
1346
1347 memset(&enc_cil, 0, sizeof(enc_cil));
1348 enc_cil.id_discr = CELL_IDENT_LAC;
1349 enc_cil.id_list[0].lac = 0x2342;
1350 enc_cil.id_list[1].lac = 0x2443;
1351 enc_cil.id_list[2].lac = 0x2544;
1352 enc_cil.id_list[3].lac = 0x2645;
1353 enc_cil.id_list[4].lac = 0x2746;
1354 enc_cil.id_list_len = 5;
1355
1356 msg = msgb_alloc(1024, "output buffer");
1357 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1358 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1359 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1360
1361 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1362 OSMO_ASSERT(rc_dec == msg->len - 2);
1363 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1364
1365 msgb_free(msg);
1366}
1367
Harald Weltee61d4592022-11-03 11:05:58 +01001368static void test_gsm0808_enc_dec_cell_id_list_bss(void)
Philipp Maier783047e2017-03-29 11:35:50 +02001369{
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001370 struct gsm0808_cell_id_list2 enc_cil;
1371 struct gsm0808_cell_id_list2 dec_cil;
Philipp Maier783047e2017-03-29 11:35:50 +02001372 struct msgb *msg;
1373 uint8_t rc_enc;
1374 int rc_dec;
1375
1376 memset(&enc_cil, 0, sizeof(enc_cil));
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001377 enc_cil.id_discr = CELL_IDENT_BSS;
Philipp Maier783047e2017-03-29 11:35:50 +02001378
1379 msg = msgb_alloc(1024, "output buffer");
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001380 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
Philipp Maier783047e2017-03-29 11:35:50 +02001381 OSMO_ASSERT(rc_enc == 3);
1382
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001383 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
Philipp Maier783047e2017-03-29 11:35:50 +02001384 OSMO_ASSERT(rc_dec == 1);
1385
1386 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1387
1388 msgb_free(msg);
1389}
1390
Harald Weltee61d4592022-11-03 11:05:58 +01001391static void test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac(void)
Stefan Sperling23381452018-03-15 19:38:15 +01001392{
1393 struct gsm0808_cell_id_list2 enc_cil;
1394 struct gsm0808_cell_id_list2 dec_cil;
1395 struct osmo_location_area_id id;
1396 struct msgb *msg;
1397 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x10, 0x04,
1398 0x92, 0x61, 0x54, 0x23, 0x42,
1399 0x92, 0x72, 0x54, 0x24, 0x43,
1400 0x92, 0x83, 0x54, 0x25, 0x44
1401 };
1402 uint8_t rc_enc;
1403 int rc_dec, i;
1404
1405 memset(&enc_cil, 0, sizeof(enc_cil));
1406 enc_cil.id_discr = CELL_IDENT_LAI_AND_LAC;
1407
1408 id.plmn.mcc = 0x123;
1409 osmo_mnc_from_str("456", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1410 id.lac = 0x2342;
1411 memcpy(&enc_cil.id_list[0].lai_and_lac, &id, sizeof(id));
1412
1413 id.plmn.mcc = 0x124;
1414 osmo_mnc_from_str("457", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1415 id.lac = 0x2443;
1416 memcpy(&enc_cil.id_list[1].lai_and_lac, &id, sizeof(id));
1417
1418 id.plmn.mcc = 0x125;
1419 osmo_mnc_from_str("458", &id.plmn.mnc, &id.plmn.mnc_3_digits);
1420 id.lac = 0x2544;
1421 memcpy(&enc_cil.id_list[2].lai_and_lac, &id, sizeof(id));
1422
1423 enc_cil.id_list_len = 3;
1424
1425 msg = msgb_alloc(1024, "output buffer");
1426 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1427 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1428 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1429
1430 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1431 OSMO_ASSERT(rc_dec == msg->len - 2);
1432
1433 OSMO_ASSERT(dec_cil.id_list_len == 3);
1434 /* Check MAXLEN elements to ensure everything has been initialized. */
1435 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1436 struct osmo_location_area_id *enc_id;
1437 struct osmo_location_area_id *dec_id;
1438 enc_id = &enc_cil.id_list[i].lai_and_lac;
1439 dec_id = &dec_cil.id_list[i].lai_and_lac;
1440 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->plmn, &dec_id->plmn) == 0);
1441 OSMO_ASSERT(enc_id->lac == dec_id->lac);
1442 }
1443
1444 msgb_free(msg);
1445}
1446
Harald Weltee61d4592022-11-03 11:05:58 +01001447static void test_gsm0808_enc_dec_cell_id_list_multi_ci(void)
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001448{
1449 struct gsm0808_cell_id_list2 enc_cil;
1450 struct gsm0808_cell_id_list2 dec_cil;
1451 struct msgb *msg;
1452 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x09, 0x02,
1453 0x00, 0x01,
1454 0x00, 0x02,
1455 0x00, 0x77,
1456 0x01, 0xff,
1457 };
1458 uint8_t rc_enc;
1459 int rc_dec;
1460
1461 memset(&enc_cil, 0, sizeof(enc_cil));
1462 enc_cil.id_discr = CELL_IDENT_CI;
1463 enc_cil.id_list[0].ci = 1;
1464 enc_cil.id_list[1].ci = 2;
1465 enc_cil.id_list[2].ci = 119;
1466 enc_cil.id_list[3].ci = 511;
1467 enc_cil.id_list_len = 4;
1468
1469 msg = msgb_alloc(1024, "output buffer");
1470 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1471 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1472 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1473
1474 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1475 OSMO_ASSERT(rc_dec == msg->len - 2);
1476 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1477
1478 msgb_free(msg);
1479}
1480
Harald Weltee61d4592022-11-03 11:05:58 +01001481static void test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci(void)
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001482{
1483 struct gsm0808_cell_id_list2 enc_cil;
1484 struct gsm0808_cell_id_list2 dec_cil;
1485 struct msgb *msg;
1486 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x15, 0x01,
1487 0x23, 0x42, 0x00, 0x01,
1488 0x24, 0x43, 0x00, 0x02,
1489 0x25, 0x44, 0x00, 0x77,
1490 0x26, 0x45, 0x01, 0xff,
1491 0x27, 0x46, 0x02, 0xfe,
1492 };
1493 uint8_t rc_enc;
1494 int rc_dec;
1495
1496 memset(&enc_cil, 0, sizeof(enc_cil));
1497 enc_cil.id_discr = CELL_IDENT_LAC_AND_CI;
1498 enc_cil.id_list[0].lac_and_ci.lac = 0x2342;
1499 enc_cil.id_list[0].lac_and_ci.ci = 1;
1500 enc_cil.id_list[1].lac_and_ci.lac = 0x2443;
1501 enc_cil.id_list[1].lac_and_ci.ci = 2;
1502 enc_cil.id_list[2].lac_and_ci.lac = 0x2544;
1503 enc_cil.id_list[2].lac_and_ci.ci = 119;
1504 enc_cil.id_list[3].lac_and_ci.lac = 0x2645;
1505 enc_cil.id_list[3].lac_and_ci.ci = 511;
1506 enc_cil.id_list[4].lac_and_ci.lac = 0x2746;
1507 enc_cil.id_list[4].lac_and_ci.ci = 766;
1508 enc_cil.id_list_len = 5;
1509
1510 msg = msgb_alloc(1024, "output buffer");
1511 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1512 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
1513 OSMO_ASSERT(memcmp(cil_enc_expected, msg->data, msg->len) == 0);
1514
1515 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1516 OSMO_ASSERT(rc_dec == msg->len - 2);
1517 OSMO_ASSERT(memcmp(&enc_cil, &dec_cil, sizeof(enc_cil)) == 0);
1518
1519 msgb_free(msg);
1520}
1521
Harald Weltee61d4592022-11-03 11:05:58 +01001522static void test_gsm0808_enc_dec_cell_id_list_multi_global(void)
Stefan Sperling483f3862018-03-16 12:21:26 +01001523{
1524 struct gsm0808_cell_id_list2 enc_cil;
1525 struct gsm0808_cell_id_list2 dec_cil;
1526 struct msgb *msg;
1527 uint8_t cil_enc_expected[] = { GSM0808_IE_CELL_IDENTIFIER_LIST, 0x16, 0x00,
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001528 0x21, 0x63, 0x54, 0x23, 0x42, 0x00, 0x1,
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001529 0x21, 0xf4, 0x75, 0x24, 0x43, 0x00, 0x2,
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +01001530 0x21, 0x75, 0x00, 0x25, 0x44, 0x00, 0x77
Stefan Sperling483f3862018-03-16 12:21:26 +01001531 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001532 uint8_t rc_enc;
1533 int rc_dec, i;
1534
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001535 enc_cil = (struct gsm0808_cell_id_list2){
1536 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1537 .id_list_len = 3,
1538 .id_list = {
1539 {
1540 .global = {
Neels Hofmeyr473485c2018-03-23 02:04:18 +01001541 .lai = { .plmn = { .mcc = 123, .mnc = 456 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001542 .lac = 0x2342 },
1543 .cell_identity = 1,
1544 }
1545 },
1546 {
1547 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001548 .lai = { .plmn = { .mcc = 124, .mnc = 57 },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001549 .lac = 0x2443 },
1550 .cell_identity = 2,
1551 }
1552 },
1553 {
1554 .global = {
Neels Hofmeyrc44fc232018-03-23 02:15:12 +01001555 .lai = { .plmn = { .mcc = 125, .mnc = 7,
1556 .mnc_3_digits = true },
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001557 .lac = 0x2544 },
1558 .cell_identity = 119,
1559 }
1560 },
1561 }
1562 };
Stefan Sperling483f3862018-03-16 12:21:26 +01001563
1564 msg = msgb_alloc(1024, "output buffer");
1565 rc_enc = gsm0808_enc_cell_id_list2(msg, &enc_cil);
1566 OSMO_ASSERT(rc_enc == sizeof(cil_enc_expected));
Neels Hofmeyrc1991df2018-03-23 02:00:00 +01001567 if (memcmp(cil_enc_expected, msg->data, msg->len)) {
1568 printf(" got: %s\n", osmo_hexdump(msg->data, msg->len));
1569 printf("expect: %s\n", osmo_hexdump(cil_enc_expected, sizeof(cil_enc_expected)));
1570 OSMO_ASSERT(false);
1571 }
Stefan Sperling483f3862018-03-16 12:21:26 +01001572
1573 rc_dec = gsm0808_dec_cell_id_list2(&dec_cil, msg->data + 2, msg->len - 2);
1574 OSMO_ASSERT(rc_dec == msg->len - 2);
1575
1576 /* Check MAXLEN elements to ensure everything has been initialized. */
1577 for (i = 0; i < GSM0808_CELL_ID_LIST2_MAXLEN; i++) {
1578 struct osmo_cell_global_id *enc_id;
1579 struct osmo_cell_global_id *dec_id;
1580 enc_id = &enc_cil.id_list[i].global;
1581 dec_id = &dec_cil.id_list[i].global;
1582 OSMO_ASSERT(osmo_plmn_cmp(&enc_id->lai.plmn, &dec_id->lai.plmn) == 0);
1583 OSMO_ASSERT(enc_id->lai.lac == dec_id->lai.lac);
1584 OSMO_ASSERT(enc_id->cell_identity == dec_id->cell_identity);
1585 }
1586
1587 msgb_free(msg);
1588}
1589
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001590static void print_cil(const struct gsm0808_cell_id_list2 *cil)
1591{
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001592 printf(" cell_id_list == %s\n", gsm0808_cell_id_list_name(cil));
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001593}
1594
Harald Weltee61d4592022-11-03 11:05:58 +01001595void test_cell_id_list_add(void) {
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001596 size_t zu;
1597
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001598 const struct gsm0808_cell_id_list2 cgi1 = {
1599 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1600 .id_list_len = 1,
1601 .id_list = {
1602 {
1603 .global = {
1604 .lai = {
1605 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = false },
1606 .lac = 3,
1607 },
1608 .cell_identity = 4,
1609 }
1610 },
1611 },
1612 };
1613
1614 const struct gsm0808_cell_id_list2 cgi2 = {
1615 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1616 .id_list_len = 2,
1617 .id_list = {
1618 {
1619 .global = {
1620 .lai = {
1621 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = true },
1622 .lac = 3,
1623 },
1624 .cell_identity = 4,
1625 }
1626 },
1627 {
1628 .global = {
1629 .lai = {
1630 .plmn = { .mcc = 5, .mnc = 6, .mnc_3_digits = true },
1631 .lac = 7,
1632 },
1633 .cell_identity = 8,
1634 }
1635 },
1636 },
1637 };
1638
1639 const struct gsm0808_cell_id_list2 cgi2a = {
1640 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1641 .id_list_len = 2,
1642 .id_list = {
1643 {
1644 .global = cgi2.id_list[0].global
1645 },
1646 {
1647 .global = {
1648 .lai = {
1649 .plmn = { .mcc = 9, .mnc = 10, .mnc_3_digits = true },
1650 .lac = 11,
1651 },
1652 .cell_identity = 12,
1653 }
1654 },
1655 },
1656 };
1657
1658 const struct gsm0808_cell_id_list2 cgi3 = {
1659 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1660 .id_list_len = 2,
1661 .id_list = {
1662 {
1663 .global = {
1664 .lai = {
1665 .plmn = { .mcc = 13, .mnc = 14, .mnc_3_digits = true },
1666 .lac = 15,
1667 },
1668 .cell_identity = 16,
1669 }
1670 },
1671 {
1672 .global = {
1673 .lai = {
1674 .plmn = { .mcc = 16, .mnc = 17, .mnc_3_digits = true },
1675 .lac = 18,
1676 },
1677 .cell_identity = 19,
1678 }
1679 },
1680 },
1681 };
1682
1683
1684 const struct gsm0808_cell_id_list2 lac1 = {
1685 .id_discr = CELL_IDENT_LAC,
1686 .id_list_len = 1,
1687 .id_list = {
1688 {
1689 .lac = 123
1690 },
1691 },
1692 };
1693
1694 const struct gsm0808_cell_id_list2 lac2 = {
1695 .id_discr = CELL_IDENT_LAC,
1696 .id_list_len = 2,
1697 .id_list = {
1698 {
1699 .lac = 456
1700 },
1701 {
1702 .lac = 789
1703 },
1704 },
1705 };
1706
1707 struct gsm0808_cell_id_list2 cil = {};
1708
1709 printf("------- %s\n", __func__);
1710
1711 print_cil(&cil);
1712
1713#define ADD_QUIET(other_cil, expect_rc) do { \
1714 int rc = gsm0808_cell_id_list_add(&cil, &other_cil); \
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001715 printf("gsm0808_cell_id_list_add(&cil, &" #other_cil ") --> rc = %d\n", rc); \
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001716 OSMO_ASSERT(rc == expect_rc); \
1717 } while(0)
1718
1719#define ADD(other_cil, expect_rc) ADD_QUIET(other_cil, expect_rc); print_cil(&cil)
1720
1721 ADD(lac1, 1);
1722 ADD(lac1, 0);
1723 ADD(lac2, 2);
1724 ADD(lac2, 0);
1725 ADD(cil, 0);
1726 ADD(cgi1, -EINVAL);
1727
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001728 printf("* can't add to BSS list\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001729 cil.id_list_len = 0;
1730 cil.id_discr = CELL_IDENT_BSS;
1731 print_cil(&cil);
1732 ADD(lac1, -EINVAL);
1733
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001734 printf("* other types (including NO_CELL) take on new type iff empty\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001735 cil.id_list_len = 0;
1736 cil.id_discr = CELL_IDENT_NO_CELL;
1737 print_cil(&cil);
1738 ADD(cgi1, 1);
1739 ADD(cgi1, 0);
1740 ADD(cgi2, 2);
1741 ADD(cgi2, 0);
1742
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001743 printf("* test gsm0808_cell_id_list_name_buf()'s return val\n");
1744 zu = strlen(gsm0808_cell_id_list_name(&cil));
1745 printf(" strlen(gsm0808_cell_id_list_name(cil)) == %zu\n", zu);
1746 zu ++;
1747 while (1) {
1748 char buf[128] = "?";
1749 int rc;
1750 OSMO_ASSERT(zu < sizeof(buf));
1751 buf[zu] = '#';
1752 rc = gsm0808_cell_id_list_name_buf(buf, zu, &cil);
1753 printf(" gsm0808_cell_id_list_name_buf(buf, %zu, cil)) == %d \"%s\"\n",
1754 zu, rc, buf);
1755 OSMO_ASSERT(buf[zu] == '#');
1756 if (!zu)
1757 break;
1758 zu /= 2;
1759 }
1760
1761 printf("* list-full behavior\n");
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001762 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001763 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001764 ADD_QUIET(cgi2a, 1);
1765 printf("cil.id_list_len = %u\n", cil.id_list_len);
1766
1767 cil.id_list_len = GSM0808_CELL_ID_LIST2_MAXLEN - 1;
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001768 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001769 ADD_QUIET(cgi3, -ENOSPC);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001770 printf("cil.id_list_len = %u\n", cil.id_list_len);
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001771 ADD_QUIET(cgi2a, -ENOSPC);
1772 printf("cil.id_list_len = %u\n", cil.id_list_len);
1773
1774 printf("------- %s done\n", __func__);
1775}
1776
Harald Weltee61d4592022-11-03 11:05:58 +01001777static void test_gsm0808_enc_dec_cell_id_lac(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001778{
1779 struct gsm0808_cell_id enc_ci = {
1780 .id_discr = CELL_IDENT_LAC,
1781 .id.lac = 0x0124,
1782 };
1783 struct gsm0808_cell_id dec_ci;
1784 struct msgb *msg;
1785 uint8_t rc_enc;
1786 int rc_dec;
1787
1788 memset(&dec_ci, 0xa5, sizeof(dec_ci));
1789
1790 msg = msgb_alloc(1024, "output buffer");
1791 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1792 EXPECT_ENCODED("05 03 05 01 24");
1793
1794 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1795 OSMO_ASSERT(rc_dec == 3);
1796
1797 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1798 && enc_ci.id.lac == dec_ci.id.lac);
1799
1800 msgb_free(msg);
1801}
1802
Harald Weltee61d4592022-11-03 11:05:58 +01001803static void test_gsm0808_enc_dec_cell_id_bss(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001804{
1805 struct gsm0808_cell_id enc_ci = {
1806 .id_discr = CELL_IDENT_BSS,
1807 };
1808 struct gsm0808_cell_id dec_ci;
1809 struct msgb *msg;
1810 uint8_t rc_enc;
1811 int rc_dec;
1812
1813 msg = msgb_alloc(1024, "output buffer");
1814 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1815 EXPECT_ENCODED("05 01 06");
1816
1817 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1818 OSMO_ASSERT(rc_dec == 1);
1819
1820 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr);
1821
1822 msgb_free(msg);
1823}
1824
Harald Weltee61d4592022-11-03 11:05:58 +01001825static void test_gsm0808_enc_dec_cell_id_no_cell(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001826{
1827 struct gsm0808_cell_id enc_ci = {
1828 .id_discr = CELL_IDENT_NO_CELL,
1829 };
1830 struct gsm0808_cell_id dec_ci;
1831 struct msgb *msg;
1832 uint8_t rc_enc;
1833 int rc_dec;
1834
1835 msg = msgb_alloc(1024, "output buffer");
1836 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1837 EXPECT_ENCODED("05 01 03");
1838
1839 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1840 OSMO_ASSERT(rc_dec == 1);
1841
1842 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr);
1843
1844 msgb_free(msg);
1845}
1846
Harald Weltee61d4592022-11-03 11:05:58 +01001847static void test_gsm0808_enc_dec_cell_id_lai_and_lac(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001848{
1849 struct gsm0808_cell_id enc_ci = {
1850 .id_discr = CELL_IDENT_LAI_AND_LAC,
1851 .id.lai_and_lac = {
1852 .plmn = {
1853 .mcc = 123,
1854 .mnc = 456,
1855 },
1856 .lac = 0x2342,
1857 },
1858 };
1859 struct gsm0808_cell_id dec_ci;
1860 struct msgb *msg;
1861 uint8_t rc_enc;
1862 int rc_dec;
1863
1864 msg = msgb_alloc(1024, "output buffer");
1865 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1866 EXPECT_ENCODED("05 06 04 21 63 54 23 42");
1867
1868 memset(&dec_ci, 0xa5, sizeof(dec_ci));
1869 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1870 OSMO_ASSERT(rc_dec == msg->len - 2);
1871
1872 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1873 && osmo_plmn_cmp(&enc_ci.id.lai_and_lac.plmn, &dec_ci.id.lai_and_lac.plmn) == 0
1874 && enc_ci.id.lai_and_lac.lac == dec_ci.id.lai_and_lac.lac);
1875 msgb_free(msg);
1876}
1877
Harald Weltee61d4592022-11-03 11:05:58 +01001878static void test_gsm0808_enc_dec_cell_id_ci(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001879{
1880 struct gsm0808_cell_id enc_ci = {
1881 .id_discr = CELL_IDENT_CI,
1882 .id.ci = 0x423,
1883 };
1884 struct gsm0808_cell_id dec_ci;
1885 struct msgb *msg;
1886 uint8_t rc_enc;
1887 int rc_dec;
1888
1889 msg = msgb_alloc(1024, "output buffer");
1890 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1891 EXPECT_ENCODED("05 03 02 04 23");
1892
1893 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1894 OSMO_ASSERT(rc_dec == msg->len - 2);
1895 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1896 && enc_ci.id.ci == dec_ci.id.ci);
1897
1898 msgb_free(msg);
1899}
1900
Harald Weltee61d4592022-11-03 11:05:58 +01001901static void test_gsm0808_enc_dec_cell_id_lac_and_ci(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001902{
1903 struct gsm0808_cell_id enc_ci = {
1904 .id_discr = CELL_IDENT_LAC_AND_CI,
1905 .id.lac_and_ci = {
1906 .lac = 0x423,
1907 .ci = 0x235,
1908 },
1909 };
1910 struct gsm0808_cell_id dec_ci;
1911 struct msgb *msg;
1912 uint8_t rc_enc;
1913 int rc_dec;
1914
1915 msg = msgb_alloc(1024, "output buffer");
1916 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1917 EXPECT_ENCODED("05 05 01 04 23 02 35");
1918
1919 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1920 OSMO_ASSERT(rc_dec == msg->len - 2);
1921 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1922 && enc_ci.id.lac_and_ci.lac == dec_ci.id.lac_and_ci.lac
1923 && enc_ci.id.lac_and_ci.ci == dec_ci.id.lac_and_ci.ci);
1924
1925 msgb_free(msg);
1926}
1927
Harald Weltee61d4592022-11-03 11:05:58 +01001928static void test_gsm0808_enc_dec_cell_id_global(void)
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001929{
1930 struct gsm0808_cell_id enc_ci = {
1931 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1932 .id.global = {
1933 .lai = {
1934 .plmn = { .mcc = 123, .mnc = 456 },
1935 .lac = 0x2342
1936 },
1937 .cell_identity = 0x423,
1938 }
1939 };
1940 struct gsm0808_cell_id dec_ci;
1941 struct msgb *msg;
1942 uint8_t rc_enc;
1943 int rc_dec;
1944
1945 msg = msgb_alloc(1024, "output buffer");
1946 rc_enc = gsm0808_enc_cell_id(msg, &enc_ci);
1947 EXPECT_ENCODED("05 08 00 21 63 54 23 42 04 23");
1948
1949 rc_dec = gsm0808_dec_cell_id(&dec_ci, msg->data + 2, msg->len - 2);
1950 OSMO_ASSERT(rc_dec == msg->len - 2);
1951
1952 OSMO_ASSERT(enc_ci.id_discr == dec_ci.id_discr
1953 && osmo_plmn_cmp(&enc_ci.id.global.lai.plmn,
1954 &dec_ci.id.global.lai.plmn) == 0
1955 && enc_ci.id.global.lai.lac == dec_ci.id.global.lai.lac
1956 && enc_ci.id.global.cell_identity == dec_ci.id.global.cell_identity);
1957 msgb_free(msg);
1958}
1959
Harald Weltee61d4592022-11-03 11:05:58 +01001960static void test_gsm0808_enc_dec_cell_id_global_ps(void)
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001961{
1962 struct gsm0808_cell_id enc_cgi = {
1963 .id_discr = CELL_IDENT_WHOLE_GLOBAL,
1964 .id.global = {
1965 .lai = {
1966 .plmn = { .mcc = 123, .mnc = 456 },
1967 .lac = 0x2342
1968 },
1969 .cell_identity = 0x423,
1970 }
1971 };
1972 struct gsm0808_cell_id enc_cgi_ps = {
1973 .id_discr = CELL_IDENT_WHOLE_GLOBAL_PS,
1974 .id.global_ps = {
1975 .rai = {
1976 .lac = {
1977 .plmn = { .mcc = 123, .mnc = 456 },
1978 .lac = 0x2342
1979 },
1980 .rac = 0xcc,
1981 },
1982 .cell_identity = 0x423,
1983 }
1984 };
1985 struct msgb *msg_cgi, *msg_cgi_ps;
1986 uint8_t rc_enc;
1987
1988 msg_cgi = msgb_alloc(1024, "output buffer (CGI)");
1989 rc_enc = gsm0808_enc_cell_id(msg_cgi, &enc_cgi);
1990 OSMO_ASSERT(rc_enc > 0);
1991
1992 msg_cgi_ps = msgb_alloc(1024, "output buffer (CGI-PS)");
1993 rc_enc = gsm0808_enc_cell_id(msg_cgi_ps, &enc_cgi_ps);
1994 OSMO_ASSERT(rc_enc > 0);
1995
1996 OSMO_ASSERT(msgb_eq(msg_cgi, msg_cgi_ps));
1997
1998 msgb_free(msg_cgi);
1999 msgb_free(msg_cgi_ps);
2000}
2001
Philipp Maier5f2eb152018-09-19 13:40:21 +02002002static void test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(struct gsm48_multi_rate_conf *cfg)
2003{
2004 uint16_t s15_s0;
2005
2006 printf("Input:\n");
2007 printf(" m4_75= %u smod= %u\n", cfg->m4_75, cfg->smod);
2008 printf(" m5_15= %u spare= %u\n", cfg->m5_15, cfg->spare);
2009 printf(" m5_90= %u icmi= %u\n", cfg->m5_90, cfg->icmi);
2010 printf(" m6_70= %u nscb= %u\n", cfg->m6_70, cfg->nscb);
2011 printf(" m7_40= %u ver= %u\n", cfg->m7_40, cfg->ver);
2012 printf(" m7_95= %u\n", cfg->m7_95);
2013 printf(" m10_2= %u\n", cfg->m10_2);
2014 printf(" m12_2= %u\n", cfg->m12_2);
2015
2016 s15_s0 = gsm0808_sc_cfg_from_gsm48_mr_cfg(cfg, true);
2017 printf("Result (fr):\n");
2018 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
2019 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
2020
2021 s15_s0 = gsm0808_sc_cfg_from_gsm48_mr_cfg(cfg, false);
2022 printf("Result (hr):\n");
2023 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
2024 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
2025
2026 printf("\n");
2027}
2028
2029static void test_gsm0808_sc_cfg_from_gsm48_mr_cfg(void)
2030{
2031 struct gsm48_multi_rate_conf cfg;
2032
2033 printf("Testing gsm0808_sc_cfg_from_gsm48_mr_cfg():\n");
2034
2035 memset(&cfg, 0, sizeof(cfg));
2036
2037 cfg.m4_75 = 0;
2038 cfg.m5_15 = 0;
2039 cfg.m5_90 = 0;
2040 cfg.m6_70 = 0;
2041 cfg.m7_40 = 0;
2042 cfg.m7_95 = 0;
2043 cfg.m10_2 = 0;
2044 cfg.m12_2 = 0;
2045 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2046
2047 cfg.m4_75 = 1;
2048 cfg.m5_15 = 0;
2049 cfg.m5_90 = 0;
2050 cfg.m6_70 = 0;
2051 cfg.m7_40 = 0;
2052 cfg.m7_95 = 0;
2053 cfg.m10_2 = 0;
2054 cfg.m12_2 = 0;
2055 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2056
2057 cfg.m4_75 = 0;
2058 cfg.m5_15 = 1;
2059 cfg.m5_90 = 0;
2060 cfg.m6_70 = 0;
2061 cfg.m7_40 = 0;
2062 cfg.m7_95 = 0;
2063 cfg.m10_2 = 0;
2064 cfg.m12_2 = 0;
2065 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2066
2067 cfg.m4_75 = 0;
2068 cfg.m5_15 = 0;
2069 cfg.m5_90 = 1;
2070 cfg.m6_70 = 0;
2071 cfg.m7_40 = 0;
2072 cfg.m7_95 = 0;
2073 cfg.m10_2 = 0;
2074 cfg.m12_2 = 0;
2075 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2076
2077 cfg.m4_75 = 0;
2078 cfg.m5_15 = 0;
2079 cfg.m5_90 = 0;
2080 cfg.m6_70 = 1;
2081 cfg.m7_40 = 0;
2082 cfg.m7_95 = 0;
2083 cfg.m10_2 = 0;
2084 cfg.m12_2 = 0;
2085 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2086
2087 cfg.m4_75 = 0;
2088 cfg.m5_15 = 0;
2089 cfg.m5_90 = 0;
2090 cfg.m6_70 = 0;
2091 cfg.m7_40 = 1;
2092 cfg.m7_95 = 0;
2093 cfg.m10_2 = 0;
2094 cfg.m12_2 = 0;
2095 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2096
2097 cfg.m4_75 = 0;
2098 cfg.m5_15 = 0;
2099 cfg.m5_90 = 0;
2100 cfg.m6_70 = 0;
2101 cfg.m7_40 = 0;
2102 cfg.m7_95 = 1;
2103 cfg.m10_2 = 0;
2104 cfg.m12_2 = 0;
2105 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2106
2107 cfg.m4_75 = 0;
2108 cfg.m5_15 = 0;
2109 cfg.m5_90 = 0;
2110 cfg.m6_70 = 0;
2111 cfg.m7_40 = 0;
2112 cfg.m7_95 = 0;
2113 cfg.m10_2 = 1;
2114 cfg.m12_2 = 0;
2115 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2116
2117 cfg.m4_75 = 0;
2118 cfg.m5_15 = 0;
2119 cfg.m5_90 = 0;
2120 cfg.m6_70 = 0;
2121 cfg.m7_40 = 0;
2122 cfg.m7_95 = 0;
2123 cfg.m10_2 = 0;
2124 cfg.m12_2 = 1;
2125 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2126
2127 cfg.m4_75 = 1;
2128 cfg.m5_15 = 1;
2129 cfg.m5_90 = 1;
2130 cfg.m6_70 = 1;
2131 cfg.m7_40 = 0;
2132 cfg.m7_95 = 0;
2133 cfg.m10_2 = 0;
2134 cfg.m12_2 = 0;
2135 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2136
2137 cfg.m4_75 = 0;
2138 cfg.m5_15 = 0;
2139 cfg.m5_90 = 0;
2140 cfg.m6_70 = 0;
2141 cfg.m7_40 = 1;
2142 cfg.m7_95 = 1;
2143 cfg.m10_2 = 1;
2144 cfg.m12_2 = 1;
2145 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2146
2147 cfg.m4_75 = 0;
2148 cfg.m5_15 = 0;
2149 cfg.m5_90 = 1;
2150 cfg.m6_70 = 1;
2151 cfg.m7_40 = 0;
2152 cfg.m7_95 = 0;
2153 cfg.m10_2 = 1;
2154 cfg.m12_2 = 1;
2155 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2156
2157 cfg.m4_75 = 1;
2158 cfg.m5_15 = 1;
2159 cfg.m5_90 = 0;
2160 cfg.m6_70 = 0;
2161 cfg.m7_40 = 1;
2162 cfg.m7_95 = 1;
2163 cfg.m10_2 = 0;
2164 cfg.m12_2 = 0;
2165 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2166
2167 cfg.m4_75 = 0;
2168 cfg.m5_15 = 1;
2169 cfg.m5_90 = 0;
2170 cfg.m6_70 = 1;
2171 cfg.m7_40 = 0;
2172 cfg.m7_95 = 1;
2173 cfg.m10_2 = 0;
2174 cfg.m12_2 = 1;
2175 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2176
2177 cfg.m4_75 = 1;
2178 cfg.m5_15 = 0;
2179 cfg.m5_90 = 1;
2180 cfg.m6_70 = 0;
2181 cfg.m7_40 = 1;
2182 cfg.m7_95 = 0;
2183 cfg.m10_2 = 1;
2184 cfg.m12_2 = 0;
2185 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2186
2187 cfg.m4_75 = 1;
2188 cfg.m5_15 = 1;
2189 cfg.m5_90 = 1;
2190 cfg.m6_70 = 1;
2191 cfg.m7_40 = 1;
2192 cfg.m7_95 = 1;
2193 cfg.m10_2 = 1;
2194 cfg.m12_2 = 1;
2195 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
Philipp Maier94d79fd2019-03-01 10:40:48 +01002196
2197 cfg.m4_75 = 1;
2198 cfg.m5_15 = 0;
2199 cfg.m5_90 = 1;
2200 cfg.m6_70 = 0;
2201 cfg.m7_40 = 1;
2202 cfg.m7_95 = 0;
2203 cfg.m10_2 = 0;
2204 cfg.m12_2 = 1;
2205 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
2206
2207 cfg.m4_75 = 1;
2208 cfg.m5_15 = 0;
2209 cfg.m5_90 = 1;
2210 cfg.m6_70 = 0;
2211 cfg.m7_40 = 1;
2212 cfg.m7_95 = 0;
2213 cfg.m10_2 = 0;
2214 cfg.m12_2 = 0;
2215 test_gsm0808_sc_cfg_from_gsm48_mr_cfg_single(&cfg);
Pau Espin Pedrol1bd495a2021-02-15 16:15:59 +01002216
Philipp Maier5f2eb152018-09-19 13:40:21 +02002217}
2218
Philipp Maier8515d032018-09-25 15:57:49 +02002219static void test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(uint16_t s15_s0)
2220{
2221 struct gsm48_multi_rate_conf cfg;
Philipp Maier3713af82019-02-27 16:48:25 +01002222 int rc;
Philipp Maier8515d032018-09-25 15:57:49 +02002223
2224 printf("Input:\n");
2225 printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
2226 OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
2227
Philipp Maier3713af82019-02-27 16:48:25 +01002228 rc = gsm48_mr_cfg_from_gsm0808_sc_cfg(&cfg, s15_s0);
Philipp Maier8515d032018-09-25 15:57:49 +02002229
2230 printf("Output:\n");
2231 printf(" m4_75= %u smod= %u\n", cfg.m4_75, cfg.smod);
2232 printf(" m5_15= %u spare= %u\n", cfg.m5_15, cfg.spare);
2233 printf(" m5_90= %u icmi= %u\n", cfg.m5_90, cfg.icmi);
2234 printf(" m6_70= %u nscb= %u\n", cfg.m6_70, cfg.nscb);
2235 printf(" m7_40= %u ver= %u\n", cfg.m7_40, cfg.ver);
2236 printf(" m7_95= %u\n", cfg.m7_95);
2237 printf(" m10_2= %u\n", cfg.m10_2);
2238 printf(" m12_2= %u\n", cfg.m12_2);
2239
Philipp Maier3713af82019-02-27 16:48:25 +01002240 if (rc != 0)
2241 printf(" Result invalid!\n");
2242
Philipp Maier8515d032018-09-25 15:57:49 +02002243 printf("\n");
2244}
2245
Harald Weltee61d4592022-11-03 11:05:58 +01002246void test_gsm48_mr_cfg_from_gsm0808_sc_cfg(void)
Philipp Maier8515d032018-09-25 15:57:49 +02002247{
2248 printf("Testing gsm48_mr_cfg_from_gsm0808_sc_cfg():\n");
2249
Philipp Maier3713af82019-02-27 16:48:25 +01002250 /* Test with settings as defined in 3GPP TS 28.062, Table 7.11.3.1.3-2,
2251 * (up to four codecs may become selected) */
Philipp Maier8515d032018-09-25 15:57:49 +02002252 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2253 (GSM0808_SC_CFG_DEFAULT_AMR_4_75);
2254 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2255 (GSM0808_SC_CFG_DEFAULT_AMR_5_15);
2256 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2257 (GSM0808_SC_CFG_DEFAULT_AMR_5_90);
2258 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2259 (GSM0808_SC_CFG_DEFAULT_AMR_6_70);
2260 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2261 (GSM0808_SC_CFG_DEFAULT_AMR_7_40);
2262 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2263 (GSM0808_SC_CFG_DEFAULT_AMR_7_95);
2264 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2265 (GSM0808_SC_CFG_DEFAULT_AMR_10_2);
2266 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
2267 (GSM0808_SC_CFG_DEFAULT_AMR_12_2);
2268
Philipp Maier3713af82019-02-27 16:48:25 +01002269 /* Test with settings as defined in 3GPP TS 28.062, Table 7.11.3.1.3-2,
2270 * but pick only one distinctive setting at a time */
2271 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_4_75);
Philipp Maier8515d032018-09-25 15:57:49 +02002272 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
Philipp Maier3713af82019-02-27 16:48:25 +01002273 (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20);
2274 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_5_90);
2275 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_6_70);
2276 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40);
2277 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_95);
2278 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_10_2);
2279 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_12_2);
2280
2281 /* Arbitrary, but valid combinations */
2282 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40 |
2283 GSM0808_SC_CFG_AMR_6_70 |
2284 GSM0808_SC_CFG_AMR_10_2);
2285 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_95 |
2286 GSM0808_SC_CFG_AMR_4_75);
2287 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_5_90 |
2288 GSM0808_SC_CFG_AMR_12_2);
Philipp Maier8515d032018-09-25 15:57:49 +02002289 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
Philipp Maier3713af82019-02-27 16:48:25 +01002290 (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 | GSM0808_SC_CFG_AMR_5_90 |
2291 GSM0808_SC_CFG_AMR_12_2);
2292
2293 /* Invalid combinations */
Philipp Maier8515d032018-09-25 15:57:49 +02002294 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
Philipp Maier3713af82019-02-27 16:48:25 +01002295 (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 | GSM0808_SC_CFG_AMR_6_70);
2296 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40 |
2297 GSM0808_SC_CFG_AMR_6_70 |
2298 GSM0808_SC_CFG_AMR_10_2 |
2299 GSM0808_SC_CFG_AMR_7_95 |
2300 GSM0808_SC_CFG_AMR_4_75);
2301 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(0x0000);
2302 test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(0xffff);
Philipp Maier8515d032018-09-25 15:57:49 +02002303}
2304
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002305struct test_cell_id_matching_data {
2306 struct gsm0808_cell_id id;
2307 struct gsm0808_cell_id match_id;
2308 bool expect_match;
2309 bool expect_exact_match;
2310};
2311
2312#define lac_23 { .id_discr = CELL_IDENT_LAC, .id.lac = 23, }
2313#define lac_42 { .id_discr = CELL_IDENT_LAC, .id.lac = 42, }
2314#define ci_5 { .id_discr = CELL_IDENT_CI, .id.ci = 5, }
2315#define ci_6 { .id_discr = CELL_IDENT_CI, .id.ci = 6, }
2316#define lac_ci_23_5 { \
2317 .id_discr = CELL_IDENT_LAC_AND_CI, \
2318 .id.lac_and_ci = { .lac = 23, .ci = 5, }, \
2319 }
2320#define lac_ci_42_6 { \
2321 .id_discr = CELL_IDENT_LAC_AND_CI, \
2322 .id.lac_and_ci = { .lac = 42, .ci = 6, }, \
2323 }
2324#define lai_23_042_23 { \
2325 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2326 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 23, }, \
2327 }
2328#define lai_23_042_42 { \
2329 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2330 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 42, }, \
2331 }
2332#define lai_23_99_23 { \
2333 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2334 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 99, .mnc_3_digits = false }, .lac = 23, }, \
2335 }
2336#define lai_23_42_23 { \
2337 .id_discr = CELL_IDENT_LAI_AND_LAC, \
2338 .id.lai_and_lac = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = false }, .lac = 23, }, \
2339 }
2340#define cgi_23_042_23_5 { \
2341 .id_discr = CELL_IDENT_WHOLE_GLOBAL, \
2342 .id.global = { \
2343 .lai = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 23, }, \
2344 .cell_identity = 5, \
2345 }, \
2346 }
2347#define cgi_23_042_42_6 { \
2348 .id_discr = CELL_IDENT_WHOLE_GLOBAL, \
2349 .id.global = { \
2350 .lai = { .plmn = { .mcc = 23, .mnc = 42, .mnc_3_digits = true }, .lac = 42, }, \
2351 .cell_identity = 6, \
2352 }, \
2353 }
2354#define cgi_23_99_23_5 { \
2355 .id_discr = CELL_IDENT_WHOLE_GLOBAL, \
2356 .id.global = { \
2357 .lai = { .plmn = { .mcc = 23, .mnc = 99, .mnc_3_digits = false }, .lac = 23, }, \
2358 .cell_identity = 5, \
2359 }, \
2360 }
2361
2362
2363static const struct test_cell_id_matching_data test_cell_id_matching_tests[] = {
2364 { .id = lac_23, .match_id = lac_23, .expect_match = true, .expect_exact_match = true },
2365 { .id = lac_23, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2366 { .id = lac_23, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2367 { .id = lac_23, .match_id = ci_6, .expect_match = true, .expect_exact_match = false },
2368 { .id = lac_23, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2369 { .id = lac_23, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2370 { .id = lac_23, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2371 { .id = lac_23, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2372 { .id = lac_23, .match_id = lai_23_99_23, .expect_match = true, .expect_exact_match = false },
2373 { .id = lac_23, .match_id = lai_23_42_23, .expect_match = true, .expect_exact_match = false },
2374 { .id = lac_23, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2375 { .id = lac_23, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2376 { .id = lac_23, .match_id = cgi_23_99_23_5, .expect_match = true, .expect_exact_match = false },
2377 { .id = ci_5, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2378 { .id = ci_5, .match_id = lac_42, .expect_match = true, .expect_exact_match = false },
2379 { .id = ci_5, .match_id = ci_5, .expect_match = true, .expect_exact_match = true },
2380 { .id = ci_5, .match_id = ci_6, .expect_match = false, .expect_exact_match = false },
2381 { .id = ci_5, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2382 { .id = ci_5, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2383 { .id = ci_5, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2384 { .id = ci_5, .match_id = lai_23_042_42, .expect_match = true, .expect_exact_match = false },
2385 { .id = ci_5, .match_id = lai_23_99_23, .expect_match = true, .expect_exact_match = false },
2386 { .id = ci_5, .match_id = lai_23_42_23, .expect_match = true, .expect_exact_match = false },
2387 { .id = ci_5, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2388 { .id = ci_5, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2389 { .id = ci_5, .match_id = cgi_23_99_23_5, .expect_match = true, .expect_exact_match = false },
2390 { .id = lac_ci_23_5, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2391 { .id = lac_ci_23_5, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2392 { .id = lac_ci_23_5, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2393 { .id = lac_ci_23_5, .match_id = ci_6, .expect_match = false, .expect_exact_match = false },
2394 { .id = lac_ci_23_5, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = true },
2395 { .id = lac_ci_23_5, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2396 { .id = lac_ci_23_5, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2397 { .id = lac_ci_23_5, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2398 { .id = lac_ci_23_5, .match_id = lai_23_99_23, .expect_match = true, .expect_exact_match = false },
2399 { .id = lac_ci_23_5, .match_id = lai_23_42_23, .expect_match = true, .expect_exact_match = false },
2400 { .id = lac_ci_23_5, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2401 { .id = lac_ci_23_5, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2402 { .id = lac_ci_23_5, .match_id = cgi_23_99_23_5, .expect_match = true, .expect_exact_match = false },
2403 { .id = lai_23_042_23, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2404 { .id = lai_23_042_23, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2405 { .id = lai_23_042_23, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2406 { .id = lai_23_042_23, .match_id = ci_6, .expect_match = true, .expect_exact_match = false },
2407 { .id = lai_23_042_23, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2408 { .id = lai_23_042_23, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2409 { .id = lai_23_042_23, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = true },
2410 { .id = lai_23_042_23, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2411 { .id = lai_23_042_23, .match_id = lai_23_99_23, .expect_match = false, .expect_exact_match = false },
2412 { .id = lai_23_042_23, .match_id = lai_23_42_23, .expect_match = false, .expect_exact_match = false },
2413 { .id = lai_23_042_23, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = false },
2414 { .id = lai_23_042_23, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2415 { .id = lai_23_042_23, .match_id = cgi_23_99_23_5, .expect_match = false, .expect_exact_match = false },
2416 { .id = cgi_23_042_23_5, .match_id = lac_23, .expect_match = true, .expect_exact_match = false },
2417 { .id = cgi_23_042_23_5, .match_id = lac_42, .expect_match = false, .expect_exact_match = false },
2418 { .id = cgi_23_042_23_5, .match_id = ci_5, .expect_match = true, .expect_exact_match = false },
2419 { .id = cgi_23_042_23_5, .match_id = ci_6, .expect_match = false, .expect_exact_match = false },
2420 { .id = cgi_23_042_23_5, .match_id = lac_ci_23_5, .expect_match = true, .expect_exact_match = false },
2421 { .id = cgi_23_042_23_5, .match_id = lac_ci_42_6, .expect_match = false, .expect_exact_match = false },
2422 { .id = cgi_23_042_23_5, .match_id = lai_23_042_23, .expect_match = true, .expect_exact_match = false },
2423 { .id = cgi_23_042_23_5, .match_id = lai_23_042_42, .expect_match = false, .expect_exact_match = false },
2424 { .id = cgi_23_042_23_5, .match_id = lai_23_99_23, .expect_match = false, .expect_exact_match = false },
2425 { .id = cgi_23_042_23_5, .match_id = lai_23_42_23, .expect_match = false, .expect_exact_match = false },
2426 { .id = cgi_23_042_23_5, .match_id = cgi_23_042_23_5, .expect_match = true, .expect_exact_match = true },
2427 { .id = cgi_23_042_23_5, .match_id = cgi_23_042_42_6, .expect_match = false, .expect_exact_match = false },
2428 { .id = cgi_23_042_23_5, .match_id = cgi_23_99_23_5, .expect_match = false, .expect_exact_match = false },
2429};
2430
Harald Weltee61d4592022-11-03 11:05:58 +01002431static void test_cell_id_matching(void)
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002432{
2433 int i;
2434 bool ok = true;
2435 printf("\n%s\n", __func__);
2436
2437 for (i = 0; i < ARRAY_SIZE(test_cell_id_matching_tests); i++) {
2438 const struct test_cell_id_matching_data *d = &test_cell_id_matching_tests[i];
2439 int exact_match;
2440
2441 for (exact_match = 0; exact_match < 2; exact_match++) {
2442 bool result;
2443 bool expect_result = exact_match ? d->expect_exact_match : d->expect_match;
2444
2445 result = gsm0808_cell_ids_match(&d->id, &d->match_id, (bool)exact_match);
2446
2447 printf("[%d] %s %s %s%s\n",
2448 i,
2449 gsm0808_cell_id_name(&d->id),
2450 gsm0808_cell_id_name2(&d->match_id),
2451 result ? "MATCH" : "don't match",
2452 exact_match ? " exactly" : "");
2453 if (result != expect_result) {
2454 printf(" ERROR: expected %s\n", d->expect_match ? "MATCH" : "no match");
2455 ok = false;
2456 }
2457 }
2458 }
2459
2460 OSMO_ASSERT(ok);
2461}
2462
2463static bool test_cell_id_list_matching_discrs(bool test_match,
2464 enum CELL_IDENT id_discr,
2465 enum CELL_IDENT list_discr)
2466{
2467 int i, j;
2468 const struct gsm0808_cell_id *id = NULL;
2469 struct gsm0808_cell_id_list2 list = {};
2470 int match_idx = -1;
2471 int result;
2472
2473 for (i = 0; i < ARRAY_SIZE(test_cell_id_matching_tests); i++) {
2474 const struct test_cell_id_matching_data *d = &test_cell_id_matching_tests[i];
2475 if (id_discr != d->id.id_discr)
2476 continue;
2477 id = &d->id;
2478 break;
2479 }
2480
2481 if (!id) {
2482 printf("Did not find any entry for %s\n", gsm0808_cell_id_discr_name(id_discr));
2483 return true;
2484 }
2485
2486 /* Collect those entries with exactly this id on the left, of type list_discr on the right.
2487 * Collect the mismatches first, for more interesting match indexes in the results. */
2488 for (j = 0; j < 2; j++) {
2489 bool collect_matches = (bool)j;
2490
2491 /* If we want to have a mismatching list, don't add any entries that match. */
2492 if (!test_match && collect_matches)
2493 continue;
2494
2495 for (i = 0; i < ARRAY_SIZE(test_cell_id_matching_tests); i++) {
2496 const struct test_cell_id_matching_data *d = &test_cell_id_matching_tests[i];
2497 struct gsm0808_cell_id_list2 add;
2498
2499 /* Ignore those with a different d->id */
2500 if (!gsm0808_cell_ids_match(&d->id, id, true))
2501 continue;
2502
2503 /* Ignore those with a different d->match_id discr */
2504 if (d->match_id.id_discr != list_discr)
2505 continue;
2506
2507 if (collect_matches != d->expect_match)
2508 continue;
2509
2510 if (match_idx < 0 && d->expect_match) {
2511 match_idx = list.id_list_len;
2512 }
2513
2514 gsm0808_cell_id_to_list(&add, &d->match_id);
2515 gsm0808_cell_id_list_add(&list, &add);
2516 }
2517 }
2518
2519 if (!list.id_list_len) {
2520 printf("%s vs. %s: No match_id entries to test %s\n",
2521 gsm0808_cell_id_name(id),
2522 gsm0808_cell_id_discr_name(list_discr),
2523 test_match ? "MATCH" : "mismatch");
2524 return true;
2525 }
2526
2527 result = gsm0808_cell_id_matches_list(id, &list, 0, false);
2528
2529 printf("%s and %s: ",
2530 gsm0808_cell_id_name(id),
2531 gsm0808_cell_id_list_name(&list));
2532 if (result >= 0)
2533 printf("MATCH at [%d]\n", result);
2534 else
2535 printf("mismatch\n");
2536
2537 if (test_match
2538 && (result < 0 || result != match_idx)) {
2539 printf(" ERROR: expected MATCH at %d\n", match_idx);
2540 return false;
2541 }
2542
2543 if (!test_match && result >= 0) {
2544 printf(" ERROR: expected mismatch\n");
2545 return false;
2546 }
2547
2548 return true;
2549}
2550
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002551const enum CELL_IDENT cell_ident_discrs[] = {
2552 CELL_IDENT_LAC, CELL_IDENT_CI, CELL_IDENT_LAC_AND_CI, CELL_IDENT_LAI_AND_LAC,
2553 CELL_IDENT_WHOLE_GLOBAL,
2554};
2555
2556
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002557static void test_cell_id_list_matching(bool test_match)
2558{
2559 int i, j;
2560 bool ok = true;
2561
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002562 printf("\n%s(%s)\n", __func__, test_match ? "test match" : "test mismatch");
2563
2564 /* Autogenerate Cell ID lists from above dataset, which should match / not match. */
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002565 for (i = 0; i < ARRAY_SIZE(cell_ident_discrs); i++) {
2566 for (j = 0; j < ARRAY_SIZE(cell_ident_discrs); j++)
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002567 if (!test_cell_id_list_matching_discrs(test_match,
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002568 cell_ident_discrs[i], cell_ident_discrs[j]))
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002569 ok = false;
2570 }
2571
2572 OSMO_ASSERT(ok);
2573}
2574
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002575
2576static const struct gsm0808_cell_id test_gsm0808_cell_id_to_from_cgi_data[] = {
2577 lac_23,
2578 lac_42,
2579 ci_5,
2580 ci_6,
2581 lac_ci_23_5,
2582 lac_ci_42_6,
2583 lai_23_042_23,
2584 lai_23_042_42,
2585 lai_23_99_23,
2586 lai_23_42_23,
2587 cgi_23_042_23_5,
2588 cgi_23_042_42_6,
2589 cgi_23_99_23_5,
2590 { .id_discr = CELL_IDENT_NO_CELL },
2591 { .id_discr = 423 },
2592};
2593
Harald Weltee61d4592022-11-03 11:05:58 +01002594static void test_gsm0808_cell_id_to_from_cgi(void)
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002595{
2596 int i;
2597 int j;
2598
2599 printf("\n%s()\n", __func__);
2600
2601 for (i = 0; i < ARRAY_SIZE(test_gsm0808_cell_id_to_from_cgi_data); i++) {
2602 const struct gsm0808_cell_id *from_cid = &test_gsm0808_cell_id_to_from_cgi_data[i];
2603 struct osmo_cell_global_id cgi = {
2604 .lai = {
2605 .plmn = {
2606 .mcc = 777,
2607 .mnc = 7,
2608 .mnc_3_digits = true,
2609 },
2610 .lac = 7777,
2611 },
2612 .cell_identity = 7777,
2613 };
2614 struct gsm0808_cell_id cid = {};
2615 int rc;
2616
2617 rc = gsm0808_cell_id_to_cgi(&cgi, from_cid);
2618 printf("cid %s -> cgi %s", gsm0808_cell_id_name(from_cid), osmo_cgi_name(&cgi));
2619
2620 if (rc & OSMO_CGI_PART_PLMN)
2621 printf(" PLMN");
2622 if (rc & OSMO_CGI_PART_LAC)
2623 printf(" LAC");
2624 if (rc & OSMO_CGI_PART_CI)
2625 printf(" CI");
2626
2627 gsm0808_cell_id_from_cgi(&cid, from_cid->id_discr, &cgi);
2628 printf(" -> cid %s\n", gsm0808_cell_id_name(&cid));
2629 if (!gsm0808_cell_ids_match(from_cid, &cid, true))
2630 printf(" MISMATCH!\n");
2631
2632 for (j = 0; j < ARRAY_SIZE(cell_ident_discrs); j++) {
2633 enum CELL_IDENT discr = cell_ident_discrs[j];
2634
2635 gsm0808_cell_id_from_cgi(&cid, discr, &cgi);
2636 printf(" --> gsm0808_cell_id{%s} = %s\n", gsm0808_cell_id_discr_name(discr), gsm0808_cell_id_name(&cid));
2637 }
2638 }
2639}
2640
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002641int main(int argc, char **argv)
2642{
Max969fb2e2018-12-10 11:01:10 +01002643 void *ctx = talloc_named_const(NULL, 0, "gsm0808 test");
2644 msgb_talloc_ctx_init(ctx, 0);
2645 osmo_init_logging2(ctx, NULL);
2646
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002647 printf("Testing generation of GSM0808 messages\n");
Philipp Maier4f4905f2018-11-30 13:36:12 +01002648 test_gsm0808_enc_cause();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002649 test_create_layer3();
Philipp Maierfa896ab2017-03-27 16:55:32 +02002650 test_create_layer3_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002651 test_create_reset();
Philipp Maier15596e22017-04-05 17:55:27 +02002652 test_create_reset_ack();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002653 test_create_clear_command();
Harald Weltecf665fc2019-02-18 13:45:36 +01002654 test_create_clear_command2();
2655 test_create_clear_command2_csfb();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002656 test_create_clear_complete();
Philipp Maierb478dd32017-03-29 15:50:05 +02002657 test_create_cipher();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002658 test_create_cipher_complete();
2659 test_create_cipher_reject();
Maxed651d22018-11-07 15:25:05 +01002660 test_create_cipher_reject_ext();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002661 test_create_cm_u();
2662 test_create_sapi_reject();
Philipp Maierc6144a22017-03-29 17:53:43 +02002663 test_create_ass();
Max52074322018-11-30 10:44:07 +01002664 test_create_ass2();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002665 test_create_ass_compl();
Philipp Maierfa896ab2017-03-27 16:55:32 +02002666 test_create_ass_compl_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002667 test_create_ass_fail();
Philipp Maierfa896ab2017-03-27 16:55:32 +02002668 test_create_ass_fail_aoip();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002669 test_create_clear_rqst();
Philipp Maier3d48ec02017-03-29 17:37:55 +02002670 test_create_paging();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002671 test_create_dtap();
2672 test_prepend_dtap();
Max969fb2e2018-12-10 11:01:10 +01002673
Max47022152018-12-19 18:51:00 +01002674 test_enc_dec_lcls();
Max969fb2e2018-12-10 11:01:10 +01002675
Philipp Maier22401432017-03-24 17:59:26 +01002676 test_enc_dec_aoip_trasp_addr_v4();
2677 test_enc_dec_aoip_trasp_addr_v6();
Oliver Smithc66b35b2022-08-05 11:27:55 +02002678 test_enc_aoip_trasp_addr_msg_too_small();
Philipp Maier6f725d62017-03-24 18:03:17 +01002679 test_gsm0808_enc_dec_speech_codec();
Philipp Maier6f725d62017-03-24 18:03:17 +01002680 test_gsm0808_enc_dec_speech_codec_ext_with_cfg();
Philipp Maierbb839662017-06-01 17:11:19 +02002681 test_gsm0808_enc_dec_speech_codec_with_cfg();
Philipp Maier6f725d62017-03-24 18:03:17 +01002682 test_gsm0808_enc_dec_speech_codec_list();
Philipp Maierf6c369f2018-10-16 15:24:47 +02002683 test_gsm0808_enc_dec_empty_speech_codec_list();
Oliver Smithf047a4a2023-01-31 13:10:03 +01002684 test_gsm0808_enc_dec_channel_type_data();
2685 test_gsm0808_enc_dec_channel_type_data_asym_pref();
Oliver Smith02b90952023-01-31 16:42:55 +01002686 test_gsm0808_enc_dec_channel_type_speech();
Oliver Smith81e5a6f2023-01-31 12:34:26 +01002687 test_gsm0808_dec_channel_type_err();
Philipp Maier14e76b92017-03-28 18:36:52 +02002688 test_gsm0808_enc_dec_encrypt_info();
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02002689
Philipp Maier783047e2017-03-29 11:35:50 +02002690 test_gsm0808_enc_dec_cell_id_list_lac();
2691 test_gsm0808_enc_dec_cell_id_list_single_lac();
Stefan Sperlinge1a86742018-03-15 18:05:02 +01002692 test_gsm0808_enc_dec_cell_id_list_multi_lac();
Philipp Maier783047e2017-03-29 11:35:50 +02002693 test_gsm0808_enc_dec_cell_id_list_bss();
Stefan Sperling23381452018-03-15 19:38:15 +01002694 test_gsm0808_enc_dec_cell_id_list_multi_lai_and_lac();
Stefan Sperling9c62fc62018-03-16 10:23:34 +01002695 test_gsm0808_enc_dec_cell_id_list_multi_ci();
Stefan Sperlinged4327c2018-03-16 11:02:59 +01002696 test_gsm0808_enc_dec_cell_id_list_multi_lac_and_ci();
Stefan Sperling483f3862018-03-16 12:21:26 +01002697 test_gsm0808_enc_dec_cell_id_list_multi_global();
Harald Weltea87526d2022-02-15 20:06:16 +01002698 test_gsm0808_dec_cell_id_list_srvcc();
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002699
Neels Hofmeyr74663d92018-03-23 01:46:42 +01002700 test_cell_id_list_add();
2701
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02002702 test_gsm0808_enc_dec_cell_id_lac();
2703 test_gsm0808_enc_dec_cell_id_bss();
2704 test_gsm0808_enc_dec_cell_id_no_cell();
2705 test_gsm0808_enc_dec_cell_id_lai_and_lac();
2706 test_gsm0808_enc_dec_cell_id_ci();
2707 test_gsm0808_enc_dec_cell_id_lac_and_ci();
2708 test_gsm0808_enc_dec_cell_id_global();
Pau Espin Pedrol52489852021-02-15 16:26:37 +01002709 test_gsm0808_enc_dec_cell_id_global_ps();
Philipp Maier5f2eb152018-09-19 13:40:21 +02002710 test_gsm0808_sc_cfg_from_gsm48_mr_cfg();
Philipp Maier8515d032018-09-25 15:57:49 +02002711 test_gsm48_mr_cfg_from_gsm0808_sc_cfg();
Philipp Maier5f2eb152018-09-19 13:40:21 +02002712
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02002713 test_cell_id_matching();
2714 test_cell_id_list_matching(true);
2715 test_cell_id_list_matching(false);
2716
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002717 test_gsm0808_cell_id_to_from_cgi();
2718
Alexander Chemeris22630e62020-05-13 00:44:04 +03002719 test_dec_confusion();
Pau Espin Pedrola3987fc2022-03-18 13:17:28 +01002720 test_dec_perform_location_report_sys5891();
Alexander Chemeris22630e62020-05-13 00:44:04 +03002721
Holger Hans Peter Freyther97510812012-01-22 13:36:52 +01002722 printf("Done\n");
2723 return EXIT_SUCCESS;
2724}