quality: Add test case for Tres.SecureChannel

This commit is contained in:
Eishun Kondoh 2019-05-10 23:57:21 +09:00
parent 2239fcf1cd
commit f7c5d203f7
2 changed files with 70 additions and 22 deletions

View file

@ -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

View file

@ -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