blob: 721b283bb3dd83dc825dc7de55a37d6b40bb606f [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 Freyther5d0e56f2009-08-20 08:41:24 +020028#include <openbsc/gsm_subscriber.h>
29#include <openbsc/debug.h>
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010030#include <openbsc/arfcn_range_encode.h>
31#include <osmocom/core/application.h>
Holger Freytheraa0fb362008-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 Freyther5d0e56f2009-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 Freytheraa0fb362008-12-28 21:55:40 +000044
45/*
46 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
47 */
48static void test_location_area_identifier(void)
49{
50 struct gsm48_loc_area_id lai48;
51
52 printf("Testing test location area identifier\n");
53
54 /*
55 * Test the default/test setup. Coming from
56 * bsc_hack.c dumps
57 */
Harald Welteafedeab2010-03-04 10:55:40 +010058 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytheraa0fb362008-12-28 21:55:40 +000059 COMPARE(lai48.digits[0], ==, 0x00);
60 COMPARE(lai48.digits[1], ==, 0xF1);
61 COMPARE(lai48.digits[2], ==, 0x10);
62 COMPARE(lai48.lac, ==, htons(0x0001));
63
Harald Welteafedeab2010-03-04 10:55:40 +010064 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytheraa0fb362008-12-28 21:55:40 +000065 COMPARE(lai48.digits[0], ==, 0x06);
66 COMPARE(lai48.digits[1], ==, 0xF2);
67 COMPARE(lai48.digits[2], ==, 0x10);
68 COMPARE(lai48.lac, ==, htons(0x000f));
69}
70
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020071static void test_mi_functionality(void)
72{
73 const char *imsi_odd = "987654321098763";
74 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020075 const uint32_t tmsi = 0xfabeacd0;
76 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020077 unsigned int mi_len;
78 char mi_parsed[GSM48_MI_SIZE];
79
80 printf("Testing parsing and generating TMSI/IMSI\n");
81
82 /* tmsi code */
83 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
84 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020086
87 /* imsi code */
88 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
89 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +020090 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020091 COMPARE_STR(mi_parsed, imsi_odd);
92
93 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
94 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +020095 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020096 COMPARE_STR(mi_parsed, imsi_even);
97}
98
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010099struct {
100 int range;
101 int arfcns_num;
102 int arfcns[RANGE_ENC_MAX_ARFCNS];
103} arfcn_test_ranges[] = {
104 {ARFCN_RANGE_512, 12,
105 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
106 {ARFCN_RANGE_512, 17,
107 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
108 {ARFCN_RANGE_512, 18,
109 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
110 {ARFCN_RANGE_512, 18,
111 { 1, 17, 31, 45, 58, 79, 81, 97,
112 113, 127, 213, 277, 287, 311, 331, 391,
113 417, 511 }},
114 {ARFCN_RANGE_512, 6,
115 { 1, 17, 31, 45, 58, 79 }},
116 {ARFCN_RANGE_512, 6,
117 { 10, 17, 31, 45, 58, 79 }},
118 {ARFCN_RANGE_1024, 17,
119 { 0, 17, 31, 45, 58, 79, 81, 97,
120 113, 127, 213, 277, 287, 311, 331, 391,
121 1023 }},
122 {ARFCN_RANGE_1024, 16,
123 { 17, 31, 45, 58, 79, 81, 97, 113,
124 127, 213, 277, 287, 311, 331, 391, 1023 }},
125 {-1}
126};
127
128static int test_single_range_encoding(int range, const int *orig_arfcns,
129 int arfcns_num, int silent)
130{
131 int arfcns[RANGE_ENC_MAX_ARFCNS];
132 int w[RANGE_ENC_MAX_ARFCNS];
133 int f0_included = 0;
134 int rc, f0;
135 uint8_t chan_list[16] = {0};
136 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
137 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
138 int dec_arfcns_count = 0;
139 int arfcns_used = 0;
140 int i;
141
142 arfcns_used = arfcns_num;
143 memmove(arfcns, orig_arfcns, sizeof(arfcns));
144
145 f0 = arfcns[0];
146 /*
147 * Manipulate the ARFCN list according to the rules in J4 depending
148 * on the selected range.
149 */
150 arfcns_used = range_enc_filter_arfcns(range, arfcns, arfcns_used,
151 f0, &f0_included);
152
153 memset(w, 0, sizeof(w));
154 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
155 if (rc != 0) {
156 printf("Cannot compute range W(k), rc = %d\n", rc);
157 return 1;
158 }
159
160 if (!silent)
161 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
162 range, arfcns_used, f0, f0_included);
163
164 /* Select the range and the amount of bits needed */
165 switch (range) {
166 case ARFCN_RANGE_128:
167 rc = range_enc_range128(chan_list, f0, w);
168 break;
169 case ARFCN_RANGE_256:
170 rc = range_enc_range256(chan_list, f0, w);
171 break;
172 case ARFCN_RANGE_512:
173 rc = range_enc_range512(chan_list, f0, w);
174 break;
175 case ARFCN_RANGE_1024:
176 rc = range_enc_range1024(chan_list, f0, f0_included, w);
177 break;
178 default:
179 return 1;
180 };
181 if (rc != 0) {
182 printf("Cannot encode range, rc = %d\n", rc);
183 return 1;
184 }
185
186 if (!silent)
187 printf("chan_list = %s\n",
188 osmo_hexdump(chan_list, sizeof(chan_list)));
189
190 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
191 0xfe, 1);
192 if (rc != 0) {
193 printf("Cannot decode freq list, rc = %d\n", rc);
194 return 1;
195 }
196
197 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
198 if (dec_freq[i].mask &&
199 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
200 dec_arfcns[dec_arfcns_count++] = i;
201 }
202
203 if (!silent) {
204 printf("Decoded freqs %d (expected %d)\n",
205 dec_arfcns_count, arfcns_num);
206 printf("Decoded: ");
207 for (i = 0; i < dec_arfcns_count; i++) {
208 printf("%d ", dec_arfcns[i]);
209 if (dec_arfcns[i] != orig_arfcns[i])
210 printf("(!= %d) ", orig_arfcns[i]);
211 }
212 printf("\n");
213 }
214
215 if (dec_arfcns_count != arfcns_num) {
216 printf("Wrong number of arfcns\n");
217 return 1;
218 }
219
220 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
221 printf("Decoding error, got wrong freqs\n");
222 fprintf(stderr, " w = ");
223 for (i = 0; i < ARRAY_SIZE(w); i++)
224 fprintf(stderr, "%d ", w[i]);
225 fprintf(stderr, "\n");
226 return 1;
227 }
228
229 return 0;
230}
231
232static void test_random_range_encoding(int range, int max_arfcn_num)
233{
234 int arfcns_num = 0;
235 int test_idx;
236 int rc, max_count;
237 int num_tests = 1024;
238
239 printf("Random range test: range %d, max num ARFCNs %d\n",
240 range, max_arfcn_num);
241
242 srandom(1);
243
244 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
245 for (test_idx = 0; test_idx < num_tests; test_idx++) {
246 int count;
247 int i;
248 int min_freq = 0;
249
250 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
251 char rnd_arfcns_set[1024] = {0};
252
253 if (range < ARFCN_RANGE_1024)
254 min_freq = random() % (1023 - range);
255
256 for (count = max_count; count; ) {
257 int arfcn = min_freq + random() % (range + 1);
258 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
259
260 if (!rnd_arfcns_set[arfcn]) {
261 rnd_arfcns_set[arfcn] = 1;
262 count -= 1;
263 }
264 }
265
266 arfcns_num = 0;
267 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
268 if (rnd_arfcns_set[i])
269 rnd_arfcns[arfcns_num++] = i;
270
271 rc = test_single_range_encoding(range, rnd_arfcns,
272 arfcns_num, 1);
273 if (rc != 0) {
274 printf("Failed on test %d, range %d, num ARFCNs %d\n",
275 test_idx, range, max_count);
276 test_single_range_encoding(range, rnd_arfcns,
277 arfcns_num, 0);
278 return;
279 }
280 }
281 }
282}
283
284static void test_range_encoding()
285{
286 int *arfcns;
287 int arfcns_num = 0;
288 int test_idx;
289 int range;
290
291 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
292 {
293 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
294 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
295 range = arfcn_test_ranges[test_idx].range;
296
297 printf("Range test %d: range %d, num ARFCNs %d\n",
298 test_idx, range, arfcns_num);
299
300 test_single_range_encoding(range, arfcns, arfcns_num, 0);
301 }
302
303 test_random_range_encoding(ARFCN_RANGE_128, 29);
304 test_random_range_encoding(ARFCN_RANGE_256, 22);
305 test_random_range_encoding(ARFCN_RANGE_512, 18);
306 test_random_range_encoding(ARFCN_RANGE_1024, 16);
307}
308
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800309int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000310{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100311 osmo_init_logging(&log_info);
312 log_set_log_level(osmo_stderr_target, LOGL_INFO);
313
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200314 test_location_area_identifier();
315 test_mi_functionality();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100316 test_range_encoding();
Harald Welteafedeab2010-03-04 10:55:40 +0100317
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100318 printf("Done.\n");
319 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000320}