Skip to content

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
macpie committed Sep 28, 2023
1 parent 3297317 commit 4f32de5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/grpc/packet_router/hpr_packet_router_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ route(eos, StreamState) ->
lager:debug("received eos for stream"),
{stop, StreamState};
route(EnvUp, StreamState) ->
lager:debug("got env up ~p", [EnvUp]),
ct:pal("got env up ~p", [EnvUp]),
try hpr_envelope_up:data(EnvUp) of
{packet, PacketUp} ->
handle_packet(PacketUp, StreamState);
Expand Down Expand Up @@ -78,13 +78,15 @@ handle_info({give_away, NewPid, PubKeyBin}, StreamState) ->
handle_info(?SESSION_RESET, StreamState0) ->
{EnvDown, StreamState1} = create_session_offer(StreamState0),
grpcbox_stream:send(false, EnvDown, StreamState1);
handle_info(?SESSION_OFFER_TIMEOUT, StreamState) ->
handle_info({?SESSION_OFFER_TIMEOUT, OldSessionKey}, StreamState) ->
lager:debug("got session offer timeout ~s", [hpr_utils:bin_to_hex_string(OldSessionKey)]),
HandlerState = grpcbox_stream:stream_handler_state(StreamState),
case HandlerState#handler_state.session_key =/= undefined of
true ->
lager:debug("session offer timeout triggered but session_key is set"),
StreamState;
CurrSessionKey = HandlerState#handler_state.session_key,
case CurrSessionKey =/= undefined andalso CurrSessionKey =/= OldSessionKey of
false ->
lager:debug("session offer timeout triggered but session_key is set and new"),
StreamState;
true ->
lager:warning(
"session offer ~s timeout triggered, closing stream", [
hpr_utils:bin_to_hex_string(HandlerState#handler_state.nonce)
Expand Down Expand Up @@ -225,7 +227,11 @@ handle_session_init(SessionInit, StreamState) ->
create_session_offer(StreamState0) ->
HandlerState0 = grpcbox_stream:stream_handler_state(StreamState0),
Nonce = crypto:strong_rand_bytes(32),
Timer = erlang:send_after(?SESSION_OFFER_RES_TIMEOUT, self(), ?SESSION_OFFER_TIMEOUT),
Timer = erlang:send_after(
?SESSION_OFFER_RES_TIMEOUT,
self(),
{?SESSION_OFFER_TIMEOUT, HandlerState0#handler_state.session_key}
),
EnvDown = hpr_envelope_down:new(hpr_session_offer:new(Nonce)),
StreamState1 = grpcbox_stream:stream_handler_state(
StreamState0, HandlerState0#handler_state{nonce = Nonce, session_offer_timer = Timer}
Expand Down
45 changes: 45 additions & 0 deletions test/hpr_packet_router_service_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ timeout_test(_Config) ->
route_id => RouteID, start_addr => 16#00000000, end_addr => 16#00000010
})
],

%% Normal test
{ok, GatewayPid1} = hpr_test_gateway:start(#{
forward => self(),
route => Route,
Expand All @@ -173,8 +175,17 @@ timeout_test(_Config) ->
{ok, _} = hpr_test_gateway:receive_session_init(GatewayPid1, timer:seconds(1)),
{error, timeout} = hpr_test_gateway:receive_stream_down(GatewayPid1),

SessionKey1 = hpr_test_gateway:session_key(GatewayPid1),

PubKeyBin1 = hpr_test_gateway:pubkey_bin(GatewayPid1),
{ok, Pid1} = hpr_packet_router_service:locate(PubKeyBin1),

%% Checking that session keys are the same
?assertEqual(SessionKey1, session_key_from_stream(Pid1)),

gen_server:stop(GatewayPid1),

%% Testing with session key offer disabled, should result in closing of stream
{ok, GatewayPid2} = hpr_test_gateway:start(#{
forward => self(),
route => Route,
Expand All @@ -188,8 +199,42 @@ timeout_test(_Config) ->

gen_server:stop(GatewayPid2),

%% Normal test with session reset
{ok, GatewayPid3} = hpr_test_gateway:start(#{
forward => self(),
route => Route,
eui_pairs => EUIPairs,
devaddr_ranges => DevAddrRanges,
ignore_session_offer => false
}),

{ok, _} = hpr_test_gateway:receive_session_init(GatewayPid3, timer:seconds(1)),
{error, timeout} = hpr_test_gateway:receive_stream_down(GatewayPid3),

SessionKey3 = hpr_test_gateway:session_key(GatewayPid3),
PubKeyBin3 = hpr_test_gateway:pubkey_bin(GatewayPid3),
{ok, Pid3} = hpr_packet_router_service:locate(PubKeyBin3),

%% Checking that session keys are the same
?assertEqual(SessionKey3, session_key_from_stream(Pid3)),
Pid3 ! session_reset,

{ok, _} = hpr_test_gateway:receive_session_init(GatewayPid3, timer:seconds(1)),
{error, timeout} = hpr_test_gateway:receive_stream_down(GatewayPid3),

SessionKey4 = hpr_test_gateway:session_key(GatewayPid3),
?assert(SessionKey3 =/= SessionKey4),
?assertEqual(SessionKey4, session_key_from_stream(Pid3)),

ok.

%% ===================================================================
%% Helpers
%% ===================================================================

session_key_from_stream(Pid) ->
State = sys:get_state(Pid),
StreamState = erlang:element(2, State),
CallbackState = erlang:element(20, StreamState),
HandlerState = erlang:element(3, CallbackState),
erlang:element(5, HandlerState).
9 changes: 8 additions & 1 deletion test/hpr_test_gateway.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
-export([
start/1,
pubkey_bin/1,
session_key/1,
send_packet/2,
receive_send_packet/1,
receive_env_down/1,
Expand Down Expand Up @@ -63,6 +64,10 @@ start(Args) ->
pubkey_bin(Pid) ->
gen_server:call(Pid, pubkey_bin).

-spec session_key(Pid :: pid()) -> binary().
session_key(Pid) ->
gen_server:call(Pid, session_key).

-spec send_packet(Pid :: pid(), Args :: map()) -> ok.
send_packet(Pid, Args) ->
gen_server:cast(Pid, {?SEND_PACKET, Args}).
Expand Down Expand Up @@ -150,6 +155,8 @@ init(

handle_call(pubkey_bin, _From, #state{pubkey_bin = PubKeyBin} = State) ->
{reply, PubKeyBin, State};
handle_call(session_key, _From, #state{session_key = {SessionKey, _}} = State) ->
{reply, SessionKey, State};
handle_call(_Msg, _From, State) ->
lager:debug("unknown call ~p", [_Msg]),
{reply, ok, State}.
Expand Down Expand Up @@ -234,7 +241,7 @@ handle_info(?CONNECT, #state{forward = Pid, pubkey_bin = PubKeyBin, sig_fun = Si
EnvUp = hpr_envelope_up:new(SignedReg),
ok = grpcbox_client:send(Stream, EnvUp),
Pid ! {?MODULE, self(), {?REGISTER, EnvUp}},
lager:debug("connected and registered"),
lager:debug("connected and registering"),
{noreply, State#state{stream = Stream}}
end;
%% GRPC stream callbacks
Expand Down

0 comments on commit 4f32de5

Please sign in to comment.