blob: 5a8c6ca521223d33db275bbbb2c6f4c7412bff39 [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>
Max845a3a42017-05-15 12:02:29 +020031#include <openbsc/gsm_data_shared.h>
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020032#include <openbsc/debug.h>
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010033#include <openbsc/arfcn_range_encode.h>
Max59a1bf32016-04-15 16:04:46 +020034#include <openbsc/system_information.h>
35#include <openbsc/abis_rsl.h>
36
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010037#include <osmocom/core/application.h>
Max59a1bf32016-04-15 16:04:46 +020038#include <osmocom/gsm/sysinfo.h>
Holger Freytheraa0fb362008-12-28 21:55:40 +000039
40#define COMPARE(result, op, value) \
41 if (!((result) op (value))) {\
42 fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \
43 exit(-1); \
44 }
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020045
46#define COMPARE_STR(result, value) \
47 if (strcmp(result, value) != 0) { \
48 fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \
49 exit(-1); \
50 }
Holger Freytheraa0fb362008-12-28 21:55:40 +000051
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010052#define DBG(...)
53
54#define VERIFY(res, cmp, wanted) \
55 if (!(res cmp wanted)) { \
56 printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \
Holger Hans Peter Freytherdaaea0c2015-08-03 09:28:41 +020057 __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010058 }
59
60
61
Holger Freytheraa0fb362008-12-28 21:55:40 +000062/*
63 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
64 */
65static void test_location_area_identifier(void)
66{
67 struct gsm48_loc_area_id lai48;
68
69 printf("Testing test location area identifier\n");
70
71 /*
72 * Test the default/test setup. Coming from
73 * bsc_hack.c dumps
74 */
Harald Welteafedeab2010-03-04 10:55:40 +010075 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytheraa0fb362008-12-28 21:55:40 +000076 COMPARE(lai48.digits[0], ==, 0x00);
77 COMPARE(lai48.digits[1], ==, 0xF1);
78 COMPARE(lai48.digits[2], ==, 0x10);
79 COMPARE(lai48.lac, ==, htons(0x0001));
80
Harald Welteafedeab2010-03-04 10:55:40 +010081 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytheraa0fb362008-12-28 21:55:40 +000082 COMPARE(lai48.digits[0], ==, 0x06);
83 COMPARE(lai48.digits[1], ==, 0xF2);
84 COMPARE(lai48.digits[2], ==, 0x10);
85 COMPARE(lai48.lac, ==, htons(0x000f));
86}
87
Maxf39d03a2017-05-12 17:00:30 +020088static inline void gen(struct gsm_bts *bts, const char *s)
Max59a1bf32016-04-15 16:04:46 +020089{
Max845a3a42017-05-15 12:02:29 +020090 int r;
91
Max69e9c0d2016-05-18 13:04:47 +020092 bts->si_valid = 0;
93 bts->si_valid |= (1 << SYSINFO_TYPE_2quater);
Max845a3a42017-05-15 12:02:29 +020094
Max845a3a42017-05-15 12:02:29 +020095 printf("generating SI2quater for %zu EARFCNs and %zu UARFCNs...\n",
96 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list), bts->si_common.uarfcn_length);
97
98 r = gsm_generate_si(bts, SYSINFO_TYPE_2quater);
Max59a1bf32016-04-15 16:04:46 +020099 if (r > 0)
Max70fdd242017-06-15 15:10:53 +0200100 for (bts->si2q_index = 0; bts->si2q_index < bts->si2q_count + 1; bts->si2q_index++)
101 printf("generated %s SI2quater [%02u/%02u]: [%d] %s\n",
102 GSM_BTS_HAS_SI(bts, SYSINFO_TYPE_2quater) ? "valid" : "invalid",
103 bts->si2q_index, bts->si2q_count, r,
104 osmo_hexdump((void *)GSM_BTS_SI2Q(bts, bts->si2q_index), GSM_MACBLOCK_LEN));
Max59a1bf32016-04-15 16:04:46 +0200105 else
Maxf39d03a2017-05-12 17:00:30 +0200106 printf("%s() failed to generate SI2quater: %s\n", s, strerror(-r));
Max59a1bf32016-04-15 16:04:46 +0200107}
108
Max845a3a42017-05-15 12:02:29 +0200109static inline void del_earfcn_b(struct gsm_bts *bts, uint16_t earfcn)
110{
111 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
112 int r = osmo_earfcn_del(e, earfcn);
113 if (r)
114 printf("failed to remove EARFCN %u: %s\n", earfcn, strerror(-r));
115 else
116 printf("removed EARFCN %u - ", earfcn);
117
118 gen(bts, __func__);
119}
120
Maxf39d03a2017-05-12 17:00:30 +0200121static inline void add_earfcn_b(struct gsm_bts *bts, uint16_t earfcn, uint8_t bw)
Maxaafff962016-04-20 15:57:14 +0200122{
Maxf39d03a2017-05-12 17:00:30 +0200123 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
124 int r = osmo_earfcn_add(e, earfcn, bw);
125 if (r)
126 printf("failed to add EARFCN %u: %s\n", earfcn, strerror(-r));
127 else
128 printf("added EARFCN %u - ", earfcn);
129
130 gen(bts, __func__);
131}
132
133static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble, bool diversity)
134{
135 int r;
136
137 bts->u_offset = 0;
138
139 r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
Maxaafff962016-04-20 15:57:14 +0200140 if (r < 0)
141 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
Max70fdd242017-06-15 15:10:53 +0200142 else {
143 bts->si2q_count = si2q_num(bts) - 1;
Maxf39d03a2017-05-12 17:00:30 +0200144 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200145 }
Maxaafff962016-04-20 15:57:14 +0200146}
147
Max881064e2016-12-14 14:51:40 +0100148static inline void test_si2q_segfault(void)
149{
150 struct gsm_bts *bts;
151 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
152 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
153
154 if (!network)
155 exit(1);
156 bts = gsm_bts_alloc(network);
157
158 _bts_uarfcn_add(bts, 10564, 319, 0);
159 _bts_uarfcn_add(bts, 10612, 319, 0);
Maxf39d03a2017-05-12 17:00:30 +0200160 gen(bts, __func__);
Max881064e2016-12-14 14:51:40 +0100161}
162
Maxe610e702016-12-19 13:41:48 +0100163static inline void test_si2q_mu(void)
164{
165 struct gsm_bts *bts;
166 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
167 printf("Test SI2quater multiple UARFCNs:\n");
168
169 if (!network)
170 exit(1);
171 bts = gsm_bts_alloc(network);
172
173 _bts_uarfcn_add(bts, 10564, 318, 0);
174 _bts_uarfcn_add(bts, 10612, 319, 0);
175 _bts_uarfcn_add(bts, 10612, 31, 0);
176 _bts_uarfcn_add(bts, 10612, 19, 0);
177 _bts_uarfcn_add(bts, 10613, 64, 0);
178 _bts_uarfcn_add(bts, 10613, 164, 0);
179 _bts_uarfcn_add(bts, 10613, 14, 0);
Maxe610e702016-12-19 13:41:48 +0100180}
181
Max26679e02016-04-20 15:57:13 +0200182static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200183{
184 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200185 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200186 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
187
188 if (!network)
189 exit(1);
Max70fdd242017-06-15 15:10:53 +0200190
Max26679e02016-04-20 15:57:13 +0200191 bts = gsm_bts_alloc(network);
192
Max69e9c0d2016-05-18 13:04:47 +0200193 /* first generate invalid SI as no UARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200194 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200195
Max69e9c0d2016-05-18 13:04:47 +0200196 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200197 _bts_uarfcn_add(bts, 1982, 13, 1);
198 _bts_uarfcn_add(bts, 1982, 44, 0);
199 _bts_uarfcn_add(bts, 1982, 61, 1);
200 _bts_uarfcn_add(bts, 1982, 89, 1);
201 _bts_uarfcn_add(bts, 1982, 113, 0);
202 _bts_uarfcn_add(bts, 1982, 123, 0);
203 _bts_uarfcn_add(bts, 1982, 56, 1);
204 _bts_uarfcn_add(bts, 1982, 72, 1);
205 _bts_uarfcn_add(bts, 1982, 223, 1);
206 _bts_uarfcn_add(bts, 1982, 14, 0);
207 _bts_uarfcn_add(bts, 1982, 88, 0);
Max26679e02016-04-20 15:57:13 +0200208}
209
210static inline void test_si2q_e(void)
211{
212 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200213 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200214 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200215
216 if (!network)
217 exit(1);
Max70fdd242017-06-15 15:10:53 +0200218
Max59a1bf32016-04-15 16:04:46 +0200219 bts = gsm_bts_alloc(network);
220
Maxf39d03a2017-05-12 17:00:30 +0200221 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
222 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
Max59a1bf32016-04-15 16:04:46 +0200223 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
224 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
225
226 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max70fdd242017-06-15 15:10:53 +0200227
Max69e9c0d2016-05-18 13:04:47 +0200228 /* first generate invalid SI as no EARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200229 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200230
Max845a3a42017-05-15 12:02:29 +0200231 /* subsequent calls should produce valid SI if there's enough memory and EARFCNs */
232 add_earfcn_b(bts, 1917, 5);
233 del_earfcn_b(bts, 1917);
Maxf39d03a2017-05-12 17:00:30 +0200234 add_earfcn_b(bts, 1917, 1);
235 add_earfcn_b(bts, 1932, OSMO_EARFCN_MEAS_INVALID);
236 add_earfcn_b(bts, 1937, 2);
237 add_earfcn_b(bts, 1945, OSMO_EARFCN_MEAS_INVALID);
238 add_earfcn_b(bts, 1965, OSMO_EARFCN_MEAS_INVALID);
239 add_earfcn_b(bts, 1967, 4);
240 add_earfcn_b(bts, 1982, 3);
Max59a1bf32016-04-15 16:04:46 +0200241}
242
Max70fdd242017-06-15 15:10:53 +0200243static inline void test_si2q_long(void)
244{
245 struct gsm_bts *bts;
246 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
247 printf("Testing SYSINFO_TYPE_2quater combined EARFCN & UARFCN generation:\n");
248
249 if (!network)
250 exit(1);
251
252 bts = gsm_bts_alloc(network);
253
254 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
255 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
256 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
257 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
258
259 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
260
261 bts_earfcn_add(bts, 1922, 11, 22, 8,32, 8);
262 bts_earfcn_add(bts, 1922, 11, 22, 8, 32, 8);
263 bts_earfcn_add(bts, 1924, 11, 12, 6, 11, 5);
264 bts_earfcn_add(bts, 1923, 11, 12, 6, 11, 5);
265 bts_earfcn_add(bts, 1925, 11, 12, 6, 11, 5);
266 bts_earfcn_add(bts, 2111, 11, 12, 6, 11, 5);
267 bts_earfcn_add(bts, 2112, 11, 12, 6, 11, 4);
268 bts_earfcn_add(bts, 2113, 11, 12, 6, 11, 3);
269 bts_earfcn_add(bts, 2114, 11, 12, 6, 11, 2);
270 bts_earfcn_add(bts, 2131, 11, 12, 6, 11, 5);
271 bts_earfcn_add(bts, 2132, 11, 12, 6, 11, 4);
272 bts_earfcn_add(bts, 2133, 11, 12, 6, 11, 3);
273 bts_earfcn_add(bts, 2134, 11, 12, 6, 11, 2);
274 bts_earfcn_add(bts, 2121, 11, 12, 6, 11, 5);
275 bts_earfcn_add(bts, 2122, 11, 12, 6, 11, 4);
276 bts_earfcn_add(bts, 2123, 11, 12, 6, 11, 3);
277 bts_earfcn_add(bts, 2124, 11, 12, 6, 11, 2);
278 _bts_uarfcn_add(bts, 1976, 13, 1);
279 _bts_uarfcn_add(bts, 1976, 38, 1);
280 _bts_uarfcn_add(bts, 1976, 44, 1);
281 _bts_uarfcn_add(bts, 1976, 120, 1);
282 _bts_uarfcn_add(bts, 1976, 140, 1);
283 _bts_uarfcn_add(bts, 1976, 163, 1);
284 _bts_uarfcn_add(bts, 1976, 166, 1);
285 _bts_uarfcn_add(bts, 1976, 217, 1);
286 _bts_uarfcn_add(bts, 1976, 224, 1);
287 _bts_uarfcn_add(bts, 1976, 225, 1);
288 _bts_uarfcn_add(bts, 1976, 226, 1);
289}
290
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200291static void test_mi_functionality(void)
292{
293 const char *imsi_odd = "987654321098763";
294 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200295 const uint32_t tmsi = 0xfabeacd0;
296 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200297 unsigned int mi_len;
298 char mi_parsed[GSM48_MI_SIZE];
299
300 printf("Testing parsing and generating TMSI/IMSI\n");
301
302 /* tmsi code */
303 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
304 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100305 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200306
307 /* imsi code */
308 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
309 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200310 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200311 COMPARE_STR(mi_parsed, imsi_odd);
312
313 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
314 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200315 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200316 COMPARE_STR(mi_parsed, imsi_even);
317}
318
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100319struct {
320 int range;
321 int arfcns_num;
322 int arfcns[RANGE_ENC_MAX_ARFCNS];
323} arfcn_test_ranges[] = {
324 {ARFCN_RANGE_512, 12,
325 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
326 {ARFCN_RANGE_512, 17,
327 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
328 {ARFCN_RANGE_512, 18,
329 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
330 {ARFCN_RANGE_512, 18,
331 { 1, 17, 31, 45, 58, 79, 81, 97,
332 113, 127, 213, 277, 287, 311, 331, 391,
333 417, 511 }},
334 {ARFCN_RANGE_512, 6,
335 { 1, 17, 31, 45, 58, 79 }},
336 {ARFCN_RANGE_512, 6,
337 { 10, 17, 31, 45, 58, 79 }},
338 {ARFCN_RANGE_1024, 17,
339 { 0, 17, 31, 45, 58, 79, 81, 97,
340 113, 127, 213, 277, 287, 311, 331, 391,
341 1023 }},
342 {ARFCN_RANGE_1024, 16,
343 { 17, 31, 45, 58, 79, 81, 97, 113,
344 127, 213, 277, 287, 311, 331, 391, 1023 }},
345 {-1}
346};
347
348static int test_single_range_encoding(int range, const int *orig_arfcns,
349 int arfcns_num, int silent)
350{
351 int arfcns[RANGE_ENC_MAX_ARFCNS];
352 int w[RANGE_ENC_MAX_ARFCNS];
353 int f0_included = 0;
354 int rc, f0;
355 uint8_t chan_list[16] = {0};
356 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
357 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
358 int dec_arfcns_count = 0;
359 int arfcns_used = 0;
360 int i;
361
362 arfcns_used = arfcns_num;
363 memmove(arfcns, orig_arfcns, sizeof(arfcns));
364
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100365 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100366 /*
367 * Manipulate the ARFCN list according to the rules in J4 depending
368 * on the selected range.
369 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100370 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100371 f0, &f0_included);
372
373 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200374 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100375
376 if (!silent)
377 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
378 range, arfcns_used, f0, f0_included);
379
380 /* Select the range and the amount of bits needed */
381 switch (range) {
382 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200383 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100384 break;
385 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200386 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100387 break;
388 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200389 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100390 break;
391 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200392 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100393 break;
394 default:
395 return 1;
396 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100397
398 if (!silent)
399 printf("chan_list = %s\n",
400 osmo_hexdump(chan_list, sizeof(chan_list)));
401
402 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
403 0xfe, 1);
404 if (rc != 0) {
405 printf("Cannot decode freq list, rc = %d\n", rc);
406 return 1;
407 }
408
409 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
410 if (dec_freq[i].mask &&
411 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
412 dec_arfcns[dec_arfcns_count++] = i;
413 }
414
415 if (!silent) {
416 printf("Decoded freqs %d (expected %d)\n",
417 dec_arfcns_count, arfcns_num);
418 printf("Decoded: ");
419 for (i = 0; i < dec_arfcns_count; i++) {
420 printf("%d ", dec_arfcns[i]);
421 if (dec_arfcns[i] != orig_arfcns[i])
422 printf("(!= %d) ", orig_arfcns[i]);
423 }
424 printf("\n");
425 }
426
427 if (dec_arfcns_count != arfcns_num) {
428 printf("Wrong number of arfcns\n");
429 return 1;
430 }
431
432 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
433 printf("Decoding error, got wrong freqs\n");
434 fprintf(stderr, " w = ");
435 for (i = 0; i < ARRAY_SIZE(w); i++)
436 fprintf(stderr, "%d ", w[i]);
437 fprintf(stderr, "\n");
438 return 1;
439 }
440
441 return 0;
442}
443
444static void test_random_range_encoding(int range, int max_arfcn_num)
445{
446 int arfcns_num = 0;
447 int test_idx;
448 int rc, max_count;
449 int num_tests = 1024;
450
451 printf("Random range test: range %d, max num ARFCNs %d\n",
452 range, max_arfcn_num);
453
454 srandom(1);
455
456 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
457 for (test_idx = 0; test_idx < num_tests; test_idx++) {
458 int count;
459 int i;
460 int min_freq = 0;
461
462 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
463 char rnd_arfcns_set[1024] = {0};
464
465 if (range < ARFCN_RANGE_1024)
466 min_freq = random() % (1023 - range);
467
468 for (count = max_count; count; ) {
469 int arfcn = min_freq + random() % (range + 1);
470 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
471
472 if (!rnd_arfcns_set[arfcn]) {
473 rnd_arfcns_set[arfcn] = 1;
474 count -= 1;
475 }
476 }
477
478 arfcns_num = 0;
479 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
480 if (rnd_arfcns_set[i])
481 rnd_arfcns[arfcns_num++] = i;
482
483 rc = test_single_range_encoding(range, rnd_arfcns,
484 arfcns_num, 1);
485 if (rc != 0) {
486 printf("Failed on test %d, range %d, num ARFCNs %d\n",
487 test_idx, range, max_count);
488 test_single_range_encoding(range, rnd_arfcns,
489 arfcns_num, 0);
490 return;
491 }
492 }
493 }
494}
495
496static void test_range_encoding()
497{
498 int *arfcns;
499 int arfcns_num = 0;
500 int test_idx;
501 int range;
502
503 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
504 {
505 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
506 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
507 range = arfcn_test_ranges[test_idx].range;
508
509 printf("Range test %d: range %d, num ARFCNs %d\n",
510 test_idx, range, arfcns_num);
511
512 test_single_range_encoding(range, arfcns, arfcns_num, 0);
513 }
514
515 test_random_range_encoding(ARFCN_RANGE_128, 29);
516 test_random_range_encoding(ARFCN_RANGE_256, 22);
517 test_random_range_encoding(ARFCN_RANGE_512, 18);
518 test_random_range_encoding(ARFCN_RANGE_1024, 16);
519}
520
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100521static int freqs1[] = {
522 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
523};
524
525static int freqs2[] = {
526 402, 460, 1, 67, 131, 197, 272, 347,
527};
528
529static int freqs3[] = {
530 68, 128, 198, 279, 353, 398, 452,
531
532};
533
534static int w_out[] = {
535 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
536};
537
538static int range128[] = {
539 1, 1 + 127,
540};
541
542static int range256[] = {
543 1, 1 + 128,
544};
545
546static int range512[] = {
547 1, 1+ 511,
548};
549
550
551static void test_arfcn_filter()
552{
553 int arfcns[50], i, res, f0_included;
554 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
555 arfcns[i] = (i + 1) * 2;
556
557 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
558 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100559 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100560 arfcns[0], &f0_included);
561 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
562 VERIFY(f0_included, ==, 1);
563 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100564 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100565
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100566 /* check with range1024, ARFCN 0 is included */
567 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
568 arfcns[i] = i * 2;
569 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
570 0, &f0_included);
571 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
572 VERIFY(f0_included, ==, 1);
573 for (i = 0; i < res; ++i)
574 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
575
576 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100577 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
578 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100579 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
580 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100581 VERIFY(res, ==, ARRAY_SIZE(arfcns));
582 VERIFY(f0_included, ==, 0);
583 for (i = 0; i < res; ++i)
584 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
585}
586
587static void test_print_encoding()
588{
589 int rc;
590 int w[17];
591 uint8_t chan_list[16];
592 memset(chan_list, 0x23, sizeof(chan_list));
593
594 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
595 switch (rc % 3) {
596 case 0:
597 w[rc] = 0xAAAA;
598 break;
599 case 1:
600 w[rc] = 0x5555;
601 break;
602 case 2:
603 w[rc] = 0x9696;
604 break;
605 }
606
Max26679e02016-04-20 15:57:13 +0200607 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100608
609 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
610}
611
612static void test_si_range_helpers()
613{
614 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
615 int i, f0 = 0xFFFFFF;
616
617 memset(&ws[0], 0x23, sizeof(ws));
618
619 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100620 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100621 VERIFY(i, ==, 2);
622
623 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100624 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100625 VERIFY(i, ==, 2);
626
627 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100628 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100629 VERIFY(i, ==, 0);
630
Max26679e02016-04-20 15:57:13 +0200631 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100632
633 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
634 printf("w[%d]=%d\n", i, ws[i]);
635 VERIFY(ws[i], ==, w_out[i]);
636 }
637
638 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
639 VERIFY(i, ==, ARFCN_RANGE_128);
640 VERIFY(f0, ==, 1);
641
642 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
643 VERIFY(i, ==, ARFCN_RANGE_256);
644 VERIFY(f0, ==, 1);
645
646 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
647 VERIFY(i, ==, ARFCN_RANGE_512);
648 VERIFY(f0, ==, 1);
649}
650
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100651static void test_gsm411_rp_ref_wrap(void)
652{
653 struct gsm_subscriber_connection conn;
654 int res;
655
656 printf("testing RP-Reference wrap\n");
657
658 memset(&conn, 0, sizeof(conn));
659 conn.next_rp_ref = 255;
660
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200661 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100662 printf("Allocated reference: %d\n", res);
663 OSMO_ASSERT(res == 255);
664
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200665 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100666 printf("Allocated reference: %d\n", res);
667 OSMO_ASSERT(res == 0);
668
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200669 res = sms_next_rp_msg_ref(&conn.next_rp_ref);
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100670 printf("Allocated reference: %d\n", res);
671 OSMO_ASSERT(res == 1);
672}
673
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800674int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000675{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100676 osmo_init_logging(&log_info);
677 log_set_log_level(osmo_stderr_target, LOGL_INFO);
678
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200679 test_location_area_identifier();
680 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100681
682 test_si_range_helpers();
683 test_arfcn_filter();
684 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100685 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100686 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100687
Max881064e2016-12-14 14:51:40 +0100688 test_si2q_segfault();
Max26679e02016-04-20 15:57:13 +0200689 test_si2q_e();
690 test_si2q_u();
Maxe610e702016-12-19 13:41:48 +0100691 test_si2q_mu();
Max70fdd242017-06-15 15:10:53 +0200692 test_si2q_long();
693
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100694 printf("Done.\n");
Max70fdd242017-06-15 15:10:53 +0200695
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100696 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000697}