blob: 4330ea869c839d0a56b40315fe560a9efc6f7f29 [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#pragma once
25
26#include <stdbool.h>
27#include <stdio.h>
28
Neels Hofmeyr90843962017-09-04 15:04:35 +020029#include <osmocom/msc/gsm_data.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010031#include <osmocom/msc/msub.h>
32#include <osmocom/msc/msc_a.h>
Neels Hofmeyra99b4272017-11-21 17:13:23 +010033#include <osmocom/msc/mncc.h>
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010034
Neels Hofmeyra4d7a762019-07-31 15:21:19 +020035#define A5_0 (1 << 0)
36#define A5_0_3 ((1 << 0) | (1 << 3))
37
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010038extern bool _log_lines;
39#define _log(fmt, args...) do { \
40 if (_log_lines) \
41 fprintf(stderr, " %4d:%s: " fmt "\n", \
42 __LINE__, __FILE__, ## args ); \
43 else \
44 fprintf(stderr, fmt "\n", ## args ); \
45 } while (false)
46
47/* btw means "by the way", the test tells the log what's happening.
48 * BTW() marks a larger section, btw() is the usual logging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010049#define BTW(fmt, args...) _log("\n\n- " fmt, ## args )
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010050#define btw(fmt, args...) _log("- " fmt, ## args )
51#define log(fmt, args...) _log(" " fmt, ## args )
52
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +010053#define comment_start() fprintf(stderr, "===== %s\n", __func__);
54#define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010055
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010056extern struct msub *g_msub;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010057extern struct gsm_network *net;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010058extern void *msgb_ctx;
59
Neels Hofmeyr7814a832018-12-26 00:40:18 +010060extern enum osmo_rat_type rx_from_ran;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010061
62extern const char *gsup_tx_expected;
63extern bool gsup_tx_confirmed;
64
65extern struct msgb *dtap_tx_expected;
66extern bool dtap_tx_confirmed;
67
68enum result_sent {
69 RES_NONE = 0,
70 RES_ACCEPT = 1,
71 RES_REJECT = 2,
72};
73extern enum result_sent lu_result_sent;
74extern enum result_sent cm_service_result_sent;
75
76extern bool auth_request_sent;
77extern const char *auth_request_expect_rand;
78extern const char *auth_request_expect_autn;
79
80extern bool cipher_mode_cmd_sent;
81extern bool cipher_mode_cmd_sent_with_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +010082extern const char *cipher_mode_expect_kc;
83
84extern bool security_mode_ctrl_sent;
85extern const char *security_mode_expect_ck;
86extern const char *security_mode_expect_ik;
87
88static inline void expect_cipher_mode_cmd(const char *kc)
89{
90 cipher_mode_cmd_sent = false;
91 cipher_mode_expect_kc = kc;
92 /* make sure we don't mix up the two */
93 security_mode_ctrl_sent = false;
94}
95
96static inline void expect_security_mode_ctrl(const char *ck, const char *ik)
97{
98 security_mode_ctrl_sent = false;
99 security_mode_expect_ck = ck;
100 security_mode_expect_ik = ik;
101 /* make sure we don't mix up the two */
102 cipher_mode_cmd_sent = false;
103}
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100104
105extern bool paging_sent;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100106
Philipp Maierfbf66102017-04-09 12:32:51 +0200107extern bool iu_release_expected;
108extern bool iu_release_sent;
109extern bool bssap_clear_expected;
110extern bool bssap_clear_sent;
111
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100112extern uint32_t cc_to_mncc_tx_expected_msg_type;
113extern const char *cc_to_mncc_tx_expected_imsi;
114extern bool cc_to_mncc_tx_confirmed;
115extern uint32_t cc_to_mncc_tx_got_callref;
116
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100117extern struct gsm_mncc *on_call_release_mncc_sends_to_cc_data;
118
Philipp Maierfbf66102017-04-09 12:32:51 +0200119static inline void expect_iu_release()
120{
121 iu_release_expected = true;
122 iu_release_sent = false;
123}
124
125static inline void expect_bssap_clear()
126{
127 bssap_clear_expected = true;
128 bssap_clear_sent = false;
129}
130
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100131static inline void expect_release_clear(enum osmo_rat_type via_ran)
Philipp Maierfbf66102017-04-09 12:32:51 +0200132{
133 switch (via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100134 case OSMO_RAT_GERAN_A:
Philipp Maierfbf66102017-04-09 12:32:51 +0200135 expect_bssap_clear();
136 return;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100137 case OSMO_RAT_UTRAN_IU:
Philipp Maierfbf66102017-04-09 12:32:51 +0200138 expect_iu_release();
139 return;
140 default:
141 OSMO_ASSERT(false);
142 break;
143 }
144}
145
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100146struct msc_vlr_test_cmdline_opts {
147 bool verbose;
148 int run_test_nr;
149};
150
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100151typedef void (* msc_vlr_test_func_t )(void);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100152extern msc_vlr_test_func_t msc_vlr_tests[];
153
154struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex);
155
156void clear_vlr();
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100157bool conn_exists(const struct msub *msub);
158void conn_conclude_cm_service_req(struct msub *msub, const char *cm_service_use);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100159
160void dtap_expect_tx(const char *hex);
161void dtap_expect_tx_ussd(char *ussd_text);
162void paging_expect_imsi(const char *imsi);
163void paging_expect_tmsi(uint32_t tmsi);
164
165void ms_sends_msg(const char *hex);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100166void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark);
167void ms_sends_ciphering_mode_complete(const char *inner_nas_msg);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200168void ms_sends_security_mode_complete();
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100169void gsup_rx(const char *rx_hex, const char *expect_tx_hex);
170void send_sms(struct vlr_subscr *receiver,
171 struct vlr_subscr *sender,
172 char *str);
173
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100174void ran_sends_clear_complete();
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200175
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100176void thwart_rx_non_initial_requests();
177
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100178#define EXPECT_ACCEPTED(expect_accepted) do { \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100179 if (g_msub) \
180 OSMO_ASSERT(conn_exists(g_msub)); \
181 bool accepted = msc_a_is_accepted(msub_msc_a(g_msub)); \
182 fprintf(stderr, "msc_a_is_accepted() == %s\n", \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100183 accepted ? "true" : "false"); \
184 OSMO_ASSERT(accepted == expect_accepted); \
185 } while (false)
186
Maxbb53a3f2018-12-17 17:09:17 +0100187#define VAL_ASSERT(desc, val, expect_op, fmt) \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100188 do { \
Maxbb53a3f2018-12-17 17:09:17 +0100189 log(desc " == " fmt, (val)); \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100190 OSMO_ASSERT((val) expect_op); \
Maxbb53a3f2018-12-17 17:09:17 +0100191 } while (0)
192
193#define VERBOSE_ASSERT(val, expect_op, fmt) VAL_ASSERT(#val, val, expect_op, fmt)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100194
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100195#define EXPECT_CONN_COUNT(N) VERBOSE_ASSERT(llist_count(&msub_list), == N, "%d")
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100196
197#define gsup_expect_tx(hex) do \
198{ \
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200199 if (gsup_tx_expected) { \
200 log("Previous expected GSUP tx was not confirmed!"); \
201 OSMO_ASSERT(!gsup_tx_expected); \
202 } \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100203 if (!hex) \
204 break; \
205 gsup_tx_expected = hex; \
206 gsup_tx_confirmed = false; \
207} while (0)
208
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100209#define cc_to_mncc_expect_tx(imsi, msg_type) do \
210{ \
211 if (cc_to_mncc_tx_expected_msg_type) { \
212 log("Previous expected MNCC tx was not confirmed!"); \
213 OSMO_ASSERT(!cc_to_mncc_tx_expected_msg_type); \
214 } \
215 cc_to_mncc_tx_expected_imsi = imsi; \
216 cc_to_mncc_tx_expected_msg_type = msg_type; \
217 cc_to_mncc_tx_confirmed = false; \
218} while (0)
219
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100220void fake_time_start();
221
222/* as macro to get the test file's source line number */
223#define fake_time_passes(secs, usecs) do \
224{ \
225 struct timeval diff; \
226 osmo_gettimeofday_override_add(secs, usecs); \
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200227 osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100228 timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
229 btw("Total time passed: %d.%06d s", \
230 (int)diff.tv_sec, (int)diff.tv_usec); \
231 osmo_timers_prepare(); \
232 osmo_timers_update(); \
233} while (0)
234
235extern const struct timeval fake_time_start_time;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200236
237#define ASSERT_RELEASE_CLEAR(via_ran) \
238 switch (via_ran) { \
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100239 case OSMO_RAT_GERAN_A: \
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200240 VERBOSE_ASSERT(bssap_clear_sent, == true, "%d"); \
241 break; \
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100242 case OSMO_RAT_UTRAN_IU: \
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200243 VERBOSE_ASSERT(iu_release_sent, == true, "%d"); \
244 break; \
245 default: \
246 OSMO_ASSERT(false); \
247 break; \
248 }
249
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100250#define HLR_TO_VLR "0a0101"
251#define VLR_TO_HLR "0a0101"
252#define EUSE_TO_MSC_USSD "0a0103"
253#define MSC_USSD_TO_EUSE "0a0103"