blob: 81dc177d76def7e695e66d1ac01a927bb3473455 [file] [log] [blame]
Holger Freytheraa0fb362008-12-28 21:55:40 +00001/* simple test for the gsm0408 formatting functions */
2/*
3 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Freytheraa0fb362008-12-28 21:55:40 +00009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Freytheraa0fb362008-12-28 21:55:40 +000018 *
19 */
20
21#include <assert.h>
22#include <stdio.h>
23#include <stdlib.h>
Max69e9c0d2016-05-18 13:04:47 +020024#include <stdbool.h>
Harald Welteafedeab2010-03-04 10:55:40 +010025#include <arpa/inet.h>
26
Neels Hofmeyr2d521a02016-05-14 00:57:04 +020027#include <openbsc/common_bsc.h>
Holger Freytheraa0fb362008-12-28 21:55:40 +000028#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +010029#include <openbsc/gsm_04_11.h>
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020030#include <openbsc/gsm_subscriber.h>
31#include <openbsc/debug.h>
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010032#include <openbsc/arfcn_range_encode.h>
Max59a1bf32016-04-15 16:04:46 +020033#include <openbsc/system_information.h>
34#include <openbsc/abis_rsl.h>
35
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010036#include <osmocom/core/application.h>
Max59a1bf32016-04-15 16:04:46 +020037#include <osmocom/gsm/sysinfo.h>
Holger Freytheraa0fb362008-12-28 21:55:40 +000038
39#define COMPARE(result, op, value) \
40 if (!((result) op (value))) {\
41 fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \
42 exit(-1); \
43 }
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020044
45#define COMPARE_STR(result, value) \
46 if (strcmp(result, value) != 0) { \
47 fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \
48 exit(-1); \
49 }
Holger Freytheraa0fb362008-12-28 21:55:40 +000050
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010051#define DBG(...)
52
53#define VERIFY(res, cmp, wanted) \
54 if (!(res cmp wanted)) { \
55 printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \
Holger Hans Peter Freytherdaaea0c2015-08-03 09:28:41 +020056 __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010057 }
58
59
60
Holger Freytheraa0fb362008-12-28 21:55:40 +000061/*
62 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
63 */
64static void test_location_area_identifier(void)
65{
66 struct gsm48_loc_area_id lai48;
67
68 printf("Testing test location area identifier\n");
69
70 /*
71 * Test the default/test setup. Coming from
72 * bsc_hack.c dumps
73 */
Harald Welteafedeab2010-03-04 10:55:40 +010074 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytheraa0fb362008-12-28 21:55:40 +000075 COMPARE(lai48.digits[0], ==, 0x00);
76 COMPARE(lai48.digits[1], ==, 0xF1);
77 COMPARE(lai48.digits[2], ==, 0x10);
78 COMPARE(lai48.lac, ==, htons(0x0001));
79
Harald Welteafedeab2010-03-04 10:55:40 +010080 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytheraa0fb362008-12-28 21:55:40 +000081 COMPARE(lai48.digits[0], ==, 0x06);
82 COMPARE(lai48.digits[1], ==, 0xF2);
83 COMPARE(lai48.digits[2], ==, 0x10);
84 COMPARE(lai48.lac, ==, htons(0x000f));
85}
86
Max59a1bf32016-04-15 16:04:46 +020087static inline void add_arfcn_b(struct osmo_earfcn_si2q *e, uint16_t earfcn,
88 uint8_t bw)
89{
90 int r = osmo_earfcn_add(e, earfcn, bw);
91 if (r)
Harald Weltec59e28f2016-11-26 15:08:00 +010092 printf("failed to add EARFCN %u: %s\n", earfcn, strerror(-r));
Max59a1bf32016-04-15 16:04:46 +020093 else
94 printf("added EARFCN %u - ", earfcn);
95}
96
97static inline void gen(struct gsm_bts *bts)
98{
Max69e9c0d2016-05-18 13:04:47 +020099 bts->si_valid = 0;
100 bts->si_valid |= (1 << SYSINFO_TYPE_2quater);
101 /* should be no-op as entire buffer is filled with padding: */
Max6f0e50c2017-04-12 15:30:54 +0200102 memset(GSM_BTS_SI(bts, SYSINFO_TYPE_2quater), 0xAE, GSM_MACBLOCK_LEN);
Max59a1bf32016-04-15 16:04:46 +0200103 int r = gsm_generate_si(bts, SYSINFO_TYPE_2quater);
Max69e9c0d2016-05-18 13:04:47 +0200104 bool v = bts->si_valid & (1 << SYSINFO_TYPE_2quater);
Max59a1bf32016-04-15 16:04:46 +0200105 if (r > 0)
Max69e9c0d2016-05-18 13:04:47 +0200106 printf("generated %s SI2quater: [%d] %s\n",
Max6f0e50c2017-04-12 15:30:54 +0200107 v ? "valid" : "invalid", r, osmo_hexdump(GSM_BTS_SI(bts, SYSINFO_TYPE_2quater), r));
Max59a1bf32016-04-15 16:04:46 +0200108 else
109 printf("failed to generate SI2quater: %s\n", strerror(-r));
110}
111
Maxaafff962016-04-20 15:57:14 +0200112static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn,
113 uint16_t scramble, bool diversity)
114{
115 int r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
116 if (r < 0)
117 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
118 else
119 gen(bts);
120}
121
Max881064e2016-12-14 14:51:40 +0100122static inline void test_si2q_segfault(void)
123{
124 struct gsm_bts *bts;
125 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
126 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
127
128 if (!network)
129 exit(1);
130 bts = gsm_bts_alloc(network);
131
132 _bts_uarfcn_add(bts, 10564, 319, 0);
133 _bts_uarfcn_add(bts, 10612, 319, 0);
134 gen(bts);
135}
136
Maxe610e702016-12-19 13:41:48 +0100137static inline void test_si2q_mu(void)
138{
139 struct gsm_bts *bts;
140 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
141 printf("Test SI2quater multiple UARFCNs:\n");
142
143 if (!network)
144 exit(1);
145 bts = gsm_bts_alloc(network);
146
147 _bts_uarfcn_add(bts, 10564, 318, 0);
148 _bts_uarfcn_add(bts, 10612, 319, 0);
149 _bts_uarfcn_add(bts, 10612, 31, 0);
150 _bts_uarfcn_add(bts, 10612, 19, 0);
151 _bts_uarfcn_add(bts, 10613, 64, 0);
152 _bts_uarfcn_add(bts, 10613, 164, 0);
153 _bts_uarfcn_add(bts, 10613, 14, 0);
154 gen(bts);
155}
156
Max26679e02016-04-20 15:57:13 +0200157static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200158{
159 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200160 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200161 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
162
163 if (!network)
164 exit(1);
165 bts = gsm_bts_alloc(network);
166
Max69e9c0d2016-05-18 13:04:47 +0200167 /* first generate invalid SI as no UARFCN added */
168 gen(bts);
169 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200170 _bts_uarfcn_add(bts, 1982, 13, 1);
171 _bts_uarfcn_add(bts, 1982, 44, 0);
172 _bts_uarfcn_add(bts, 1982, 61, 1);
173 _bts_uarfcn_add(bts, 1982, 89, 1);
174 _bts_uarfcn_add(bts, 1982, 113, 0);
175 _bts_uarfcn_add(bts, 1982, 123, 0);
176 _bts_uarfcn_add(bts, 1982, 56, 1);
177 _bts_uarfcn_add(bts, 1982, 72, 1);
178 _bts_uarfcn_add(bts, 1982, 223, 1);
179 _bts_uarfcn_add(bts, 1982, 14, 0);
180 _bts_uarfcn_add(bts, 1982, 88, 0);
Max26679e02016-04-20 15:57:13 +0200181 gen(bts);
182}
183
184static inline void test_si2q_e(void)
185{
186 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200187 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200188 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200189
190 if (!network)
191 exit(1);
192 bts = gsm_bts_alloc(network);
193
194 bts->si_common.si2quater_neigh_list.arfcn =
195 bts->si_common.data.earfcn_list;
196 bts->si_common.si2quater_neigh_list.meas_bw =
197 bts->si_common.data.meas_bw_list;
198 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
199 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
200
201 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max69e9c0d2016-05-18 13:04:47 +0200202 /* first generate invalid SI as no EARFCN added */
203 gen(bts);
204 /* subsequent calls should produce valid SI if there's enough memory */
Max59a1bf32016-04-15 16:04:46 +0200205 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1917, 1);
206 gen(bts);
207
208 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1932,
209 OSMO_EARFCN_MEAS_INVALID);
210 gen(bts);
211
212 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1937, 2);
213 gen(bts);
214
215 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1945,
216 OSMO_EARFCN_MEAS_INVALID);
217 gen(bts);
218
219 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1965,
220 OSMO_EARFCN_MEAS_INVALID);
221 gen(bts);
222
223 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1967, 4);
224 gen(bts);
225
226 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1982, 3);
227 gen(bts);
228}
229
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200230static void test_mi_functionality(void)
231{
232 const char *imsi_odd = "987654321098763";
233 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200234 const uint32_t tmsi = 0xfabeacd0;
235 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200236 unsigned int mi_len;
237 char mi_parsed[GSM48_MI_SIZE];
238
239 printf("Testing parsing and generating TMSI/IMSI\n");
240
241 /* tmsi code */
242 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
243 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100244 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200245
246 /* imsi code */
247 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
248 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200249 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200250 COMPARE_STR(mi_parsed, imsi_odd);
251
252 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
253 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200254 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200255 COMPARE_STR(mi_parsed, imsi_even);
256}
257
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100258struct {
259 int range;
260 int arfcns_num;
261 int arfcns[RANGE_ENC_MAX_ARFCNS];
262} arfcn_test_ranges[] = {
263 {ARFCN_RANGE_512, 12,
264 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
265 {ARFCN_RANGE_512, 17,
266 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
267 {ARFCN_RANGE_512, 18,
268 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
269 {ARFCN_RANGE_512, 18,
270 { 1, 17, 31, 45, 58, 79, 81, 97,
271 113, 127, 213, 277, 287, 311, 331, 391,
272 417, 511 }},
273 {ARFCN_RANGE_512, 6,
274 { 1, 17, 31, 45, 58, 79 }},
275 {ARFCN_RANGE_512, 6,
276 { 10, 17, 31, 45, 58, 79 }},
277 {ARFCN_RANGE_1024, 17,
278 { 0, 17, 31, 45, 58, 79, 81, 97,
279 113, 127, 213, 277, 287, 311, 331, 391,
280 1023 }},
281 {ARFCN_RANGE_1024, 16,
282 { 17, 31, 45, 58, 79, 81, 97, 113,
283 127, 213, 277, 287, 311, 331, 391, 1023 }},
284 {-1}
285};
286
287static int test_single_range_encoding(int range, const int *orig_arfcns,
288 int arfcns_num, int silent)
289{
290 int arfcns[RANGE_ENC_MAX_ARFCNS];
291 int w[RANGE_ENC_MAX_ARFCNS];
292 int f0_included = 0;
293 int rc, f0;
294 uint8_t chan_list[16] = {0};
295 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
296 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
297 int dec_arfcns_count = 0;
298 int arfcns_used = 0;
299 int i;
300
301 arfcns_used = arfcns_num;
302 memmove(arfcns, orig_arfcns, sizeof(arfcns));
303
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100304 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100305 /*
306 * Manipulate the ARFCN list according to the rules in J4 depending
307 * on the selected range.
308 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100309 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100310 f0, &f0_included);
311
312 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200313 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100314
315 if (!silent)
316 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
317 range, arfcns_used, f0, f0_included);
318
319 /* Select the range and the amount of bits needed */
320 switch (range) {
321 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200322 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100323 break;
324 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200325 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100326 break;
327 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200328 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100329 break;
330 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200331 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100332 break;
333 default:
334 return 1;
335 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100336
337 if (!silent)
338 printf("chan_list = %s\n",
339 osmo_hexdump(chan_list, sizeof(chan_list)));
340
341 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
342 0xfe, 1);
343 if (rc != 0) {
344 printf("Cannot decode freq list, rc = %d\n", rc);
345 return 1;
346 }
347
348 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
349 if (dec_freq[i].mask &&
350 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
351 dec_arfcns[dec_arfcns_count++] = i;
352 }
353
354 if (!silent) {
355 printf("Decoded freqs %d (expected %d)\n",
356 dec_arfcns_count, arfcns_num);
357 printf("Decoded: ");
358 for (i = 0; i < dec_arfcns_count; i++) {
359 printf("%d ", dec_arfcns[i]);
360 if (dec_arfcns[i] != orig_arfcns[i])
361 printf("(!= %d) ", orig_arfcns[i]);
362 }
363 printf("\n");
364 }
365
366 if (dec_arfcns_count != arfcns_num) {
367 printf("Wrong number of arfcns\n");
368 return 1;
369 }
370
371 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
372 printf("Decoding error, got wrong freqs\n");
373 fprintf(stderr, " w = ");
374 for (i = 0; i < ARRAY_SIZE(w); i++)
375 fprintf(stderr, "%d ", w[i]);
376 fprintf(stderr, "\n");
377 return 1;
378 }
379
380 return 0;
381}
382
383static void test_random_range_encoding(int range, int max_arfcn_num)
384{
385 int arfcns_num = 0;
386 int test_idx;
387 int rc, max_count;
388 int num_tests = 1024;
389
390 printf("Random range test: range %d, max num ARFCNs %d\n",
391 range, max_arfcn_num);
392
393 srandom(1);
394
395 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
396 for (test_idx = 0; test_idx < num_tests; test_idx++) {
397 int count;
398 int i;
399 int min_freq = 0;
400
401 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
402 char rnd_arfcns_set[1024] = {0};
403
404 if (range < ARFCN_RANGE_1024)
405 min_freq = random() % (1023 - range);
406
407 for (count = max_count; count; ) {
408 int arfcn = min_freq + random() % (range + 1);
409 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
410
411 if (!rnd_arfcns_set[arfcn]) {
412 rnd_arfcns_set[arfcn] = 1;
413 count -= 1;
414 }
415 }
416
417 arfcns_num = 0;
418 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
419 if (rnd_arfcns_set[i])
420 rnd_arfcns[arfcns_num++] = i;
421
422 rc = test_single_range_encoding(range, rnd_arfcns,
423 arfcns_num, 1);
424 if (rc != 0) {
425 printf("Failed on test %d, range %d, num ARFCNs %d\n",
426 test_idx, range, max_count);
427 test_single_range_encoding(range, rnd_arfcns,
428 arfcns_num, 0);
429 return;
430 }
431 }
432 }
433}
434
435static void test_range_encoding()
436{
437 int *arfcns;
438 int arfcns_num = 0;
439 int test_idx;
440 int range;
441
442 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
443 {
444 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
445 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
446 range = arfcn_test_ranges[test_idx].range;
447
448 printf("Range test %d: range %d, num ARFCNs %d\n",
449 test_idx, range, arfcns_num);
450
451 test_single_range_encoding(range, arfcns, arfcns_num, 0);
452 }
453
454 test_random_range_encoding(ARFCN_RANGE_128, 29);
455 test_random_range_encoding(ARFCN_RANGE_256, 22);
456 test_random_range_encoding(ARFCN_RANGE_512, 18);
457 test_random_range_encoding(ARFCN_RANGE_1024, 16);
458}
459
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100460static int freqs1[] = {
461 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
462};
463
464static int freqs2[] = {
465 402, 460, 1, 67, 131, 197, 272, 347,
466};
467
468static int freqs3[] = {
469 68, 128, 198, 279, 353, 398, 452,
470
471};
472
473static int w_out[] = {
474 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
475};
476
477static int range128[] = {
478 1, 1 + 127,
479};
480
481static int range256[] = {
482 1, 1 + 128,
483};
484
485static int range512[] = {
486 1, 1+ 511,
487};
488
489
490static void test_arfcn_filter()
491{
492 int arfcns[50], i, res, f0_included;
493 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
494 arfcns[i] = (i + 1) * 2;
495
496 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
497 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100498 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100499 arfcns[0], &f0_included);
500 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
501 VERIFY(f0_included, ==, 1);
502 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100503 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100504
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100505 /* check with range1024, ARFCN 0 is included */
506 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
507 arfcns[i] = i * 2;
508 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
509 0, &f0_included);
510 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
511 VERIFY(f0_included, ==, 1);
512 for (i = 0; i < res; ++i)
513 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
514
515 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100516 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
517 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100518 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
519 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100520 VERIFY(res, ==, ARRAY_SIZE(arfcns));
521 VERIFY(f0_included, ==, 0);
522 for (i = 0; i < res; ++i)
523 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
524}
525
526static void test_print_encoding()
527{
528 int rc;
529 int w[17];
530 uint8_t chan_list[16];
531 memset(chan_list, 0x23, sizeof(chan_list));
532
533 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
534 switch (rc % 3) {
535 case 0:
536 w[rc] = 0xAAAA;
537 break;
538 case 1:
539 w[rc] = 0x5555;
540 break;
541 case 2:
542 w[rc] = 0x9696;
543 break;
544 }
545
Max26679e02016-04-20 15:57:13 +0200546 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100547
548 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
549}
550
551static void test_si_range_helpers()
552{
553 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
554 int i, f0 = 0xFFFFFF;
555
556 memset(&ws[0], 0x23, sizeof(ws));
557
558 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100559 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100560 VERIFY(i, ==, 2);
561
562 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100563 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100564 VERIFY(i, ==, 2);
565
566 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100567 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100568 VERIFY(i, ==, 0);
569
Max26679e02016-04-20 15:57:13 +0200570 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100571
572 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
573 printf("w[%d]=%d\n", i, ws[i]);
574 VERIFY(ws[i], ==, w_out[i]);
575 }
576
577 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
578 VERIFY(i, ==, ARFCN_RANGE_128);
579 VERIFY(f0, ==, 1);
580
581 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
582 VERIFY(i, ==, ARFCN_RANGE_256);
583 VERIFY(f0, ==, 1);
584
585 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
586 VERIFY(i, ==, ARFCN_RANGE_512);
587 VERIFY(f0, ==, 1);
588}
589
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100590static void test_gsm411_rp_ref_wrap(void)
591{
592 struct gsm_subscriber_connection conn;
593 int res;
594
595 printf("testing RP-Reference wrap\n");
596
597 memset(&conn, 0, sizeof(conn));
598 conn.next_rp_ref = 255;
599
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200600 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100601 printf("Allocated reference: %d\n", res);
602 OSMO_ASSERT(res == 255);
603
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200604 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100605 printf("Allocated reference: %d\n", res);
606 OSMO_ASSERT(res == 0);
607
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200608 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100609 printf("Allocated reference: %d\n", res);
610 OSMO_ASSERT(res == 1);
611}
612
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800613int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000614{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100615 osmo_init_logging(&log_info);
616 log_set_log_level(osmo_stderr_target, LOGL_INFO);
617
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200618 test_location_area_identifier();
619 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100620
621 test_si_range_helpers();
622 test_arfcn_filter();
623 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100624 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100625 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100626
Max881064e2016-12-14 14:51:40 +0100627 test_si2q_segfault();
Max26679e02016-04-20 15:57:13 +0200628 test_si2q_e();
629 test_si2q_u();
Maxe610e702016-12-19 13:41:48 +0100630 test_si2q_mu();
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100631 printf("Done.\n");
632 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000633}