Jacob Erlbeck | cc0d884 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * BSC Message filtering |
| 3 | * |
| 4 | * (C) 2013 by sysmocom s.f.m.c. GmbH |
| 5 | * Written by Jacob Erlbeck <jerlbeck@sysmocom.de> |
| 6 | * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 7 | * (C) 2010-2013 by On-Waves |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Affero General Public License as published by |
| 13 | * the Free Software Foundation; either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Affero General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <openbsc/debug.h> |
| 28 | #include <openbsc/gsm_data.h> |
| 29 | |
| 30 | #include <openbsc/osmo_bsc.h> |
| 31 | #include <openbsc/osmo_msc_data.h> |
| 32 | #include <openbsc/gsm_04_80.h> |
| 33 | #include <openbsc/gsm_subscriber.h> |
| 34 | |
| 35 | #include <osmocom/core/application.h> |
| 36 | #include <osmocom/core/backtrace.h> |
| 37 | #include <osmocom/core/talloc.h> |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <search.h> |
| 41 | |
| 42 | enum test { |
| 43 | TEST_SCAN_TO_BTS, |
| 44 | TEST_SCAN_TO_MSC, |
| 45 | }; |
| 46 | |
| 47 | /* GSM 04.08 MM INFORMATION test message */ |
| 48 | static uint8_t gsm48_mm_info_nn_tzt[] = { |
| 49 | 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb, |
| 50 | 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61, |
| 51 | 0x11, 0x02, 0x73, 0x00, |
| 52 | }; |
| 53 | |
| 54 | static uint8_t gsm48_mm_info_nn_tzt_out[] = { |
| 55 | 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb, |
| 56 | 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61, |
| 57 | 0x11, 0x02, 0x73, 0x1a, |
| 58 | }; |
| 59 | |
| 60 | static uint8_t gsm48_mm_info_nn_tzt_dst[] = { |
| 61 | 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb, |
| 62 | 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61, |
| 63 | 0x11, 0x02, 0x73, 0x00, 0x49, 0x01, 0x00, |
| 64 | }; |
| 65 | |
| 66 | static uint8_t gsm48_mm_info_nn_tzt_dst_out[] = { |
| 67 | 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb, |
| 68 | 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61, |
| 69 | 0x11, 0x02, 0x73, 0x1a, 0x49, 0x01, 0x02, |
| 70 | }; |
| 71 | |
| 72 | struct test_definition { |
| 73 | const uint8_t *data; |
| 74 | const uint16_t length; |
| 75 | const int dir; |
| 76 | const int result; |
| 77 | const uint8_t *out_data; |
| 78 | const uint16_t out_length; |
| 79 | const char* params; |
| 80 | const int n_params; |
| 81 | }; |
| 82 | |
| 83 | static int get_int(const char *params, size_t nmemb, const char *key, int def, int *is_set) |
| 84 | { |
| 85 | const char *kv = NULL; |
| 86 | |
| 87 | kv = strstr(params, key); |
| 88 | if (kv) { |
| 89 | kv += strlen(key) + 1; |
| 90 | fprintf(stderr, "get_int(%s) -> %d\n", key, atoi(kv)); |
| 91 | if (is_set) |
| 92 | *is_set = 1; |
| 93 | } |
| 94 | |
| 95 | return kv ? atoi(kv) : def; |
| 96 | } |
| 97 | |
| 98 | static const struct test_definition test_scan_defs[] = { |
| 99 | { |
| 100 | .data = gsm48_mm_info_nn_tzt_dst, |
| 101 | .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt), |
| 102 | .dir = TEST_SCAN_TO_BTS, |
| 103 | .result = 0, |
| 104 | .out_data = gsm48_mm_info_nn_tzt_dst_out, |
| 105 | .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_out), |
| 106 | .params = "tz_hr=-5 tz_mn=15 tz_dst=2", |
| 107 | .n_params = 3, |
| 108 | }, |
| 109 | { |
| 110 | .data = gsm48_mm_info_nn_tzt_dst, |
| 111 | .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst), |
| 112 | .dir = TEST_SCAN_TO_BTS, |
| 113 | .result = 0, |
| 114 | .out_data = gsm48_mm_info_nn_tzt_dst_out, |
| 115 | .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst_out), |
| 116 | .params = "tz_hr=-5 tz_mn=15 tz_dst=2", |
| 117 | .n_params = 3, |
| 118 | }, |
| 119 | }; |
| 120 | |
| 121 | static void test_scan(void) |
| 122 | { |
| 123 | int i; |
| 124 | |
| 125 | struct gsm_network *net; |
| 126 | struct gsm_bts *bts; |
| 127 | struct osmo_bsc_sccp_con *sccp_con; |
| 128 | struct osmo_msc_data *msc; |
| 129 | struct gsm_subscriber_connection *conn; |
| 130 | |
| 131 | net = talloc_zero(NULL, struct gsm_network); |
| 132 | bts = talloc_zero(net, struct gsm_bts); |
| 133 | sccp_con = talloc_zero(net, struct osmo_bsc_sccp_con); |
| 134 | msc = talloc_zero(net, struct osmo_msc_data); |
| 135 | conn = talloc_zero(net, struct gsm_subscriber_connection); |
| 136 | |
| 137 | bts->network = net; |
| 138 | sccp_con->msc = msc; |
| 139 | conn->bts = bts; |
| 140 | conn->sccp_con = sccp_con; |
| 141 | |
| 142 | /* start testinh with proper messages */ |
| 143 | printf("Testing BTS<->MSC message scan.\n"); |
| 144 | for (i = 0; i < ARRAY_SIZE(test_scan_defs); ++i) { |
| 145 | const struct test_definition *test_def = &test_scan_defs[i]; |
| 146 | int result; |
| 147 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 148 | int is_set = 0; |
| 149 | |
| 150 | bts->tz.hr = get_int(test_def->params, test_def->n_params, "tz_hr", 0, &is_set); |
| 151 | bts->tz.mn = get_int(test_def->params, test_def->n_params, "tz_mn", 0, &is_set); |
| 152 | bts->tz.dst = get_int(test_def->params, test_def->n_params, "tz_dst", 0, &is_set); |
| 153 | bts->tz.override = get_int(test_def->params, test_def->n_params, "tz_dst", is_set ? 1 : 0, NULL); |
| 154 | |
| 155 | printf("Going to test item: %d\n", i); |
| 156 | msg->l3h = msgb_put(msg, test_def->length); |
| 157 | memcpy(msg->l3h, test_def->data, test_def->length); |
| 158 | |
| 159 | switch (test_def->dir) { |
| 160 | case TEST_SCAN_TO_BTS: |
| 161 | result = bsc_scan_msc_msg(conn, msg); |
| 162 | break; |
| 163 | case TEST_SCAN_TO_MSC: |
| 164 | result = bsc_scan_msc_msg(conn, msg); |
| 165 | break; |
| 166 | default: |
| 167 | abort(); |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | if (result != test_def->result) { |
| 172 | printf("FAIL: Not the expected result, got: %d wanted: %d\n", |
| 173 | result, test_def->result); |
| 174 | goto out; |
| 175 | } |
| 176 | |
| 177 | if (msgb_l3len(msg) != test_def->out_length) { |
| 178 | printf("FAIL: Not the expected message size, got: %d wanted: %d\n", |
| 179 | msgb_l3len(msg), test_def->out_length); |
| 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | if (memcmp(msgb_l3(msg), test_def->out_data, test_def->out_length) != 0) { |
| 184 | printf("FAIL: Not the expected message\n"); |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | out: |
| 189 | msgb_free(msg); |
| 190 | } |
| 191 | |
| 192 | talloc_free(net); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | int main(int argc, char **argv) |
| 197 | { |
| 198 | osmo_init_logging(&log_info); |
| 199 | |
| 200 | test_scan(); |
| 201 | |
| 202 | printf("Testing execution completed.\n"); |
| 203 | return 0; |
| 204 | } |