Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1 | /* Osmocom MSC+VLR end-to-end tests */ |
| 2 | |
| 3 | /* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #pragma once |
| 25 | |
| 26 | #include <stdbool.h> |
| 27 | #include <stdio.h> |
| 28 | |
Alexander Couzens | 8b7d785 | 2021-11-05 01:52:05 +0100 | [diff] [blame] | 29 | #include <osmocom/crypt/utran_cipher.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 30 | #include <osmocom/msc/gsm_data.h> |
Alexander Couzens | eff28ab | 2024-09-12 00:55:25 +0200 | [diff] [blame^] | 31 | #include <osmocom/vlr/vlr.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 32 | #include <osmocom/msc/msub.h> |
| 33 | #include <osmocom/msc/msc_a.h> |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 34 | #include <osmocom/msc/mncc.h> |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 35 | #include <osmocom/msc/call_leg.h> |
| 36 | #include <osmocom/msc/rtp_stream.h> |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 37 | |
| 38 | extern bool _log_lines; |
Neels Hofmeyr | 35fb679 | 2019-10-21 06:06:27 +0200 | [diff] [blame] | 39 | #define LOG_COLOR "\033[1;33m" |
| 40 | #define LOG_COLOR_OFF "\033[0;m" |
| 41 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 42 | #define _log(fmt, args...) do { \ |
| 43 | if (_log_lines) \ |
Neels Hofmeyr | 35fb679 | 2019-10-21 06:06:27 +0200 | [diff] [blame] | 44 | fprintf(stderr, LOG_COLOR " %4d:%s: " fmt LOG_COLOR_OFF "\n", \ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 45 | __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 Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 52 | #define BTW(fmt, args...) _log("\n\n- " fmt, ## args ) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 53 | #define btw(fmt, args...) _log("- " fmt, ## args ) |
| 54 | #define log(fmt, args...) _log(" " fmt, ## args ) |
| 55 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 56 | #define comment_start() fprintf(stderr, "===== %s\n", __func__); |
| 57 | #define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 58 | |
Neels Hofmeyr | 3c323dc | 2022-01-14 03:35:08 +0100 | [diff] [blame] | 59 | extern void *msc_vlr_tests_ctx; |
| 60 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 61 | extern struct msub *g_msub; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 62 | extern struct gsm_network *net; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 63 | extern void *msgb_ctx; |
| 64 | |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 65 | extern enum osmo_rat_type rx_from_ran; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 66 | |
| 67 | extern const char *gsup_tx_expected; |
| 68 | extern bool gsup_tx_confirmed; |
| 69 | |
| 70 | extern struct msgb *dtap_tx_expected; |
| 71 | extern bool dtap_tx_confirmed; |
| 72 | |
| 73 | enum result_sent { |
| 74 | RES_NONE = 0, |
| 75 | RES_ACCEPT = 1, |
| 76 | RES_REJECT = 2, |
| 77 | }; |
| 78 | extern enum result_sent lu_result_sent; |
| 79 | extern enum result_sent cm_service_result_sent; |
| 80 | |
| 81 | extern bool auth_request_sent; |
| 82 | extern const char *auth_request_expect_rand; |
| 83 | extern const char *auth_request_expect_autn; |
| 84 | |
| 85 | extern bool cipher_mode_cmd_sent; |
| 86 | extern bool cipher_mode_cmd_sent_with_imeisv; |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 87 | extern const char *cipher_mode_expect_kc; |
| 88 | |
| 89 | extern bool security_mode_ctrl_sent; |
| 90 | extern const char *security_mode_expect_ck; |
| 91 | extern const char *security_mode_expect_ik; |
| 92 | |
| 93 | static inline void expect_cipher_mode_cmd(const char *kc) |
| 94 | { |
| 95 | cipher_mode_cmd_sent = false; |
| 96 | cipher_mode_expect_kc = kc; |
| 97 | /* make sure we don't mix up the two */ |
| 98 | security_mode_ctrl_sent = false; |
| 99 | } |
| 100 | |
| 101 | static inline void expect_security_mode_ctrl(const char *ck, const char *ik) |
| 102 | { |
| 103 | security_mode_ctrl_sent = false; |
| 104 | security_mode_expect_ck = ck; |
| 105 | security_mode_expect_ik = ik; |
| 106 | /* make sure we don't mix up the two */ |
| 107 | cipher_mode_cmd_sent = false; |
| 108 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 109 | |
| 110 | extern bool paging_sent; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 111 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 112 | extern bool iu_release_expected; |
| 113 | extern bool iu_release_sent; |
| 114 | extern bool bssap_clear_expected; |
| 115 | extern bool bssap_clear_sent; |
| 116 | |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 117 | extern uint32_t cc_to_mncc_tx_expected_msg_type; |
| 118 | extern const char *cc_to_mncc_tx_expected_imsi; |
| 119 | extern bool cc_to_mncc_tx_confirmed; |
| 120 | extern uint32_t cc_to_mncc_tx_got_callref; |
Neels Hofmeyr | 3c323dc | 2022-01-14 03:35:08 +0100 | [diff] [blame] | 121 | extern char cc_to_mncc_tx_last_sdp[1024]; |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 122 | |
Neels Hofmeyr | cbcf89c | 2018-03-13 17:52:07 +0100 | [diff] [blame] | 123 | extern struct gsm_mncc *on_call_release_mncc_sends_to_cc_data; |
| 124 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 125 | static inline void expect_iu_release() |
| 126 | { |
| 127 | iu_release_expected = true; |
| 128 | iu_release_sent = false; |
| 129 | } |
| 130 | |
| 131 | static inline void expect_bssap_clear() |
| 132 | { |
| 133 | bssap_clear_expected = true; |
| 134 | bssap_clear_sent = false; |
| 135 | } |
| 136 | |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 137 | static inline void expect_release_clear(enum osmo_rat_type via_ran) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 138 | { |
| 139 | switch (via_ran) { |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 140 | case OSMO_RAT_GERAN_A: |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 141 | expect_bssap_clear(); |
| 142 | return; |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 143 | case OSMO_RAT_UTRAN_IU: |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 144 | expect_iu_release(); |
| 145 | return; |
| 146 | default: |
| 147 | OSMO_ASSERT(false); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 152 | extern bool bssap_assignment_expected; |
| 153 | extern bool bssap_assignment_sent; |
Neels Hofmeyr | 3c323dc | 2022-01-14 03:35:08 +0100 | [diff] [blame] | 154 | extern struct gsm0808_channel_type bssap_assignment_command_last_channel_type; |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 155 | extern bool iu_rab_assignment_expected; |
| 156 | extern bool iu_rab_assignment_sent; |
| 157 | |
| 158 | static inline void expect_bssap_assignment() |
| 159 | { |
| 160 | bssap_assignment_expected = true; |
| 161 | bssap_assignment_sent = false; |
| 162 | } |
| 163 | |
| 164 | static inline void expect_iu_rab_assignment() |
| 165 | { |
| 166 | iu_rab_assignment_expected = true; |
| 167 | iu_rab_assignment_sent = false; |
| 168 | } |
| 169 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 170 | struct msc_vlr_test_cmdline_opts { |
| 171 | bool verbose; |
| 172 | int run_test_nr; |
| 173 | }; |
| 174 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 175 | typedef void (* msc_vlr_test_func_t )(void); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 176 | extern msc_vlr_test_func_t msc_vlr_tests[]; |
| 177 | |
| 178 | struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex); |
| 179 | |
| 180 | void clear_vlr(); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 181 | bool conn_exists(const struct msub *msub); |
| 182 | void conn_conclude_cm_service_req(struct msub *msub, const char *cm_service_use); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 183 | |
| 184 | void dtap_expect_tx(const char *hex); |
| 185 | void dtap_expect_tx_ussd(char *ussd_text); |
| 186 | void paging_expect_imsi(const char *imsi); |
| 187 | void paging_expect_tmsi(uint32_t tmsi); |
| 188 | |
| 189 | void ms_sends_msg(const char *hex); |
Neels Hofmeyr | 3c323dc | 2022-01-14 03:35:08 +0100 | [diff] [blame] | 190 | void ms_sends_msgf(const char *fmt, ...); |
| 191 | void ms_sends_compl_l3(const char *hex, const struct gsm0808_speech_codec_list *codec_list_bss_supported); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 192 | void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark); |
| 193 | void ms_sends_ciphering_mode_complete(const char *inner_nas_msg); |
Alexander Couzens | 2aaff75 | 2021-10-19 17:09:11 +0200 | [diff] [blame] | 194 | void ms_sends_security_mode_complete(uint8_t utran_encryption); |
Neels Hofmeyr | cec51b3 | 2023-03-01 03:47:45 +0100 | [diff] [blame] | 195 | void ms_sends_assignment_complete(const char *sdp_codec_name); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 196 | void gsup_rx(const char *rx_hex, const char *expect_tx_hex); |
| 197 | void send_sms(struct vlr_subscr *receiver, |
| 198 | struct vlr_subscr *sender, |
| 199 | char *str); |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 200 | void crcx_ok(enum rtp_direction dir); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 201 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 202 | void ran_sends_clear_complete(); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 203 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 204 | void thwart_rx_non_initial_requests(); |
| 205 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 206 | #define EXPECT_ACCEPTED(expect_accepted) do { \ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 207 | if (g_msub) \ |
| 208 | OSMO_ASSERT(conn_exists(g_msub)); \ |
| 209 | bool accepted = msc_a_is_accepted(msub_msc_a(g_msub)); \ |
| 210 | fprintf(stderr, "msc_a_is_accepted() == %s\n", \ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 211 | accepted ? "true" : "false"); \ |
| 212 | OSMO_ASSERT(accepted == expect_accepted); \ |
| 213 | } while (false) |
| 214 | |
Max | bb53a3f | 2018-12-17 17:09:17 +0100 | [diff] [blame] | 215 | #define VAL_ASSERT(desc, val, expect_op, fmt) \ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 216 | do { \ |
Max | bb53a3f | 2018-12-17 17:09:17 +0100 | [diff] [blame] | 217 | log(desc " == " fmt, (val)); \ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 218 | OSMO_ASSERT((val) expect_op); \ |
Max | bb53a3f | 2018-12-17 17:09:17 +0100 | [diff] [blame] | 219 | } while (0) |
| 220 | |
| 221 | #define VERBOSE_ASSERT(val, expect_op, fmt) VAL_ASSERT(#val, val, expect_op, fmt) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 222 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 223 | #define EXPECT_CONN_COUNT(N) VERBOSE_ASSERT(llist_count(&msub_list), == N, "%d") |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 224 | |
| 225 | #define gsup_expect_tx(hex) do \ |
| 226 | { \ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 227 | if (gsup_tx_expected) { \ |
| 228 | log("Previous expected GSUP tx was not confirmed!"); \ |
| 229 | OSMO_ASSERT(!gsup_tx_expected); \ |
| 230 | } \ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 231 | if (!hex) \ |
| 232 | break; \ |
| 233 | gsup_tx_expected = hex; \ |
| 234 | gsup_tx_confirmed = false; \ |
| 235 | } while (0) |
| 236 | |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 237 | #define cc_to_mncc_expect_tx(imsi, msg_type) do \ |
| 238 | { \ |
| 239 | if (cc_to_mncc_tx_expected_msg_type) { \ |
| 240 | log("Previous expected MNCC tx was not confirmed!"); \ |
| 241 | OSMO_ASSERT(!cc_to_mncc_tx_expected_msg_type); \ |
| 242 | } \ |
| 243 | cc_to_mncc_tx_expected_imsi = imsi; \ |
| 244 | cc_to_mncc_tx_expected_msg_type = msg_type; \ |
| 245 | cc_to_mncc_tx_confirmed = false; \ |
| 246 | } while (0) |
| 247 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 248 | void fake_time_start(); |
| 249 | |
| 250 | /* as macro to get the test file's source line number */ |
| 251 | #define fake_time_passes(secs, usecs) do \ |
| 252 | { \ |
| 253 | struct timeval diff; \ |
| 254 | osmo_gettimeofday_override_add(secs, usecs); \ |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 255 | osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 256 | timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \ |
| 257 | btw("Total time passed: %d.%06d s", \ |
| 258 | (int)diff.tv_sec, (int)diff.tv_usec); \ |
| 259 | osmo_timers_prepare(); \ |
| 260 | osmo_timers_update(); \ |
| 261 | } while (0) |
| 262 | |
| 263 | extern const struct timeval fake_time_start_time; |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 264 | |
| 265 | #define ASSERT_RELEASE_CLEAR(via_ran) \ |
| 266 | switch (via_ran) { \ |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 267 | case OSMO_RAT_GERAN_A: \ |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 268 | VERBOSE_ASSERT(bssap_clear_sent, == true, "%d"); \ |
| 269 | break; \ |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 270 | case OSMO_RAT_UTRAN_IU: \ |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 271 | VERBOSE_ASSERT(iu_release_sent, == true, "%d"); \ |
| 272 | break; \ |
| 273 | default: \ |
| 274 | OSMO_ASSERT(false); \ |
| 275 | break; \ |
| 276 | } |
| 277 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 278 | #define HLR_TO_VLR "0a0101" |
| 279 | #define VLR_TO_HLR "0a0101" |
Neels Hofmeyr | 63b2464 | 2019-12-12 01:31:04 +0100 | [diff] [blame] | 280 | #define CN_DOMAIN "280102" |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 281 | #define EUSE_TO_MSC_USSD "0a0103" |
| 282 | #define MSC_USSD_TO_EUSE "0a0103" |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 283 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 284 | void expect_crcx(enum rtp_direction towards); |
Neels Hofmeyr | 81938fd | 2022-01-14 03:39:58 +0100 | [diff] [blame] | 285 | bool crcx_scheduled(enum rtp_direction towards); |