blob: 08cf43f50cce930361b128606e74b9ca02bc8d56 [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
Maxe610e702016-12-19 13:41:48 +0100138static inline void test_si2q_mu(void)
139{
140 struct gsm_bts *bts;
141 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
142 printf("Test SI2quater multiple UARFCNs:\n");
143
144 if (!network)
145 exit(1);
146 bts = gsm_bts_alloc(network);
147
148 _bts_uarfcn_add(bts, 10564, 318, 0);
149 _bts_uarfcn_add(bts, 10612, 319, 0);
150 _bts_uarfcn_add(bts, 10612, 31, 0);
151 _bts_uarfcn_add(bts, 10612, 19, 0);
152 _bts_uarfcn_add(bts, 10613, 64, 0);
153 _bts_uarfcn_add(bts, 10613, 164, 0);
154 _bts_uarfcn_add(bts, 10613, 14, 0);
155 gen(bts);
156}
157
Max26679e02016-04-20 15:57:13 +0200158static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200159{
160 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200161 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200162 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
163
164 if (!network)
165 exit(1);
166 bts = gsm_bts_alloc(network);
167
Max69e9c0d2016-05-18 13:04:47 +0200168 /* first generate invalid SI as no UARFCN added */
169 gen(bts);
170 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200171 _bts_uarfcn_add(bts, 1982, 13, 1);
172 _bts_uarfcn_add(bts, 1982, 44, 0);
173 _bts_uarfcn_add(bts, 1982, 61, 1);
174 _bts_uarfcn_add(bts, 1982, 89, 1);
175 _bts_uarfcn_add(bts, 1982, 113, 0);
176 _bts_uarfcn_add(bts, 1982, 123, 0);
177 _bts_uarfcn_add(bts, 1982, 56, 1);
178 _bts_uarfcn_add(bts, 1982, 72, 1);
179 _bts_uarfcn_add(bts, 1982, 223, 1);
180 _bts_uarfcn_add(bts, 1982, 14, 0);
181 _bts_uarfcn_add(bts, 1982, 88, 0);
Max26679e02016-04-20 15:57:13 +0200182 gen(bts);
183}
184
185static inline void test_si2q_e(void)
186{
187 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200188 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200189 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200190
191 if (!network)
192 exit(1);
193 bts = gsm_bts_alloc(network);
194
195 bts->si_common.si2quater_neigh_list.arfcn =
196 bts->si_common.data.earfcn_list;
197 bts->si_common.si2quater_neigh_list.meas_bw =
198 bts->si_common.data.meas_bw_list;
199 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
200 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
201
202 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max69e9c0d2016-05-18 13:04:47 +0200203 /* first generate invalid SI as no EARFCN added */
204 gen(bts);
205 /* subsequent calls should produce valid SI if there's enough memory */
Max59a1bf32016-04-15 16:04:46 +0200206 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1917, 1);
207 gen(bts);
208
209 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1932,
210 OSMO_EARFCN_MEAS_INVALID);
211 gen(bts);
212
213 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1937, 2);
214 gen(bts);
215
216 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1945,
217 OSMO_EARFCN_MEAS_INVALID);
218 gen(bts);
219
220 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1965,
221 OSMO_EARFCN_MEAS_INVALID);
222 gen(bts);
223
224 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1967, 4);
225 gen(bts);
226
227 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1982, 3);
228 gen(bts);
229}
230
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200231static void test_mi_functionality(void)
232{
233 const char *imsi_odd = "987654321098763";
234 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200235 const uint32_t tmsi = 0xfabeacd0;
236 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200237 unsigned int mi_len;
238 char mi_parsed[GSM48_MI_SIZE];
239
240 printf("Testing parsing and generating TMSI/IMSI\n");
241
242 /* tmsi code */
243 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
244 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100245 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200246
247 /* imsi code */
248 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
249 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200250 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200251 COMPARE_STR(mi_parsed, imsi_odd);
252
253 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
254 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200255 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200256 COMPARE_STR(mi_parsed, imsi_even);
257}
258
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100259struct {
260 int range;
261 int arfcns_num;
262 int arfcns[RANGE_ENC_MAX_ARFCNS];
263} arfcn_test_ranges[] = {
264 {ARFCN_RANGE_512, 12,
265 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
266 {ARFCN_RANGE_512, 17,
267 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
268 {ARFCN_RANGE_512, 18,
269 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
270 {ARFCN_RANGE_512, 18,
271 { 1, 17, 31, 45, 58, 79, 81, 97,
272 113, 127, 213, 277, 287, 311, 331, 391,
273 417, 511 }},
274 {ARFCN_RANGE_512, 6,
275 { 1, 17, 31, 45, 58, 79 }},
276 {ARFCN_RANGE_512, 6,
277 { 10, 17, 31, 45, 58, 79 }},
278 {ARFCN_RANGE_1024, 17,
279 { 0, 17, 31, 45, 58, 79, 81, 97,
280 113, 127, 213, 277, 287, 311, 331, 391,
281 1023 }},
282 {ARFCN_RANGE_1024, 16,
283 { 17, 31, 45, 58, 79, 81, 97, 113,
284 127, 213, 277, 287, 311, 331, 391, 1023 }},
285 {-1}
286};
287
288static int test_single_range_encoding(int range, const int *orig_arfcns,
289 int arfcns_num, int silent)
290{
291 int arfcns[RANGE_ENC_MAX_ARFCNS];
292 int w[RANGE_ENC_MAX_ARFCNS];
293 int f0_included = 0;
294 int rc, f0;
295 uint8_t chan_list[16] = {0};
296 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
297 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
298 int dec_arfcns_count = 0;
299 int arfcns_used = 0;
300 int i;
301
302 arfcns_used = arfcns_num;
303 memmove(arfcns, orig_arfcns, sizeof(arfcns));
304
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100305 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100306 /*
307 * Manipulate the ARFCN list according to the rules in J4 depending
308 * on the selected range.
309 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100310 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100311 f0, &f0_included);
312
313 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200314 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100315
316 if (!silent)
317 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
318 range, arfcns_used, f0, f0_included);
319
320 /* Select the range and the amount of bits needed */
321 switch (range) {
322 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200323 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100324 break;
325 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200326 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100327 break;
328 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200329 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100330 break;
331 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200332 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100333 break;
334 default:
335 return 1;
336 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100337
338 if (!silent)
339 printf("chan_list = %s\n",
340 osmo_hexdump(chan_list, sizeof(chan_list)));
341
342 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
343 0xfe, 1);
344 if (rc != 0) {
345 printf("Cannot decode freq list, rc = %d\n", rc);
346 return 1;
347 }
348
349 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
350 if (dec_freq[i].mask &&
351 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
352 dec_arfcns[dec_arfcns_count++] = i;
353 }
354
355 if (!silent) {
356 printf("Decoded freqs %d (expected %d)\n",
357 dec_arfcns_count, arfcns_num);
358 printf("Decoded: ");
359 for (i = 0; i < dec_arfcns_count; i++) {
360 printf("%d ", dec_arfcns[i]);
361 if (dec_arfcns[i] != orig_arfcns[i])
362 printf("(!= %d) ", orig_arfcns[i]);
363 }
364 printf("\n");
365 }
366
367 if (dec_arfcns_count != arfcns_num) {
368 printf("Wrong number of arfcns\n");
369 return 1;
370 }
371
372 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
373 printf("Decoding error, got wrong freqs\n");
374 fprintf(stderr, " w = ");
375 for (i = 0; i < ARRAY_SIZE(w); i++)
376 fprintf(stderr, "%d ", w[i]);
377 fprintf(stderr, "\n");
378 return 1;
379 }
380
381 return 0;
382}
383
384static void test_random_range_encoding(int range, int max_arfcn_num)
385{
386 int arfcns_num = 0;
387 int test_idx;
388 int rc, max_count;
389 int num_tests = 1024;
390
391 printf("Random range test: range %d, max num ARFCNs %d\n",
392 range, max_arfcn_num);
393
394 srandom(1);
395
396 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
397 for (test_idx = 0; test_idx < num_tests; test_idx++) {
398 int count;
399 int i;
400 int min_freq = 0;
401
402 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
403 char rnd_arfcns_set[1024] = {0};
404
405 if (range < ARFCN_RANGE_1024)
406 min_freq = random() % (1023 - range);
407
408 for (count = max_count; count; ) {
409 int arfcn = min_freq + random() % (range + 1);
410 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
411
412 if (!rnd_arfcns_set[arfcn]) {
413 rnd_arfcns_set[arfcn] = 1;
414 count -= 1;
415 }
416 }
417
418 arfcns_num = 0;
419 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
420 if (rnd_arfcns_set[i])
421 rnd_arfcns[arfcns_num++] = i;
422
423 rc = test_single_range_encoding(range, rnd_arfcns,
424 arfcns_num, 1);
425 if (rc != 0) {
426 printf("Failed on test %d, range %d, num ARFCNs %d\n",
427 test_idx, range, max_count);
428 test_single_range_encoding(range, rnd_arfcns,
429 arfcns_num, 0);
430 return;
431 }
432 }
433 }
434}
435
436static void test_range_encoding()
437{
438 int *arfcns;
439 int arfcns_num = 0;
440 int test_idx;
441 int range;
442
443 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
444 {
445 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
446 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
447 range = arfcn_test_ranges[test_idx].range;
448
449 printf("Range test %d: range %d, num ARFCNs %d\n",
450 test_idx, range, arfcns_num);
451
452 test_single_range_encoding(range, arfcns, arfcns_num, 0);
453 }
454
455 test_random_range_encoding(ARFCN_RANGE_128, 29);
456 test_random_range_encoding(ARFCN_RANGE_256, 22);
457 test_random_range_encoding(ARFCN_RANGE_512, 18);
458 test_random_range_encoding(ARFCN_RANGE_1024, 16);
459}
460
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100461static int freqs1[] = {
462 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
463};
464
465static int freqs2[] = {
466 402, 460, 1, 67, 131, 197, 272, 347,
467};
468
469static int freqs3[] = {
470 68, 128, 198, 279, 353, 398, 452,
471
472};
473
474static int w_out[] = {
475 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
476};
477
478static int range128[] = {
479 1, 1 + 127,
480};
481
482static int range256[] = {
483 1, 1 + 128,
484};
485
486static int range512[] = {
487 1, 1+ 511,
488};
489
490
491static void test_arfcn_filter()
492{
493 int arfcns[50], i, res, f0_included;
494 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
495 arfcns[i] = (i + 1) * 2;
496
497 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
498 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100499 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100500 arfcns[0], &f0_included);
501 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
502 VERIFY(f0_included, ==, 1);
503 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100504 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100505
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100506 /* check with range1024, ARFCN 0 is included */
507 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
508 arfcns[i] = i * 2;
509 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
510 0, &f0_included);
511 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
512 VERIFY(f0_included, ==, 1);
513 for (i = 0; i < res; ++i)
514 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
515
516 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100517 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
518 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100519 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
520 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100521 VERIFY(res, ==, ARRAY_SIZE(arfcns));
522 VERIFY(f0_included, ==, 0);
523 for (i = 0; i < res; ++i)
524 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
525}
526
527static void test_print_encoding()
528{
529 int rc;
530 int w[17];
531 uint8_t chan_list[16];
532 memset(chan_list, 0x23, sizeof(chan_list));
533
534 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
535 switch (rc % 3) {
536 case 0:
537 w[rc] = 0xAAAA;
538 break;
539 case 1:
540 w[rc] = 0x5555;
541 break;
542 case 2:
543 w[rc] = 0x9696;
544 break;
545 }
546
Max26679e02016-04-20 15:57:13 +0200547 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100548
549 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
550}
551
552static void test_si_range_helpers()
553{
554 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
555 int i, f0 = 0xFFFFFF;
556
557 memset(&ws[0], 0x23, sizeof(ws));
558
559 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100560 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100561 VERIFY(i, ==, 2);
562
563 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100564 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100565 VERIFY(i, ==, 2);
566
567 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100568 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100569 VERIFY(i, ==, 0);
570
Max26679e02016-04-20 15:57:13 +0200571 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100572
573 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
574 printf("w[%d]=%d\n", i, ws[i]);
575 VERIFY(ws[i], ==, w_out[i]);
576 }
577
578 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
579 VERIFY(i, ==, ARFCN_RANGE_128);
580 VERIFY(f0, ==, 1);
581
582 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
583 VERIFY(i, ==, ARFCN_RANGE_256);
584 VERIFY(f0, ==, 1);
585
586 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
587 VERIFY(i, ==, ARFCN_RANGE_512);
588 VERIFY(f0, ==, 1);
589}
590
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100591static void test_gsm411_rp_ref_wrap(void)
592{
593 struct gsm_subscriber_connection conn;
594 int res;
595
596 printf("testing RP-Reference wrap\n");
597
598 memset(&conn, 0, sizeof(conn));
599 conn.next_rp_ref = 255;
600
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200601 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100602 printf("Allocated reference: %d\n", res);
603 OSMO_ASSERT(res == 255);
604
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200605 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100606 printf("Allocated reference: %d\n", res);
607 OSMO_ASSERT(res == 0);
608
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200609 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100610 printf("Allocated reference: %d\n", res);
611 OSMO_ASSERT(res == 1);
612}
613
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800614int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000615{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100616 osmo_init_logging(&log_info);
617 log_set_log_level(osmo_stderr_target, LOGL_INFO);
618
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200619 test_location_area_identifier();
620 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100621
622 test_si_range_helpers();
623 test_arfcn_filter();
624 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100625 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100626 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100627
Max881064e2016-12-14 14:51:40 +0100628 test_si2q_segfault();
Max26679e02016-04-20 15:57:13 +0200629 test_si2q_e();
630 test_si2q_u();
Maxe610e702016-12-19 13:41:48 +0100631 test_si2q_mu();
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100632 printf("Done.\n");
633 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000634}