Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 1 | /* 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 |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (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 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <assert.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 26 | #include <arpa/inet.h> |
| 27 | |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 28 | #include <openbsc/gsm_04_08.h> |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
| 30 | #include <openbsc/debug.h> |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 31 | |
| 32 | #define COMPARE(result, op, value) \ |
| 33 | if (!((result) op (value))) {\ |
| 34 | fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \ |
| 35 | exit(-1); \ |
| 36 | } |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 37 | |
| 38 | #define COMPARE_STR(result, value) \ |
| 39 | if (strcmp(result, value) != 0) { \ |
| 40 | fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \ |
| 41 | exit(-1); \ |
| 42 | } |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Test Location Area Identifier formatting. Table 10.5.3 of 04.08 |
| 46 | */ |
| 47 | static void test_location_area_identifier(void) |
| 48 | { |
| 49 | struct gsm48_loc_area_id lai48; |
| 50 | |
| 51 | printf("Testing test location area identifier\n"); |
| 52 | |
| 53 | /* |
| 54 | * Test the default/test setup. Coming from |
| 55 | * bsc_hack.c dumps |
| 56 | */ |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 57 | gsm48_generate_lai(&lai48, 1, 1, 1); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 58 | COMPARE(lai48.digits[0], ==, 0x00); |
| 59 | COMPARE(lai48.digits[1], ==, 0xF1); |
| 60 | COMPARE(lai48.digits[2], ==, 0x10); |
| 61 | COMPARE(lai48.lac, ==, htons(0x0001)); |
| 62 | |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 63 | gsm48_generate_lai(&lai48, 602, 1, 15); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 64 | COMPARE(lai48.digits[0], ==, 0x06); |
| 65 | COMPARE(lai48.digits[1], ==, 0xF2); |
| 66 | COMPARE(lai48.digits[2], ==, 0x10); |
| 67 | COMPARE(lai48.lac, ==, htons(0x000f)); |
| 68 | } |
| 69 | |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 70 | static void test_mi_functionality(void) |
| 71 | { |
| 72 | const char *imsi_odd = "987654321098763"; |
| 73 | const char *imsi_even = "9876543210987654"; |
| 74 | const u_int32_t tmsi = 0xfabeacd0; |
| 75 | u_int8_t mi[128]; |
| 76 | unsigned int mi_len; |
| 77 | char mi_parsed[GSM48_MI_SIZE]; |
| 78 | |
| 79 | printf("Testing parsing and generating TMSI/IMSI\n"); |
| 80 | |
| 81 | /* tmsi code */ |
| 82 | mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi); |
| 83 | gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2); |
| 84 | COMPARE((u_int32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi); |
| 85 | |
| 86 | /* imsi code */ |
| 87 | mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd); |
| 88 | gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2); |
| 89 | printf("hex: %s\n", hexdump(mi, mi_len)); |
| 90 | COMPARE_STR(mi_parsed, imsi_odd); |
| 91 | |
| 92 | mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even); |
| 93 | gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2); |
| 94 | printf("hex: %s\n", hexdump(mi, mi_len)); |
| 95 | COMPARE_STR(mi_parsed, imsi_even); |
| 96 | } |
| 97 | |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 98 | int main(int argc, char** argv) |
| 99 | { |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 100 | test_location_area_identifier(); |
| 101 | test_mi_functionality(); |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 102 | |
| 103 | exit(0); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | /* |
| 109 | * Stubs to compile and link |
| 110 | */ |
Holger Freyther | 3281f6e | 2009-02-20 18:33:00 +0000 | [diff] [blame] | 111 | void input_event(void) {} |
| 112 | void nm_state_event(void) {} |