Refactor tests

This commit is contained in:
Mike Cifelli 2024-04-01 10:48:49 -04:00
parent c1c46707ba
commit d278cfa353
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
5 changed files with 156 additions and 146 deletions

View File

@ -8,36 +8,40 @@ defmodule Chronoscope.NTS.CertificateTest do
setup :verify_on_exit! setup :verify_on_exit!
test "parses the expiration date of a certificate" do describe "Chronoscope.NTS.Certificate.expiration_date()" do
{:ok, expiration, _} = test "parses the expiration date of a certificate" do
:secp256r1 {:ok, expiration, _} =
|> X509.PrivateKey.new_ec() :secp256r1
|> X509.Certificate.self_signed("/C=US/ST=CA/L=San Francisco/O=Acme/CN=Test", validity: 12) |> X509.PrivateKey.new_ec()
|> X509.Certificate.to_der() |> X509.Certificate.self_signed("/C=US/ST=CA/L=San Francisco/O=Acme/CN=Test", validity: 12)
|> expiration_date() |> X509.Certificate.to_der()
|> DateTime.from_iso8601() |> expiration_date()
|> DateTime.from_iso8601()
assert DateTime.diff(expiration, DateTime.utc_now(), :day) == 12 assert DateTime.diff(expiration, DateTime.utc_now(), :day) == 12
end
end end
test "converts certificate datetime to iso8601" do describe "Chronoscope.NTS.Certificate.cert_time_to_iso8601()" do
DateTimeMock test "converts certificate datetime to iso8601" do
|> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end) DateTimeMock
|> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end)
assert cert_time_to_iso8601("240326110000Z") == "2024-03-26T11:00:00Z" assert cert_time_to_iso8601("240326110000Z") == "2024-03-26T11:00:00Z"
end end
test "handles century rollover" do test "handles century rollover" do
DateTimeMock DateTimeMock
|> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end) |> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end)
assert cert_time_to_iso8601("010326110000Z") == "2101-03-26T11:00:00Z" assert cert_time_to_iso8601("010326110000Z") == "2101-03-26T11:00:00Z"
end end
test "handles millenium rollover" do test "handles millenium rollover" do
DateTimeMock DateTimeMock
|> expect(:utc_now, fn -> ~U[2999-03-31 01:23:45Z] end) |> expect(:utc_now, fn -> ~U[2999-03-31 01:23:45Z] end)
assert cert_time_to_iso8601("010326110000Z") == "3001-03-26T11:00:00Z" assert cert_time_to_iso8601("010326110000Z") == "3001-03-26T11:00:00Z"
end
end end
end end

View File

@ -3,8 +3,10 @@ defmodule Chronoscope.NTS.KeyEstablishmentRequestTest do
import Chronoscope.NTS.KeyEstablishmentRequest import Chronoscope.NTS.KeyEstablishmentRequest
test "builds request" do describe "Chronoscope.NTS.KeyEstablishmentRequest.create()" do
assert create() == test "builds request" do
<<0x80, 0x01, 0x00, 0x02, 0x00, 0x00, 0x80, 0x04, 0x00, 0x04, 0x00, 0x1E, 0x00, 0x0F, 0x80, 0x00, 0x00, 0x00>> assert create() ==
<<0x80, 0x01, 0x00, 0x02, 0x00, 0x00, 0x80, 0x04, 0x00, 0x04, 0x00, 0x1E, 0x00, 0x0F, 0x80, 0x00, 0x00, 0x00>>
end
end end
end end

View File

