blob: d3e338afc11797ac8352e97695bde52ebf194f0b [file] [log] [blame]
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001/* 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>
Harald Welte1ea6baf2018-07-31 19:40:52 +020033#include <osmocom/gsupclient/gsup_client.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020034#include <osmocom/msc/gsm_04_11.h>
35#include <osmocom/msc/debug.h>
Max43b01b02017-09-15 11:22:30 +020036#include <osmocom/msc/gsm_04_08.h>
Neels Hofmeyra99b4272017-11-21 17:13:23 +010037#include <osmocom/msc/transaction.h>
Neels Hofmeyr3117b702018-09-13 03:23:07 +020038#include <osmocom/msc/a_iface_bssap.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020039
40#if BUILD_IU
Neels Hofmeyr90843962017-09-04 15:04:35 +020041#include <osmocom/msc/iucs_ranap.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020042#include <osmocom/ranap/iu_client.h>
43#else
Neels Hofmeyr90843962017-09-04 15:04:35 +020044#include <osmocom/msc/iu_dummy.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020045#endif
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010046
47#include "msc_vlr_tests.h"
48
Neels Hofmeyrc01e9092018-03-22 15:56:49 +010049void *msc_vlr_tests_ctx = NULL;
50
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010051bool _log_lines = false;
52
53struct gsm_network *net = NULL;
54
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010055const char *gsup_tx_expected = NULL;
56bool gsup_tx_confirmed;
57
58struct msgb *dtap_tx_expected = NULL;
59bool dtap_tx_confirmed;
60
61enum result_sent lu_result_sent;
62enum result_sent cm_service_result_sent;
63bool auth_request_sent;
64const char *auth_request_expect_rand;
65const char *auth_request_expect_autn;
66bool cipher_mode_cmd_sent;
67bool cipher_mode_cmd_sent_with_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +010068const char *cipher_mode_expect_kc;
69bool security_mode_ctrl_sent;
70const char *security_mode_expect_ck;
71const char *security_mode_expect_ik;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010072
Philipp Maierfbf66102017-04-09 12:32:51 +020073bool iu_release_expected = false;
74bool iu_release_sent = false;
75bool bssap_clear_expected = false;
76bool bssap_clear_sent = false;
77
Neels Hofmeyra99b4272017-11-21 17:13:23 +010078uint32_t cc_to_mncc_tx_expected_msg_type = 0;
79const char *cc_to_mncc_tx_expected_imsi = NULL;
80bool cc_to_mncc_tx_confirmed = false;
81uint32_t cc_to_mncc_tx_got_callref = 0;
82
Harald Weltec2007852018-02-03 21:08:26 +010083extern int gsm0407_pdisc_ctr_bin(uint8_t pdisc);
84
85/* static state variables for the L3 send sequence numbers */
86static uint8_t n_sd[4];
87
88/* patch a correct send sequence number into the given message */
89static void patch_l3_seq_nr(struct msgb *msg)
90{
91 struct gsm48_hdr *gh = msgb_l3(msg);
92 uint8_t pdisc = gsm48_hdr_pdisc(gh);
93 uint8_t *msg_type_oct = &msg->l3h[1];
94 int bin = gsm0407_pdisc_ctr_bin(pdisc);
95
96 if (bin >= 0 && bin < ARRAY_SIZE(n_sd)) {
97 /* patch in n_sd into the msg_type octet */
98 *msg_type_oct = (*msg_type_oct & 0x3f) | ((n_sd[bin] & 0x3) << 6);
99 //fprintf(stderr, "pdisc=0x%02x bin=%d, patched n_sd=%u\n\n", pdisc, bin, n_sd[bin] & 3);
100 /* increment N(SD) */
101 n_sd[bin] = (n_sd[bin] + 1) % 4;
102 } else {
103 //fprintf(stderr, "pdisc=0x%02x NO SEQ\n\n", pdisc);
104 }
105}
106
107/* reset L3 sequence numbers (e.g. new RR connection) */
108static void reset_l3_seq_nr()
109{
110 memset(n_sd, 0, sizeof(n_sd));
111}
112
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100113struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
114{
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200115 struct msgb *msg = msgb_alloc_headroom(size, 4, label);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100116 unsigned char *rc;
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200117 msg->l2h = msg->data;
118 rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg)));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100119 OSMO_ASSERT(rc == msg->l2h);
120 return msg;
121}
122
Maxd8d1a9e2018-02-06 15:50:20 +0100123static const char *gh_type_name(struct gsm48_hdr *gh)
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100124{
125 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
126 gsm48_hdr_msg_type(gh));
127}
128
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100129void dtap_expect_tx(const char *hex)
130{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200131 /* Has the previously expected dtap been received? */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100132 OSMO_ASSERT(!dtap_tx_expected);
133 if (!hex)
134 return;
135 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
Harald Weltec2007852018-02-03 21:08:26 +0100136 /* Mask the sequence number out */
137 if (msgb_length(dtap_tx_expected) >= 2)
138 dtap_tx_expected->data[1] &= 0x3f;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100139 dtap_tx_confirmed = false;
140}
141
Harald Welte1ea6baf2018-07-31 19:40:52 +0200142int vlr_gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100143
144void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
145{
146 int rc;
147 struct msgb *msg;
148 const char *label;
149
150 gsup_expect_tx(expect_tx_hex);
151
152 msg = msgb_from_hex("gsup", 1024, rx_hex);
153 label = osmo_gsup_message_type_name(msg->l2h[0]);
154 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
155 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
Neels Hofmeyr834f94a2017-09-28 03:07:16 +0200156 /* GSUP read cb takes ownership of msgb */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100157 rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg);
158 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
159 label, rc);
160 if (expect_tx_hex)
161 OSMO_ASSERT(gsup_tx_confirmed);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100162}
163
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100164bool conn_exists(const struct ran_conn *conn)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100165{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100166 struct ran_conn *c;
Maxba8a0072018-10-25 17:58:43 +0200167
168 if (!conn)
169 return false;
170
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100171 llist_for_each_entry(c, &net->ran_conns, entry) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100172 if (c == conn)
173 return true;
174 }
Maxba8a0072018-10-25 17:58:43 +0200175
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100176 return false;
177}
178
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700179/* Simplified version of the cm_service_request_concludes() */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100180void conn_conclude_cm_service_req(struct ran_conn *conn,
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100181 enum osmo_rat_type via_ran)
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700182{
183 btw("Concluding CM Service Request");
184
185 OSMO_ASSERT(conn);
186 OSMO_ASSERT(conn->received_cm_service_request);
187
188 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100189 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700190
191 ASSERT_RELEASE_CLEAR(via_ran);
192}
193
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100194enum osmo_rat_type rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100195
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100196struct ran_conn *conn_new(void)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100197{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100198 struct ran_conn *conn;
199 conn = ran_conn_alloc(net, rx_from_ran, 23);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100200 if (conn->via_ran == OSMO_RAT_UTRAN_IU) {
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200201 struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx);
202 *ue_ctx = (struct ranap_ue_conn_ctx){
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200203 .conn_id = 42,
204 };
205 conn->iu.ue_ctx = ue_ctx;
206 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100207 return conn;
208}
209
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100210struct ran_conn *g_conn = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100211
212void rx_from_ms(struct msgb *msg)
213{
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100214 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100215
216 log("MSC <--%s-- MS: %s",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100217 osmo_rat_type_name(rx_from_ran),
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100218 gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100219
Maxba8a0072018-10-25 17:58:43 +0200220 if (!conn_exists(g_conn))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100221 g_conn = NULL;
222
223 if (!g_conn) {
224 log("new conn");
225 g_conn = conn_new();
Harald Weltec2007852018-02-03 21:08:26 +0100226 reset_l3_seq_nr();
227 patch_l3_seq_nr(msg);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100228 ran_conn_compl_l3(g_conn, msg, 23);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100229 } else {
Harald Weltec2007852018-02-03 21:08:26 +0100230 patch_l3_seq_nr(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100231 if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR)
232 && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL))
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100233 ran_conn_cipher_mode_compl(g_conn, msg, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100234 else
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100235 ran_conn_dtap(g_conn, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100236 }
237
Maxba8a0072018-10-25 17:58:43 +0200238 if (!conn_exists(g_conn))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100239 g_conn = NULL;
240}
241
242void ms_sends_msg(const char *hex)
243{
244 struct msgb *msg;
245
246 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
247 msg->l1h = msg->l2h = msg->l3h = msg->data;
248 rx_from_ms(msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200249 msgb_free(msg);
250}
251
252void bss_sends_bssap_mgmt(const char *hex)
253{
254 struct msgb *msg;
255 struct bssmap_header *bh;
256 struct a_conn_info a_conn_info;
257
258 msg = msgb_from_hex("bss_sends_bssap_mgmt", 1024, hex);
259 msg->l3h = msg->data;
260
261 msg->l2h = msgb_push(msg, sizeof(*bh));
262 bh = (void*)msg->l2h;
263 bh->type = BSSAP_MSG_BSS_MANAGEMENT;
264 bh->length = msgb_l3len(msg);
265
Maxba8a0072018-10-25 17:58:43 +0200266 if (!conn_exists(g_conn))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200267 g_conn = NULL;
268
269 OSMO_ASSERT(g_conn);
270 a_conn_info.network = net;
271 a_conn_info.conn_id = g_conn->a.conn_id;
272
273 a_sccp_rx_dt((struct osmo_sccp_user*)0x1, &a_conn_info, msg);
274 msgb_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100275}
276
Maxd8d1a9e2018-02-06 15:50:20 +0100277static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100278{
279 int rc;
280 struct msgb *msg;
281 struct gsm48_hdr *gh;
282
283 msg = msgb_alloc(1024, "ms_sends_msg_fake");
284 msg->l1h = msg->l2h = msg->l3h = msg->data;
285
286 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
287 gh->proto_discr = pdisc;
288 gh->msg_type = msg_type;
289 /* some amount of data, whatever */
290 msgb_put(msg, 123);
291
Harald Weltec2007852018-02-03 21:08:26 +0100292 patch_l3_seq_nr(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100293 rc = gsm0408_dispatch(g_conn, msg);
294
295 talloc_free(msg);
296 return rc;
297}
298
Max68171902018-02-06 17:55:19 +0100299static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype)
300{
301 int rc = ms_sends_msg_fake(val, msgtype);
302 if (rc != -EACCES)
303 log("Unexpected return value %u != %u for %s/%s",
304 -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype));
305}
306
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100307void thwart_rx_non_initial_requests()
308{
309 log("requests shall be thwarted");
Max68171902018-02-06 17:55:19 +0100310
311 ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP);
312 ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */
313 ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1);
314 ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100315}
316
317void send_sms(struct vlr_subscr *receiver,
318 struct vlr_subscr *sender,
319 char *str)
320{
Harald Welte39b55482018-04-09 19:19:33 +0200321 struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str);
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700322 gsm411_send_sms(net, receiver, sms);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100323}
324
325unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100326/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
327int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
328int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100329{
Max753c15d2017-12-21 14:50:44 +0100330 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100331 for (i = 0; i < num; i++)
332 buf[i] = next_rand_byte++;
333 return 1;
334}
335
336/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
337void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
338void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
339{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200340 /* Write fixed time bytes for deterministic test results */
341 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100342}
343
344const char *paging_expecting_imsi = NULL;
345uint32_t paging_expecting_tmsi;
346bool paging_sent;
347bool paging_stopped;
348
349void paging_expect_imsi(const char *imsi)
350{
351 paging_expecting_imsi = imsi;
352 paging_expecting_tmsi = GSM_RESERVED_TMSI;
353}
354
355void paging_expect_tmsi(uint32_t tmsi)
356{
357 paging_expecting_tmsi = tmsi;
358 paging_expecting_imsi = NULL;
359}
360
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100361static int _paging_sent(enum osmo_rat_type via_ran, const char *imsi, uint32_t tmsi, uint32_t lac)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100362{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200363 log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100364 osmo_rat_type_name(via_ran), imsi, tmsi, lac);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100365 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
366 if (paging_expecting_imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200367 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d");
368 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
369 VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x");
370 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100371 paging_sent = true;
372 paging_stopped = false;
373 return 1;
374}
375
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200376/* override, requires '-Wl,--wrap=ranap_iu_page_cs' */
377int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
378int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200379{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100380 return _paging_sent(OSMO_RAT_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200381}
382
Philipp Maierfbf66102017-04-09 12:32:51 +0200383/* override, requires '-Wl,--wrap=a_iface_tx_paging' */
384int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac);
385int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200386{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100387 return _paging_sent(OSMO_RAT_GERAN_A, imsi, tmsi, lac);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200388}
389
390/* override, requires '-Wl,--wrap=msc_stop_paging' */
391void __real_msc_stop_paging(struct vlr_subscr *vsub);
392void __wrap_msc_stop_paging(struct vlr_subscr *vsub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100393{
394 paging_stopped = true;
395}
396
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200397
398/* override, requires '-Wl,--wrap=osmo_sccp_tx_data_msg' */
399int __real_osmo_sccp_tx_data_msg(struct osmo_sccp_user *scu, uint32_t conn_id,
400 struct msgb *msg);
401int __wrap_osmo_sccp_tx_data_msg(struct osmo_sccp_user *scu, uint32_t conn_id,
402 struct msgb *msg)
403{
404 const char *proto_str;
405 const char *msg_str = gsm0808_bssmap_name(msg->l3h[2]);
406 switch (*msg->l3h) {
407 case BSSAP_MSG_BSS_MANAGEMENT:
408 proto_str = "BSSAP-BSS-MANAGEMENT";
409 break;
410 case BSSAP_MSG_DTAP:
411 proto_str = "BSSAP-DTAP";
412 break;
413 default:
414 proto_str = "";
415 msg_str = "";
416 break;
417 }
418
419 log("BSC <--%s-- MSC: %s %s", proto_str, msg_str, msgb_hexdump(msg));
420 msgb_free(msg);
421 return 0;
422}
423
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100424void clear_vlr()
425{
426 struct vlr_subscr *vsub, *n;
427 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
428 vlr_subscr_free(vsub);
429 }
430
431 net->authentication_required = false;
Harald Welte7b222aa2017-12-23 19:30:32 +0100432 net->a5_encryption_mask = (1 << 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100433 net->vlr->cfg.check_imei_rqd = false;
434 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200435 net->vlr->cfg.retrieve_imeisv_early = false;
436 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100437 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
438 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100439
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100440 rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100441 auth_request_sent = false;
442 auth_request_expect_rand = NULL;
443 auth_request_expect_autn = NULL;
444
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100445 cipher_mode_cmd_sent = false;
446 cipher_mode_cmd_sent_with_imeisv = false;
447 cipher_mode_expect_kc = NULL;
448
449 security_mode_ctrl_sent = false;
450 security_mode_expect_ck = NULL;
451 security_mode_expect_ik = NULL;
452
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100453 next_rand_byte = 0;
454
Philipp Maierfbf66102017-04-09 12:32:51 +0200455 iu_release_expected = false;
456 iu_release_sent = false;
457 bssap_clear_expected = false;
458 bssap_clear_sent = false;
459
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100460 osmo_gettimeofday_override = false;
461}
462
463static struct log_info_cat test_categories[] = {
464 [DMSC] = {
465 .name = "DMSC",
466 .description = "Mobile Switching Center",
467 .enabled = 1, .loglevel = LOGL_DEBUG,
468 },
469 [DRLL] = {
470 .name = "DRLL",
471 .description = "A-bis Radio Link Layer (RLL)",
472 .enabled = 1, .loglevel = LOGL_DEBUG,
473 },
474 [DMM] = {
475 .name = "DMM",
476 .description = "Layer3 Mobility Management (MM)",
477 .enabled = 1, .loglevel = LOGL_DEBUG,
478 },
479 [DRR] = {
480 .name = "DRR",
481 .description = "Layer3 Radio Resource (RR)",
482 .enabled = 1, .loglevel = LOGL_DEBUG,
483 },
484 [DCC] = {
485 .name = "DCC",
486 .description = "Layer3 Call Control (CC)",
Neels Hofmeyr12e17be2018-03-12 23:59:37 +0100487 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100488 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100489 [DVLR] = {
490 .name = "DVLR",
491 .description = "Visitor Location Register",
492 .enabled = 1, .loglevel = LOGL_DEBUG,
493 },
494 [DREF] = {
495 .name = "DREF",
496 .description = "Reference Counting",
497 .enabled = 1, .loglevel = LOGL_DEBUG,
498 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200499 [DPAG] = {
500 .name = "DPAG",
501 .description = "Paging Subsystem",
502 .enabled = 1, .loglevel = LOGL_DEBUG,
503 },
504 [DIUCS] = {
505 .name = "DIUCS",
506 .description = "Iu-CS Protocol",
507 .enabled = 1, .loglevel = LOGL_DEBUG,
508 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100509 [DMNCC] = {
510 .name = "DMNCC",
511 .description = "MNCC API for Call Control application",
512 .enabled = 1, .loglevel = LOGL_DEBUG,
513 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100514};
515
516static struct log_info info = {
517 .cat = test_categories,
518 .num_cat = ARRAY_SIZE(test_categories),
519};
520
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100521int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100522{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100523 struct gsm_mncc *mncc = (void*)msg->data;
524 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
525 get_mncc_name(mncc->msg_type));
526
527 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
528 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
529 log("Mismatch! Expected MNCC msg type: %s",
530 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
531 abort();
532 }
533
534 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
535 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
536 cc_to_mncc_tx_expected_imsi,
537 mncc->imsi);
538 abort();
539 }
540
541 cc_to_mncc_tx_confirmed = true;
542 cc_to_mncc_tx_got_callref = mncc->callref;
543 cc_to_mncc_tx_expected_imsi = NULL;
544 cc_to_mncc_tx_expected_msg_type = 0;
545 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100546 return 0;
547}
548
Harald Welte1ea6baf2018-07-31 19:40:52 +0200549struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100550__real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
551 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
552 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200553struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100554__wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
555 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
556 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100557{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200558 struct osmo_gsup_client *gsupc;
559 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100560 OSMO_ASSERT(gsupc);
561 return gsupc;
562}
563
564/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200565int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
566int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100567{
568 const char *is = osmo_hexdump_nospc(msg->data, msg->len);
569 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
570 osmo_gsup_message_type_name(msg->data[0]), is);
571
572 OSMO_ASSERT(gsup_tx_expected);
573 if (strcmp(gsup_tx_expected, is)) {
574 fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected);
575 abort();
576 }
577
578 talloc_free(msg);
579 gsup_tx_confirmed = true;
580 gsup_tx_expected = NULL;
581 return 0;
582}
583
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100584static int _validate_dtap(struct msgb *msg, enum osmo_rat_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100585{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100586 btw("DTAP --%s--> MS: %s: %s",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100587 osmo_rat_type_name(to_ran), gh_type_name((void*)msg->data),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200588 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100589
590 OSMO_ASSERT(dtap_tx_expected);
Harald Weltec2007852018-02-03 21:08:26 +0100591
592 /* Mask the sequence number out before comparing */
593 msg->data[1] &= 0x3f;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100594 if (msg->len != dtap_tx_expected->len
595 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
596 fprintf(stderr, "Mismatch! Expected:\n%s\n",
597 osmo_hexdump_nospc(dtap_tx_expected->data,
598 dtap_tx_expected->len));
599 abort();
600 }
601
602 btw("DTAP matches expected message");
603
604 talloc_free(msg);
605 dtap_tx_confirmed = true;
606 talloc_free(dtap_tx_expected);
607 dtap_tx_expected = NULL;
608 return 0;
609}
610
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200611/* override, requires '-Wl,--wrap=ranap_iu_tx' */
612int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
613int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200614{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100615 return _validate_dtap(msg, OSMO_RAT_UTRAN_IU);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200616}
617
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200618/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
619int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
620int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200621{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100622 btw("Iu Release --%s--> MS", osmo_rat_type_name(OSMO_RAT_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200623 OSMO_ASSERT(iu_release_expected);
624 iu_release_expected = false;
625 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200626 return 0;
627}
628
629/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200630int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
631int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200632{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100633 btw("Iu Common ID --%s--> MS (IMSI=%s)", osmo_rat_type_name(OSMO_RAT_UTRAN_IU), imsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200634 return 0;
635}
636
Philipp Maierfbf66102017-04-09 12:32:51 +0200637/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
638int __real_a_iface_tx_dtap(struct msgb *msg);
639int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200640{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100641 return _validate_dtap(msg, OSMO_RAT_GERAN_A);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200642}
643
Philipp Maierfbf66102017-04-09 12:32:51 +0200644/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100645int __real_a_iface_tx_clear_cmd(struct ran_conn *conn);
646int __wrap_a_iface_tx_clear_cmd(struct ran_conn *conn)
Philipp Maierfbf66102017-04-09 12:32:51 +0200647{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100648 btw("BSSAP Clear --%s--> MS", osmo_rat_type_name(OSMO_RAT_GERAN_A));
Philipp Maierfbf66102017-04-09 12:32:51 +0200649 OSMO_ASSERT(bssap_clear_expected);
650 bssap_clear_expected = false;
651 bssap_clear_sent = true;
652 return 0;
653}
654
Neels Hofmeyrb16259f2018-12-20 02:57:56 +0100655/* override, requires '-Wl,--wrap=msc_mgcp_try_call_assignment' */
656int __real_msc_mgcp_try_call_assignment(struct gsm_trans *trans);
657int __wrap_msc_mgcp_try_call_assignment(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100658{
659 log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x",
660 vlr_subscr_name(trans->vsub), trans->callref);
661 return 0;
662}
663
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100664struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
665
Philipp Maier621ba032017-11-07 17:19:25 +0100666/* override, requires '-Wl,--wrap=msc_mgcp_call_release' */
667void __real_msc_mgcp_call_release(struct gsm_trans *trans);
668void __wrap_msc_mgcp_call_release(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100669{
670 log("MS <--Call Release-- MSC: subscr=%s callref=0x%x",
671 vlr_subscr_name(trans->vsub), trans->callref);
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100672 if (on_call_release_mncc_sends_to_cc_data) {
673 mncc_tx_to_cc(trans->net, on_call_release_mncc_sends_to_cc_data->msg_type,
674 on_call_release_mncc_sends_to_cc_data);
675 on_call_release_mncc_sends_to_cc_data = NULL;
676 }
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100677}
678
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100679static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
680{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100681 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100682 if (send_tmsi == GSM_RESERVED_TMSI)
683 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
684 else
685 btw("sending LU Accept for %s, with TMSI 0x%08x",
686 vlr_subscr_name(conn->vsub), send_tmsi);
687 lu_result_sent |= RES_ACCEPT;
688 return 0;
689}
690
Neels Hofmeyr15809592018-04-06 02:57:51 +0200691static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100692{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100693 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100694 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
695 lu_result_sent |= RES_REJECT;
696 return 0;
697}
698
699static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
700{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100701 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100702 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
703 cm_service_result_sent |= RES_ACCEPT;
704 return 0;
705}
706
Neels Hofmeyr15809592018-04-06 02:57:51 +0200707static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100708{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100709 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200710 btw("sending CM Service Reject for %s, cause: %s",
711 vlr_subscr_name(conn->vsub), gsm48_reject_value_name(cause));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100712 cm_service_result_sent |= RES_REJECT;
713 return 0;
714}
715
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100716static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100717 bool send_autn)
718{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100719 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100720 char *hex;
721 bool ok = true;
722 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
723 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
724 at->use_count, at->key_seq, at->vec.auth_types);
725
726 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
727 btw("...rand=%s", hex);
728 if (!auth_request_expect_rand
729 || strcmp(hex, auth_request_expect_rand) != 0) {
730 ok = false;
731 log("FAILURE: expected rand=%s",
732 auth_request_expect_rand ? auth_request_expect_rand : "-");
733 }
734
735 if (send_autn) {
736 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
737 btw("...autn=%s", hex);
738 if (!auth_request_expect_autn
739 || strcmp(hex, auth_request_expect_autn) != 0) {
740 ok = false;
741 log("FAILURE: expected autn=%s",
742 auth_request_expect_autn ? auth_request_expect_autn : "-");
743 }
744 } else if (auth_request_expect_autn) {
745 ok = false;
746 log("FAILURE: no AUTN sent, expected AUTN = %s",
747 auth_request_expect_autn);
748 }
749
750 if (send_autn)
751 btw("...expecting res=%s",
752 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
753 else
754 btw("...expecting sres=%s",
755 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
756
757 auth_request_sent = ok;
758 return 0;
759}
760
761static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
762{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100763 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100764 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
765 return 0;
766}
767
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100768/* override, requires '-Wl,--wrap=a_iface_tx_cipher_mode' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100769int __real_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100770 struct gsm0808_encrypt_info *ei, int include_imeisv);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100771int __wrap_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100772 struct gsm0808_encrypt_info *ei, int include_imeisv)
773{
774 int i;
775 btw("sending Ciphering Mode Command for %s: include_imeisv=%d",
776 vlr_subscr_name(conn->vsub), include_imeisv);
777 for (i = 0; i < ei->perm_algo_len; i++)
Neels Hofmeyrd28ea6c2018-09-17 00:54:52 +0200778 btw("...perm algo: A5/%u", ei->perm_algo[i] - 1);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100779 OSMO_ASSERT(ei->key_len <= sizeof(ei->key));
780 btw("...key: %s", osmo_hexdump_nospc(ei->key, ei->key_len));
781 cipher_mode_cmd_sent = true;
782 cipher_mode_cmd_sent_with_imeisv = include_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100783
784 if (!cipher_mode_expect_kc
785 || strcmp(cipher_mode_expect_kc, osmo_hexdump_nospc(ei->key, ei->key_len))) {
786 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
787 OSMO_ASSERT(false);
788 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100789 return 0;
790}
791
792/* override, requires '-Wl,--wrap=ranap_iu_tx_sec_mode_cmd' */
793int __real_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
794 int send_ck, int new_key);
795int __wrap_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
796 int send_ck, int new_key)
797{
798 btw("sending SecurityModeControl for UE ctx %u send_ck=%d new_key=%d",
799 uectx->conn_id, send_ck, new_key);
800 btw("...ik=%s", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
801 if (send_ck)
802 btw("...ck=%s", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100803 security_mode_ctrl_sent = true;
804 if (!security_mode_expect_ik
805 || strcmp(security_mode_expect_ik, osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)))) {
806 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
807 OSMO_ASSERT(false);
808 }
809 if (((!!send_ck) != (!!security_mode_expect_ck))
810 || (security_mode_expect_ck
811 && strcmp(security_mode_expect_ck, osmo_hexdump_nospc(vec->ck, sizeof(vec->ck))))) {
812 log("FAILURE: expected ck=%s", security_mode_expect_ck ? : "NULL");
813 OSMO_ASSERT(false);
814 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100815 return 0;
816}
817
818extern int msc_vlr_set_ciph_mode(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv);
819
Harald Welte71c51df2017-12-23 18:51:48 +0100820static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100821{
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100822 int rc;
823#ifndef BUILD_IU
824 /* If we built without support for IU, fake the IU part here. The root cause is that we don't
825 * have differing sets of expected outputs for --enable-iu and --disable-iu. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100826 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100827 if (conn->via_ran == OSMO_RAT_UTRAN_IU) {
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100828 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n", vlr_subscr_name(conn->vsub));
829 rc = __wrap_ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &conn->vsub->last_tuple->vec,
830 0, 1);
831 } else
832#endif
833 rc = msc_vlr_set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
834 if (rc)
835 btw("ERROR sending ciphering mode command: rc=%d", rc);
836 return rc;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100837}
838
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200839void ms_sends_security_mode_complete()
840{
841 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100842 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200843 OSMO_ASSERT(g_conn->iu.ue_ctx);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100844 ran_conn_rx_sec_mode_compl(g_conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200845}
846
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200847void bss_sends_clear_complete()
848{
849 btw("BSS sends BSSMAP Clear Complete");
850 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100851 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_GERAN_A);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100852 ran_conn_rx_bssmap_clear_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200853}
854
855void rnc_sends_release_complete()
856{
857 btw("RNC sends Iu Release Complete");
858 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100859 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100860 ran_conn_rx_iu_release_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200861}
862
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100863const struct timeval fake_time_start_time = { 123, 456 };
864
865void fake_time_start()
866{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200867 struct timespec *clock_override;
868
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100869 osmo_gettimeofday_override_time = fake_time_start_time;
870 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200871 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
872 OSMO_ASSERT(clock_override);
873 clock_override->tv_sec = fake_time_start_time.tv_sec;
874 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
875 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100876 fake_time_passes(0, 0);
877}
878
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200879static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100880{
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200881 /* Verifying that the msgb context is empty */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100882 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100883 /* Expecting these to stick around in msc_vlr_tests_ctx:
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100884 * talloc_total_blocks(tall_bsc_ctx) == 13
885 * full talloc report on 'msc_vlr_tests_ctx' (total 4638 bytes in 13 blocks)
886 * struct osmo_gsup_client contains 256 bytes in 1 blocks (ref 0) 0x61300000dd20
887 * struct gsm_network contains 2983 bytes in 5 blocks (ref 0) 0x61400000fea0
888 * struct vlr_instance contains 320 bytes in 2 blocks (ref 0) 0x61300000dee0
889 * struct ipaccess_unit contains 64 bytes in 1 blocks (ref 0) 0x60e0000244c0
890 * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x60b00000af40
891 * rate_ctr.c:234 contains 2352 bytes in 1 blocks (ref 0) 0x61e00000f0e0
892 * logging contains 1399 bytes in 5 blocks (ref 0) 0x60b00000aff0
893 * struct log_target contains 238 bytes in 2 blocks (ref 0) 0x61200000bf20
894 * struct log_category contains 70 bytes in 1 blocks (ref 0) 0x60f00000efb0
895 * struct log_info contains 1160 bytes in 2 blocks (ref 0) 0x60d00000cfd0
896 * struct log_info_cat contains 1120 bytes in 1 blocks (ref 0) 0x61a00001f2e0
897 * msgb contains 0 bytes in 1 blocks (ref 0) 0x60800000bf80
898 * (That's 13 counting the root ctx)
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200899 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100900 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100901 talloc_total_blocks(msc_vlr_tests_ctx));
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100902 if (talloc_total_blocks(msc_vlr_tests_ctx) != 13)
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100903 talloc_report_full(msc_vlr_tests_ctx, stderr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100904 fprintf(stderr, "\n");
905}
906
907static struct {
908 bool verbose;
909 int run_test_nr;
910} cmdline_opts = {
911 .verbose = false,
912 .run_test_nr = -1,
913};
914
915static void print_help(const char *program)
916{
917 printf("Usage:\n"
918 " %s [-v] [N [N...]]\n"
919 "Options:\n"
920 " -h --help show this text.\n"
921 " -v --verbose print source file and line numbers\n"
922 " N run only the Nth test (first test is N=1)\n",
923 program
924 );
925}
926
927static void handle_options(int argc, char **argv)
928{
929 while (1) {
930 int option_index = 0, c;
931 static struct option long_options[] = {
932 {"help", 0, 0, 'h'},
933 {"verbose", 1, 0, 'v'},
934 {0, 0, 0, 0}
935 };
936
937 c = getopt_long(argc, argv, "hv",
938 long_options, &option_index);
939 if (c == -1)
940 break;
941
942 switch (c) {
943 case 'h':
944 print_help(argv[0]);
945 exit(0);
946 case 'v':
947 cmdline_opts.verbose = true;
948 break;
949 default:
950 /* catch unknown options *as well as* missing arguments. */
951 fprintf(stderr, "Error in command line options. Exiting.\n");
952 exit(-1);
953 break;
954 }
955 }
956}
957
958void *msgb_ctx = NULL;
959
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100960static void run_tests(int nr)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100961{
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100962 int test_nr;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200963
964 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
965
Max5e2e9bd2018-02-06 19:31:08 +0100966 nr--; /* arg's first test is 1, in here it's 0 */
967 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100968 if (nr >= 0 && test_nr != nr)
969 continue;
970
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100971 if (cmdline_opts.verbose)
972 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
973
974 msc_vlr_tests[test_nr]();
975
976 if (cmdline_opts.verbose)
977 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100978
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200979 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Max29ce08a2018-02-06 18:46:57 +0100980 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100981}
982
Max5e60de62018-02-07 12:56:09 +0100983struct gsm_network *test_net(void *ctx)
984{
Neels Hofmeyr7f484202018-02-27 12:59:45 +0100985 struct gsm_network *net = gsm_network_init(ctx, mncc_recv);
Max5e60de62018-02-07 12:56:09 +0100986
987 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
988 net->gsup_server_port = 0;
989
990 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
991 OSMO_ASSERT(msc_vlr_start(net) == 0);
992 OSMO_ASSERT(net->vlr);
993 OSMO_ASSERT(net->vlr->gsup_client);
994
995 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
996 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
997 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
998 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
999 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
1000 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
1001 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
1002
1003 return net;
1004}
1005
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001006int main(int argc, char **argv)
1007{
1008 handle_options(argc, argv);
1009
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001010 msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx");
1011 msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0);
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001012 osmo_init_logging2(msc_vlr_tests_ctx, &info);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001013
1014 _log_lines = cmdline_opts.verbose;
1015
1016 OSMO_ASSERT(osmo_stderr_target);
1017 log_set_use_color(osmo_stderr_target, 0);
1018 log_set_print_timestamp(osmo_stderr_target, 0);
1019 log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0);
1020 log_set_print_category(osmo_stderr_target, 1);
1021
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +01001022 if (cmdline_opts.verbose)
1023 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
1024
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001025 net = test_net(msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001026
1027 osmo_fsm_log_addr(false);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001028
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001029 ran_conn_init();
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001030
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001031 clear_vlr();
1032
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001033 if (optind >= argc)
1034 run_tests(-1);
1035 else {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001036 int arg;
1037 long int nr;
1038 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +01001039 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001040 nr = strtol(argv[arg], NULL, 10);
1041 if (errno) {
1042 fprintf(stderr, "Invalid argument: %s\n",
1043 argv[arg]);
1044 exit(1);
1045 }
1046
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001047 run_tests(nr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001048 }
1049 }
1050
1051 printf("Done\n");
1052
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001053 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001054 return 0;
1055}