73 lines
1.9 KiB
Elixir
73 lines
1.9 KiB
Elixir
defmodule Chronoscope.NTSTest do
|
|
use Chronoscope.Case
|
|
|
|
alias Chronoscope.NTS.SSLMock
|
|
|
|
import Chronoscope.NTS
|
|
import Mox
|
|
|
|
setup :verify_on_exit!
|
|
setup :set_mox_global
|
|
|
|
defp expect_key_establishment(host, port) do
|
|
host_charlist = to_charlist(host)
|
|
|
|
SSLMock
|
|
|> expect(:connect, fn ^host_charlist, ^port, _, _ -> {:ok, :socket} end)
|
|
|> expect(:send, fn :socket, _ -> send_ssl_response([]) end)
|
|
|> expect(:peercert, fn :socket -> {:ok, peercert()} end)
|
|
|> expect(:close, fn :socket -> :ok end)
|
|
end
|
|
|
|
setup do
|
|
clear()
|
|
on_exit(fn -> clear() end)
|
|
end
|
|
|
|
describe "Chronoscope.NTS.healthy?()" do
|
|
test "is healthy" do
|
|
assert healthy?() == true
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.NTS.list()" do
|
|
test "shows empty client list" do
|
|
assert list() == []
|
|
end
|
|
|
|
test "shows all clients" do
|
|
expect_key_establishment("localhost", 4444)
|
|
key_establishment("localhost", 4444)
|
|
|
|
assert [%{host: "localhost", key_establishment_response: _, last_key_establishment: _, port: 4444}] = list()
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.NTS.remove()" do
|
|
test "removes a client" do
|
|
expect_key_establishment("localhost", 4444)
|
|
key_establishment("localhost", 4444)
|
|
|
|
assert {:ok, _} = remove("localhost", 4444)
|
|
assert list() == []
|
|
end
|
|
|
|
test "does nothing if the client doesn't exist" do
|
|
expect_key_establishment("localhost", 4444)
|
|
key_establishment("localhost", 4444)
|
|
|
|
assert remove("localhost", 1111) == {:error, :notfound}
|
|
assert [%{host: "localhost", key_establishment_response: _, last_key_establishment: _, port: 4444}] = list()
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.NTS.key_establishment()" do
|
|
test "creates and reuses a client" do
|
|
expect_key_establishment("localhost", 4444)
|
|
|
|
assert {:ok, %{cert_expiration: _}} = key_establishment("localhost", 4444)
|
|
assert {:ok, %{cert_expiration: _}} = key_establishment("localhost", 4444)
|
|
end
|
|
end
|
|
end
|