Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 1 | module Asterisk_Tests { |
| 2 | |
| 3 | /* Asterisk test suite in TTCN-3 |
| 4 | * (C) 2024 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * All rights reserved. |
| 6 | * Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 7 | * |
| 8 | * Released under the terms of GNU General Public License, Version 2 or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 12 | */ |
| 13 | |
Pau Espin Pedrol | 05eaa1a | 2024-04-02 12:56:26 +0200 | [diff] [blame] | 14 | import from TCCOpenSecurity_Functions all; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 15 | import from General_Types all; |
| 16 | import from Osmocom_Types all; |
| 17 | import from Native_Functions all; |
| 18 | import from Misc_Helpers all; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 19 | import from TELNETasp_PortType all; |
| 20 | import from AMI_Functions all; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 21 | |
| 22 | import from SDP_Types all; |
| 23 | import from SDP_Templates all; |
| 24 | |
| 25 | import from SIP_Emulation all; |
| 26 | import from SIPmsg_Types all; |
| 27 | import from SIP_Templates all; |
| 28 | |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 29 | import from SIP_ConnectionHandler all; |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 30 | import from IMS_ConnectionHandler all; |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 31 | |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 32 | modulepar { |
| 33 | charstring mp_local_sip_host := "127.0.0.2"; |
| 34 | integer mp_local_sip_port := 5060; |
| 35 | charstring mp_remote_sip_host := "127.0.0.1"; |
| 36 | integer mp_remote_sip_port := 5060; |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 37 | |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 38 | charstring mp_local_ims_host := "127.0.0.3"; |
| 39 | integer mp_local_ims_port := 5060; |
| 40 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 41 | /* Asterisk AMI: */ |
| 42 | charstring mp_ami_user := "test_user"; |
| 43 | charstring mp_ami_secret := "1234"; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type component test_CT { |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 47 | /* Manages all local VoIP users Asterisk is serving: */ |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 48 | var SIP_Emulation_CT vc_SIP; |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 49 | /* Manages the IMS server Asterisk connects to: */ |
| 50 | var SIP_Emulation_CT vc_IMS; |
| 51 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 52 | port TELNETasp_PT AMI; |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 53 | |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 54 | port Coord_PT COORD; |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 55 | port IMSCoord_PT IMS_COORD; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 58 | const charstring broadcast_sip_extension := "0500"; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 59 | |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 60 | function f_init_ConnHdlrPars(integer idx := 1) runs on test_CT return SIPConnHdlrPars { |
| 61 | var template (value) CallPars cp := t_CallPars(mp_local_sip_host, 1234 + 2*idx); |
| 62 | var template (value) SIPConnHdlrPars pars := t_Pars(mp_local_sip_host, |
| 63 | mp_local_sip_port, |
| 64 | mp_remote_sip_host, |
| 65 | mp_remote_sip_port, |
| 66 | "0" & int2str(str2int(broadcast_sip_extension) + idx), |
| 67 | cp := cp); |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 68 | return valueof(pars); |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 71 | /* Initialize connection towards Asterisk AMI */ |
| 72 | private function f_init_ami() runs on test_CT { |
| 73 | map(self:AMI, system:AMI); |
| 74 | f_ami_action_login(AMI, mp_ami_user, mp_ami_secret); |
| 75 | } |
| 76 | |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 77 | /* Local SIP UAs */ |
| 78 | private function f_init_sip_local() runs on test_CT { |
| 79 | var charstring id := "Asterisk_Tests_LOCAL_SIP_EMU"; |
| 80 | f_init_sip(vc_SIP, id); |
| 81 | } |
| 82 | |
| 83 | /* IMS Server connection */ |
| 84 | private function f_init_sip_ims() runs on test_CT { |
| 85 | var charstring id := "Asterisk_Tests_IMS_SIP_EMU"; |
| 86 | f_init_sip(vc_IMS, id); |
| 87 | } |
| 88 | |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 89 | function f_init() runs on test_CT { |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 90 | var charstring id; |
| 91 | |
Pau Espin Pedrol | 54b614a | 2024-04-17 18:58:36 +0200 | [diff] [blame] | 92 | f_init_ami(); |
Pau Espin Pedrol | ac8a054 | 2024-04-19 17:30:57 +0200 | [diff] [blame] | 93 | f_init_sip_local(); |
| 94 | f_init_sip_ims(); |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 95 | log("end of f_init"); |
| 96 | } |
| 97 | |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 98 | function f_start_handler(void_fn fn, SIPConnHdlrPars pars) |
| 99 | runs on test_CT return SIPConnHdlr { |
| 100 | var SIPConnHdlr vc_conn; |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 101 | var charstring id := testcasename() & "-ConnHdlr-" & pars.user; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 102 | |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 103 | vc_conn := SIPConnHdlr.create(id) alive; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 104 | |
| 105 | connect(vc_conn:SIP, vc_SIP:CLIENT); |
| 106 | connect(vc_conn:SIP_PROC, vc_SIP:CLIENT_PROC); |
| 107 | |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 108 | connect(vc_conn:COORD, self:COORD); |
| 109 | |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 110 | vc_conn.start(f_handler_init(fn, id, pars)); |
| 111 | return vc_conn; |
| 112 | } |
| 113 | |
Pau Espin Pedrol | 05eaa1a | 2024-04-02 12:56:26 +0200 | [diff] [blame] | 114 | /* Test SIP registration of local clients */ |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 115 | private function f_TC_internal_registration(charstring id) runs on SIPConnHdlr { |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 116 | |
| 117 | f_SIP_register(); |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 118 | // f_SIP_deregister(); |
| 119 | setverdict(pass); |
| 120 | } |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 121 | testcase TC_internal_registration() runs on test_CT { |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 122 | var SIPConnHdlrPars pars; |
| 123 | var SIPConnHdlr vc_conn; |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 124 | f_init(); |
| 125 | pars := f_init_ConnHdlrPars(); |
| 126 | vc_conn := f_start_handler(refers(f_TC_internal_registration), pars); |
| 127 | vc_conn.done; |
| 128 | } |
| 129 | |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 130 | /* Successful SIP MO-MT Call between local clients: */ |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 131 | private function f_TC_internal_call_mo(charstring id) runs on SIPConnHdlr { |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 132 | |
| 133 | f_SIP_register(); |
| 134 | COORD.send(COORD_CMD_REGISTERED); |
| 135 | |
| 136 | COORD.receive(COORD_CMD_START); |
| 137 | f_SIP_mo_call_setup(); |
| 138 | COORD.send(COORD_CMD_CALL_ESTABLISHED); |
| 139 | |
| 140 | COORD.receive(COORD_CMD_HANGUP); |
| 141 | f_SIP_do_call_hangup(); |
| 142 | |
| 143 | setverdict(pass); |
| 144 | } |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 145 | private function f_TC_internal_call_mt(charstring id) runs on SIPConnHdlr { |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 146 | |
| 147 | f_create_sip_expect(valueof(ts_SipUrl_from_Addr_Union(g_pars.cp.called.addr))); |
| 148 | |
| 149 | f_SIP_register(); |
| 150 | COORD.send(COORD_CMD_REGISTERED); |
| 151 | |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 152 | if (g_pars.cp.mt.exp_cancel) { |
| 153 | as_SIP_mt_call_cancelled(); |
| 154 | COORD.send(COORD_CMD_CALL_CANCELLED); |
| 155 | setverdict(pass); |
| 156 | return; |
| 157 | } |
| 158 | |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 159 | as_SIP_mt_call_accept(); |
| 160 | COORD.send(COORD_CMD_CALL_ESTABLISHED); |
| 161 | |
| 162 | /* Once MO hangs up, Asterisk updates us to point RTP to it: */ |
| 163 | as_SIP_exp_call_update(g_pars.cp.sip_seq_nr + 1); |
| 164 | as_SIP_exp_call_hangup(g_pars.cp.sip_seq_nr + 1); |
| 165 | |
| 166 | setverdict(pass); |
| 167 | } |
| 168 | testcase TC_internal_call_momt() runs on test_CT { |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 169 | var SIPConnHdlrPars pars[2]; |
| 170 | var SIPConnHdlr vc_conn[2]; |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 171 | |
| 172 | f_init(); |
| 173 | |
| 174 | pars[0] := f_init_ConnHdlrPars(idx := 1); |
| 175 | pars[1] := f_init_ConnHdlrPars(idx := 2); |
| 176 | |
| 177 | pars[0].cp.calling := pars[0].registrar_sip_record; |
| 178 | pars[0].cp.called := pars[1].registrar_sip_record; |
| 179 | |
| 180 | pars[1].cp.calling := pars[0].registrar_sip_record; |
| 181 | pars[1].cp.called := pars[1].local_sip_record; |
| 182 | |
| 183 | vc_conn[0] := f_start_handler(refers(f_TC_internal_call_mo), pars[0]); |
| 184 | vc_conn[1] := f_start_handler(refers(f_TC_internal_call_mt), pars[1]); |
| 185 | |
| 186 | interleave { |
| 187 | [] COORD.receive(COORD_CMD_REGISTERED) from vc_conn[0]; |
| 188 | [] COORD.receive(COORD_CMD_REGISTERED) from vc_conn[1]; |
| 189 | } |
| 190 | |
| 191 | COORD.send(COORD_CMD_START) to vc_conn[0]; |
| 192 | |
| 193 | interleave { |
| 194 | [] COORD.receive(COORD_CMD_CALL_ESTABLISHED) from vc_conn[0]; |
| 195 | [] COORD.receive(COORD_CMD_CALL_ESTABLISHED) from vc_conn[1]; |
| 196 | } |
| 197 | |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 198 | /* Call on-going */ |
| 199 | f_sleep(1.0); |
| 200 | |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 201 | COORD.send(COORD_CMD_HANGUP) to vc_conn[0]; |
| 202 | |
| 203 | |
| 204 | vc_conn[0].done; |
| 205 | vc_conn[1].done; |
| 206 | } |
| 207 | |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 208 | /* One of the users calls (INVITE) shared extension, which makes all other user |
| 209 | * equipments ring (INVITE). The first one to pick up the call (OK 200) gets the |
| 210 | * call established (ACK), others get a CANCEL event. */ |
| 211 | private function TC_internal_call_all_Nregistered(integer num_conns := 2) runs on test_CT { |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 212 | var SIPConnHdlrList vc_conn_list := {}; |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 213 | const integer vc_conn_mo_idx := 0; /* Index of MO leg in vc_conn_list */ |
| 214 | const integer vc_conn_mt_idx := 1; /* Index of MT leg in vc_conn_list, peer picking up first the call */ |
| 215 | var SipAddr broadcast_sip_record; |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 216 | var SIPConnHdlrPars pars_mo; |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 217 | |
| 218 | f_init(); |
| 219 | |
| 220 | broadcast_sip_record := valueof(ts_SipAddr(ts_HostPort(mp_local_sip_host), |
| 221 | ts_UserInfo(broadcast_sip_extension))); |
| 222 | |
| 223 | for (var integer i := 0; i < num_conns; i := i + 1) { |
Pau Espin Pedrol | caf028d | 2024-04-18 13:47:07 +0200 | [diff] [blame] | 224 | var SIPConnHdlrPars pars; |
| 225 | var SIPConnHdlr vc_conn; |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 226 | pars := f_init_ConnHdlrPars(idx := i + 1); |
| 227 | if (i == vc_conn_mo_idx) { /* MO */ |
| 228 | pars.cp.calling := pars.registrar_sip_record; |
| 229 | pars.cp.called := broadcast_sip_record; |
| 230 | vc_conn := f_start_handler(refers(f_TC_internal_call_mo), pars); |
| 231 | pars_mo := pars; |
| 232 | } else { /* MT */ |
| 233 | pars.cp.calling := pars_mo.registrar_sip_record; |
| 234 | pars.cp.called := pars.local_sip_record; |
| 235 | pars.cp.mt.wait_coord_cmd_pickup := true; |
| 236 | if (i != vc_conn_mt_idx) { |
| 237 | /* Only first MT picking up (OK 200 INVITE) will be ACKed, others CANCELed: */ |
| 238 | pars.cp.mt.exp_cancel := true; |
| 239 | } |
| 240 | vc_conn := f_start_handler(refers(f_TC_internal_call_mt), pars); |
| 241 | } |
| 242 | vc_conn_list := vc_conn_list & { vc_conn }; |
| 243 | } |
| 244 | |
| 245 | /* Wait all users are registered: */ |
| 246 | for (var integer i := 0; i < num_conns; i := i + 1) { |
| 247 | /* Note: "from vc_conn_list[i]" can't be used since they may arrive from components in any order: */ |
| 248 | COORD.receive(COORD_CMD_REGISTERED); |
| 249 | } |
| 250 | |
| 251 | /* Ask MO user to start the call: */ |
| 252 | COORD.send(COORD_CMD_START) to vc_conn_list[vc_conn_mo_idx]; |
| 253 | |
| 254 | /* Make sure the desired MT is the one picking up first the call: */ |
| 255 | COORD.send(COORD_CMD_PICKUP) to vc_conn_list[vc_conn_mt_idx]; |
| 256 | interleave { |
| 257 | [] COORD.receive(COORD_CMD_CALL_ESTABLISHED) from vc_conn_list[vc_conn_mo_idx]; |
| 258 | [] COORD.receive(COORD_CMD_CALL_ESTABLISHED) from vc_conn_list[vc_conn_mt_idx]; |
| 259 | } |
| 260 | |
| 261 | /* Pick up from other phone calls and expect CANCEL: */ |
| 262 | for (var integer i := 0; i < num_conns; i := i + 1) { |
| 263 | if (i != vc_conn_mo_idx and i != vc_conn_mt_idx) { |
| 264 | COORD.send(COORD_CMD_PICKUP) to vc_conn_list[i]; |
| 265 | COORD.receive(COORD_CMD_CALL_CANCELLED) from vc_conn_list[i]; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* Call on-going */ |
| 270 | f_sleep(1.0); |
| 271 | |
| 272 | COORD.send(COORD_CMD_HANGUP) to vc_conn_list[vc_conn_mo_idx]; |
| 273 | |
| 274 | for (var integer i := 0; i < num_conns; i := i + 1) { |
| 275 | vc_conn_list[i].done; |
| 276 | } |
| 277 | } |
| 278 | testcase TC_internal_call_all_2registered() runs on test_CT { |
| 279 | TC_internal_call_all_Nregistered(2); |
| 280 | } |
| 281 | testcase TC_internal_call_all_3registered() runs on test_CT { |
| 282 | TC_internal_call_all_Nregistered(3); |
| 283 | } |
| 284 | testcase TC_internal_call_all_4registered() runs on test_CT { |
| 285 | TC_internal_call_all_Nregistered(4); |
| 286 | } |
| 287 | |
Pau Espin Pedrol | 05eaa1a | 2024-04-02 12:56:26 +0200 | [diff] [blame] | 288 | testcase TC_selftest() runs on test_CT { |
| 289 | f_sip_digest_selftest(); |
| 290 | setverdict(pass); |
| 291 | } |
| 292 | |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 293 | control { |
| 294 | execute( TC_internal_registration() ); |
Pau Espin Pedrol | 7011bf4 | 2024-04-08 17:56:58 +0200 | [diff] [blame] | 295 | execute( TC_internal_call_momt() ); |
Pau Espin Pedrol | 32167d8 | 2024-04-10 13:14:51 +0200 | [diff] [blame] | 296 | execute( TC_internal_call_all_2registered() ); |
| 297 | execute( TC_internal_call_all_3registered() ); |
| 298 | execute( TC_internal_call_all_4registered() ); |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | } |