blob: 2443e3107bb8ad68856b9ae7c96c5200a3bd515d [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,
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700181 enum ran_type via_ran)
182{
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 Hofmeyr6a29d322017-01-25 15:04:16 +0100194enum ran_type rx_from_ran = RAN_GERAN_A;
195
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 Hofmeyr84da6b12016-05-20 21:59:55 +0200200 if (conn->via_ran == RAN_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",
217 ran_type_name(rx_from_ran),
218 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
Maxd8d1a9e2018-02-06 15:50:20 +0100361static int _paging_sent(enum ran_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",
364 ran_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{
380 return _paging_sent(RAN_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
381}
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{
387 return _paging_sent(RAN_GERAN_A, imsi, tmsi, lac);
388}
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
440 rx_from_ran = RAN_GERAN_A;
441 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
549/* override, requires '-Wl,--wrap=gsup_client_create' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200550struct osmo_gsup_client *
551__real_osmo_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
552 osmo_gsup_client_read_cb_t read_cb,
Harald Weltea44b9702018-07-30 17:53:28 +0200553 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200554struct osmo_gsup_client *
555__wrap_osmo_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
556 osmo_gsup_client_read_cb_t read_cb,
Harald Weltea44b9702018-07-30 17:53:28 +0200557 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100558{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200559 struct osmo_gsup_client *gsupc;
560 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100561 OSMO_ASSERT(gsupc);
562 return gsupc;
563}
564
565/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200566int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
567int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100568{
569 const char *is = osmo_hexdump_nospc(msg->data, msg->len);
570 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
571 osmo_gsup_message_type_name(msg->data[0]), is);
572
573 OSMO_ASSERT(gsup_tx_expected);
574 if (strcmp(gsup_tx_expected, is)) {
575 fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected);
576 abort();
577 }
578
579 talloc_free(msg);
580 gsup_tx_confirmed = true;
581 gsup_tx_expected = NULL;
582 return 0;
583}
584
Maxd8d1a9e2018-02-06 15:50:20 +0100585static int _validate_dtap(struct msgb *msg, enum ran_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100586{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100587 btw("DTAP --%s--> MS: %s: %s",
Maxd8d1a9e2018-02-06 15:50:20 +0100588 ran_type_name(to_ran), gh_type_name((void*)msg->data),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200589 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100590
591 OSMO_ASSERT(dtap_tx_expected);
Harald Weltec2007852018-02-03 21:08:26 +0100592
593 /* Mask the sequence number out before comparing */
594 msg->data[1] &= 0x3f;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100595 if (msg->len != dtap_tx_expected->len
596 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
597 fprintf(stderr, "Mismatch! Expected:\n%s\n",
598 osmo_hexdump_nospc(dtap_tx_expected->data,
599 dtap_tx_expected->len));
600 abort();
601 }
602
603 btw("DTAP matches expected message");
604
605 talloc_free(msg);
606 dtap_tx_confirmed = true;
607 talloc_free(dtap_tx_expected);
608 dtap_tx_expected = NULL;
609 return 0;
610}
611
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200612/* override, requires '-Wl,--wrap=ranap_iu_tx' */
613int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
614int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200615{
616 return _validate_dtap(msg, RAN_UTRAN_IU);
617}
618
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200619/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
620int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
621int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200622{
623 btw("Iu Release --%s--> MS", ran_type_name(RAN_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200624 OSMO_ASSERT(iu_release_expected);
625 iu_release_expected = false;
626 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200627 return 0;
628}
629
630/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200631int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
632int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200633{
634 btw("Iu Common ID --%s--> MS (IMSI=%s)", ran_type_name(RAN_UTRAN_IU), imsi);
635 return 0;
636}
637
Philipp Maierfbf66102017-04-09 12:32:51 +0200638/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
639int __real_a_iface_tx_dtap(struct msgb *msg);
640int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200641{
642 return _validate_dtap(msg, RAN_GERAN_A);
643}
644
Philipp Maierfbf66102017-04-09 12:32:51 +0200645/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100646int __real_a_iface_tx_clear_cmd(struct ran_conn *conn);
647int __wrap_a_iface_tx_clear_cmd(struct ran_conn *conn)
Philipp Maierfbf66102017-04-09 12:32:51 +0200648{
649 btw("BSSAP Clear --%s--> MS", ran_type_name(RAN_GERAN_A));
650 OSMO_ASSERT(bssap_clear_expected);
651 bssap_clear_expected = false;
652 bssap_clear_sent = true;
653 return 0;
654}
655
Philipp Maier621ba032017-11-07 17:19:25 +0100656/* override, requires '-Wl,--wrap=msc_mgcp_call_assignment' */
657int __real_msc_mgcp_call_assignment(struct gsm_trans *trans);
658int __wrap_msc_mgcp_call_assignment(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100659{
660 log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x",
661 vlr_subscr_name(trans->vsub), trans->callref);
662 return 0;
663}
664
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100665struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
666
Philipp Maier621ba032017-11-07 17:19:25 +0100667/* override, requires '-Wl,--wrap=msc_mgcp_call_release' */
668void __real_msc_mgcp_call_release(struct gsm_trans *trans);
669void __wrap_msc_mgcp_call_release(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100670{
671 log("MS <--Call Release-- MSC: subscr=%s callref=0x%x",
672 vlr_subscr_name(trans->vsub), trans->callref);
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100673 if (on_call_release_mncc_sends_to_cc_data) {
674 mncc_tx_to_cc(trans->net, on_call_release_mncc_sends_to_cc_data->msg_type,
675 on_call_release_mncc_sends_to_cc_data);
676 on_call_release_mncc_sends_to_cc_data = NULL;
677 }
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100678}
679
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100680static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
681{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100682 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100683 if (send_tmsi == GSM_RESERVED_TMSI)
684 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
685 else
686 btw("sending LU Accept for %s, with TMSI 0x%08x",
687 vlr_subscr_name(conn->vsub), send_tmsi);
688 lu_result_sent |= RES_ACCEPT;
689 return 0;
690}
691
Neels Hofmeyr15809592018-04-06 02:57:51 +0200692static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100693{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100694 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100695 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
696 lu_result_sent |= RES_REJECT;
697 return 0;
698}
699
700static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
701{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100702 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100703 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
704 cm_service_result_sent |= RES_ACCEPT;
705 return 0;
706}
707
Neels Hofmeyr15809592018-04-06 02:57:51 +0200708static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100709{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100710 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200711 btw("sending CM Service Reject for %s, cause: %s",
712 vlr_subscr_name(conn->vsub), gsm48_reject_value_name(cause));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100713 cm_service_result_sent |= RES_REJECT;
714 return 0;
715}
716
717static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
718 bool send_autn)
719{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100720 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100721 char *hex;
722 bool ok = true;
723 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
724 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
725 at->use_count, at->key_seq, at->vec.auth_types);
726
727 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
728 btw("...rand=%s", hex);
729 if (!auth_request_expect_rand
730 || strcmp(hex, auth_request_expect_rand) != 0) {
731 ok = false;
732 log("FAILURE: expected rand=%s",
733 auth_request_expect_rand ? auth_request_expect_rand : "-");
734 }
735
736 if (send_autn) {
737 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
738 btw("...autn=%s", hex);
739 if (!auth_request_expect_autn
740 || strcmp(hex, auth_request_expect_autn) != 0) {
741 ok = false;
742 log("FAILURE: expected autn=%s",
743 auth_request_expect_autn ? auth_request_expect_autn : "-");
744 }
745 } else if (auth_request_expect_autn) {
746 ok = false;
747 log("FAILURE: no AUTN sent, expected AUTN = %s",
748 auth_request_expect_autn);
749 }
750
751 if (send_autn)
752 btw("...expecting res=%s",
753 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
754 else
755 btw("...expecting sres=%s",
756 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
757
758 auth_request_sent = ok;
759 return 0;
760}
761
762static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
763{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100764 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100765 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
766 return 0;
767}
768
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100769/* override, requires '-Wl,--wrap=a_iface_tx_cipher_mode' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100770int __real_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100771 struct gsm0808_encrypt_info *ei, int include_imeisv);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100772int __wrap_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100773 struct gsm0808_encrypt_info *ei, int include_imeisv)
774{
775 int i;
776 btw("sending Ciphering Mode Command for %s: include_imeisv=%d",
777 vlr_subscr_name(conn->vsub), include_imeisv);
778 for (i = 0; i < ei->perm_algo_len; i++)
Neels Hofmeyrd28ea6c2018-09-17 00:54:52 +0200779 btw("...perm algo: A5/%u", ei->perm_algo[i] - 1);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100780 OSMO_ASSERT(ei->key_len <= sizeof(ei->key));
781 btw("...key: %s", osmo_hexdump_nospc(ei->key, ei->key_len));
782 cipher_mode_cmd_sent = true;
783 cipher_mode_cmd_sent_with_imeisv = include_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100784
785 if (!cipher_mode_expect_kc
786 || strcmp(cipher_mode_expect_kc, osmo_hexdump_nospc(ei->key, ei->key_len))) {
787 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
788 OSMO_ASSERT(false);
789 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100790 return 0;
791}
792
793/* override, requires '-Wl,--wrap=ranap_iu_tx_sec_mode_cmd' */
794int __real_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
795 int send_ck, int new_key);
796int __wrap_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
797 int send_ck, int new_key)
798{
799 btw("sending SecurityModeControl for UE ctx %u send_ck=%d new_key=%d",
800 uectx->conn_id, send_ck, new_key);
801 btw("...ik=%s", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
802 if (send_ck)
803 btw("...ck=%s", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100804 security_mode_ctrl_sent = true;
805 if (!security_mode_expect_ik
806 || strcmp(security_mode_expect_ik, osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)))) {
807 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
808 OSMO_ASSERT(false);
809 }
810 if (((!!send_ck) != (!!security_mode_expect_ck))
811 || (security_mode_expect_ck
812 && strcmp(security_mode_expect_ck, osmo_hexdump_nospc(vec->ck, sizeof(vec->ck))))) {
813 log("FAILURE: expected ck=%s", security_mode_expect_ck ? : "NULL");
814 OSMO_ASSERT(false);
815 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100816 return 0;
817}
818
819extern int msc_vlr_set_ciph_mode(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv);
820
Harald Welte71c51df2017-12-23 18:51:48 +0100821static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100822{
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100823 int rc;
824#ifndef BUILD_IU
825 /* If we built without support for IU, fake the IU part here. The root cause is that we don't
826 * have differing sets of expected outputs for --enable-iu and --disable-iu. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100827 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100828 if (conn->via_ran == RAN_UTRAN_IU) {
829 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n", vlr_subscr_name(conn->vsub));
830 rc = __wrap_ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &conn->vsub->last_tuple->vec,
831 0, 1);
832 } else
833#endif
834 rc = msc_vlr_set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
835 if (rc)
836 btw("ERROR sending ciphering mode command: rc=%d", rc);
837 return rc;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100838}
839
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200840void ms_sends_security_mode_complete()
841{
842 OSMO_ASSERT(g_conn);
843 OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU);
844 OSMO_ASSERT(g_conn->iu.ue_ctx);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100845 ran_conn_rx_sec_mode_compl(g_conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200846}
847
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200848void bss_sends_clear_complete()
849{
850 btw("BSS sends BSSMAP Clear Complete");
851 OSMO_ASSERT(g_conn);
852 OSMO_ASSERT(g_conn->via_ran == RAN_GERAN_A);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100853 ran_conn_rx_bssmap_clear_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200854}
855
856void rnc_sends_release_complete()
857{
858 btw("RNC sends Iu Release Complete");
859 OSMO_ASSERT(g_conn);
860 OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100861 ran_conn_rx_iu_release_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200862}
863
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100864const struct timeval fake_time_start_time = { 123, 456 };
865
866void fake_time_start()
867{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200868 struct timespec *clock_override;
869
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100870 osmo_gettimeofday_override_time = fake_time_start_time;
871 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200872 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
873 OSMO_ASSERT(clock_override);
874 clock_override->tv_sec = fake_time_start_time.tv_sec;
875 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
876 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100877 fake_time_passes(0, 0);
878}
879
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200880static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100881{
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200882 /* Verifying that the msgb context is empty */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100883 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100884 /* Expecting these to stick around in msc_vlr_tests_ctx:
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200885 * talloc_total_blocks(tall_bsc_ctx) == 12
886 * full talloc report on 'msc_vlr_tests_ctx' (total 3636 bytes in 12 blocks)
Harald Welte1ea6baf2018-07-31 19:40:52 +0200887 * struct osmo_gsup_client contains 248 bytes in 1 blocks (ref 0) 0x563a489c05f0
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200888 * struct gsm_network contains 2031 bytes in 4 blocks (ref 0) 0x563a489bfbb0
889 * struct vlr_instance contains 168 bytes in 1 blocks (ref 0) 0x563a489c04e0
890 * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x563a489c0460
891 * ../../../src/libosmocore/src/rate_ctr.c:228 contains 1552 bytes in 1 blocks (ref 0) 0x563a489bfd40
892 * logging contains 1357 bytes in 5 blocks (ref 0) 0x563a489bf440
893 * struct log_target contains 228 bytes in 2 blocks (ref 0) 0x563a489bf9f0
894 * struct log_category contains 68 bytes in 1 blocks (ref 0) 0x563a489bfb00
895 * struct log_info contains 1128 bytes in 2 blocks (ref 0) 0x563a489bf4b0
896 * struct log_info_cat contains 1088 bytes in 1 blocks (ref 0) 0x563a489bf540
897 * msgb contains 0 bytes in 1 blocks (ref 0) 0x563a489bf3d0
898 * (That's 12 counting the root ctx)
899 */
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));
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200902 if (talloc_total_blocks(msc_vlr_tests_ctx) != 12)
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}