blob: 265e4b20c69ecd6df735c625dd335d499fe5e0f5 [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
Maxf39d03a2017-05-12 17:00:30 +020087static inline void gen(struct gsm_bts *bts, const char *s)
Max59a1bf32016-04-15 16:04:46 +020088{
Maxf39d03a2017-05-12 17:00:30 +020089 bts->u_offset = 0;
90 bts->e_offset = 0;
91 bts->si2q_index = 0;
92 bts->si2q_count = 0;
Max69e9c0d2016-05-18 13:04:47 +020093 bts->si_valid = 0;
94 bts->si_valid |= (1 << SYSINFO_TYPE_2quater);
95 /* should be no-op as entire buffer is filled with padding: */
Max6f0e50c2017-04-12 15:30:54 +020096 memset(GSM_BTS_SI(bts, SYSINFO_TYPE_2quater), 0xAE, GSM_MACBLOCK_LEN);
Max59a1bf32016-04-15 16:04:46 +020097 int r = gsm_generate_si(bts, SYSINFO_TYPE_2quater);
Max69e9c0d2016-05-18 13:04:47 +020098 bool v = bts->si_valid & (1 << SYSINFO_TYPE_2quater);
Max59a1bf32016-04-15 16:04:46 +020099 if (r > 0)
Max69e9c0d2016-05-18 13:04:47 +0200100 printf("generated %s SI2quater: [%d] %s\n",
Max6f0e50c2017-04-12 15:30:54 +0200101 v ? "valid" : "invalid", r, osmo_hexdump(GSM_BTS_SI(bts, SYSINFO_TYPE_2quater), r));
Max59a1bf32016-04-15 16:04:46 +0200102 else
Maxf39d03a2017-05-12 17:00:30 +0200103 printf("%s() failed to generate SI2quater: %s\n", s, strerror(-r));
Max59a1bf32016-04-15 16:04:46 +0200104}
105
Maxf39d03a2017-05-12 17:00:30 +0200106static inline void add_earfcn_b(struct gsm_bts *bts, uint16_t earfcn, uint8_t bw)
Maxaafff962016-04-20 15:57:14 +0200107{
Maxf39d03a2017-05-12 17:00:30 +0200108 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
109 int r = osmo_earfcn_add(e, earfcn, bw);
110 if (r)
111 printf("failed to add EARFCN %u: %s\n", earfcn, strerror(-r));
112 else
113 printf("added EARFCN %u - ", earfcn);
114
115 gen(bts, __func__);
116}
117
118static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble, bool diversity)
119{
120 int r;
121
122 bts->u_offset = 0;
123
124 r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
Maxaafff962016-04-20 15:57:14 +0200125 if (r < 0)
126 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
127 else
Maxf39d03a2017-05-12 17:00:30 +0200128 gen(bts, __func__);
Maxaafff962016-04-20 15:57:14 +0200129}
130
Max881064e2016-12-14 14:51:40 +0100131static inline void test_si2q_segfault(void)
132{
133 struct gsm_bts *bts;
134 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
135 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
136
137 if (!network)
138 exit(1);
139 bts = gsm_bts_alloc(network);
140
141 _bts_uarfcn_add(bts, 10564, 319, 0);
142 _bts_uarfcn_add(bts, 10612, 319, 0);
Maxf39d03a2017-05-12 17:00:30 +0200143 gen(bts, __func__);
Max881064e2016-12-14 14:51:40 +0100144}
145
Maxe610e702016-12-19 13:41:48 +0100146static inline void test_si2q_mu(void)
147{
148 struct gsm_bts *bts;
149 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
150 printf("Test SI2quater multiple UARFCNs:\n");
151
152 if (!network)
153 exit(1);
154 bts = gsm_bts_alloc(network);
155
156 _bts_uarfcn_add(bts, 10564, 318, 0);
157 _bts_uarfcn_add(bts, 10612, 319, 0);
158 _bts_uarfcn_add(bts, 10612, 31, 0);
159 _bts_uarfcn_add(bts, 10612, 19, 0);
160 _bts_uarfcn_add(bts, 10613, 64, 0);
161 _bts_uarfcn_add(bts, 10613, 164, 0);
162 _bts_uarfcn_add(bts, 10613, 14, 0);
Maxf39d03a2017-05-12 17:00:30 +0200163 gen(bts, __func__);
Maxe610e702016-12-19 13:41:48 +0100164}
165
Max26679e02016-04-20 15:57:13 +0200166static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200167{
168 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200169 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200170 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
171
172 if (!network)
173 exit(1);
174 bts = gsm_bts_alloc(network);
175
Max69e9c0d2016-05-18 13:04:47 +0200176 /* first generate invalid SI as no UARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200177 gen(bts, __func__);
Max69e9c0d2016-05-18 13:04:47 +0200178 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200179 _bts_uarfcn_add(bts, 1982, 13, 1);
180 _bts_uarfcn_add(bts, 1982, 44, 0);
181 _bts_uarfcn_add(bts, 1982, 61, 1);
182 _bts_uarfcn_add(bts, 1982, 89, 1);
183 _bts_uarfcn_add(bts, 1982, 113, 0);
184 _bts_uarfcn_add(bts, 1982, 123, 0);
185 _bts_uarfcn_add(bts, 1982, 56, 1);
186 _bts_uarfcn_add(bts, 1982, 72, 1);
187 _bts_uarfcn_add(bts, 1982, 223, 1);
188 _bts_uarfcn_add(bts, 1982, 14, 0);
189 _bts_uarfcn_add(bts, 1982, 88, 0);
Maxf39d03a2017-05-12 17:00:30 +0200190 gen(bts, __func__);
Max26679e02016-04-20 15:57:13 +0200191}
192
193static inline void test_si2q_e(void)
194{
195 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200196 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200197 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200198
199 if (!network)
200 exit(1);
201 bts = gsm_bts_alloc(network);
202
Maxf39d03a2017-05-12 17:00:30 +0200203 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
204 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
Max59a1bf32016-04-15 16:04:46 +0200205 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
206 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
207
208 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max69e9c0d2016-05-18 13:04:47 +0200209 /* first generate invalid SI as no EARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200210 gen(bts, __func__);
Max69e9c0d2016-05-18 13:04:47 +0200211 /* subsequent calls should produce valid SI if there's enough memory */
Maxf39d03a2017-05-12 17:00:30 +0200212 add_earfcn_b(bts, 1917, 1);
213 add_earfcn_b(bts, 1932, OSMO_EARFCN_MEAS_INVALID);
214 add_earfcn_b(bts, 1937, 2);
215 add_earfcn_b(bts, 1945, OSMO_EARFCN_MEAS_INVALID);
216 add_earfcn_b(bts, 1965, OSMO_EARFCN_MEAS_INVALID);
217 add_earfcn_b(bts, 1967, 4);
218 add_earfcn_b(bts, 1982, 3);
Max59a1bf32016-04-15 16:04:46 +0200219}
220
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200221static void test_mi_functionality(void)
222{
223 const char *imsi_odd = "987654321098763";
224 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200225 const uint32_t tmsi = 0xfabeacd0;
226 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200227 unsigned int mi_len;
228 char mi_parsed[GSM48_MI_SIZE];
229
230 printf("Testing parsing and generating TMSI/IMSI\n");
231
232 /* tmsi code */
233 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
234 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100235 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200236
237 /* imsi code */
238 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
239 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200240 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200241 COMPARE_STR(mi_parsed, imsi_odd);
242
243 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
244 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200245 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200246 COMPARE_STR(mi_parsed, imsi_even);
247}
248
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100249struct {
250 int range;
251 int arfcns_num;
252 int arfcns[RANGE_ENC_MAX_ARFCNS];
253} arfcn_test_ranges[] = {
254 {ARFCN_RANGE_512, 12,
255 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
256 {ARFCN_RANGE_512, 17,
257 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
258 {ARFCN_RANGE_512, 18,
259 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
260 {ARFCN_RANGE_512, 18,
261 { 1, 17, 31, 45, 58, 79, 81, 97,
262 113, 127, 213, 277, 287, 311, 331, 391,
263 417, 511 }},
264 {ARFCN_RANGE_512, 6,
265 { 1, 17, 31, 45, 58, 79 }},
266 {ARFCN_RANGE_512, 6,
267 { 10, 17, 31, 45, 58, 79 }},
268 {ARFCN_RANGE_1024, 17,
269 { 0, 17, 31, 45, 58, 79, 81, 97,
270 113, 127, 213, 277, 287, 311, 331, 391,
271 1023 }},
272 {ARFCN_RANGE_1024, 16,
273 { 17, 31, 45, 58, 79, 81, 97, 113,
274 127, 213, 277, 287, 311, 331, 391, 1023 }},
275 {-1}
276};
277
278static int test_single_range_encoding(int range, const int *orig_arfcns,
279 int arfcns_num, int silent)
280{
281 int arfcns[RANGE_ENC_MAX_ARFCNS];
282 int w[RANGE_ENC_MAX_ARFCNS];
283 int f0_included = 0;
284 int rc, f0;
285 uint8_t chan_list[16] = {0};
286 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
287 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
288 int dec_arfcns_count = 0;
289 int arfcns_used = 0;
290 int i;
291
292 arfcns_used = arfcns_num;
293 memmove(arfcns, orig_arfcns, sizeof(arfcns));
294
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100295 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100296 /*
297 * Manipulate the ARFCN list according to the rules in J4 depending
298 * on the selected range.
299 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100300 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100301 f0, &f0_included);
302
303 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200304 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100305
306 if (!silent)
307 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
308 range, arfcns_used, f0, f0_included);
309
310 /* Select the range and the amount of bits needed */
311 switch (range) {
312 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200313 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100314 break;
315 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200316 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100317 break;
318 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200319 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100320 break;
321 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200322 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100323 break;
324 default:
325 return 1;
326 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100327
328 if (!silent)
329 printf("chan_list = %s\n",
330 osmo_hexdump(chan_list, sizeof(chan_list)));
331
332 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
333 0xfe, 1);
334 if (rc != 0) {
335 printf("Cannot decode freq list, rc = %d\n", rc);
336 return 1;
337 }
338
339 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
340 if (dec_freq[i].mask &&
341 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
342 dec_arfcns[dec_arfcns_count++] = i;
343 }
344
345 if (!silent) {
346 printf("Decoded freqs %d (expected %d)\n",
347 dec_arfcns_count, arfcns_num);
348 printf("Decoded: ");
349 for (i = 0; i < dec_arfcns_count; i++) {
350 printf("%d ", dec_arfcns[i]);
351 if (dec_arfcns[i] != orig_arfcns[i])
352 printf("(!= %d) ", orig_arfcns[i]);
353 }
354 printf("\n");
355 }
356
357 if (dec_arfcns_count != arfcns_num) {
358 printf("Wrong number of arfcns\n");
359 return 1;
360 }
361
362 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
363 printf("Decoding error, got wrong freqs\n");
364 fprintf(stderr, " w = ");
365 for (i = 0; i < ARRAY_SIZE(w); i++)
366 fprintf(stderr, "%d ", w[i]);
367 fprintf(stderr, "\n");
368 return 1;
369 }
370
371 return 0;
372}
373
374static void test_random_range_encoding(int range, int max_arfcn_num)
375{
376 int arfcns_num = 0;
377 int test_idx;
378 int rc, max_count;
379 int num_tests = 1024;
380
381 printf("Random range test: range %d, max num ARFCNs %d\n",
382 range, max_arfcn_num);
383
384 srandom(1);
385
386 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
387 for (test_idx = 0; test_idx < num_tests; test_idx++) {
388 int count;
389 int i;
390 int min_freq = 0;
391
392 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
393 char rnd_arfcns_set[1024] = {0};
394
395 if (range < ARFCN_RANGE_1024)
396 min_freq = random() % (1023 - range);
397
398 for (count = max_count; count; ) {
399 int arfcn = min_freq + random() % (range + 1);
400 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
401
402 if (!rnd_arfcns_set[arfcn]) {
403 rnd_arfcns_set[arfcn] = 1;
404 count -= 1;
405 }
406 }
407
408 arfcns_num = 0;
409 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
410 if (rnd_arfcns_set[i])
411 rnd_arfcns[arfcns_num++] = i;
412
413 rc = test_single_range_encoding(range, rnd_arfcns,
414 arfcns_num, 1);
415 if (rc != 0) {
416 printf("Failed on test %d, range %d, num ARFCNs %d\n",
417 test_idx, range, max_count);
418 test_single_range_encoding(range, rnd_arfcns,
419 arfcns_num, 0);
420 return;
421 }
422 }
423 }
424}
425
426static void test_range_encoding()
427{
428 int *arfcns;
429 int arfcns_num = 0;
430 int test_idx;
431 int range;
432
433 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
434 {
435 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
436 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
437 range = arfcn_test_ranges[test_idx].range;
438
439 printf("Range test %d: range %d, num ARFCNs %d\n",
440 test_idx, range, arfcns_num);
441
442 test_single_range_encoding(range, arfcns, arfcns_num, 0);
443 }
444
445 test_random_range_encoding(ARFCN_RANGE_128, 29);
446 test_random_range_encoding(ARFCN_RANGE_256, 22);
447 test_random_range_encoding(ARFCN_RANGE_512, 18);
448 test_random_range_encoding(ARFCN_RANGE_1024, 16);
449}
450
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100451static int freqs1[] = {
452 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
453};
454
455static int freqs2[] = {
456 402, 460, 1, 67, 131, 197, 272, 347,
457};
458
459static int freqs3[] = {
460 68, 128, 198, 279, 353, 398, 452,
461
462};
463
464static int w_out[] = {
465 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
466};
467
468static int range128[] = {
469 1, 1 + 127,
470};
471
472static int range256[] = {
473 1, 1 + 128,
474};
475
476static int range512[] = {
477 1, 1+ 511,
478};
479
480
481static void test_arfcn_filter()
482{
483 int arfcns[50], i, res, f0_included;
484 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
485 arfcns[i] = (i + 1) * 2;
486
487 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
488 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100489 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100490 arfcns[0], &f0_included);
491 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
492 VERIFY(f0_included, ==, 1);
493 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100494 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100495
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100496 /* check with range1024, ARFCN 0 is included */
497 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
498 arfcns[i] = i * 2;
499 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
500 0, &f0_included);
501 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
502 VERIFY(f0_included, ==, 1);
503 for (i = 0; i < res; ++i)
504 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
505
506 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100507 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
508 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100509 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
510 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100511 VERIFY(res, ==, ARRAY_SIZE(arfcns));
512 VERIFY(f0_included, ==, 0);
513 for (i = 0; i < res; ++i)
514 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
515}
516
517static void test_print_encoding()
518{
519 int rc;
520 int w[17];
521 uint8_t chan_list[16];
522 memset(chan_list, 0x23, sizeof(chan_list));
523
524 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
525 switch (rc % 3) {
526 case 0:
527 w[rc] = 0xAAAA;
528 break;
529 case 1:
530 w[rc] = 0x5555;
531 break;
532 case 2:
533 w[rc] = 0x9696;
534 break;
535 }
536
Max26679e02016-04-20 15:57:13 +0200537 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100538
539 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
540}
541
542static void test_si_range_helpers()
543{
544 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
545 int i, f0 = 0xFFFFFF;
546
547 memset(&ws[0], 0x23, sizeof(ws));
548
549 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100550 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100551 VERIFY(i, ==, 2);
552
553 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100554 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100555 VERIFY(i, ==, 2);
556
557 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100558 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100559 VERIFY(i, ==, 0);
560
Max26679e02016-04-20 15:57:13 +0200561 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100562
563 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
564 printf("w[%d]=%d\n", i, ws[i]);
565 VERIFY(ws[i], ==, w_out[i]);
566 }
567
568 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
569 VERIFY(i, ==, ARFCN_RANGE_128);
570 VERIFY(f0, ==, 1);
571
572 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
573 VERIFY(i, ==, ARFCN_RANGE_256);
574 VERIFY(f0, ==, 1);
575
576 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
577 VERIFY(i, ==, ARFCN_RANGE_512);
578 VERIFY(f0, ==, 1);
579}
580
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100581static void test_gsm411_rp_ref_wrap(void)
582{
583 struct gsm_subscriber_connection conn;
584 int res;
585
586 printf("testing RP-Reference wrap\n");
587
588 memset(&conn, 0, sizeof(conn));
589 conn.next_rp_ref = 255;
590
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200591 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100592 printf("Allocated reference: %d\n", res);
593 OSMO_ASSERT(res == 255);
594
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200595 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100596 printf("Allocated reference: %d\n", res);
597 OSMO_ASSERT(res == 0);
598
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200599 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100600 printf("Allocated reference: %d\n", res);
601 OSMO_ASSERT(res == 1);
602}
603
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800604int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000605{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100606 osmo_init_logging(&log_info);
607 log_set_log_level(osmo_stderr_target, LOGL_INFO);
608
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200609 test_location_area_identifier();
610 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100611
612 test_si_range_helpers();
613 test_arfcn_filter();
614 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100615 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100616 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100617
Max881064e2016-12-14 14:51:40 +0100618 test_si2q_segfault();
Max26679e02016-04-20 15:57:13 +0200619 test_si2q_e();
620 test_si2q_u();
Maxe610e702016-12-19 13:41:48 +0100621 test_si2q_mu();
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100622 printf("Done.\n");
623 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000624}