Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Feb 23, 2024
1 parent cfb6416 commit 4c59d56
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions test/thousand_island/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ defmodule ThousandIsland.ServerTest do
end
end

defmodule ReadOpt do
use ThousandIsland.Handler

@impl ThousandIsland.Handler
def handle_data(data, socket, state) do
opts = [String.to_atom(data)]
ThousandIsland.Socket.send(socket, inspect(ThousandIsland.Socket.getopts(socket, opts)))
{:close, state}
end
end

defmodule Error do
use ThousandIsland.Handler

Expand Down Expand Up @@ -364,6 +375,66 @@ defmodule ThousandIsland.ServerTest do
end
end

describe "configuration" do
test "tcp should allow default options to be overridden" do
{:ok, _, port} = start_handler(ReadOpt, transport_options: [send_timeout: 1230])
{:ok, client} = :gen_tcp.connect(:localhost, port, active: false)
:gen_tcp.send(client, "send_timeout")
{:ok, ~c"{:ok, [send_timeout: 1230]}"} = :gen_tcp.recv(client, 0, 100)
end

test "tcp should not allow hardcoded options to be overridden" do
{:ok, _, port} = start_handler(ReadOpt, transport_options: [mode: :list])
{:ok, client} = :gen_tcp.connect(:localhost, port, active: false)
:gen_tcp.send(client, "mode")
{:ok, ~c"{:ok, [mode: :binary]}"} = :gen_tcp.recv(client, 0, 100)
end

test "ssl should allow default options to be overridden" do
{:ok, _, port} =
start_handler(ReadOpt,
transport_module: ThousandIsland.Transports.SSL,
transport_options: [
send_timeout: 1230,
certfile: Path.join(__DIR__, "../support/cert.pem"),
keyfile: Path.join(__DIR__, "../support/key.pem")
]
)

{:ok, client} =
:ssl.connect(:localhost, port,
active: false,
verify: :verify_none,
cacertfile: Path.join(__DIR__, "../support/ca.pem")
)

:ssl.send(client, "send_timeout")
{:ok, ~c"{:ok, [send_timeout: 1230]}"} = :ssl.recv(client, 0, 100)
end

test "ssl should not allow hardcoded options to be overridden" do
{:ok, _, port} =
start_handler(ReadOpt,
transport_module: ThousandIsland.Transports.SSL,
transport_options: [
mode: :list,
certfile: Path.join(__DIR__, "../support/cert.pem"),
keyfile: Path.join(__DIR__, "../support/key.pem")
]
)

{:ok, client} =
:ssl.connect(:localhost, port,
active: false,
verify: :verify_none,
cacertfile: Path.join(__DIR__, "../support/ca.pem")
)

:ssl.send(client, "mode")
{:ok, ~c"{:ok, [mode: :binary]}"} = :ssl.recv(client, 0, 100)
end
end

describe "invalid configuration" do
@tag capture_log: true
test "it should error if a certificate is not found" do
Expand Down

0 comments on commit 4c59d56

Please sign in to comment.