@ -3,142 +3,144 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do
import Chronoscope.NTS.KeyEstablishmentResponse import Chronoscope.NTS.KeyEstablishmentResponse
test "handles empty response" do describe "Chronoscope.NTS.KeyEstablishmentResponse.parse()" do
assert parse([]) == {:ok, %{}} test "handles empty response" do
end assert parse([]) == {:ok, %{}}
end
test "ignores unkown record" do test "ignores unkown record" do
assert parse([0x80, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]) == {:ok, %{}} assert parse([0x80, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]) == {:ok, %{}}
end end
test "handles end of message record" do test "handles end of message record" do
assert parse([0x80, 0x00, 0x00, 0x00]) == {:ok, %{}} assert parse([0x80, 0x00, 0x00, 0x00]) == {:ok, %{}}
end end
test "handles next protocol negotiation record" do test "handles next protocol negotiation record" do
assert parse([0x80, 0x01, 0x00, 0x02, 0x00, 0x00]) == {:ok, %{next_protocols: ["NTPv4"]}} assert parse([0x80, 0x01, 0x00, 0x02, 0x00, 0x00]) == {:ok, %{next_protocols: ["NTPv4"]}}
end end
test "does not handle next protocol negotiation record without critical bit" do test "does not handle next protocol negotiation record without critical bit" do
assert parse([0x00, 0x01, 0x00, 0x02, 0x00, 0x00]) == {:ok, %{}} assert parse([0x00, 0x01, 0x00, 0x02, 0x00, 0x00]) == {:ok, %{}}
end end
test "handles empty next protocols" do test "handles empty next protocols" do
assert parse([0x80, 0x01, 0x00, 0x00]) == {:ok, %{next_protocols: []}} assert parse([0x80, 0x01, 0x00, 0x00]) == {:ok, %{next_protocols: []}}
end end
test "handles multiple next protocols" do test "handles multiple next protocols" do
assert parse([0x80, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00]) == assert parse([0x80, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00]) ==
{:ok, %{next_protocols: ["NTPv4", "UNASSIGNED", "NTPv4"]}} {:ok, %{next_protocols: ["NTPv4", "UNASSIGNED", "NTPv4"]}}
end end
test "handles aead algorithm negotiation record" do test "handles aead algorithm negotiation record" do
assert parse([0x80, 0x04, 0x00, 0x02, 0x00, 0x0F]) == {:ok, %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256"]}} assert parse([0x80, 0x04, 0x00, 0x02, 0x00, 0x0F]) == {:ok, %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256"]}}
end end
test "handles aead algorithm negotiation record without critical bit" do test "handles aead algorithm negotiation record without critical bit" do
assert parse([0x00, 0x04, 0x00, 0x02, 0x00, 0x1E]) == {:ok, %{aead_algorithms: ["AEAD_AES_128_GCM_SIV"]}} assert parse([0x00, 0x04, 0x00, 0x02, 0x00, 0x1E]) == {:ok, %{aead_algorithms: ["AEAD_AES_128_GCM_SIV"]}}
end end
test "handles empty aead algorithms" do test "handles empty aead algorithms" do
assert parse([0x80, 0x04, 0x00, 0x00]) == {:ok, %{aead_algorithms: []}} assert parse([0x80, 0x04, 0x00, 0x00]) == {:ok, %{aead_algorithms: []}}
end end
test "handles multiple aead algorithms" do test "handles multiple aead algorithms" do
assert parse([0x80, 0x04, 0x00, 0x06, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x0F]) == assert parse([0x80, 0x04, 0x00, 0x06, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x0F]) ==
{:ok, %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"]}} {:ok, %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"]}}
end end
test "handles error record" do test "handles error record" do
assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{error: "Bad Request"}} assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{error: "Bad Request"}}
end end
test "handles unknown error record" do test "handles unknown error record" do
assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x99]) == {:ok, %{error: "153"}} assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x99]) == {:ok, %{error: "153"}}
end end
test "does not handle error record without critical bit" do test "does not handle error record without critical bit" do
assert parse([0x00, 0x02, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{}} assert parse([0x00, 0x02, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{}}
end end
test "handles warning record" do test "handles warning record" do
assert parse([0x80, 0x03, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{warning: "1"}} assert parse([0x80, 0x03, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{warning: "1"}}
end end
test "does not handle warning record without critical bit" do test "does not handle warning record without critical bit" do
assert parse([0x00, 0x03, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{}} assert parse([0x00, 0x03, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{}}
end end
test "handles server record" do test "handles server record" do
assert parse([0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == {:ok, %{server: "127.0.0.1"}} assert parse([0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == {:ok, %{server: "127.0.0.1"}}
end end
test "handles server record without critical bit" do test "handles server record without critical bit" do
assert parse([0x00, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == {:ok, %{server: "127.0.0.1"}} assert parse([0x00, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == {:ok, %{server: "127.0.0.1"}}
end end
test "handles port record" do test "handles port record" do
assert parse([0x80, 0x07, 0x00, 0x02, 0x04, 0xCE]) == {:ok, %{port: 1230}} assert parse([0x80, 0x07, 0x00, 0x02, 0x04, 0xCE]) == {:ok, %{port: 1230}}
end end
test "handles port record without critical bit" do test "handles port record without critical bit" do
assert parse([0x00, 0x07, 0x00, 0x02, 0x04, 0xCE]) == {:ok, %{port: 1230}} assert parse([0x00, 0x07, 0x00, 0x02, 0x04, 0xCE]) == {:ok, %{port: 1230}}
end end
test "handles cookie record" do test "handles cookie record" do
assert parse([0x80, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) == assert parse([0x80, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) ==
{:ok, %{cookies: [~c"chocolate_chip"], cookie_length: 14}} {:ok, %{cookies: [~c"chocolate_chip"], cookie_length: 14}}
end end
test "handles cookie record without critical bit" do test "handles cookie record without critical bit" do
assert parse([0x00, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) == assert parse([0x00, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) ==
{:ok, %{cookies: [~c"chocolate_chip"], cookie_length: 14}} {:ok, %{cookies: [~c"chocolate_chip"], cookie_length: 14}}
end end
test "sets cookie length to longest cookie" do test "sets cookie length to longest cookie" do
assert parse( assert parse(
[0x80, 0x05, 0x00, 0x01, ?c] ++
[0x80, 0x05, 0x00, 0x03, ?c, ?c, ?c] ++
[0x80, 0x05, 0x00, 0x01, ?c]
) ==
{:ok, %{cookies: [~c"c", ~c"ccc", ~c"c"], cookie_length: 3}}
end
test "handles full response" do
assert parse(
[0x80, 0x01, 0x00, 0x02, 0x00, 0x00] ++
[0x80, 0x04, 0x00, 0x06, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x0F] ++
[0x80, 0x05, 0x00, 0x01, ?c] ++ [0x80, 0x05, 0x00, 0x01, ?c] ++
[0x00, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03] ++ [0x80, 0x05, 0x00, 0x03, ?c, ?c, ?c] ++
[0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1] ++ [0x80, 0x05, 0x00, 0x01, ?c]
[0x80, 0x07, 0x00, 0x02, 0x04, 0xCE] ++ ) ==
[0x80, 0x00, 0x00, 0x00] {:ok, %{cookies: [~c"c", ~c"ccc", ~c"c"], cookie_length: 3}}
) == end
{:ok,
%{
cookies: [~c"c"],
aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"],
cookie_length: 1,
next_protocols: ["NTPv4"],
port: 1230,
server: "127.0.0.1"
}}
end
test "doesn't read past end of message record" do test "handles full response" do
assert parse( assert parse(
[0x80, 0x01, 0x00, 0x02, 0x00, 0x00] ++ [0x80, 0x01, 0x00, 0x02, 0x00, 0x00] ++
[0x00, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03] ++ [0x80, 0x04, 0x00, 0x06, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x0F] ++
[0x80, 0x04, 0x00, 0x02, 0x00, 0x1E] ++ [0x80, 0x05, 0x00, 0x01, ?c] ++
[0x80, 0x00, 0x00, 0x00] ++ [0x00, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03] ++
[0x80, 0x05, 0x00, 0x01, ?c] ++ [0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1] ++
[0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1] ++ [0x80, 0x07, 0x00, 0x02, 0x04, 0xCE] ++
[0x80, 0x07, 0x00, 0x02, 0x04, 0xCE] [0x80, 0x00, 0x00, 0x00]
) == ) ==
{:ok, {:ok,
%{ %{
aead_algorithms: ["AEAD_AES_128_GCM_SIV"], cookies: [~c"c"],
next_protocols: ["NTPv4"] aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"],
}} cookie_length: 1,
next_protocols: ["NTPv4"],
port: 1230,
server: "127.0.0.1"
}}
end
test "doesn't read past end of message record" do
assert parse(
[0x80, 0x01, 0x00, 0x02, 0x00, 0x00] ++
[0x00, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03] ++
[0x80, 0x04, 0x00, 0x02, 0x00, 0x1E] ++
[0x80, 0x00, 0x00, 0x00] ++
[0x80, 0x05, 0x00, 0x01, ?c] ++
[0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1] ++
[0x80, 0x07, 0x00, 0x02, 0x04, 0xCE]
) ==
{:ok,
%{
aead_algorithms: ["AEAD_AES_128_GCM_SIV"],
next_protocols: ["NTPv4"]
}}
end
end end
end end

View File

@ -1,12 +1,14 @@
defmodule ChronoscopeWeb.API.V1.HealthControllerTest do defmodule ChronoscopeWeb.API.V1.HealthControllerTest do
use ChronoscopeWeb.ConnCase, async: true use ChronoscopeWeb.ConnCase, async: true
test "indicates health", %{conn: conn} do describe "GET /api/v1/health" do
response = test "is healthy", %{conn: conn} do
conn response =
|> get(~p"/api/v1/health") conn
|> json_response(200) |> get(~p"/api/v1/health")
|> json_response(200)
assert %{"healthy" => true} == response assert %{"healthy" => true} == response
end
end end
end end

View File

@ -5,7 +5,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
setup :verify_on_exit! setup :verify_on_exit!
describe "/api/v1/nts/key-establishment" do describe "GET /api/v1/nts/key-establishment" do
test "requires a host name", %{conn: conn} do test "requires a host name", %{conn: conn} do
response = response =
conn conn