chronoscope/test/chronoscope/nts/client_test.exs

84 lines
2.7 KiB
Elixir

defmodule Chronoscope.NTS.ClientTest do
use Chronoscope.Case, async: true
alias Chronoscope.NTS.Certificate
alias Chronoscope.NTS.DateTimeMock
alias Chronoscope.NTS.SSLMock
import Chronoscope.NTS.Client
import Mox
setup :verify_on_exit!
setup _tags do
DateTimeMock
|> stub(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end)
:ok
end
describe "Chronoscope.NTS.Client.init()" do
test "initializes successfully" do
assert init(%{host: "localhost", port: 3333}) ==
{:ok,
%{
host: "localhost",
key_establishment_response: {:error, "initializing"},
last_key_establishment: ~U[2024-03-31 01:23:15Z],
port: 3333
}}
end
end
describe "Chronoscope.NTS.Client.handle_call()" do
test ":terminate" do
assert handle_call(:terminate, nil, %{state: true}) == {:stop, :normal, self(), %{state: true}}
end
test ":list" do
assert handle_call(:list, nil, %{state: true}) == {:reply, %{state: true}, %{state: true}}
end
test ":key_establishment - cached" do
assert handle_call(:key_establishment, nil, %{
host: "localhost",
key_establishment_response: {:error, "initializing"},
last_key_establishment: ~U[2024-03-31 01:23:45Z],
port: 3333
}) ==
{:reply, {:error, "initializing"},
%{
host: "localhost",
key_establishment_response: {:error, "initializing"},
last_key_establishment: ~U[2024-03-31 01:23:45Z],
port: 3333
}}
end
test ":key_establishment - not cached" do
peercert = peercert()
peercert_expiration = Certificate.expiration_date(peercert)
SSLMock
|> expect(:connect, fn ~c"localhost", 3333, _, _ -> {:ok, :socket} end)
|> expect(:send, fn :socket, _ -> send_ssl_response([]) end)
|> expect(:peercert, fn :socket -> {:ok, peercert} end)
|> expect(:close, fn :socket -> :ok end)
assert {:reply, {:ok, %{cert_expiration: ^peercert_expiration}},
%{
host: "localhost",
key_establishment_response: {:ok, %{cert_expiration: ^peercert_expiration}},
last_key_establishment: ~U[2024-03-31 01:23:45Z],
port: 3333
}} =
handle_call(:key_establishment, nil, %{
host: "localhost",
key_establishment_response: {:error, "initializing"},
last_key_establishment: ~U[2024-03-31 01:23:00Z],
port: 3333
})
end
end
end