blob: 46e93b838f2e878fe994aedcaa6c95bd332e05a1 [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>
Holger Freytheraa0fb362008-12-28 21:55:40 +000030
31#define COMPARE(result, op, value) \
32 if (!((result) op (value))) {\
33 fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \
34 exit(-1); \
35 }
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020036
37#define COMPARE_STR(result, value) \
38 if (strcmp(result, value) != 0) { \
39 fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \
40 exit(-1); \
41 }
Holger Freytheraa0fb362008-12-28 21:55:40 +000042
43/*
44 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
45 */
46static void test_location_area_identifier(void)
47{
48 struct gsm48_loc_area_id lai48;
49
50 printf("Testing test location area identifier\n");
51
52 /*
53 * Test the default/test setup. Coming from
54 * bsc_hack.c dumps
55 */
Harald Welteafedeab2010-03-04 10:55:40 +010056 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytheraa0fb362008-12-28 21:55:40 +000057 COMPARE(lai48.digits[0], ==, 0x00);
58 COMPARE(lai48.digits[1], ==, 0xF1);
59 COMPARE(lai48.digits[2], ==, 0x10);
60 COMPARE(lai48.lac, ==, htons(0x0001));
61
Harald Welteafedeab2010-03-04 10:55:40 +010062 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytheraa0fb362008-12-28 21:55:40 +000063 COMPARE(lai48.digits[0], ==, 0x06);
64 COMPARE(lai48.digits[1], ==, 0xF2);
65 COMPARE(lai48.digits[2], ==, 0x10);
66 COMPARE(lai48.lac, ==, htons(0x000f));
67}
68
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020069static void test_mi_functionality(void)
70{
71 const char *imsi_odd = "987654321098763";
72 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020073 const uint32_t tmsi = 0xfabeacd0;
74 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020075 unsigned int mi_len;
76 char mi_parsed[GSM48_MI_SIZE];
77
78 printf("Testing parsing and generating TMSI/IMSI\n");
79
80 /* tmsi code */
81 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
82 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020084
85 /* imsi code */
86 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
87 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
88 printf("hex: %s\n", hexdump(mi, mi_len));
89 COMPARE_STR(mi_parsed, imsi_odd);
90
91 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
92 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
93 printf("hex: %s\n", hexdump(mi, mi_len));
94 COMPARE_STR(mi_parsed, imsi_even);
95}
96
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +080097int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +000098{
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020099 test_location_area_identifier();
100 test_mi_functionality();
Harald Welteafedeab2010-03-04 10:55:40 +0100101
102 exit(0);
Holger Freytheraa0fb362008-12-28 21:55:40 +0000103}