Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 1 | module PGW_Tests { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from Native_Functions all; |
| 6 | |
| 7 | import from GTPv2_Types all; |
| 8 | import from GTPv2_Templates all; |
| 9 | import from GTPv2_Emulation all; |
| 10 | |
| 11 | import from UECUPS_Types all; |
| 12 | |
| 13 | import from DNS_Helpers all; |
| 14 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 15 | |
| 16 | import from DIAMETER_Types all; |
| 17 | import from DIAMETER_Templates all; |
| 18 | import from DIAMETER_Emulation all; |
| 19 | |
| 20 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 21 | modulepar { |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 22 | charstring mp_pgw_hostname := "127.0.0.4"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 23 | charstring mp_local_hostname_c := "127.0.0.1"; |
| 24 | charstring mp_local_hostname_u := "127.0.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 25 | |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 26 | charstring mp_run_prog_log_path := "/tmp"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 27 | charstring mp_run_prog_as_user := "laforge"; |
| 28 | charstring mp_ping_hostname := "10.45.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 29 | |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 30 | charstring mp_pcrf_local_ip := "127.0.0.9"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 31 | integer mp_pcrf_local_port := 3868; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* main component, we typically have one per testcase */ |
| 35 | type component PGW_Test_CT { |
| 36 | var GTPv2_Emulation_CT vc_GTP2; |
| 37 | port GTP2EM_PT TEID0; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 38 | |
| 39 | /* emulated PCRF */ |
| 40 | var DIAMETER_Emulation_CT vc_DIAMETER; |
| 41 | port DIAMETER_PT DIAMETER_UNIT; |
| 42 | port DIAMETEREM_PROC_PT DIAMETER_PROC; |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 43 | /* global test case guard timer (actual timeout value is set in f_init()) */ |
| 44 | timer T_guard; |
| 45 | } |
| 46 | |
| 47 | /* global altstep for global guard timer; */ |
| 48 | altstep as_Tguard() runs on PGW_Test_CT { |
| 49 | [] T_guard.timeout { |
| 50 | setverdict(fail, "Timeout of T_guard"); |
| 51 | mtc.stop; |
| 52 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* per-session component; we typically have 1..N per testcase */ |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 56 | type component PGW_Session_CT extends GTP2_ConnHdlr, DIAMETER_ConnHdlr { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 57 | var SessionPars g_pars; |
| 58 | |
| 59 | /* TEI (Data) local side */ |
| 60 | var OCT4 g_teid; |
| 61 | /* TEI (Control) local side */ |
| 62 | var OCT4 g_teic; |
| 63 | /* TEI (Data) remote side */ |
| 64 | var OCT4 g_teid_remote; |
| 65 | /* TEI (Control) remote side */ |
| 66 | var OCT4 g_teic_remote; |
| 67 | /* GTP-U IPv4 address remote sie */ |
| 68 | var OCT4 g_gtpu4_remote; |
| 69 | var OCT16 g_gtpu6_remote; |
| 70 | |
| 71 | /* Address allocation */ |
| 72 | var OCT4 g_ip4_addr; |
| 73 | var OCT16 g_ip6_addr; |
| 74 | var integer g_ip6_plen; |
| 75 | } |
| 76 | |
| 77 | /* configuration data for a given Session */ |
| 78 | type record SessionPars { |
| 79 | hexstring imsi, |
| 80 | octetstring msisdn optional, |
| 81 | // serving network |
| 82 | integer rat_type, |
| 83 | // flags? |
| 84 | charstring apn, |
| 85 | /* Apn subscribed or non-subscribed */ |
| 86 | boolean selection_mode, |
| 87 | BIT3 pdn_type, |
| 88 | /* PAA */ |
| 89 | /* Max APN Restriction */ |
| 90 | /* APN-AMBR */ |
| 91 | octetstring pco optional, |
| 92 | octetstring epco optional, |
| 93 | /* Bearer Contexts to be created */ |
| 94 | |
| 95 | charstring tun_dev_name, |
| 96 | charstring tun_netns_name optional |
| 97 | } |
| 98 | |
| 99 | template (value) SessionPars |
| 100 | t_SessionPars(hexstring imsi, charstring tundev, integer rat_type := 6, charstring apn := "internet", |
| 101 | boolean selection_mode := false, BIT3 pdn_type := '001'B) := { |
| 102 | imsi := imsi, |
| 103 | msisdn := omit, |
| 104 | rat_type := rat_type, |
| 105 | apn := apn, |
| 106 | selection_mode := selection_mode, |
| 107 | pdn_type := pdn_type, |
| 108 | pco := omit, |
| 109 | epco := omit, |
| 110 | tun_dev_name := tundev, |
| 111 | tun_netns_name := tundev |
| 112 | } |
| 113 | |
| 114 | type record BearerConfig { |
| 115 | integer eps_bearer_id |
| 116 | } |
| 117 | |
| 118 | type function void_fn() runs on PGW_Session_CT; |
| 119 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 120 | friend function DiameterForwardUnitdataCallback(PDU_DIAMETER msg) |
| 121 | runs on DIAMETER_Emulation_CT return template PDU_DIAMETER { |
| 122 | DIAMETER_UNIT.send(msg); |
| 123 | return omit; |
| 124 | } |
| 125 | |
| 126 | friend function f_init_diameter(charstring id) runs on PGW_Test_CT { |
| 127 | var DIAMETEROps ops := { |
| 128 | create_cb := refers(DIAMETER_Emulation.ExpectedCreateCallback), |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 129 | unitdata_cb := refers(DiameterForwardUnitdataCallback), |
| 130 | raw := false /* handler mode (IMSI based routing) */ |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 131 | }; |
| 132 | var DIAMETER_conn_parameters pars := { |
| 133 | remote_ip := mp_pgw_hostname, |
| 134 | remote_sctp_port := -1, |
| 135 | local_ip := mp_pcrf_local_ip, |
| 136 | local_sctp_port := mp_pcrf_local_port, |
| 137 | origin_host := "pcrf.localdomain", |
| 138 | origin_realm := "localdomain", |
Pau Espin Pedrol | 33b4749 | 2022-03-08 17:43:01 +0100 | [diff] [blame] | 139 | auth_app_id := omit, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 140 | vendor_app_id := c_DIAMETER_3GPP_Gx_AID |
| 141 | }; |
| 142 | vc_DIAMETER := DIAMETER_Emulation_CT.create(id); |
| 143 | map(vc_DIAMETER:DIAMETER, system:DIAMETER_CODEC_PT); |
| 144 | connect(vc_DIAMETER:DIAMETER_UNIT, self:DIAMETER_UNIT); |
| 145 | connect(vc_DIAMETER:DIAMETER_PROC, self:DIAMETER_PROC); |
| 146 | vc_DIAMETER.start(DIAMETER_Emulation.main(ops, pars, id)); |
| 147 | |
| 148 | f_diameter_wait_capability(DIAMETER_UNIT); |
| 149 | } |
| 150 | |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 151 | private function f_init(float guard_timeout := 60.0) runs on PGW_Test_CT { |
| 152 | T_guard.start(guard_timeout); |
| 153 | activate(as_Tguard()); |
| 154 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 155 | var Gtp2EmulationCfg cfg := { |
| 156 | gtpc_bind_ip := mp_local_hostname_c, |
| 157 | gtpc_bind_port := GTP2C_PORT, |
| 158 | gtpc_remote_ip := mp_pgw_hostname, |
| 159 | gtpc_remote_port := GTP2C_PORT, |
| 160 | sgw_role := true, |
| 161 | use_gtpu_daemon := true |
| 162 | }; |
| 163 | |
| 164 | vc_GTP2 := GTPv2_Emulation_CT.create("GTP2_EM"); |
| 165 | map(vc_GTP2:GTP2C, system:GTP2C); |
| 166 | connect(vc_GTP2:TEID0, self:TEID0); |
| 167 | vc_GTP2.start(GTPv2_Emulation.main(cfg)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 168 | |
| 169 | if (mp_pcrf_local_ip != "") { |
| 170 | f_init_diameter(testcasename()); |
| 171 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | function f_start_handler(void_fn fn, template (omit) SessionPars pars := omit) |
| 175 | runs on PGW_Test_CT return PGW_Session_CT { |
| 176 | var charstring id := testcasename(); |
| 177 | var PGW_Session_CT vc_conn; |
| 178 | vc_conn := PGW_Session_CT.create(id); |
| 179 | connect(vc_conn:GTP2, vc_GTP2:CLIENT); |
| 180 | connect(vc_conn:GTP2_PROC, vc_GTP2:CLIENT_PROC); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 181 | |
| 182 | if (isbound(vc_DIAMETER)) { |
| 183 | connect(vc_conn:DIAMETER, vc_DIAMETER:DIAMETER_CLIENT); |
| 184 | connect(vc_conn:DIAMETER_PROC, vc_DIAMETER:DIAMETER_PROC); |
| 185 | } |
| 186 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 187 | vc_conn.start(f_handler_init(fn, pars)); |
| 188 | return vc_conn; |
| 189 | } |
| 190 | |
| 191 | private function f_handler_init(void_fn fn, template (omit) SessionPars pars := omit) |
| 192 | runs on PGW_Session_CT { |
| 193 | if (isvalue(pars)) { |
| 194 | g_pars := valueof(pars); |
| 195 | } |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 196 | if (DIAMETER_PROC.checkstate("Connected")) { |
| 197 | f_diameter_expect(g_pars.imsi); |
| 198 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 199 | fn.apply(); |
| 200 | } |
| 201 | |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 202 | private function f_concat_pad(integer tot_len, hexstring prefix, integer suffix) return hexstring { |
| 203 | var integer suffix_len := tot_len - lengthof(prefix); |
| 204 | var charstring suffix_ch := int2str(suffix); |
| 205 | var integer pad_len := suffix_len - lengthof(suffix_ch); |
| 206 | |
| 207 | return prefix & int2hex(0, pad_len) & str2hex(suffix_ch); |
| 208 | } |
| 209 | |
| 210 | function f_gen_imei(integer suffix) return hexstring { |
| 211 | return f_concat_pad(14, '49999'H, suffix); |
| 212 | } |
| 213 | |
| 214 | function f_gen_imsi(integer suffix) return hexstring { |
| 215 | return f_concat_pad(15, '26242'H, suffix); |
| 216 | } |
| 217 | |
| 218 | function f_gen_msisdn(integer suffix) return hexstring { |
| 219 | return f_concat_pad(12, '49123'H, suffix); |
| 220 | } |
| 221 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 222 | private altstep as_DIA_CCR(DCC_NONE_CC_Request_Type req_type) runs on PGW_Session_CT { |
| 223 | var PDU_DIAMETER rx_dia; |
Pau Espin Pedrol | 77fdd0b | 2022-03-08 14:03:33 +0100 | [diff] [blame] | 224 | [] DIAMETER.receive(tr_DIA_Gx_CCR(req_type := req_type)) -> value rx_dia { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 225 | var template (omit) AVP avp; |
| 226 | var octetstring sess_id; |
| 227 | var AVP_Unsigned32 req_num; |
| 228 | |
| 229 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_BASE_NONE_Session_Id); |
| 230 | sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id); |
| 231 | |
| 232 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_DCC_NONE_CC_Request_Number); |
| 233 | req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number); |
| 234 | |
Pau Espin Pedrol | 77fdd0b | 2022-03-08 14:03:33 +0100 | [diff] [blame] | 235 | DIAMETER.send(ts_DIA_Gx_CCA(rx_dia.hop_by_hop_id, rx_dia.end_to_end_id, sess_id, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 236 | req_type, req_num)); |
| 237 | } |
| 238 | [] DIAMETER.receive(PDU_DIAMETER:?) -> value rx_dia { |
| 239 | setverdict(fail, "Received unexpected DIAMETER ", rx_dia); |
| 240 | self.stop; |
| 241 | } |
| 242 | } |
| 243 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 244 | |
| 245 | /* find TEID of given interface type (and optionally instance) */ |
| 246 | private function f_find_teid(FullyQualifiedTEID_List list, |
| 247 | template (present) integer if_type, |
| 248 | template (present) BIT4 instance := ?) |
| 249 | return template (omit) FullyQualifiedTEID |
| 250 | { |
| 251 | var integer i; |
| 252 | for (i := 0; i < lengthof(list); i := i+1) { |
| 253 | if (match(list[i].interfaceType, if_type) and |
| 254 | match(list[i].instance, instance)) { |
| 255 | return list[i]; |
| 256 | } |
| 257 | } |
| 258 | return omit; |
| 259 | } |
| 260 | |
| 261 | /* process one to-be-created bearer context */ |
| 262 | private function process_bctx_create(BearerContextGrouped bctx) runs on PGW_Session_CT |
| 263 | { |
| 264 | /* FIXME: EPS Bearer ID */ |
| 265 | /* FIXME: Cause */ |
| 266 | |
| 267 | /* find F-TEID of the P-GW U side */ |
| 268 | var FullyQualifiedTEID rx_fteid; |
| 269 | rx_fteid := valueof(f_find_teid(bctx.bearerContextIEs.fullyQualifiedTEID, 5, '0010'B)); |
| 270 | g_teid_remote := rx_fteid.tEID_GRE_Key; |
| 271 | if (rx_fteid.v4_Flag == '1'B) { |
| 272 | g_gtpu4_remote := rx_fteid.iPv4_Address; |
| 273 | } |
| 274 | if (rx_fteid.v6_Flag == '1'B) { |
| 275 | g_gtpu6_remote := rx_fteid.iPv6_Address; |
| 276 | } |
| 277 | |
| 278 | var UECUPS_CreateTun uecups_create := { |
| 279 | tx_teid := oct2int(g_teid_remote), |
| 280 | rx_teid := oct2int(g_teid), |
| 281 | user_addr_type := IPV4, |
| 282 | user_addr := '00000000'O, |
| 283 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 284 | remote_gtp_ep := valueof(ts_UECUPS_SockAddr(g_gtpu4_remote)), |
| 285 | tun_dev_name := g_pars.tun_dev_name, |
| 286 | tun_netns_name := g_pars.tun_netns_name |
| 287 | }; |
| 288 | |
| 289 | /* create tunnel in daemon */ |
| 290 | if (isbound(g_ip4_addr)) { |
| 291 | uecups_create.user_addr := g_ip4_addr; |
| 292 | f_gtp2_create_tunnel(uecups_create); |
| 293 | } |
| 294 | if (isbound(g_ip6_addr)) { |
| 295 | uecups_create.user_addr_type := IPV6; |
| 296 | uecups_create.user_addr := g_ip6_addr; |
| 297 | f_gtp2_create_tunnel(uecups_create); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* create a session on the PGW */ |
| 302 | private function f_create_session() runs on PGW_Session_CT { |
| 303 | var PDU_GTPCv2 rx; |
| 304 | |
| 305 | /* allocate + register TEID-C on local side */ |
| 306 | g_teic := f_gtp2_allocate_teid(); |
| 307 | g_teid := g_teic; |
| 308 | |
| 309 | var template (value) FullyQualifiedTEID fteid_c_ie, fteid_u_ie; |
| 310 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 311 | f_inet_addr(mp_local_hostname_c), omit); |
| 312 | fteid_u_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPU, g_teid, 2, |
| 313 | f_inet_addr(mp_local_hostname_u), omit); |
| 314 | var template (value) PDU_GTPCv2 g2c := |
| 315 | ts_GTP2C_CreateSessionReq(imsi := g_pars.imsi, msisdn := omit, rat_type := 6, |
| 316 | sender_fteid := fteid_c_ie, |
| 317 | apn := f_enc_dns_hostname(g_pars.apn), |
| 318 | pdn_type := g_pars.pdn_type, teid_list := { fteid_u_ie }, |
| 319 | chg_car := '0000'O, bearer_id := 1); |
| 320 | /* open5gs up to 1.2.3 won't accept it without ULI, despite not mandatory */ |
| 321 | var template (value) TAI tai := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0001'O }; |
| 322 | var template (value) ECGI ecgi := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0'H, 23 }; |
| 323 | g2c.gtpcv2_pdu.createSessionRequest.userLocationInfo := ts_GTP2C_UserLocInfo(tai := tai, ecgi := ecgi); |
Vadim Yanitskiy | bada3c9 | 2022-01-20 18:59:07 +0600 | [diff] [blame] | 324 | g2c.gtpcv2_pdu.createSessionRequest.servingNetwork := ts_GTP2C_ServingNetwork('001'H, '01F'H); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 325 | |
| 326 | GTP2.send(g2c); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 327 | if (DIAMETER_PROC.checkstate("Connected")) { |
| 328 | as_DIA_CCR(INITIAL_REQUEST); |
| 329 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 330 | alt { |
| 331 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:='10'O)) -> value rx { |
| 332 | /* extract TEIDs */ |
| 333 | var CreateSessionResponse resp := rx.gtpcv2_pdu.createSessionResponse; |
| 334 | g_teic_remote := resp.fullyQualifiedTEID[0].tEID_GRE_Key; |
| 335 | |
| 336 | /* extract allocated address[es] */ |
| 337 | var PDN_Address_and_Prefix paa := resp.pDN_AddressAllocation.pDN_Address_and_Prefix; |
| 338 | if (ischosen(paa.iPv4_Address)) { |
| 339 | g_ip4_addr := paa.iPv4_Address; |
| 340 | } else if (ischosen(paa.iPv6_Address)) { |
| 341 | g_ip6_addr := paa.iPv6_Address.iPv6_Address; |
| 342 | g_ip6_plen := paa.iPv6_Address.prefixLength; |
| 343 | } else if (ischosen(paa.iPv4_IPv6)) { |
| 344 | g_ip4_addr := paa.iPv4_IPv6.iPv4_Address; |
| 345 | g_ip6_addr := paa.iPv4_IPv6.iPv6_Address; |
| 346 | g_ip6_plen := paa.iPv4_IPv6.prefixLength; |
| 347 | } |
| 348 | var integer i; |
| 349 | for (i := 0; i < lengthof(resp.bearerContextGrouped); i := i+1) { |
| 350 | var BearerContextGrouped bctx := resp.bearerContextGrouped[i]; |
| 351 | select (bctx.instance) { |
| 352 | case ('0000'B) { // created |
| 353 | process_bctx_create(bctx); |
| 354 | } |
| 355 | case ('0001'B) { // removed |
| 356 | setverdict(fail, "We don't expect removed bearer contexts yet"); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:=?)) -> value rx { |
| 362 | setverdict(fail, "Unexpected CreateSessionResp(cause=", |
| 363 | rx.gtpcv2_pdu.createSessionResponse.cause.causeValue, ")"); |
| 364 | } |
| 365 | [] GTP2.receive { |
| 366 | setverdict(fail, "Unexpected GTPv2 while waiting for CreateSessionResp"); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | } |
| 371 | |
| 372 | /* delete the session from the PGW */ |
| 373 | private function f_delete_session(template (omit) OCT1 tx_cause := omit, |
| 374 | template (present) OCT4 exp_teid, |
| 375 | template (present) OCT1 exp_cause) runs on PGW_Session_CT { |
| 376 | var template (value) FullyQualifiedTEID fteid_c_ie |
| 377 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 378 | f_inet_addr(mp_local_hostname_c), omit); |
| 379 | var template PDU_GTPCv2 g2c := |
| 380 | ts_GTP2C_DeleteSessionReq(d_teid := g_teic_remote, cause := tx_cause, |
| 381 | sender_fteid := fteid_c_ie, |
| 382 | teid_list := {}, bearer_id := 1); |
| 383 | |
| 384 | GTP2.send(g2c); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 385 | if (DIAMETER_PROC.checkstate("Connected")) { |
| 386 | as_DIA_CCR(TERMINATION_REQUEST); |
| 387 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 388 | alt { |
| 389 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(d_teid := exp_teid, cause := exp_cause)) { |
| 390 | setverdict(pass); |
| 391 | } |
| 392 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(?, ?)) { |
| 393 | setverdict(fail, "Unexpected DeleteSessionResp"); |
| 394 | } |
| 395 | [] GTP2.receive { |
| 396 | setverdict(fail, "Unexpected GTPv2 while waiting for DeleteSessionResp"); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* destroy tunnel in daemon */ |
| 401 | if (isbound(g_teid)) { |
| 402 | var UECUPS_DestroyTun uecups_destroy := { |
| 403 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 404 | rx_teid := oct2int(g_teid) |
| 405 | }; |
| 406 | /* FIXME: what about IPv4/IPv6 differentiation? */ |
| 407 | f_gtp2_destroy_tunnel(uecups_destroy); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* start a program on the user plane side; return its PID */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 412 | private function f_start_prog(charstring command, boolean redirect_output := true) |
| 413 | runs on PGW_Session_CT return integer |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 414 | { |
| 415 | var UECUPS_StartProgram sprog := { |
| 416 | command := command, |
| 417 | environment := {}, |
| 418 | run_as_user := mp_run_prog_as_user, |
| 419 | tun_netns_name := g_pars.tun_netns_name |
| 420 | }; |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 421 | |
| 422 | /* Redirect stdout/stderr to the user-specified location */ |
| 423 | if (redirect_output) { |
| 424 | var charstring prefix := mp_run_prog_log_path & "/" & testcasename(); |
| 425 | sprog.command := sprog.command & " 1>>" & prefix & ".prog.stdout"; |
| 426 | sprog.command := sprog.command & " 2>>" & prefix & ".prog.stderr"; |
| 427 | } |
| 428 | |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 429 | log("Starting a program: ", command); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 430 | var UECUPS_StartProgramRes res := f_gtp2_start_program(sprog); |
| 431 | if (res.result != OK) { |
| 432 | setverdict(fail, "Unable to start program '", command, "'"); |
| 433 | } |
| 434 | return res.pid; |
| 435 | } |
| 436 | |
| 437 | /* wait for termination of a given PID with specified exit_code */ |
| 438 | private function f_wait_term(integer pid, template (present) integer exit_code := 0, |
| 439 | float tout := 10.0) runs on PGW_Session_CT |
| 440 | { |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 441 | var UECUPS_ProgramTermInd pti; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 442 | timer T := tout; |
| 443 | |
| 444 | T.start; |
| 445 | alt { |
| 446 | [] GTP2.receive(UECUPS_ProgramTermInd:{pid := pid, exit_code := exit_code}) { |
| 447 | setverdict(pass); |
| 448 | } |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 449 | [] GTP2.receive(UECUPS_ProgramTermInd:?) -> value pti { |
| 450 | setverdict(fail, "Received unexpected ProgramTermInd := ", pti); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 451 | } |
| 452 | [] T.timeout { |
| 453 | setverdict(fail, "timeout waiting for user-plane program termination"); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* execute a program and wait for result */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 459 | private function f_start_prog_wait(charstring command, |
| 460 | template integer exit_code := 0, |
| 461 | float tout := 10.0, |
| 462 | boolean redirect_output := true) |
| 463 | runs on PGW_Session_CT |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 464 | { |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 465 | var integer pid := f_start_prog(command, redirect_output); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 466 | f_wait_term(pid, exit_code, tout); |
| 467 | } |
| 468 | |
| 469 | /* execute ping command and wait for result */ |
| 470 | private function f_ping4(charstring host, integer interval := 1, integer count := 10) runs on PGW_Session_CT |
| 471 | { |
| 472 | var charstring ping :="ping -c " & int2str(count) & " -i " & int2str(interval); |
| 473 | ping := ping & " -I " & f_inet_ntoa(g_ip4_addr); |
| 474 | ping := ping & " " & host; |
Harald Welte | 8cfdc7c | 2020-04-21 22:48:34 +0200 | [diff] [blame] | 475 | f_start_prog_wait(ping, tout := int2float(5 + interval*count)); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | /* send echo request; expect response */ |
| 482 | testcase TC_tx_echo() runs on PGW_Test_CT { |
| 483 | timer T := 5.0; |
| 484 | |
| 485 | f_init(); |
| 486 | |
| 487 | TEID0.send(ts_GTP2C_EchoReq(0)); |
| 488 | T.start; |
| 489 | alt { |
| 490 | [] TEID0.receive(tr_GTP2C_EchoResp) { |
| 491 | setverdict(pass); |
| 492 | } |
| 493 | [] T.timeout { |
| 494 | setverdict(fail, "timeout waiting for Echo Response"); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* create a session, expect it to succeed */ |
| 500 | private function f_TC_createSession() runs on PGW_Session_CT { |
| 501 | f_create_session(); |
| 502 | setverdict(pass); |
| 503 | } |
| 504 | testcase TC_createSession() runs on PGW_Test_CT { |
| 505 | var PGW_Session_CT vc_conn; |
| 506 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun22")); |
| 507 | f_init(); |
| 508 | vc_conn := f_start_handler(refers(f_TC_createSession), pars); |
| 509 | vc_conn.done; |
| 510 | } |
| 511 | |
| 512 | /* create a session, then execute a ping command on the user plane */ |
| 513 | private function f_TC_createSession_ping4() runs on PGW_Session_CT { |
| 514 | f_create_session(); |
| 515 | f_ping4(mp_ping_hostname); |
| 516 | setverdict(pass); |
| 517 | } |
| 518 | testcase TC_createSession_ping4() runs on PGW_Test_CT { |
| 519 | var PGW_Session_CT vc_conn; |
| 520 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 521 | f_init(); |
| 522 | vc_conn := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 523 | vc_conn.done; |
| 524 | } |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 525 | testcase TC_createSession_ping4_256() runs on PGW_Test_CT { |
| 526 | var PGW_Session_CT vc_conn[256]; |
| 527 | var integer i; |
| 528 | |
| 529 | f_init(); |
| 530 | |
| 531 | for (i := 0; i < sizeof(vc_conn); i:=i+1) { |
| 532 | var charstring tundev := "ping" & int2str(i); |
| 533 | var SessionPars pars := valueof(t_SessionPars(f_gen_imsi(i), tundev)); |
| 534 | vc_conn[i] := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 535 | } |
| 536 | |
| 537 | for (i := 0; i < lengthof(vc_conn); i:=i+1) { |
| 538 | vc_conn[i].done; |
| 539 | } |
| 540 | } |
| 541 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 542 | |
| 543 | /* create a session, then delete it again */ |
| 544 | private function f_TC_createSession_deleteSession() runs on PGW_Session_CT { |
| 545 | f_create_session(); |
| 546 | f_delete_session(omit, g_teic, '10'O); |
| 547 | setverdict(pass); |
| 548 | } |
| 549 | testcase TC_createSession_deleteSession() runs on PGW_Test_CT { |
| 550 | var PGW_Session_CT vc_conn; |
| 551 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 552 | f_init(); |
| 553 | vc_conn := f_start_handler(refers(f_TC_createSession_deleteSession), pars); |
| 554 | vc_conn.done; |
| 555 | } |
| 556 | |
| 557 | /* send a DeleteSessionReq for an unknown/invalid TEID */ |
| 558 | private function f_TC_deleteSession_unknown() runs on PGW_Session_CT { |
| 559 | g_teic := f_gtp2_allocate_teid(); |
| 560 | g_teic_remote := f_rnd_octstring(4); |
| 561 | f_delete_session(omit, '00000000'O, '40'O /* Context Unknown */); |
| 562 | setverdict(pass); |
| 563 | } |
| 564 | testcase TC_deleteSession_unknown() runs on PGW_Test_CT { |
| 565 | var PGW_Session_CT vc_conn; |
| 566 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 567 | f_init(); |
| 568 | vc_conn := f_start_handler(refers(f_TC_deleteSession_unknown), pars); |
| 569 | vc_conn.done; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | control { |
| 576 | execute( TC_tx_echo() ); |
| 577 | execute( TC_createSession() ); |
| 578 | execute( TC_createSession_ping4() ); |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 579 | execute( TC_createSession_ping4_256() ); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 580 | execute( TC_createSession_deleteSession() ); |
| 581 | execute( TC_deleteSession_unknown() ); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | } |