Neels Hofmeyr | 3dc2c64 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1 | /* Osmocom MSC+VLR end-to-end tests */ |
| 2 | |
| 3 | /* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <getopt.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include <osmocom/core/logging.h> |
| 28 | #include <osmocom/core/utils.h> |
| 29 | #include <osmocom/core/msgb.h> |
| 30 | #include <osmocom/core/application.h> |
| 31 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
| 32 | #include <osmocom/gsm/gsup.h> |
| 33 | #include <openbsc/gsup_client.h> |
| 34 | #include <openbsc/gsm_04_11.h> |
| 35 | #include <openbsc/bsc_subscriber.h> |
| 36 | #include <openbsc/debug.h> |
| 37 | |
| 38 | #include "msc_vlr_tests.h" |
| 39 | |
| 40 | bool _log_lines = false; |
| 41 | |
| 42 | struct gsm_network *net = NULL; |
| 43 | |
| 44 | struct gsm_bts *the_bts; |
| 45 | |
| 46 | const char *gsup_tx_expected = NULL; |
| 47 | bool gsup_tx_confirmed; |
| 48 | |
| 49 | struct msgb *dtap_tx_expected = NULL; |
| 50 | bool dtap_tx_confirmed; |
| 51 | |
| 52 | enum result_sent lu_result_sent; |
| 53 | enum result_sent cm_service_result_sent; |
| 54 | bool auth_request_sent; |
| 55 | const char *auth_request_expect_rand; |
| 56 | const char *auth_request_expect_autn; |
| 57 | bool cipher_mode_cmd_sent; |
| 58 | bool cipher_mode_cmd_sent_with_imeisv; |
| 59 | |
| 60 | struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex) |
| 61 | { |
| 62 | struct msgb *msg = msgb_alloc(size, label); |
| 63 | unsigned char *rc; |
| 64 | msg->l2h = msg->head; |
| 65 | rc = msgb_put(msg, osmo_hexparse(hex, msg->head, msgb_tailroom(msg))); |
| 66 | OSMO_ASSERT(rc == msg->l2h); |
| 67 | return msg; |
| 68 | } |
| 69 | |
| 70 | void dtap_expect_tx(const char *hex) |
| 71 | { |
| 72 | OSMO_ASSERT(!dtap_tx_expected); |
| 73 | if (!hex) |
| 74 | return; |
| 75 | dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex); |
| 76 | dtap_tx_confirmed = false; |
| 77 | } |
| 78 | |
| 79 | void dtap_expect_tx_ussd(char *ussd_text) |
| 80 | { |
| 81 | uint8_t ussd_enc[128]; |
| 82 | int len; |
| 83 | /* header */ |
| 84 | char ussd_msg_hex[128] = "8b2a1c27a225020100302002013b301b04010f0416"; |
| 85 | |
| 86 | log("expecting USSD:\n %s", ussd_text); |
| 87 | /* append encoded USSD text */ |
| 88 | gsm_7bit_encode_n_ussd(ussd_enc, sizeof(ussd_enc), ussd_text, |
| 89 | &len); |
| 90 | strncat(ussd_msg_hex, osmo_hexdump_nospc(ussd_enc, len), |
| 91 | sizeof(ussd_msg_hex) - strlen(ussd_msg_hex)); |
| 92 | dtap_expect_tx(ussd_msg_hex); |
| 93 | } |
| 94 | |
| 95 | int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg); |
| 96 | |
| 97 | void gsup_rx(const char *rx_hex, const char *expect_tx_hex) |
| 98 | { |
| 99 | int rc; |
| 100 | struct msgb *msg; |
| 101 | const char *label; |
| 102 | |
| 103 | gsup_expect_tx(expect_tx_hex); |
| 104 | |
| 105 | msg = msgb_from_hex("gsup", 1024, rx_hex); |
| 106 | label = osmo_gsup_message_type_name(msg->l2h[0]); |
| 107 | fprintf(stderr, "<-- GSUP rx %s: %s\n", label, |
| 108 | osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg))); |
| 109 | rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg); |
| 110 | fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n", |
| 111 | label, rc); |
| 112 | if (expect_tx_hex) |
| 113 | OSMO_ASSERT(gsup_tx_confirmed); |
| 114 | talloc_free(msg); |
| 115 | } |
| 116 | |
| 117 | bool conn_exists(struct gsm_subscriber_connection *conn) |
| 118 | { |
| 119 | struct gsm_subscriber_connection *c; |
| 120 | llist_for_each_entry(c, &net->subscr_conns, entry) { |
| 121 | if (c == conn) |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | enum ran_type rx_from_ran = RAN_GERAN_A; |
| 128 | |
| 129 | struct gsm_subscriber_connection *conn_new(void) |
| 130 | { |
| 131 | struct gsm_subscriber_connection *conn; |
| 132 | conn = msc_subscr_con_allocate(net); |
| 133 | conn->bts = the_bts; |
| 134 | conn->via_ran = rx_from_ran; |
| 135 | return conn; |
| 136 | } |
| 137 | |
| 138 | struct gsm_subscriber_connection *g_conn = NULL; |
| 139 | |
| 140 | void rx_from_ms(struct msgb *msg) |
| 141 | { |
| 142 | int rc; |
| 143 | |
| 144 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 145 | log("rx from MS: pdisc=0x%02x msg_type=0x%02x", |
| 146 | gh->proto_discr, gh->msg_type); |
| 147 | |
| 148 | if (g_conn && !conn_exists(g_conn)) |
| 149 | g_conn = NULL; |
| 150 | |
| 151 | if (!g_conn) { |
| 152 | log("new conn"); |
| 153 | g_conn = conn_new(); |
| 154 | rc = net->bsc_api->compl_l3(g_conn, msg, 23); |
| 155 | if (rc == BSC_API_CONN_POL_REJECT) { |
| 156 | msc_subscr_con_free(g_conn); |
| 157 | g_conn = NULL; |
| 158 | } |
| 159 | } else { |
| 160 | if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR) |
| 161 | && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL)) |
| 162 | net->bsc_api->cipher_mode_compl(g_conn, msg, 0); |
| 163 | else |
| 164 | net->bsc_api->dtap(g_conn, 23, msg); |
| 165 | } |
| 166 | |
| 167 | if (g_conn && !conn_exists(g_conn)) |
| 168 | g_conn = NULL; |
| 169 | } |
| 170 | |
| 171 | void ms_sends_msg(const char *hex) |
| 172 | { |
| 173 | struct msgb *msg; |
| 174 | |
| 175 | msg = msgb_from_hex("ms_sends_msg", 1024, hex); |
| 176 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 177 | rx_from_ms(msg); |
| 178 | talloc_free(msg); |
| 179 | } |
| 180 | |
| 181 | int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type) |
| 182 | { |
| 183 | int rc; |
| 184 | struct msgb *msg; |
| 185 | struct gsm48_hdr *gh; |
| 186 | |
| 187 | msg = msgb_alloc(1024, "ms_sends_msg_fake"); |
| 188 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 189 | |
| 190 | gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh)); |
| 191 | gh->proto_discr = pdisc; |
| 192 | gh->msg_type = msg_type; |
| 193 | /* some amount of data, whatever */ |
| 194 | msgb_put(msg, 123); |
| 195 | |
| 196 | rc = gsm0408_dispatch(g_conn, msg); |
| 197 | |
| 198 | talloc_free(msg); |
| 199 | return rc; |
| 200 | } |
| 201 | |
| 202 | void thwart_rx_non_initial_requests() |
| 203 | { |
| 204 | log("requests shall be thwarted"); |
| 205 | OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_CC, GSM48_MT_CC_SETUP) == -EACCES); |
| 206 | OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_MM, 0x33 /* nonexistent */) == -EACCES); |
| 207 | OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1) == -EACCES); |
| 208 | OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_SMS, GSM411_MT_CP_DATA) == -EACCES); |
| 209 | } |
| 210 | |
| 211 | void send_sms(struct vlr_subscr *receiver, |
| 212 | struct vlr_subscr *sender, |
| 213 | char *str) |
| 214 | { |
| 215 | struct gsm_sms *sms = sms_from_text(receiver, sender, 0, str); |
| 216 | gsm411_send_sms_subscr(receiver, sms); |
| 217 | } |
| 218 | |
| 219 | unsigned char next_rand_byte = 0; |
| 220 | /* override, requires '-Wl,--wrap=RAND_bytes' */ |
| 221 | int __real_RAND_bytes(unsigned char *buf, int num); |
| 222 | int __wrap_RAND_bytes(unsigned char *buf, int num) |
| 223 | { |
| 224 | int i; |
| 225 | for (i = 0; i < num; i++) |
| 226 | buf[i] = next_rand_byte++; |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | /* override, requires '-Wl,--wrap=gsm340_gen_scts' */ |
| 231 | void __real_gsm340_gen_scts(uint8_t *scts, time_t time); |
| 232 | void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time) |
| 233 | { |
| 234 | /* Remove the time to encode for deterministic test results */ |
| 235 | __real_gsm340_gen_scts(scts, 0); |
| 236 | } |
| 237 | |
| 238 | const char *paging_expecting_imsi = NULL; |
| 239 | uint32_t paging_expecting_tmsi; |
| 240 | bool paging_sent; |
| 241 | bool paging_stopped; |
| 242 | |
| 243 | void paging_expect_imsi(const char *imsi) |
| 244 | { |
| 245 | paging_expecting_imsi = imsi; |
| 246 | paging_expecting_tmsi = GSM_RESERVED_TMSI; |
| 247 | } |
| 248 | |
| 249 | void paging_expect_tmsi(uint32_t tmsi) |
| 250 | { |
| 251 | paging_expecting_tmsi = tmsi; |
| 252 | paging_expecting_imsi = NULL; |
| 253 | } |
| 254 | |
| 255 | /* override, requires '-Wl,--wrap=paging_request' */ |
| 256 | int __real_paging_request(struct gsm_network *network, struct bsc_subscr *sub, |
| 257 | int type, gsm_cbfn *cbfn, void *data); |
| 258 | int __wrap_paging_request(struct gsm_network *network, struct bsc_subscr *sub, |
| 259 | int type, gsm_cbfn *cbfn, void *data) |
| 260 | { |
| 261 | log("BTS/BSC sends out paging request to %s for channel type %d", |
| 262 | bsc_subscr_name(sub), type); |
| 263 | OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI)); |
| 264 | if (paging_expecting_imsi) |
| 265 | VERBOSE_ASSERT(strcmp(paging_expecting_imsi, sub->imsi), == 0, "%d"); |
| 266 | if (paging_expecting_tmsi != GSM_RESERVED_TMSI) |
| 267 | VERBOSE_ASSERT(paging_expecting_tmsi, == sub->tmsi, "0x%08x"); |
| 268 | paging_sent = true; |
| 269 | paging_stopped = false; |
| 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | /* override, requires '-Wl,--wrap=paging_request_stop' */ |
| 274 | void __real_paging_request_stop(struct gsm_bts *_bts, |
| 275 | struct vlr_subscr *vsub, |
| 276 | struct gsm_subscriber_connection *conn, |
| 277 | struct msgb *msg); |
| 278 | void __wrap_paging_request_stop(struct gsm_bts *_bts, |
| 279 | struct vlr_subscr *vsub, |
| 280 | struct gsm_subscriber_connection *conn, |
| 281 | struct msgb *msg) |
| 282 | { |
| 283 | paging_stopped = true; |
| 284 | } |
| 285 | |
| 286 | void clear_vlr() |
| 287 | { |
| 288 | struct vlr_subscr *vsub, *n; |
| 289 | llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) { |
| 290 | vlr_subscr_free(vsub); |
| 291 | } |
| 292 | |
| 293 | net->authentication_required = false; |
| 294 | net->a5_encryption = VLR_CIPH_NONE; |
| 295 | net->vlr->cfg.check_imei_rqd = false; |
| 296 | net->vlr->cfg.assign_tmsi = false; |
| 297 | net->vlr->cfg.retrieve_imeisv = false; |
| 298 | |
| 299 | rx_from_ran = RAN_GERAN_A; |
| 300 | auth_request_sent = false; |
| 301 | auth_request_expect_rand = NULL; |
| 302 | auth_request_expect_autn = NULL; |
| 303 | |
| 304 | next_rand_byte = 0; |
| 305 | |
| 306 | osmo_gettimeofday_override = false; |
| 307 | } |
| 308 | |
| 309 | static struct log_info_cat test_categories[] = { |
| 310 | [DMSC] = { |
| 311 | .name = "DMSC", |
| 312 | .description = "Mobile Switching Center", |
| 313 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 314 | }, |
| 315 | [DRLL] = { |
| 316 | .name = "DRLL", |
| 317 | .description = "A-bis Radio Link Layer (RLL)", |
| 318 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 319 | }, |
| 320 | [DMM] = { |
| 321 | .name = "DMM", |
| 322 | .description = "Layer3 Mobility Management (MM)", |
| 323 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 324 | }, |
| 325 | [DRR] = { |
| 326 | .name = "DRR", |
| 327 | .description = "Layer3 Radio Resource (RR)", |
| 328 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 329 | }, |
| 330 | [DCC] = { |
| 331 | .name = "DCC", |
| 332 | .description = "Layer3 Call Control (CC)", |
| 333 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 334 | }, |
| 335 | [DMM] = { |
| 336 | .name = "DMM", |
| 337 | .description = "Layer3 Mobility Management (MM)", |
| 338 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 339 | }, |
| 340 | [DVLR] = { |
| 341 | .name = "DVLR", |
| 342 | .description = "Visitor Location Register", |
| 343 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 344 | }, |
| 345 | [DREF] = { |
| 346 | .name = "DREF", |
| 347 | .description = "Reference Counting", |
| 348 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 349 | }, |
| 350 | }; |
| 351 | |
| 352 | static struct log_info info = { |
| 353 | .cat = test_categories, |
| 354 | .num_cat = ARRAY_SIZE(test_categories), |
| 355 | }; |
| 356 | |
| 357 | extern void *tall_bsc_ctx; |
| 358 | |
| 359 | int fake_mncc_recv(struct gsm_network *net, struct msgb *msg) |
| 360 | { |
| 361 | fprintf(stderr, "rx MNCC\n"); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /* override, requires '-Wl,--wrap=gsup_client_create' */ |
| 366 | struct gsup_client * |
| 367 | __real_gsup_client_create(const char *ip_addr, unsigned int tcp_port, |
| 368 | gsup_client_read_cb_t read_cb, |
| 369 | struct oap_client_config *oap_config); |
| 370 | struct gsup_client * |
| 371 | __wrap_gsup_client_create(const char *ip_addr, unsigned int tcp_port, |
| 372 | gsup_client_read_cb_t read_cb, |
| 373 | struct oap_client_config *oap_config) |
| 374 | { |
| 375 | struct gsup_client *gsupc; |
| 376 | gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client); |
| 377 | OSMO_ASSERT(gsupc); |
| 378 | return gsupc; |
| 379 | } |
| 380 | |
| 381 | /* override, requires '-Wl,--wrap=gsup_client_send' */ |
| 382 | int __real_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg); |
| 383 | int __wrap_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg) |
| 384 | { |
| 385 | const char *is = osmo_hexdump_nospc(msg->data, msg->len); |
| 386 | fprintf(stderr, "GSUP --> HLR: %s: %s\n", |
| 387 | osmo_gsup_message_type_name(msg->data[0]), is); |
| 388 | |
| 389 | OSMO_ASSERT(gsup_tx_expected); |
| 390 | if (strcmp(gsup_tx_expected, is)) { |
| 391 | fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected); |
| 392 | abort(); |
| 393 | } |
| 394 | |
| 395 | talloc_free(msg); |
| 396 | gsup_tx_confirmed = true; |
| 397 | gsup_tx_expected = NULL; |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /* override, requires '-Wl,--wrap=gsm0808_submit_dtap' */ |
| 402 | int __real_gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, |
| 403 | struct msgb *msg, int link_id, int allow_sacch); |
| 404 | int __wrap_gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, |
| 405 | struct msgb *msg, int link_id, int allow_sacch) |
| 406 | { |
| 407 | btw("DTAP --> MS: %s", osmo_hexdump_nospc(msg->data, msg->len)); |
| 408 | |
| 409 | OSMO_ASSERT(dtap_tx_expected); |
| 410 | if (msg->len != dtap_tx_expected->len |
| 411 | || memcmp(msg->data, dtap_tx_expected->data, msg->len)) { |
| 412 | fprintf(stderr, "Mismatch! Expected:\n%s\n", |
| 413 | osmo_hexdump_nospc(dtap_tx_expected->data, |
| 414 | dtap_tx_expected->len)); |
| 415 | abort(); |
| 416 | } |
| 417 | |
| 418 | btw("DTAP matches expected message"); |
| 419 | |
| 420 | talloc_free(msg); |
| 421 | dtap_tx_confirmed = true; |
| 422 | talloc_free(dtap_tx_expected); |
| 423 | dtap_tx_expected = NULL; |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi) |
| 428 | { |
| 429 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 430 | if (send_tmsi == GSM_RESERVED_TMSI) |
| 431 | btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub)); |
| 432 | else |
| 433 | btw("sending LU Accept for %s, with TMSI 0x%08x", |
| 434 | vlr_subscr_name(conn->vsub), send_tmsi); |
| 435 | lu_result_sent |= RES_ACCEPT; |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int fake_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause) |
| 440 | { |
| 441 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 442 | btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause); |
| 443 | lu_result_sent |= RES_REJECT; |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref) |
| 448 | { |
| 449 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 450 | btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub)); |
| 451 | cm_service_result_sent |= RES_ACCEPT; |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, |
| 456 | enum vlr_proc_arq_result result) |
| 457 | { |
| 458 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 459 | btw("sending CM Service Reject for %s, result %s", |
| 460 | vlr_subscr_name(conn->vsub), |
| 461 | vlr_proc_arq_result_name(result)); |
| 462 | cm_service_result_sent |= RES_REJECT; |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at, |
| 467 | bool send_autn) |
| 468 | { |
| 469 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 470 | char *hex; |
| 471 | bool ok = true; |
| 472 | btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...", |
| 473 | send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub), |
| 474 | at->use_count, at->key_seq, at->vec.auth_types); |
| 475 | |
| 476 | hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand)); |
| 477 | btw("...rand=%s", hex); |
| 478 | if (!auth_request_expect_rand |
| 479 | || strcmp(hex, auth_request_expect_rand) != 0) { |
| 480 | ok = false; |
| 481 | log("FAILURE: expected rand=%s", |
| 482 | auth_request_expect_rand ? auth_request_expect_rand : "-"); |
| 483 | } |
| 484 | |
| 485 | if (send_autn) { |
| 486 | hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn)); |
| 487 | btw("...autn=%s", hex); |
| 488 | if (!auth_request_expect_autn |
| 489 | || strcmp(hex, auth_request_expect_autn) != 0) { |
| 490 | ok = false; |
| 491 | log("FAILURE: expected autn=%s", |
| 492 | auth_request_expect_autn ? auth_request_expect_autn : "-"); |
| 493 | } |
| 494 | } else if (auth_request_expect_autn) { |
| 495 | ok = false; |
| 496 | log("FAILURE: no AUTN sent, expected AUTN = %s", |
| 497 | auth_request_expect_autn); |
| 498 | } |
| 499 | |
| 500 | if (send_autn) |
| 501 | btw("...expecting res=%s", |
| 502 | osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len)); |
| 503 | else |
| 504 | btw("...expecting sres=%s", |
| 505 | osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres))); |
| 506 | |
| 507 | auth_request_sent = ok; |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int fake_vlr_tx_auth_rej(void *msc_conn_ref) |
| 512 | { |
| 513 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 514 | btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub)); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, enum vlr_ciph ciph, |
| 519 | bool retrieve_imeisv) |
| 520 | { |
| 521 | /* FIXME: we actually would like to see the message bytes checked here, |
| 522 | * not possible while msc_vlr_set_ciph_mode() calls |
| 523 | * gsm0808_cipher_mode() directly. When the MSCSPLIT is ready, check |
| 524 | * the tx bytes in the sense of dtap_expect_tx() above. */ |
| 525 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 526 | btw("sending Ciphering Mode Command for %s: cipher=%s kc=%s" |
| 527 | " retrieve_imeisv=%d", |
| 528 | vlr_subscr_name(conn->vsub), |
| 529 | vlr_ciph_name(conn->network->a5_encryption), |
| 530 | osmo_hexdump_nospc(conn->vsub->last_tuple->vec.kc, 8), |
| 531 | retrieve_imeisv); |
| 532 | cipher_mode_cmd_sent = true; |
| 533 | cipher_mode_cmd_sent_with_imeisv = retrieve_imeisv; |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | const struct timeval fake_time_start_time = { 123, 456 }; |
| 538 | |
| 539 | void fake_time_start() |
| 540 | { |
| 541 | osmo_gettimeofday_override_time = fake_time_start_time; |
| 542 | osmo_gettimeofday_override = true; |
| 543 | fake_time_passes(0, 0); |
| 544 | } |
| 545 | |
| 546 | void check_talloc(void *msgb_ctx, void *tall_bsc_ctx, int expected_blocks) |
| 547 | { |
| 548 | talloc_report_full(msgb_ctx, stderr); |
| 549 | fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n", |
| 550 | talloc_total_blocks(tall_bsc_ctx)); |
| 551 | if (talloc_total_blocks(tall_bsc_ctx) != expected_blocks) |
| 552 | talloc_report_full(tall_bsc_ctx, stderr); |
| 553 | fprintf(stderr, "\n"); |
| 554 | } |
| 555 | |
| 556 | static struct { |
| 557 | bool verbose; |
| 558 | int run_test_nr; |
| 559 | } cmdline_opts = { |
| 560 | .verbose = false, |
| 561 | .run_test_nr = -1, |
| 562 | }; |
| 563 | |
| 564 | static void print_help(const char *program) |
| 565 | { |
| 566 | printf("Usage:\n" |
| 567 | " %s [-v] [N [N...]]\n" |
| 568 | "Options:\n" |
| 569 | " -h --help show this text.\n" |
| 570 | " -v --verbose print source file and line numbers\n" |
| 571 | " N run only the Nth test (first test is N=1)\n", |
| 572 | program |
| 573 | ); |
| 574 | } |
| 575 | |
| 576 | static void handle_options(int argc, char **argv) |
| 577 | { |
| 578 | while (1) { |
| 579 | int option_index = 0, c; |
| 580 | static struct option long_options[] = { |
| 581 | {"help", 0, 0, 'h'}, |
| 582 | {"verbose", 1, 0, 'v'}, |
| 583 | {0, 0, 0, 0} |
| 584 | }; |
| 585 | |
| 586 | c = getopt_long(argc, argv, "hv", |
| 587 | long_options, &option_index); |
| 588 | if (c == -1) |
| 589 | break; |
| 590 | |
| 591 | switch (c) { |
| 592 | case 'h': |
| 593 | print_help(argv[0]); |
| 594 | exit(0); |
| 595 | case 'v': |
| 596 | cmdline_opts.verbose = true; |
| 597 | break; |
| 598 | default: |
| 599 | /* catch unknown options *as well as* missing arguments. */ |
| 600 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 601 | exit(-1); |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | void *msgb_ctx = NULL; |
| 608 | |
| 609 | void run_tests(int nr) |
| 610 | { |
| 611 | int test_nr; |
| 612 | nr --; /* arg's first test is 1, in here it's 0 */ |
| 613 | for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr ++) { |
| 614 | if (nr >= 0 && test_nr != nr) |
| 615 | continue; |
| 616 | |
| 617 | if (cmdline_opts.verbose) |
| 618 | fprintf(stderr, "(test nr %d)\n", test_nr + 1); |
| 619 | |
| 620 | msc_vlr_tests[test_nr](); |
| 621 | |
| 622 | if (cmdline_opts.verbose) |
| 623 | fprintf(stderr, "(test nr %d)\n", test_nr + 1); |
| 624 | |
| 625 | check_talloc(msgb_ctx, tall_bsc_ctx, 75); |
| 626 | } while(0); |
| 627 | } |
| 628 | |
| 629 | int main(int argc, char **argv) |
| 630 | { |
| 631 | handle_options(argc, argv); |
| 632 | |
| 633 | tall_bsc_ctx = talloc_named_const(NULL, 0, "subscr_conn_test_ctx"); |
| 634 | msgb_ctx = msgb_talloc_ctx_init(tall_bsc_ctx, 0); |
| 635 | osmo_init_logging(&info); |
| 636 | |
| 637 | _log_lines = cmdline_opts.verbose; |
| 638 | |
| 639 | OSMO_ASSERT(osmo_stderr_target); |
| 640 | log_set_use_color(osmo_stderr_target, 0); |
| 641 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 642 | log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0); |
| 643 | log_set_print_category(osmo_stderr_target, 1); |
| 644 | |
| 645 | net = gsm_network_init(tall_bsc_ctx, 1, 1, fake_mncc_recv); |
| 646 | bsc_api_init(net, msc_bsc_api()); |
| 647 | the_bts = gsm_bts_alloc(net); |
| 648 | net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server"); |
| 649 | net->gsup_server_port = 0; |
| 650 | |
| 651 | osmo_fsm_log_addr(false); |
| 652 | OSMO_ASSERT(msc_vlr_alloc(net) == 0); |
| 653 | OSMO_ASSERT(msc_vlr_start(net) == 0); |
| 654 | OSMO_ASSERT(net->vlr); |
| 655 | OSMO_ASSERT(net->vlr->gsup_client); |
| 656 | msc_subscr_conn_init(); |
| 657 | |
| 658 | net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc; |
| 659 | net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej; |
| 660 | net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc; |
| 661 | net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej; |
| 662 | net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req; |
| 663 | net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej; |
| 664 | net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd; |
| 665 | |
| 666 | if (optind >= argc) |
| 667 | run_tests(-1); |
| 668 | else { |
| 669 | int arg; |
| 670 | long int nr; |
| 671 | for (arg = optind; arg < argc; arg++) { |
| 672 | nr = strtol(argv[arg], NULL, 10); |
| 673 | if (errno) { |
| 674 | fprintf(stderr, "Invalid argument: %s\n", |
| 675 | argv[arg]); |
| 676 | exit(1); |
| 677 | } |
| 678 | |
| 679 | run_tests(nr); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | printf("Done\n"); |
| 684 | |
| 685 | talloc_free(the_bts); |
| 686 | |
| 687 | check_talloc(msgb_ctx, tall_bsc_ctx, 9); |
| 688 | return 0; |
| 689 | } |