blob: 781ef614757c687f28269a32434b7f608b217c50 [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>
24
Harald Welteafedeab2010-03-04 10:55:40 +010025#include <arpa/inet.h>
26
Holger Freytheraa0fb362008-12-28 21:55:40 +000027#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +010028#include <openbsc/gsm_04_11.h>
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020029#include <openbsc/gsm_subscriber.h>
30#include <openbsc/debug.h>
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010031#include <openbsc/arfcn_range_encode.h>
32#include <osmocom/core/application.h>
Holger Freytheraa0fb362008-12-28 21:55:40 +000033
34#define COMPARE(result, op, value) \
35 if (!((result) op (value))) {\
36 fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \
37 exit(-1); \
38 }
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020039
40#define COMPARE_STR(result, value) \
41 if (strcmp(result, value) != 0) { \
42 fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \
43 exit(-1); \
44 }
Holger Freytheraa0fb362008-12-28 21:55:40 +000045
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010046#define DBG(...)
47
48#define VERIFY(res, cmp, wanted) \
49 if (!(res cmp wanted)) { \
50 printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \
Holger Hans Peter Freytherdaaea0c2015-08-03 09:28:41 +020051 __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010052 }
53
54
55
Holger Freytheraa0fb362008-12-28 21:55:40 +000056/*
57 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
58 */
59static void test_location_area_identifier(void)
60{
61 struct gsm48_loc_area_id lai48;
62
63 printf("Testing test location area identifier\n");
64
65 /*
66 * Test the default/test setup. Coming from
67 * bsc_hack.c dumps
68 */
Harald Welteafedeab2010-03-04 10:55:40 +010069 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytheraa0fb362008-12-28 21:55:40 +000070 COMPARE(lai48.digits[0], ==, 0x00);
71 COMPARE(lai48.digits[1], ==, 0xF1);
72 COMPARE(lai48.digits[2], ==, 0x10);
73 COMPARE(lai48.lac, ==, htons(0x0001));
74
Harald Welteafedeab2010-03-04 10:55:40 +010075 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytheraa0fb362008-12-28 21:55:40 +000076 COMPARE(lai48.digits[0], ==, 0x06);
77 COMPARE(lai48.digits[1], ==, 0xF2);
78 COMPARE(lai48.digits[2], ==, 0x10);
79 COMPARE(lai48.lac, ==, htons(0x000f));
80}
81
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020082static void test_mi_functionality(void)
83{
84 const char *imsi_odd = "987654321098763";
85 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020086 const uint32_t tmsi = 0xfabeacd0;
87 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020088 unsigned int mi_len;
89 char mi_parsed[GSM48_MI_SIZE];
90
91 printf("Testing parsing and generating TMSI/IMSI\n");
92
93 /* tmsi code */
94 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
95 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020096 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020097
98 /* imsi code */
99 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
100 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200101 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200102 COMPARE_STR(mi_parsed, imsi_odd);
103
104 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
105 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200106 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200107 COMPARE_STR(mi_parsed, imsi_even);
108}
109
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100110struct {
111 int range;
112 int arfcns_num;
113 int arfcns[RANGE_ENC_MAX_ARFCNS];
114} arfcn_test_ranges[] = {
115 {ARFCN_RANGE_512, 12,
116 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
117 {ARFCN_RANGE_512, 17,
118 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
119 {ARFCN_RANGE_512, 18,
120 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
121 {ARFCN_RANGE_512, 18,
122 { 1, 17, 31, 45, 58, 79, 81, 97,
123 113, 127, 213, 277, 287, 311, 331, 391,
124 417, 511 }},
125 {ARFCN_RANGE_512, 6,
126 { 1, 17, 31, 45, 58, 79 }},
127 {ARFCN_RANGE_512, 6,
128 { 10, 17, 31, 45, 58, 79 }},
129 {ARFCN_RANGE_1024, 17,
130 { 0, 17, 31, 45, 58, 79, 81, 97,
131 113, 127, 213, 277, 287, 311, 331, 391,
132 1023 }},
133 {ARFCN_RANGE_1024, 16,
134 { 17, 31, 45, 58, 79, 81, 97, 113,
135 127, 213, 277, 287, 311, 331, 391, 1023 }},
136 {-1}
137};
138
139static int test_single_range_encoding(int range, const int *orig_arfcns,
140 int arfcns_num, int silent)
141{
142 int arfcns[RANGE_ENC_MAX_ARFCNS];
143 int w[RANGE_ENC_MAX_ARFCNS];
144 int f0_included = 0;
145 int rc, f0;
146 uint8_t chan_list[16] = {0};
147 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
148 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
149 int dec_arfcns_count = 0;
150 int arfcns_used = 0;
151 int i;
152
153 arfcns_used = arfcns_num;
154 memmove(arfcns, orig_arfcns, sizeof(arfcns));
155
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100156 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100157 /*
158 * Manipulate the ARFCN list according to the rules in J4 depending
159 * on the selected range.
160 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100161 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100162 f0, &f0_included);
163
164 memset(w, 0, sizeof(w));
165 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
166 if (rc != 0) {
167 printf("Cannot compute range W(k), rc = %d\n", rc);
168 return 1;
169 }
170
171 if (!silent)
172 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
173 range, arfcns_used, f0, f0_included);
174
175 /* Select the range and the amount of bits needed */
176 switch (range) {
177 case ARFCN_RANGE_128:
178 rc = range_enc_range128(chan_list, f0, w);
179 break;
180 case ARFCN_RANGE_256:
181 rc = range_enc_range256(chan_list, f0, w);
182 break;
183 case ARFCN_RANGE_512:
184 rc = range_enc_range512(chan_list, f0, w);
185 break;
186 case ARFCN_RANGE_1024:
187 rc = range_enc_range1024(chan_list, f0, f0_included, w);
188 break;
189 default:
190 return 1;
191 };
192 if (rc != 0) {
193 printf("Cannot encode range, rc = %d\n", rc);
194 return 1;
195 }
196
197 if (!silent)
198 printf("chan_list = %s\n",
199 osmo_hexdump(chan_list, sizeof(chan_list)));
200
201 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
202 0xfe, 1);
203 if (rc != 0) {
204 printf("Cannot decode freq list, rc = %d\n", rc);
205 return 1;
206 }
207
208 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
209 if (dec_freq[i].mask &&
210 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
211 dec_arfcns[dec_arfcns_count++] = i;
212 }
213
214 if (!silent) {
215 printf("Decoded freqs %d (expected %d)\n",
216 dec_arfcns_count, arfcns_num);
217 printf("Decoded: ");
218 for (i = 0; i < dec_arfcns_count; i++) {
219 printf("%d ", dec_arfcns[i]);
220 if (dec_arfcns[i] != orig_arfcns[i])
221 printf("(!= %d) ", orig_arfcns[i]);
222 }
223 printf("\n");
224 }
225
226 if (dec_arfcns_count != arfcns_num) {
227 printf("Wrong number of arfcns\n");
228 return 1;
229 }
230
231 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
232 printf("Decoding error, got wrong freqs\n");
233 fprintf(stderr, " w = ");
234 for (i = 0; i < ARRAY_SIZE(w); i++)
235 fprintf(stderr, "%d ", w[i]);
236 fprintf(stderr, "\n");
237 return 1;
238 }
239
240 return 0;
241}
242
243static void test_random_range_encoding(int range, int max_arfcn_num)
244{
245 int arfcns_num = 0;
246 int test_idx;
247 int rc, max_count;
248 int num_tests = 1024;
249
250 printf("Random range test: range %d, max num ARFCNs %d\n",
251 range, max_arfcn_num);
252
253 srandom(1);
254
255 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
256 for (test_idx = 0; test_idx < num_tests; test_idx++) {
257 int count;
258 int i;
259 int min_freq = 0;
260
261 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
262 char rnd_arfcns_set[1024] = {0};
263
264 if (range < ARFCN_RANGE_1024)
265 min_freq = random() % (1023 - range);
266
267 for (count = max_count; count; ) {
268 int arfcn = min_freq + random() % (range + 1);
269 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
270
271 if (!rnd_arfcns_set[arfcn]) {
272 rnd_arfcns_set[arfcn] = 1;
273 count -= 1;
274 }
275 }
276
277 arfcns_num = 0;
278 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
279 if (rnd_arfcns_set[i])
280 rnd_arfcns[arfcns_num++] = i;
281
282 rc = test_single_range_encoding(range, rnd_arfcns,
283 arfcns_num, 1);
284 if (rc != 0) {
285 printf("Failed on test %d, range %d, num ARFCNs %d\n",
286 test_idx, range, max_count);
287 test_single_range_encoding(range, rnd_arfcns,
288 arfcns_num, 0);
289 return;
290 }
291 }
292 }
293}
294
295static void test_range_encoding()
296{
297 int *arfcns;
298 int arfcns_num = 0;
299 int test_idx;
300 int range;
301
302 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
303 {
304 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
305 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
306 range = arfcn_test_ranges[test_idx].range;
307
308 printf("Range test %d: range %d, num ARFCNs %d\n",
309 test_idx, range, arfcns_num);
310
311 test_single_range_encoding(range, arfcns, arfcns_num, 0);
312 }
313
314 test_random_range_encoding(ARFCN_RANGE_128, 29);
315 test_random_range_encoding(ARFCN_RANGE_256, 22);
316 test_random_range_encoding(ARFCN_RANGE_512, 18);
317 test_random_range_encoding(ARFCN_RANGE_1024, 16);
318}
319
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100320static int freqs1[] = {
321 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
322};
323
324static int freqs2[] = {
325 402, 460, 1, 67, 131, 197, 272, 347,
326};
327
328static int freqs3[] = {
329 68, 128, 198, 279, 353, 398, 452,
330
331};
332
333static int w_out[] = {
334 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
335};
336
337static int range128[] = {
338 1, 1 + 127,
339};
340
341static int range256[] = {
342 1, 1 + 128,
343};
344
345static int range512[] = {
346 1, 1+ 511,
347};
348
349
350static void test_arfcn_filter()
351{
352 int arfcns[50], i, res, f0_included;
353 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
354 arfcns[i] = (i + 1) * 2;
355
356 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
357 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100358 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100359 arfcns[0], &f0_included);
360 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
361 VERIFY(f0_included, ==, 1);
362 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100363 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100364
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100365 /* check with range1024, ARFCN 0 is included */
366 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
367 arfcns[i] = i * 2;
368 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
369 0, &f0_included);
370 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
371 VERIFY(f0_included, ==, 1);
372 for (i = 0; i < res; ++i)
373 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
374
375 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100376 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
377 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100378 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
379 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100380 VERIFY(res, ==, ARRAY_SIZE(arfcns));
381 VERIFY(f0_included, ==, 0);
382 for (i = 0; i < res; ++i)
383 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
384}
385
386static void test_print_encoding()
387{
388 int rc;
389 int w[17];
390 uint8_t chan_list[16];
391 memset(chan_list, 0x23, sizeof(chan_list));
392
393 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
394 switch (rc % 3) {
395 case 0:
396 w[rc] = 0xAAAA;
397 break;
398 case 1:
399 w[rc] = 0x5555;
400 break;
401 case 2:
402 w[rc] = 0x9696;
403 break;
404 }
405
406 rc = range_enc_range512(chan_list, (1 << 9) | 0x96, w);
407 VERIFY(rc, ==, 0);
408
409 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
410}
411
412static void test_si_range_helpers()
413{
414 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
415 int i, f0 = 0xFFFFFF;
416
417 memset(&ws[0], 0x23, sizeof(ws));
418
419 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100420 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100421 VERIFY(i, ==, 2);
422
423 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100424 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100425 VERIFY(i, ==, 2);
426
427 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100428 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100429 VERIFY(i, ==, 0);
430
431 i = range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
432 VERIFY(i, ==, 0);
433
434 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
435 printf("w[%d]=%d\n", i, ws[i]);
436 VERIFY(ws[i], ==, w_out[i]);
437 }
438
439 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
440 VERIFY(i, ==, ARFCN_RANGE_128);
441 VERIFY(f0, ==, 1);
442
443 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
444 VERIFY(i, ==, ARFCN_RANGE_256);
445 VERIFY(f0, ==, 1);
446
447 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
448 VERIFY(i, ==, ARFCN_RANGE_512);
449 VERIFY(f0, ==, 1);
450}
451
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100452static void test_gsm411_rp_ref_wrap(void)
453{
454 struct gsm_subscriber_connection conn;
455 int res;
456
457 printf("testing RP-Reference wrap\n");
458
459 memset(&conn, 0, sizeof(conn));
460 conn.next_rp_ref = 255;
461
462 res = sms_next_rp_msg_ref(&conn);
463 printf("Allocated reference: %d\n", res);
464 OSMO_ASSERT(res == 255);
465
466 res = sms_next_rp_msg_ref(&conn);
467 printf("Allocated reference: %d\n", res);
468 OSMO_ASSERT(res == 0);
469
470 res = sms_next_rp_msg_ref(&conn);
471 printf("Allocated reference: %d\n", res);
472 OSMO_ASSERT(res == 1);
473}
474
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800475int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000476{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100477 osmo_init_logging(&log_info);
478 log_set_log_level(osmo_stderr_target, LOGL_INFO);
479
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200480 test_location_area_identifier();
481 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100482
483 test_si_range_helpers();
484 test_arfcn_filter();
485 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100486 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100487 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100488
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100489 printf("Done.\n");
490 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000491}