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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * 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 Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 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 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * 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 Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 25 | #include <arpa/inet.h> |
| 26 | |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 27 | #include <openbsc/gsm_04_08.h> |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 28 | #include <openbsc/gsm_subscriber.h> |
| 29 | #include <openbsc/debug.h> |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 30 | |
| 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 Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 36 | |
| 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 Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Test Location Area Identifier formatting. Table 10.5.3 of 04.08 |
| 45 | */ |
| 46 | static 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 Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 56 | gsm48_generate_lai(&lai48, 1, 1, 1); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 57 | 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 Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 62 | gsm48_generate_lai(&lai48, 602, 1, 15); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 63 | 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 Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 69 | static void test_mi_functionality(void) |
| 70 | { |
| 71 | const char *imsi_odd = "987654321098763"; |
| 72 | const char *imsi_even = "9876543210987654"; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 73 | const uint32_t tmsi = 0xfabeacd0; |
| 74 | uint8_t mi[128]; |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 75 | 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 Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 83 | COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi); |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 84 | |
| 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); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 88 | printf("hex: %s\n", osmo_hexdump(mi, mi_len)); |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 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); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 93 | printf("hex: %s\n", osmo_hexdump(mi, mi_len)); |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 94 | COMPARE_STR(mi_parsed, imsi_even); |
| 95 | } |
| 96 | |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 97 | int main(int argc, char **argv) |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 98 | { |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 99 | test_location_area_identifier(); |
| 100 | test_mi_functionality(); |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 101 | |
| 102 | exit(0); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 103 | } |