blob: 1f83013b0113a41030b8700b62ef70c4ff9f2aae [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
Alexander Couzens8b7d7852021-11-05 01:52:05 +010029#include <osmocom/crypt/utran_cipher.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/gsm_data.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020031#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010032#include <osmocom/msc/msub.h>
33#include <osmocom/msc/msc_a.h>
Neels Hofmeyra99b4272017-11-21 17:13:23 +010034#include <osmocom/msc/mncc.h>
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +020035#include <osmocom/msc/call_leg.h>
36#include <osmocom/msc/rtp_stream.h>
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010037
38extern bool _log_lines;
Neels Hofmeyr35fb6792019-10-21 06:06:27 +020039#define LOG_COLOR "\033[1;33m"
40#define LOG_COLOR_OFF "\033[0;m"
41
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010042#define _log(fmt, args...) do { \
43 if (_log_lines) \
Neels Hofmeyr35fb6792019-10-21 06:06:27 +020044 fprintf(stderr, LOG_COLOR " %4d:%s: " fmt LOG_COLOR_OFF "\n", \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010045 __LINE__, __FILE__, ## args ); \
46 else \
47 fprintf(stderr, fmt "\n", ## args ); \
48 } while (false)
49
50/* btw means "by the way", the test tells the log what's happening.
51 * BTW() marks a larger section, btw() is the usual logging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010052#define BTW(fmt, args...) _log("\n\n- " fmt, ## args )
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010053#define btw(fmt, args...) _log("- " fmt, ## args )
54#define log(fmt, args...) _log(" " fmt, ## args )
55
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +010056#define comment_start() fprintf(stderr, "===== %s\n", __func__);
57#define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010058
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010059extern struct msub *g_msub;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010060extern struct gsm_network *net;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010061extern void *msgb_ctx;
62
Neels Hofmeyr7814a832018-12-26 00:40:18 +010063extern enum osmo_rat_type rx_from_ran;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010064
65extern const char *gsup_tx_expected;
66extern bool gsup_tx_confirmed;
67
68extern struct msgb *dtap_tx_expected;
69extern bool dtap_tx_confirmed;
70
71enum result_sent {
72 RES_NONE = 0,
73 RES_ACCEPT = 1,
74 RES_REJECT = 2,
75};
76extern enum result_sent lu_result_sent;
77extern enum result_sent cm_service_result_sent;
78
79extern bool auth_request_sent;
80extern const char *auth_request_expect_rand;
81extern const char *auth_request_expect_autn;
82
83extern bool cipher_mode_cmd_sent;
84extern bool cipher_mode_cmd_sent_with_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +010085extern const char *cipher_mode_expect_kc;
86
87extern bool security_mode_ctrl_sent;
88extern const char *security_mode_expect_ck;
89extern const char *security_mode_expect_ik;
90
91static inline void expect_cipher_mode_cmd(const char *kc)
92{
93 cipher_mode_cmd_sent = false;
94 cipher_mode_expect_kc = kc;
95 /* make sure we don't mix up the two */
96 security_mode_ctrl_sent = false;
97}
98
99static inline void expect_security_mode_ctrl(const char *ck, const char *ik)
100{
101 security_mode_ctrl_sent = false;
102 security_mode_expect_ck = ck;
103 security_mode_expect_ik = ik;
104 /* make sure we don't mix up the two */
105 cipher_mode_cmd_sent = false;
106}
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100107
108extern bool paging_sent;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100109
Philipp Maierfbf66102017-04-09 12:32:51 +0200110extern bool iu_release_expected;
111extern bool iu_release_sent;
112extern bool bssap_clear_expected;
113extern bool bssap_clear_sent;
114
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100115extern uint32_t cc_to_mncc_tx_expected_msg_type;
116extern const char *cc_to_mncc_tx_expected_imsi;
117extern bool cc_to_mncc_tx_confirmed;
118extern uint32_t cc_to_mncc_tx_got_callref;
119
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100120extern struct gsm_mncc *on_call_release_mncc_sends_to_cc_data;
121
Philipp Maierfbf66102017-04-09 12:32:51 +0200122static inline void expect_iu_release()
123{
124 iu_release_expected = true;
125 iu_release_sent = false;
126}
127
128static inline void expect_bssap_clear()
129{
130 bssap_clear_expected = true;
131 bssap_clear_sent = false;
132}
133
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100134static inline void expect_release_clear(enum osmo_rat_type via_ran)
Philipp Maierfbf66102017-04-09 12:32:51 +0200135{
136 switch (via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100137 case OSMO_RAT_GERAN_A:
Philipp Maierfbf66102017-04-09 12:32:51 +0200138 expect_bssap_clear();
139 return;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100140 case OSMO_RAT_UTRAN_IU:
Philipp Maierfbf66102017-04-09 12:32:51 +0200141 expect_iu_release();
142 return;
143 default:
144 OSMO_ASSERT(false);
145 break;
146 }
147}
148
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200149extern bool bssap_assignment_expected;
150extern bool bssap_assignment_sent;
151extern bool iu_rab_assignment_expected;
152extern bool iu_rab_assignment_sent;
153
154static inline void expect_bssap_assignment()
155{
156 bssap_assignment_expected = true;
157 bssap_assignment_sent = false;
158}
159
160static inline void expect_iu_rab_assignment()
161{
162 iu_rab_assignment_expected = true;
163 iu_rab_assignment_sent = false;
164}
165
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100166struct msc_vlr_test_cmdline_opts {
167 bool verbose;
168 int run_test_nr;
169};
170
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100171typedef void (* msc_vlr_test_func_t )(void);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100172extern msc_vlr_test_func_t msc_vlr_tests[];
173
174struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex);
175
176void clear_vlr();
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100177bool conn_exists(const struct msub *msub);
178void conn_conclude_cm_service_req(struct msub *msub, const char *cm_service_use);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100179
180void dtap_expect_tx(const char *hex);
181void dtap_expect_tx_ussd(char *ussd_text);
182void paging_expect_imsi(const char *imsi);
183void paging_expect_tmsi(uint32_t tmsi);
184
185void ms_sends_msg(const char *hex);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100186void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark);
187void ms_sends_ciphering_mode_complete(const char *inner_nas_msg);
Alexander Couzens2aaff752021-10-19 17:09:11 +0200188void ms_sends_security_mode_complete(uint8_t utran_encryption);
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200189void ms_sends_assignment_complete(enum mgcp_codecs assigned_codec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100190void gsup_rx(const char *rx_hex, const char *expect_tx_hex);
191void send_sms(struct vlr_subscr *receiver,
192 struct vlr_subscr *sender,
193 char *str);
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200194void crcx_ok(enum rtp_direction dir);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100195
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100196void ran_sends_clear_complete();
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200197
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100198void thwart_rx_non_initial_requests();
199
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100200#define EXPECT_ACCEPTED(expect_accepted) do { \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100201 if (g_msub) \
202 OSMO_ASSERT(conn_exists(g_msub)); \
203 bool accepted = msc_a_is_accepted(msub_msc_a(g_msub)); \
204 fprintf(stderr, "msc_a_is_accepted() == %s\n", \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100205 accepted ? "true" : "false"); \
206 OSMO_ASSERT(accepted == expect_accepted); \
207 } while (false)
208
Maxbb53a3f2018-12-17 17:09:17 +0100209#define VAL_ASSERT(desc, val, expect_op, fmt) \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100210 do { \
Maxbb53a3f2018-12-17 17:09:17 +0100211 log(desc " == " fmt, (val)); \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100212 OSMO_ASSERT((val) expect_op); \
Maxbb53a3f2018-12-17 17:09:17 +0100213 } while (0)
214
215#define VERBOSE_ASSERT(val, expect_op, fmt) VAL_ASSERT(#val, val, expect_op, fmt)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100217#define EXPECT_CONN_COUNT(N) VERBOSE_ASSERT(llist_count(&msub_list), == N, "%d")
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100218
219#define gsup_expect_tx(hex) do \
220{ \
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200221 if (gsup_tx_expected) { \
222 log("Previous expected GSUP tx was not confirmed!"); \
223 OSMO_ASSERT(!gsup_tx_expected); \
224 } \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100225 if (!hex) \
226 break; \
227 gsup_tx_expected = hex; \
228 gsup_tx_confirmed = false; \
229} while (0)
230
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100231#define cc_to_mncc_expect_tx(imsi, msg_type) do \
232{ \
233 if (cc_to_mncc_tx_expected_msg_type) { \
234 log("Previous expected MNCC tx was not confirmed!"); \
235 OSMO_ASSERT(!cc_to_mncc_tx_expected_msg_type); \
236 } \
237 cc_to_mncc_tx_expected_imsi = imsi; \
238 cc_to_mncc_tx_expected_msg_type = msg_type; \
239 cc_to_mncc_tx_confirmed = false; \
240} while (0)
241
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100242void fake_time_start();
243
244/* as macro to get the test file's source line number */
245#define fake_time_passes(secs, usecs) do \
246{ \
247 struct timeval diff; \
248 osmo_gettimeofday_override_add(secs, usecs); \
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200249 osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100250 timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
251 btw("Total time passed: %d.%06d s", \
252 (int)diff.tv_sec, (int)diff.tv_usec); \
253 osmo_timers_prepare(); \
254 osmo_timers_update(); \
255} while (0)
256
257extern const struct timeval fake_time_start_time;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200258
259#define ASSERT_RELEASE_CLEAR(via_ran) \
260 switch (via_ran) { \
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100261 case OSMO_RAT_GERAN_A: \
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200262 VERBOSE_ASSERT(bssap_clear_sent, == true, "%d"); \
263 break; \
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100264 case OSMO_RAT_UTRAN_IU: \
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200265 VERBOSE_ASSERT(iu_release_sent, == true, "%d"); \
266 break; \
267 default: \
268 OSMO_ASSERT(false); \
269 break; \
270 }
271
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100272#define HLR_TO_VLR "0a0101"
273#define VLR_TO_HLR "0a0101"
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100274#define CN_DOMAIN "280102"
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100275#define EUSE_TO_MSC_USSD "0a0103"
276#define MSC_USSD_TO_EUSE "0a0103"
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200277
278extern bool got_crcx;
279void expect_crcx(enum rtp_direction towards);