From f7c5d203f7e9d6410019f4d551260b1989e7143a Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Fri, 10 May 2019 23:57:21 +0900 Subject: [PATCH] quality: Add test case for Tres.SecureChannel --- lib/tres/secure_channel.ex | 17 ++---- test/lib/tres/secure_channel_test.exs | 75 ++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/lib/tres/secure_channel.ex b/lib/tres/secure_channel.ex index afcf94d..750ad5f 100644 --- a/lib/tres/secure_channel.ex +++ b/lib/tres/secure_channel.ex @@ -10,7 +10,6 @@ defmodule Tres.SecureChannel do alias Tres.MessageHandlerSup @process_flags [ - trap_exit: true, message_queue_data: :off_heap ] @@ -589,10 +588,6 @@ defmodule Tres.SecureChannel do close_connection({:handler_down, reason}, state_data) end - defp handle_signal({:EXIT, _pid, reason}, state_data) do - close_connection({:trap_detected, reason}, state_data) - end - defp close_connection(:failed_version_negotiation, state_data) do debug("connection terminated: Version negotiation failed") {:stop, :normal, %{state_data | socket: nil}} @@ -631,21 +626,15 @@ defmodule Tres.SecureChannel do {:stop, :normal, %{state_data | socket: nil}} end - defp close_connection({:trap_detected, reason}, state_data) do - debug("connection terminated: Trapped by #{inspect(reason)}") - _ = send(state_data.handler_pid, {:switch_disconnected, :trap_detected}) - {:stop, :normal, %{state_data | socket: nil}} - end - defp close_connection(:tcp_closed, state_data) do debug("connection terminated: TCP Closed by peer") _ = send(state_data.handler_pid, {:switch_disconnected, :tcp_closed}) {:stop, :normal, %{state_data | socket: nil}} end - defp close_connection({:tcp_error, reason}, state_data) do - debug("connection terminated: TCP Error occured: #{inspect(reason)}") - _ = send(state_data.handler_pid, {:switch_disconnected, :tcp_error}) + defp close_connection(close_reason, state_data) do + debug("connection terminated: reason: #{inspect(close_reason)}") + _ = send(state_data.handler_pid, {:switch_disconnected, close_reason}) {:stop, :normal, %{state_data | socket: nil}} end end diff --git a/test/lib/tres/secure_channel_test.exs b/test/lib/tres/secure_channel_test.exs index afcc3f3..8e3a9c7 100644 --- a/test/lib/tres/secure_channel_test.exs +++ b/test/lib/tres/secure_channel_test.exs @@ -3,23 +3,82 @@ defmodule Tres.SecureChannelTest do import ExUnit.CaptureLog - @datapath_id "0000000000000001" + @datapath_id "0000000000000004" @host 'localhost' @port 6653 - @client_opts [:binary, {:packet, 0}] + @client_opts [:binary, {:packet, 0}, {:active, true}] + + @ofp_hello 0 + @ofp_echo_request 2 + @ofp_features_request 5 describe "Tres.SecureChannel" do test "if hello message sending is slow" do {:ok, socket} = connect() - _ = :gen_tcp.recv(socket, 0) + assert_receive {:tcp, ^socket, <<4::8, @ofp_hello::8, _::binary>>}, 4000 + assert_receive {:tcp_closed, ^socket}, 4000 + end - fn -> Process.sleep(4000) end - |> capture_log() - |> Kernel.=~("connection terminated: Hello handshake timed out") - |> assert() + test "if ofp_version in the hello message is mismatched" do + {:ok, socket} = connect() + assert_receive {:tcp, ^socket, <<4::8, @ofp_hello::8, _::binary>>}, 4000 + :ok = send_msg(socket, Openflow.Hello.new(0x05)) + assert_receive {:tcp_closed, ^socket}, 4000 + end - _ = :gen_tcp.close(socket) + test "if feature_reply message sending is slow" do + {:ok, socket} = connect() + assert_receive {:tcp, ^socket, <<4::8, @ofp_hello::8, _::binary>>}, 4000 + :ok = send_msg(socket, Openflow.Hello.new(0x04)) + assert_receive {:tcp, ^socket, <<4::8, @ofp_features_request::8, _::binary>>}, 4000 + assert_receive {:tcp_closed, ^socket}, 4000 + end + + test "if the datapath didn't reply to the ping from controller" do + {:ok, socket} = connect() + assert_receive {:tcp, ^socket, <<4::8, @ofp_hello::8, _::binary>>}, 4000 + :ok = send_msg(socket, Openflow.Hello.new(0x04)) + assert_receive {:tcp, ^socket, <<4::8, @ofp_features_request::8, _::binary>>}, 4000 + :ok = send_msg(socket, %Openflow.Features.Reply{datapath_id: @datapath_id}) + + # Idle check running after 5sec + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + + # Controller tries ping to the datapath per five seconds + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp, ^socket, <<4::8, @ofp_echo_request::8, _::binary>>}, 6000 + assert_receive {:tcp_closed, ^socket}, 60_000 + end + + test "if the main connection down" do + # Main connection + {:ok, socket_main} = connect() + assert_receive {:tcp, ^socket_main, <<4::8, @ofp_hello::8, _::binary>>}, 4000 + :ok = send_msg(socket_main, Openflow.Hello.new(0x04)) + assert_receive {:tcp, ^socket_main, <<4::8, @ofp_features_request::8, _::binary>>}, 4000 + :ok = send_msg(socket_main, %Openflow.Features.Reply{datapath_id: @datapath_id}) + + # Aux connection(id: 1) + {:ok, socket_aux} = connect() + assert_receive {:tcp, ^socket_aux, <<4::8, @ofp_hello::8, _::binary>>}, 4000 + :ok = send_msg(socket_aux, Openflow.Hello.new(0x04)) + assert_receive {:tcp, ^socket_aux, <<4::8, @ofp_features_request::8, _::binary>>}, 4000 + :ok = send_msg(socket_aux, %Openflow.Features.Reply{datapath_id: @datapath_id, aux_id: 1}) + + # Close the main connection + :ok = :gen_tcp.close(socket_main) + + # We expect that the aux connection was closed + assert_receive {:tcp_closed, ^socket_aux}, 60_000 end end