blob: 3a6bbddff3f79086fa3e9c8c33c88839e6b49324 [file] [log] [blame]
Holger Freytherec5b1d82008-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 Welte0e3e88e2011-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 Freytherec5b1d82008-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 Welte0e3e88e2011-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 Freytherec5b1d82008-12-28 21:55:40 +000018 *
19 */
20
21#include <assert.h>
22#include <stdio.h>
23#include <stdlib.h>
24
Harald Welted8493ac2010-03-04 10:55:40 +010025#include <arpa/inet.h>
26
Holger Freytherec5b1d82008-12-28 21:55:40 +000027#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +020028#include <openbsc/gsm_subscriber.h>
29#include <openbsc/debug.h>
Jacob Erlbeck1fe2ad22014-01-10 17:43:41 +010030#include <openbsc/arfcn_range_encode.h>
31#include <osmocom/core/application.h>
Holger Freytherec5b1d82008-12-28 21:55:40 +000032
33#define COMPARE(result, op, value) \
34 if (!((result) op (value))) {\
35 fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \
36 exit(-1); \
37 }
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +020038
39#define COMPARE_STR(result, value) \
40 if (strcmp(result, value) != 0) { \
41 fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \
42 exit(-1); \
43 }
Holger Freytherec5b1d82008-12-28 21:55:40 +000044
Jacob Erlbeck33948a82014-01-13 14:43:40 +010045#define DBG(...)
46
47#define VERIFY(res, cmp, wanted) \
48 if (!(res cmp wanted)) { \
49 printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \
50 __FILE__, __LINE__, res, # cmp, wanted); \
51 }
52
53
54
Holger Freytherec5b1d82008-12-28 21:55:40 +000055/*
56 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
57 */
58static void test_location_area_identifier(void)
59{
60 struct gsm48_loc_area_id lai48;
61
62 printf("Testing test location area identifier\n");
63
64 /*
65 * Test the default/test setup. Coming from
66 * bsc_hack.c dumps
67 */
Harald Welted8493ac2010-03-04 10:55:40 +010068 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytherec5b1d82008-12-28 21:55:40 +000069 COMPARE(lai48.digits[0], ==, 0x00);
70 COMPARE(lai48.digits[1], ==, 0xF1);
71 COMPARE(lai48.digits[2], ==, 0x10);
72 COMPARE(lai48.lac, ==, htons(0x0001));
73
Harald Welted8493ac2010-03-04 10:55:40 +010074 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytherec5b1d82008-12-28 21:55:40 +000075 COMPARE(lai48.digits[0], ==, 0x06);
76 COMPARE(lai48.digits[1], ==, 0xF2);
77 COMPARE(lai48.digits[2], ==, 0x10);
78 COMPARE(lai48.lac, ==, htons(0x000f));
79}
80
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +020081static void test_mi_functionality(void)
82{
83 const char *imsi_odd = "987654321098763";
84 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020085 const uint32_t tmsi = 0xfabeacd0;
86 uint8_t mi[128];
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +020087 unsigned int mi_len;
88 char mi_parsed[GSM48_MI_SIZE];
89
90 printf("Testing parsing and generating TMSI/IMSI\n");
91
92 /* tmsi code */
93 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
94 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020095 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +020096
97 /* imsi code */
98 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
99 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200100 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200101 COMPARE_STR(mi_parsed, imsi_odd);
102
103 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
104 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200105 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200106 COMPARE_STR(mi_parsed, imsi_even);
107}
108
Jacob Erlbeck1fe2ad22014-01-10 17:43:41 +0100109struct {
110 int range;
111 int arfcns_num;
112 int arfcns[RANGE_ENC_MAX_ARFCNS];
113} arfcn_test_ranges[] = {
114 {ARFCN_RANGE_512, 12,
115 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
116 {ARFCN_RANGE_512, 17,
117 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
118 {ARFCN_RANGE_512, 18,
119 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
120 {ARFCN_RANGE_512, 18,
121 { 1, 17, 31, 45, 58, 79, 81, 97,
122 113, 127, 213, 277, 287, 311, 331, 391,
123 417, 511 }},
124 {ARFCN_RANGE_512, 6,
125 { 1, 17, 31, 45, 58, 79 }},
126 {ARFCN_RANGE_512, 6,
127 { 10, 17, 31, 45, 58, 79 }},
128 {ARFCN_RANGE_1024, 17,
129 { 0, 17, 31, 45, 58, 79, 81, 97,
130 113, 127, 213, 277, 287, 311, 331, 391,
131 1023 }},
132 {ARFCN_RANGE_1024, 16,
133 { 17, 31, 45, 58, 79, 81, 97, 113,
134 127, 213, 277, 287, 311, 331, 391, 1023 }},
135 {-1}
136};
137
138static int test_single_range_encoding(int range, const int *orig_arfcns,
139 int arfcns_num, int silent)
140{
141 int arfcns[RANGE_ENC_MAX_ARFCNS];
142 int w[RANGE_ENC_MAX_ARFCNS];
143 int f0_included = 0;
144 int rc, f0;
145 uint8_t chan_list[16] = {0};
146 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
147 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
148 int dec_arfcns_count = 0;
149 int arfcns_used = 0;
150 int i;
151
152 arfcns_used = arfcns_num;
153 memmove(arfcns, orig_arfcns, sizeof(arfcns));
154
155 f0 = arfcns[0];
156 /*
157 * Manipulate the ARFCN list according to the rules in J4 depending
158 * on the selected range.
159 */
160 arfcns_used = range_enc_filter_arfcns(range, arfcns, arfcns_used,
161 f0, &f0_included);
162
163 memset(w, 0, sizeof(w));
164 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
165 if (rc != 0) {
166 printf("Cannot compute range W(k), rc = %d\n", rc);
167 return 1;
168 }
169
170 if (!silent)
171 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
172 range, arfcns_used, f0, f0_included);
173
174 /* Select the range and the amount of bits needed */
175 switch (range) {
176 case ARFCN_RANGE_128:
177 rc = range_enc_range128(chan_list, f0, w);
178 break;
179 case ARFCN_RANGE_256:
180 rc = range_enc_range256(chan_list, f0, w);
181 break;
182 case ARFCN_RANGE_512:
183 rc = range_enc_range512(chan_list, f0, w);
184 break;
185 case ARFCN_RANGE_1024:
186 rc = range_enc_range1024(chan_list, f0, f0_included, w);
187 break;
188 default:
189 return 1;
190 };
191 if (rc != 0) {
192 printf("Cannot encode range, rc = %d\n", rc);
193 return 1;
194 }
195
196 if (!silent)
197 printf("chan_list = %s\n",
198 osmo_hexdump(chan_list, sizeof(chan_list)));
199
200 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
201 0xfe, 1);
202 if (rc != 0) {
203 printf("Cannot decode freq list, rc = %d\n", rc);
204 return 1;
205 }
206
207 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
208 if (dec_freq[i].mask &&
209 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
210 dec_arfcns[dec_arfcns_count++] = i;
211 }
212
213 if (!silent) {
214 printf("Decoded freqs %d (expected %d)\n",
215 dec_arfcns_count, arfcns_num);
216 printf("Decoded: ");
217 for (i = 0; i < dec_arfcns_count; i++) {
218 printf("%d ", dec_arfcns[i]);
219 if (dec_arfcns[i] != orig_arfcns[i])
220 printf("(!= %d) ", orig_arfcns[i]);
221 }
222 printf("\n");
223 }
224
225 if (dec_arfcns_count != arfcns_num) {
226 printf("Wrong number of arfcns\n");
227 return 1;
228 }
229
230 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
231 printf("Decoding error, got wrong freqs\n");
232 fprintf(stderr, " w = ");
233 for (i = 0; i < ARRAY_SIZE(w); i++)
234 fprintf(stderr, "%d ", w[i]);
235 fprintf(stderr, "\n");
236 return 1;
237 }
238
239 return 0;
240}
241
242static void test_random_range_encoding(int range, int max_arfcn_num)
243{
244 int arfcns_num = 0;
245 int test_idx;
246 int rc, max_count;
247 int num_tests = 1024;
248
249 printf("Random range test: range %d, max num ARFCNs %d\n",
250 range, max_arfcn_num);
251
252 srandom(1);
253
254 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
255 for (test_idx = 0; test_idx < num_tests; test_idx++) {
256 int count;
257 int i;
258 int min_freq = 0;
259
260 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
261 char rnd_arfcns_set[1024] = {0};
262
263 if (range < ARFCN_RANGE_1024)
264 min_freq = random() % (1023 - range);
265
266 for (count = max_count; count; ) {
267 int arfcn = min_freq + random() % (range + 1);
268 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
269
270 if (!rnd_arfcns_set[arfcn]) {
271 rnd_arfcns_set[arfcn] = 1;
272 count -= 1;
273 }
274 }
275
276 arfcns_num = 0;
277 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
278 if (rnd_arfcns_set[i])
279 rnd_arfcns[arfcns_num++] = i;
280
281 rc = test_single_range_encoding(range, rnd_arfcns,
282 arfcns_num, 1);
283 if (rc != 0) {
284 printf("Failed on test %d, range %d, num ARFCNs %d\n",
285 test_idx, range, max_count);
286 test_single_range_encoding(range, rnd_arfcns,
287 arfcns_num, 0);
288 return;
289 }
290 }
291 }
292}
293
294static void test_range_encoding()
295{
296 int *arfcns;
297 int arfcns_num = 0;
298 int test_idx;
299 int range;
300
301 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
302 {
303 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
304 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
305 range = arfcn_test_ranges[test_idx].range;
306
307 printf("Range test %d: range %d, num ARFCNs %d\n",
308 test_idx, range, arfcns_num);
309
310 test_single_range_encoding(range, arfcns, arfcns_num, 0);
311 }
312
313 test_random_range_encoding(ARFCN_RANGE_128, 29);
314 test_random_range_encoding(ARFCN_RANGE_256, 22);
315 test_random_range_encoding(ARFCN_RANGE_512, 18);
316 test_random_range_encoding(ARFCN_RANGE_1024, 16);
317}
318
Jacob Erlbeck33948a82014-01-13 14:43:40 +0100319static int freqs1[] = {
320 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
321};
322
323static int freqs2[] = {
324 402, 460, 1, 67, 131, 197, 272, 347,
325};
326
327static int freqs3[] = {
328 68, 128, 198, 279, 353, 398, 452,
329
330};
331
332static int w_out[] = {
333 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
334};
335
336static int range128[] = {
337 1, 1 + 127,
338};
339
340static int range256[] = {
341 1, 1 + 128,
342};
343
344static int range512[] = {
345 1, 1+ 511,
346};
347
348
349static void test_arfcn_filter()
350{
351 int arfcns[50], i, res, f0_included;
352 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
353 arfcns[i] = (i + 1) * 2;
354
355 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
356 f0_included = 24;
357 res = range_enc_filter_arfcns(ARFCN_RANGE_512, arfcns, ARRAY_SIZE(arfcns),
358 arfcns[0], &f0_included);
359 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
360 VERIFY(f0_included, ==, 0);
361 for (i = 0; i < res; ++i)
362 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
363
364 /* check with range1024 */
365 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
366 arfcns[i] = (i + 1) * 2;
367 res = range_enc_filter_arfcns(ARFCN_RANGE_1024, arfcns, ARRAY_SIZE(arfcns),
368 arfcns[0], &f0_included);
369 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
370 VERIFY(f0_included, ==, 1);
371 for (i = 0; i < res; ++i)
372 VERIFY(arfcns[i], ==, ((i + 2) * 2) - 1);
373
374 /* check with range1024, not included */
375 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
376 arfcns[i] = (i + 1) * 2;
377 res = range_enc_filter_arfcns(ARFCN_RANGE_1024, arfcns, ARRAY_SIZE(arfcns),
378 11, &f0_included);
379 VERIFY(res, ==, ARRAY_SIZE(arfcns));
380 VERIFY(f0_included, ==, 0);
381 for (i = 0; i < res; ++i)
382 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
383}
384
385static void test_print_encoding()
386{
387 int rc;
388 int w[17];
389 uint8_t chan_list[16];
390 memset(chan_list, 0x23, sizeof(chan_list));
391
392 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
393 switch (rc % 3) {
394 case 0:
395 w[rc] = 0xAAAA;
396 break;
397 case 1:
398 w[rc] = 0x5555;
399 break;
400 case 2:
401 w[rc] = 0x9696;
402 break;
403 }
404
405 rc = range_enc_range512(chan_list, (1 << 9) | 0x96, w);
406 VERIFY(rc, ==, 0);
407
408 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
409}
410
411static void test_si_range_helpers()
412{
413 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
414 int i, f0 = 0xFFFFFF;
415
416 memset(&ws[0], 0x23, sizeof(ws));
417
418 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
419 printf("Element is: %d => freqs[i] = %d\n", i, freqs1[i]);
420 VERIFY(i, ==, 2);
421
422 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
423 printf("Element is: %d => freqs[i] = %d\n", i, freqs2[i]);
424 VERIFY(i, ==, 2);
425
426 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
427 printf("Element is: %d => freqs[i] = %d\n", i, freqs3[i]);
428 VERIFY(i, ==, 0);
429
430 i = range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
431 VERIFY(i, ==, 0);
432
433 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
434 printf("w[%d]=%d\n", i, ws[i]);
435 VERIFY(ws[i], ==, w_out[i]);
436 }
437
438 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
439 VERIFY(i, ==, ARFCN_RANGE_128);
440 VERIFY(f0, ==, 1);
441
442 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
443 VERIFY(i, ==, ARFCN_RANGE_256);
444 VERIFY(f0, ==, 1);
445
446 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
447 VERIFY(i, ==, ARFCN_RANGE_512);
448 VERIFY(f0, ==, 1);
449}
450
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800451int main(int argc, char **argv)
Holger Freytherec5b1d82008-12-28 21:55:40 +0000452{
Jacob Erlbeck1fe2ad22014-01-10 17:43:41 +0100453 osmo_init_logging(&log_info);
454 log_set_log_level(osmo_stderr_target, LOGL_INFO);
455
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200456 test_location_area_identifier();
457 test_mi_functionality();
Jacob Erlbeck33948a82014-01-13 14:43:40 +0100458
459 test_si_range_helpers();
460 test_arfcn_filter();
461 test_print_encoding();
Jacob Erlbeck1fe2ad22014-01-10 17:43:41 +0100462 test_range_encoding();
Harald Welted8493ac2010-03-04 10:55:40 +0100463
Holger Hans Peter Freytherd01a4cc2012-01-06 15:03:28 +0100464 printf("Done.\n");
465 return EXIT_SUCCESS;
Holger Freytherec5b1d82008-12-28 21:55:40 +0000466}