blob: 472c2aeefccf11cbf7a1985788e02799fcb5a232 [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: */
102 memset(bts->si_buf[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",
107 v ? "valid" : "invalid", r,
Max59a1bf32016-04-15 16:04:46 +0200108 osmo_hexdump(bts->si_buf[SYSINFO_TYPE_2quater], r));
109 else
110 printf("failed to generate SI2quater: %s\n", strerror(-r));
111}
112
Maxaafff962016-04-20 15:57:14 +0200113static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn,
114 uint16_t scramble, bool diversity)
115{
116 int r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
117 if (r < 0)
118 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
119 else
120 gen(bts);
121}
122
Max881064e2016-12-14 14:51:40 +0100123static inline void test_si2q_segfault(void)
124{
125 struct gsm_bts *bts;
126 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
127 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
128
129 if (!network)
130 exit(1);
131 bts = gsm_bts_alloc(network);
132
133 _bts_uarfcn_add(bts, 10564, 319, 0);
134 _bts_uarfcn_add(bts, 10612, 319, 0);
135 gen(bts);
136}
137
Max26679e02016-04-20 15:57:13 +0200138static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200139{
140 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200141 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200142 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
143
144 if (!network)
145 exit(1);
146 bts = gsm_bts_alloc(network);
147
Max69e9c0d2016-05-18 13:04:47 +0200148 /* first generate invalid SI as no UARFCN added */
149 gen(bts);
150 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200151 _bts_uarfcn_add(bts, 1982, 13, 1);
152 _bts_uarfcn_add(bts, 1982, 44, 0);
153 _bts_uarfcn_add(bts, 1982, 61, 1);
154 _bts_uarfcn_add(bts, 1982, 89, 1);
155 _bts_uarfcn_add(bts, 1982, 113, 0);
156 _bts_uarfcn_add(bts, 1982, 123, 0);
157 _bts_uarfcn_add(bts, 1982, 56, 1);
158 _bts_uarfcn_add(bts, 1982, 72, 1);
159 _bts_uarfcn_add(bts, 1982, 223, 1);
160 _bts_uarfcn_add(bts, 1982, 14, 0);
161 _bts_uarfcn_add(bts, 1982, 88, 0);
Max26679e02016-04-20 15:57:13 +0200162 gen(bts);
163}
164
165static inline void test_si2q_e(void)
166{
167 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200168 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200169 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200170
171 if (!network)
172 exit(1);
173 bts = gsm_bts_alloc(network);
174
175 bts->si_common.si2quater_neigh_list.arfcn =
176 bts->si_common.data.earfcn_list;
177 bts->si_common.si2quater_neigh_list.meas_bw =
178 bts->si_common.data.meas_bw_list;
179 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
180 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
181
182 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max69e9c0d2016-05-18 13:04:47 +0200183 /* first generate invalid SI as no EARFCN added */
184 gen(bts);
185 /* subsequent calls should produce valid SI if there's enough memory */
Max59a1bf32016-04-15 16:04:46 +0200186 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1917, 1);
187 gen(bts);
188
189 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1932,
190 OSMO_EARFCN_MEAS_INVALID);
191 gen(bts);
192
193 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1937, 2);
194 gen(bts);
195
196 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1945,
197 OSMO_EARFCN_MEAS_INVALID);
198 gen(bts);
199
200 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1965,
201 OSMO_EARFCN_MEAS_INVALID);
202 gen(bts);
203
204 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1967, 4);
205 gen(bts);
206
207 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1982, 3);
208 gen(bts);
209}
210
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200211static void test_mi_functionality(void)
212{
213 const char *imsi_odd = "987654321098763";
214 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200215 const uint32_t tmsi = 0xfabeacd0;
216 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200217 unsigned int mi_len;
218 char mi_parsed[GSM48_MI_SIZE];
219
220 printf("Testing parsing and generating TMSI/IMSI\n");
221
222 /* tmsi code */
223 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
224 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100225 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200226
227 /* imsi code */
228 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
229 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200230 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200231 COMPARE_STR(mi_parsed, imsi_odd);
232
233 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
234 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200235 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200236 COMPARE_STR(mi_parsed, imsi_even);
237}
238
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100239struct {
240 int range;
241 int arfcns_num;
242 int arfcns[RANGE_ENC_MAX_ARFCNS];
243} arfcn_test_ranges[] = {
244 {ARFCN_RANGE_512, 12,
245 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
246 {ARFCN_RANGE_512, 17,
247 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
248 {ARFCN_RANGE_512, 18,
249 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
250 {ARFCN_RANGE_512, 18,
251 { 1, 17, 31, 45, 58, 79, 81, 97,
252 113, 127, 213, 277, 287, 311, 331, 391,
253 417, 511 }},
254 {ARFCN_RANGE_512, 6,
255 { 1, 17, 31, 45, 58, 79 }},
256 {ARFCN_RANGE_512, 6,
257 { 10, 17, 31, 45, 58, 79 }},
258 {ARFCN_RANGE_1024, 17,
259 { 0, 17, 31, 45, 58, 79, 81, 97,
260 113, 127, 213, 277, 287, 311, 331, 391,
261 1023 }},
262 {ARFCN_RANGE_1024, 16,
263 { 17, 31, 45, 58, 79, 81, 97, 113,
264 127, 213, 277, 287, 311, 331, 391, 1023 }},
265 {-1}
266};
267
268static int test_single_range_encoding(int range, const int *orig_arfcns,
269 int arfcns_num, int silent)
270{
271 int arfcns[RANGE_ENC_MAX_ARFCNS];
272 int w[RANGE_ENC_MAX_ARFCNS];
273 int f0_included = 0;
274 int rc, f0;
275 uint8_t chan_list[16] = {0};
276 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
277 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
278 int dec_arfcns_count = 0;
279 int arfcns_used = 0;
280 int i;
281
282 arfcns_used = arfcns_num;
283 memmove(arfcns, orig_arfcns, sizeof(arfcns));
284
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100285 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100286 /*
287 * Manipulate the ARFCN list according to the rules in J4 depending
288 * on the selected range.
289 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100290 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100291 f0, &f0_included);
292
293 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200294 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100295
296 if (!silent)
297 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
298 range, arfcns_used, f0, f0_included);
299
300 /* Select the range and the amount of bits needed */
301 switch (range) {
302 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200303 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100304 break;
305 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200306 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100307 break;
308 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200309 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100310 break;
311 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200312 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100313 break;
314 default:
315 return 1;
316 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100317
318 if (!silent)
319 printf("chan_list = %s\n",
320 osmo_hexdump(chan_list, sizeof(chan_list)));
321
322 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
323 0xfe, 1);
324 if (rc != 0) {
325 printf("Cannot decode freq list, rc = %d\n", rc);
326 return 1;
327 }
328
329 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
330 if (dec_freq[i].mask &&
331 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
332 dec_arfcns[dec_arfcns_count++] = i;
333 }
334
335 if (!silent) {
336 printf("Decoded freqs %d (expected %d)\n",
337 dec_arfcns_count, arfcns_num);
338 printf("Decoded: ");
339 for (i = 0; i < dec_arfcns_count; i++) {
340 printf("%d ", dec_arfcns[i]);
341 if (dec_arfcns[i] != orig_arfcns[i])
342 printf("(!= %d) ", orig_arfcns[i]);
343 }
344 printf("\n");
345 }
346
347 if (dec_arfcns_count != arfcns_num) {
348 printf("Wrong number of arfcns\n");
349 return 1;
350 }
351
352 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
353 printf("Decoding error, got wrong freqs\n");
354 fprintf(stderr, " w = ");
355 for (i = 0; i < ARRAY_SIZE(w); i++)
356 fprintf(stderr, "%d ", w[i]);
357 fprintf(stderr, "\n");
358 return 1;
359 }
360
361 return 0;
362}
363
364static void test_random_range_encoding(int range, int max_arfcn_num)
365{
366 int arfcns_num = 0;
367 int test_idx;
368 int rc, max_count;
369 int num_tests = 1024;
370
371 printf("Random range test: range %d, max num ARFCNs %d\n",
372 range, max_arfcn_num);
373
374 srandom(1);
375
376 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
377 for (test_idx = 0; test_idx < num_tests; test_idx++) {
378 int count;
379 int i;
380 int min_freq = 0;
381
382 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
383 char rnd_arfcns_set[1024] = {0};
384
385 if (range < ARFCN_RANGE_1024)
386 min_freq = random() % (1023 - range);
387
388 for (count = max_count; count; ) {
389 int arfcn = min_freq + random() % (range + 1);
390 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
391
392 if (!rnd_arfcns_set[arfcn]) {
393 rnd_arfcns_set[arfcn] = 1;
394 count -= 1;
395 }
396 }
397
398 arfcns_num = 0;
399 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
400 if (rnd_arfcns_set[i])
401 rnd_arfcns[arfcns_num++] = i;
402
403 rc = test_single_range_encoding(range, rnd_arfcns,
404 arfcns_num, 1);
405 if (rc != 0) {
406 printf("Failed on test %d, range %d, num ARFCNs %d\n",
407 test_idx, range, max_count);
408 test_single_range_encoding(range, rnd_arfcns,
409 arfcns_num, 0);
410 return;
411 }
412 }
413 }
414}
415
416static void test_range_encoding()
417{
418 int *arfcns;
419 int arfcns_num = 0;
420 int test_idx;
421 int range;
422
423 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
424 {
425 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
426 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
427 range = arfcn_test_ranges[test_idx].range;
428
429 printf("Range test %d: range %d, num ARFCNs %d\n",
430 test_idx, range, arfcns_num);
431
432 test_single_range_encoding(range, arfcns, arfcns_num, 0);
433 }
434
435 test_random_range_encoding(ARFCN_RANGE_128, 29);
436 test_random_range_encoding(ARFCN_RANGE_256, 22);
437 test_random_range_encoding(ARFCN_RANGE_512, 18);
438 test_random_range_encoding(ARFCN_RANGE_1024, 16);
439}
440
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100441static int freqs1[] = {
442 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
443};
444
445static int freqs2[] = {
446 402, 460, 1, 67, 131, 197, 272, 347,
447};
448
449static int freqs3[] = {
450 68, 128, 198, 279, 353, 398, 452,
451
452};
453
454static int w_out[] = {
455 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
456};
457
458static int range128[] = {
459 1, 1 + 127,
460};
461
462static int range256[] = {
463 1, 1 + 128,
464};
465
466static int range512[] = {
467 1, 1+ 511,
468};
469
470
471static void test_arfcn_filter()
472{
473 int arfcns[50], i, res, f0_included;
474 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
475 arfcns[i] = (i + 1) * 2;
476
477 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
478 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100479 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100480 arfcns[0], &f0_included);
481 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
482 VERIFY(f0_included, ==, 1);
483 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100484 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100485
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100486 /* check with range1024, ARFCN 0 is included */
487 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
488 arfcns[i] = i * 2;
489 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
490 0, &f0_included);
491 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
492 VERIFY(f0_included, ==, 1);
493 for (i = 0; i < res; ++i)
494 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
495
496 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100497 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
498 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100499 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
500 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100501 VERIFY(res, ==, ARRAY_SIZE(arfcns));
502 VERIFY(f0_included, ==, 0);
503 for (i = 0; i < res; ++i)
504 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
505}
506
507static void test_print_encoding()
508{
509 int rc;
510 int w[17];
511 uint8_t chan_list[16];
512 memset(chan_list, 0x23, sizeof(chan_list));
513
514 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
515 switch (rc % 3) {
516 case 0:
517 w[rc] = 0xAAAA;
518 break;
519 case 1:
520 w[rc] = 0x5555;
521 break;
522 case 2:
523 w[rc] = 0x9696;
524 break;
525 }
526
Max26679e02016-04-20 15:57:13 +0200527 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100528
529 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
530}
531
532static void test_si_range_helpers()
533{
534 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
535 int i, f0 = 0xFFFFFF;
536
537 memset(&ws[0], 0x23, sizeof(ws));
538
539 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100540 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100541 VERIFY(i, ==, 2);
542
543 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100544 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100545 VERIFY(i, ==, 2);
546
547 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100548 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100549 VERIFY(i, ==, 0);
550
Max26679e02016-04-20 15:57:13 +0200551 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100552
553 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
554 printf("w[%d]=%d\n", i, ws[i]);
555 VERIFY(ws[i], ==, w_out[i]);
556 }
557
558 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
559 VERIFY(i, ==, ARFCN_RANGE_128);
560 VERIFY(f0, ==, 1);
561
562 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
563 VERIFY(i, ==, ARFCN_RANGE_256);
564 VERIFY(f0, ==, 1);
565
566 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
567 VERIFY(i, ==, ARFCN_RANGE_512);
568 VERIFY(f0, ==, 1);
569}
570
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100571static void test_gsm411_rp_ref_wrap(void)
572{
573 struct gsm_subscriber_connection conn;
574 int res;
575
576 printf("testing RP-Reference wrap\n");
577
578 memset(&conn, 0, sizeof(conn));
579 conn.next_rp_ref = 255;
580
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200581 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100582 printf("Allocated reference: %d\n", res);
583 OSMO_ASSERT(res == 255);
584
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200585 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100586 printf("Allocated reference: %d\n", res);
587 OSMO_ASSERT(res == 0);
588
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200589 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100590 printf("Allocated reference: %d\n", res);
591 OSMO_ASSERT(res == 1);
592}
593
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800594int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000595{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100596 osmo_init_logging(&log_info);
597 log_set_log_level(osmo_stderr_target, LOGL_INFO);
598
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200599 test_location_area_identifier();
600 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100601
602 test_si_range_helpers();
603 test_arfcn_filter();
604 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100605 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100606 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100607
Max881064e2016-12-14 14:51:40 +0100608 test_si2q_segfault();
Max26679e02016-04-20 15:57:13 +0200609 test_si2q_e();
610 test_si2q_u();
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100611 printf("Done.\n");
612 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000613}