-module(s1ap_proxy_test). -include_lib("eunit/include/eunit.hrl"). -include("pfcp_mock.hrl"). %% ------------------------------------------------------------------ %% setup functions %% ------------------------------------------------------------------ -define(TC(Fun), {setup, fun start/0, fun stop/1, Fun}). start() -> pfcp_mock:mock_all(), exometer_mock:mock_all(), {ok, Pid} = s1ap_proxy:start_link(), #{handler => Pid}. stop(#{handler := Pid}) -> s1ap_proxy:shutdown(Pid), exometer_mock:unmock_all(), pfcp_mock:unmock_all(). %% ------------------------------------------------------------------ %% testcase descriptions %% ------------------------------------------------------------------ s1ap_proxy_test_() -> [{"S1 SETUP REQUEST (unmodified)", ?TC(fun test_s1_setup_req/1)}, {"E-RAB SETUP REQUEST/RESPONSE", ?TC(fun test_e_rab_setup/1)}, {"E-RAB SETUP REQUEST (failure)", ?TC(fun test_e_rab_setup_req_fail/1)}, {"E-RAB RELEASE COMMAND/RESPONSE", ?TC(fun test_e_rab_release_cmd/1)}, {"E-RAB RELEASE INDICATION", ?TC(fun test_e_rab_release_ind/1)}, {"E-RAB MODIFICATION INDICATION", ?TC(fun test_e_rab_modify_ind/1)}, {"INITIAL CONTEXT SETUP REQUEST/RESPONSE", ?TC(fun test_initial_context_setup/1)}]. %% ------------------------------------------------------------------ %% actual testcases %% ------------------------------------------------------------------ test_s1_setup_req(#{handler := Pid}) -> SetupReq = s1_setup_req_pdu(), %% Expect the PDU to be proxied unmodified [?_assertEqual({forward, SetupReq}, s1ap_proxy:process_pdu(Pid, SetupReq))]. test_e_rab_setup(#{handler := Pid}) -> %% [eNB <- MME] E-RAB SETUP REQUEST SetupReqIn = e_rab_setup_req_pdu(?ADDR_U2C, ?TEID_U2C), SetupReqExp = e_rab_setup_req_pdu(?ADDR_A2U, ?TEID_A2U), %% [eNB -> MME] E-RAB SETUP RESPONSE SetupRspIn = e_rab_setup_rsp_pdu(?ADDR_U2A, ?TEID_U2A), SetupRspExp = e_rab_setup_rsp_pdu(?ADDR_C2U, ?TEID_C2U), [?_assertEqual({forward, SetupReqExp}, s1ap_proxy:process_pdu(Pid, SetupReqIn)), ?_assertEqual({forward, SetupRspExp}, s1ap_proxy:process_pdu(Pid, SetupRspIn)), ?_assertMatch({ok, _}, s1ap_proxy:fetch_erab(Pid, {7, 9, 6})), ?_assertMatch([_], s1ap_proxy:fetch_erab_list(Pid))]. test_e_rab_setup_req_fail(#{handler := Pid}) -> %% pfcp_peer:session_establish_req/3 responds with a reject PDU = pfcp_mock:pdu_rsp_reject(session_establishment_response, ?SEID_Loc), pfcp_mock:mock_req(session_establish_req, PDU), %% eNB <- [S1GW <- MME] E-RAB SETUP REQUEST %% eNB -- [S1GW -> MME] E-RAB SETUP RESPONSE (failure) SetupReqIn = e_rab_setup_req_pdu(?ADDR_U2C, ?TEID_U2C), SetupRspExp = e_rab_setup_rsp_fail_pdu(), [?_assertEqual({reply, SetupRspExp}, s1ap_proxy:process_pdu(Pid, SetupReqIn)), ?_assertEqual([], s1ap_proxy:fetch_erab_list(Pid))]. test_e_rab_release_cmd(#{handler := Pid}) -> %% [eNB <- MME] E-RAB SETUP REQUEST SetupReq = e_rab_setup_req_pdu(?ADDR_U2C, ?TEID_U2C), %% [eNB -> MME] E-RAB SETUP RESPONSE SetupRsp = e_rab_setup_rsp_pdu(?ADDR_U2A, ?TEID_U2A), %% [eNB <- MME] E-RAB RELEASE COMMAND ReleaseCmd = e_rab_release_cmd_pdu(), %% [eNB -> MME] E-RAB RELEASE RESPONSE ReleaseRsp = e_rab_release_rsp_pdu(), [?_assertMatch({forward, _}, s1ap_proxy:process_pdu(Pid, SetupReq)), ?_assertMatch({forward, _}, s1ap_proxy:process_pdu(Pid, SetupRsp)), ?_assertMatch([_], s1ap_proxy:fetch_erab_list(Pid)), ?_assertEqual({forward, ReleaseCmd}, s1ap_proxy:process_pdu(Pid, ReleaseCmd)), ?_assertEqual({forward, ReleaseRsp}, s1ap_proxy:process_pdu(Pid, ReleaseRsp)), ?_assertEqual([], s1ap_proxy:fetch_erab_list(Pid))]. test_e_rab_release_ind(#{handler := Pid}) -> %% [eNB <- MME] E-RAB SETUP REQUEST SetupReq = e_rab_setup_req_pdu(?ADDR_U2C, ?TEID_U2C), %% [eNB -> MME] E-RAB SETUP RESPONSE SetupRsp = e_rab_setup_rsp_pdu(?ADDR_U2A, ?TEID_U2A), %% [eNB -> MME] E-RAB RELEASE INDICATION ReleaseInd = e_rab_release_ind_pdu(), [?_assertMatch({forward, _}, s1ap_proxy:process_pdu(Pid, SetupReq)), ?_assertMatch({forward, _}, s1ap_proxy:process_pdu(Pid, SetupRsp)), ?_assertEqual({forward, ReleaseInd}, s1ap_proxy:process_pdu(Pid, ReleaseInd)), ?_assertEqual([], s1ap_proxy:fetch_erab_list(Pid))]. test_e_rab_modify_ind(#{handler := Pid}) -> %% [eNB -> MME] E-RAB MODIFICATION INDICATION ModifyIndIn = e_rab_modify_ind_pdu(?ADDR_U2A, ?TEID_U2A), %% XXX: not implemented, we should actually expect ?ADDR_C2U, ?TEID_C2U ModifyIndExp = e_rab_modify_ind_pdu(?ADDR_U2A, ?TEID_U2A), [?_assertEqual({forward, ModifyIndExp}, s1ap_proxy:process_pdu(Pid, ModifyIndIn))]. test_initial_context_setup(#{handler := Pid}) -> %% [eNB <- MME] INITIAL CONTEXT SETUP REQUEST InitCtxSetupReqIn = initial_context_setup_req_pdu(?ADDR_U2C, ?TEID_U2C), InitCtxSetupReqExp = initial_context_setup_req_pdu(?ADDR_A2U, ?TEID_A2U), %% [eNB -> MME] INITIAL CONTEXT SETUP RESPONSE InitCtxSetupRspIn = initial_context_setup_rsp_pdu(?ADDR_U2A, ?TEID_U2A), InitCtxSetupRspExp = initial_context_setup_rsp_pdu(?ADDR_C2U, ?TEID_C2U), [?_assertEqual({forward, InitCtxSetupReqExp}, s1ap_proxy:process_pdu(Pid, InitCtxSetupReqIn)), ?_assertEqual({forward, InitCtxSetupRspExp}, s1ap_proxy:process_pdu(Pid, InitCtxSetupRspIn)), ?_assertMatch([_], s1ap_proxy:fetch_erab_list(Pid))]. %% ------------------------------------------------------------------ %% S1AP PDU templates %% ------------------------------------------------------------------ %% S1 SETUP REQUEST s1_setup_req_pdu() -> << 16#00, 16#11, 16#00, 16#1a, 16#00, 16#00, 16#02, 16#00, 16#3b, 16#40, 16#08, 16#00, 16#00, 16#f1, 16#10, 16#00, 16#00, 16#00, 16#00, 16#00, 16#40, 16#00, 16#07, 16#00, 16#0c, 16#0e, 16#40, 16#00, 16#f1, 16#10 >>. %% [eNB <- MME] E-RAB SETUP REQUEST e_rab_setup_req_pdu(TLA, TEID) when is_binary(TLA), is_integer(TEID) -> << 16#00, 16#05, 16#00, 16#80, 16#9b, 16#00, 16#00, 16#03, 16#00, 16#00, 16#00, 16#02, 16#00, 16#07, 16#00, 16#08, 16#00, 16#02, 16#00, 16#09, 16#00, 16#10, 16#00, 16#80, 16#87, 16#00, 16#00, 16#11, 16#00, 16#80, 16#81, 16#0c, 16#00, 16#05, 16#04, 16#0f, 16#80, TLA/bytes, %% transportLayerAddress (IPv4) TEID:32/big, %% GTP-TEID 16#72, 16#27, 16#c8, 16#1a, 16#bc, 16#ec, 16#11, 16#62, 16#54, 16#c1, 16#01, 16#05, 16#04, 16#03, 16#69, 16#6d, 16#73, 16#05, 16#01, 16#c0, 16#a8, 16#65, 16#02, 16#5e, 16#02, 16#b3, 16#8c, 16#58, 16#32, 16#27, 16#54, 16#80, 16#80, 16#21, 16#10, 16#02, 16#00, 16#00, 16#10, 16#81, 16#06, 16#08, 16#08, 16#08, 16#08, 16#83, 16#06, 16#08, 16#08, 16#04, 16#04, 16#00, 16#0d, 16#04, 16#08, 16#08, 16#08, 16#08, 16#00, 16#0d, 16#04, 16#08, 16#08, 16#04, 16#04, 16#00, 16#03, 16#10, 16#20, 16#01, 16#48, 16#60, 16#48, 16#60, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#88, 16#88, 16#00, 16#03, 16#10, 16#20, 16#01, 16#48, 16#60, 16#48, 16#60, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#88, 16#44, 16#00, 16#0c, 16#04, 16#ac, 16#16, 16#00, 16#15, 16#00, 16#10, 16#02, 16#05, 16#78 >>. %% [eNB -> MME] E-RAB SETUP RESPONSE e_rab_setup_rsp_pdu(TLA, TEID) when is_binary(TLA), is_integer(TEID) -> << 16#20, 16#05, 16#00, 16#22, 16#00, 16#00, 16#03, 16#00, 16#00, 16#40, 16#02, 16#00, 16#07, 16#00, 16#08, 16#40, 16#02, 16#00, 16#09, 16#00, 16#1c, 16#40, 16#0f, 16#00, 16#00, 16#27, 16#40, 16#0a, 16#0c, 16#1f, TLA/bytes, %% transportLayerAddress (IPv4) TEID:32/big %% GTP-TEID >>. %% [S1GW -> MME] E-RAB SETUP RESPONSE (failure) %% Transport-cause: transport-resource-unavailable e_rab_setup_rsp_fail_pdu() -> << 16#20, 16#05, 16#00, 16#1a, 16#00, 16#00, 16#03, 16#00, 16#00, 16#40, 16#02, 16#00, 16#07, 16#00, 16#08, 16#40, 16#02, 16#00, 16#09, 16#00, 16#1d, 16#40, 16#07, 16#00, 16#00, 16#23, 16#40, 16#02, 16#0c, 16#20 >>. %% [eNB <- MME] E-RAB RELEASE COMMAND %% TODO: make E-RAB IDs configurable e_rab_release_cmd_pdu() -> << 16#00, 16#07, 16#00, 16#1a, 16#00, 16#00, 16#03, 16#00, 16#00, 16#00, 16#02, 16#00, 16#07, 16#00, 16#08, 16#00, 16#02, 16#00, 16#09, 16#00, 16#21, 16#40, 16#07, 16#00, 16#00, 16#23, 16#40, 16#02, 16#0c, 16#40 >>. %% [eNB -> MME] E-RAB RELEASE RESPONSE %% TODO: make E-RAB IDs configurable e_rab_release_rsp_pdu() -> << 16#20, 16#07, 16#00, 16#19, 16#00, 16#00, 16#03, 16#00, 16#00, 16#00, 16#02, 16#00, 16#07, 16#00, 16#08, 16#00, 16#02, 16#00, 16#09, 16#00, 16#45, 16#40, 16#06, 16#00, 16#00, 16#0f, 16#40, 16#01, 16#0c >>. %% [eNB -> MME] E-RAB RELEASE INDICATION %% TODO: make E-RAB IDs configurable e_rab_release_ind_pdu() -> << 16#00, 16#08, 16#00, 16#1a, 16#00, 16#00, 16#03, 16#00, 16#00, 16#00, 16#02, 16#00, 16#07, 16#00, 16#08, 16#00, 16#02, 16#00, 16#09, 16#00, 16#6e, 16#40, 16#07, 16#00, 16#00, 16#23, 16#40, 16#02, 16#0c, 16#40 >>. %% [eNB -> MME] E-RAB MODIFICATION INDICATION e_rab_modify_ind_pdu(TLA, TEID) when is_binary(TLA), is_integer(TEID) -> << 16#00, 16#32, 16#00, 16#24, 16#00, 16#00, 16#03, 16#00, 16#00, 16#00, 16#02, 16#00, 16#02, 16#00, 16#08, 16#00, 16#04, 16#80, 16#06, 16#69, 16#2d, 16#00, 16#c7, 16#00, 16#0f, 16#00, 16#00, 16#c8, 16#00, 16#0a, 16#0a, 16#1f, TLA/bytes, %% transportLayerAddress (IPv4) TEID:32/big %% GTP-TEID >>. %% [eNB <- MME] INITIAL CONTEXT SETUP REQUEST initial_context_setup_req_pdu(TLA, TEID) when is_binary(TLA), is_integer(TEID) -> << 16#00, 16#09, 16#00, 16#81, 16#10, 16#00, 16#00, 16#07, 16#00, 16#00, 16#00, 16#02, 16#00, 16#01, 16#00, 16#08, 16#00, 16#02, 16#00, 16#01, 16#00, 16#42, 16#00, 16#0a, 16#18, 16#3b, 16#9a, 16#ca, 16#00, 16#60, 16#3b, 16#9a, 16#ca, 16#00, 16#00, 16#18, 16#00, 16#80, 16#b5, 16#00, 16#00, 16#34, 16#00, 16#80, 16#af, 16#45, 16#00, 16#09, 16#20, 16#0f, 16#80, TLA/bytes, %% transportLayerAddress (IPv4) TEID:32/big, %% GTP-TEID 16#80, 16#9f, 16#27, 16#bd, 16#de, 16#61, 16#4e, 16#02, 16#07, 16#42, 16#02, 16#29, 16#06, 16#40, 16#32, 16#f8, 16#10, 16#00, 16#01, 16#00, 16#6e, 16#52, 16#05, 16#c1, 16#01, 16#09, 16#09, 16#08, 16#69, 16#6e, 16#74, 16#65, 16#72, 16#6e, 16#65, 16#74, 16#05, 16#01, 16#c0, 16#a8, 16#64, 16#02, 16#5e, 16#06, 16#fe, 16#fe, 16#fa, 16#fa, 16#02, 16#02, 16#58, 16#32, 16#27, 16#4d, 16#80, 16#80, 16#21, 16#10, 16#02, 16#00, 16#00, 16#10, 16#81, 16#06, 16#08, 16#08, 16#08, 16#08, 16#83, 16#06, 16#08, 16#08, 16#04, 16#04, 16#00, 16#0d, 16#04, 16#08, 16#08, 16#08, 16#08, 16#00, 16#0d, 16#04, 16#08, 16#08, 16#04, 16#04, 16#00, 16#03, 16#10, 16#20, 16#01, 16#48, 16#60, 16#48, 16#60, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#88, 16#88, 16#00, 16#03, 16#10, 16#20, 16#01, 16#48, 16#60, 16#48, 16#60, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#88, 16#44, 16#00, 16#10, 16#02, 16#05, 16#aa, 16#50, 16#0b, 16#f6, 16#32, 16#f8, 16#10, 16#00, 16#02, 16#01, 16#c0, 16#00, 16#02, 16#86, 16#13, 16#32, 16#f8, 16#10, 16#00, 16#01, 16#23, 16#05, 16#f4, 16#04, 16#94, 16#25, 16#f8, 16#64, 16#02, 16#01, 16#08, 16#00, 16#6b, 16#00, 16#05, 16#1c, 16#00, 16#0e, 16#00, 16#00, 16#00, 16#49, 16#00, 16#20, 16#57, 16#5d, 16#d3, 16#66, 16#69, 16#73, 16#4c, 16#8c, 16#48, 16#42, 16#76, 16#5f, 16#fe, 16#f9, 16#bd, 16#d5, 16#89, 16#08, 16#ae, 16#f6, 16#f4, 16#88, 16#62, 16#22, 16#d7, 16#80, 16#fb, 16#36, 16#33, 16#d9, 16#9e, 16#9f, 16#00, 16#c0, 16#40, 16#08, 16#35, 16#87, 16#61, 16#10, 16#03, 16#ff, 16#ff, 16#74 >>. %% [eNB -> MME] INITIAL CONTEXT SETUP RESPONSE initial_context_setup_rsp_pdu(TLA, TEID) when is_binary(TLA), is_integer(TEID) -> << 16#20, 16#09, 16#00, 16#22, 16#00, 16#00, 16#03, 16#00, 16#00, 16#40, 16#02, 16#00, 16#01, 16#00, 16#08, 16#40, 16#02, 16#00, 16#01, 16#00, 16#33, 16#40, 16#0f, 16#00, 16#00, 16#32, 16#40, 16#0a, 16#0a, 16#1f, TLA/bytes, %% transportLayerAddress (IPv4) TEID:32/big %% GTP-TEID >>. %% vim:set ts=4 sw=4 et: