Clean up code

This commit is contained in:
Mike Cifelli 2024-04-01 10:40:35 -04:00
parent bb33c29c44
commit c1c46707ba
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
7 changed files with 115 additions and 72 deletions

View File

@ -27,13 +27,14 @@ defmodule Chronoscope.NTS do
GenServer.call(pid, :key_establishment) GenServer.call(pid, :key_establishment)
[] -> [] ->
{:ok, pid} =
NTS.DynamicSupervisor NTS.DynamicSupervisor
|> DynamicSupervisor.start_child({ |> DynamicSupervisor.start_child({
NTS.Client, NTS.Client,
host: host, port: port, name: {:via, Registry, {NTS.Registry, name}} host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}
}) })
key_establishment(host, port) GenServer.call(pid, :key_establishment)
end end
end end
end end

View File

@ -6,6 +6,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
alias Chronoscope.NTS alias Chronoscope.NTS
@default_port 4460 @default_port 4460
@max_host_length 255
def get(conn, %{"host" => host, "port" => port}) do def get(conn, %{"host" => host, "port" => port}) do
try do try do
@ -24,7 +25,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
end end
defp handle_get(conn, %{host: host, port: port}) when port > 0 and port < 65536 do defp handle_get(conn, %{host: host, port: port}) when port > 0 and port < 65536 do
case nts_behaviour().key_establishment(host, port) do case key_establishment_response(host, port) do
{:ok, response} -> {:ok, response} ->
json(conn, %{status: :ok, response: format_response(response)}) json(conn, %{status: :ok, response: format_response(response)})
@ -37,6 +38,12 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
bad_request_response(conn, "port out of range") bad_request_response(conn, "port out of range")
end end
defp key_establishment_response(host, port) do
host
|> String.slice(0, @max_host_length)
|> nts_behaviour().key_establishment(port)
end
defp format_response(response) do defp format_response(response) do
response response
|> Map.take([:aead_algorithms, :cert_expiration, :cookie_length, :cookies, :next_protocols, :port, :server]) |> Map.take([:aead_algorithms, :cert_expiration, :cookie_length, :cookies, :next_protocols, :port, :server])

View File

@ -1,5 +1,7 @@
defmodule Chronoscope.NTS.CertificateTest do defmodule Chronoscope.NTS.CertificateTest do
use ExUnit.Case use Chronoscope.Case
alias Chronoscope.NTS.DateTimeMock
import Chronoscope.NTS.Certificate import Chronoscope.NTS.Certificate
import Mox import Mox
@ -7,9 +9,6 @@ defmodule Chronoscope.NTS.CertificateTest do
setup :verify_on_exit! setup :verify_on_exit!
test "parses the expiration date of a certificate" do test "parses the expiration date of a certificate" do
Chronoscope.NTS.DateTimeMock
|> stub(:utc_now, &DateTime.utc_now/0)
{:ok, expiration, _} = {:ok, expiration, _} =
:secp256r1 :secp256r1
|> X509.PrivateKey.new_ec() |> X509.PrivateKey.new_ec()
@ -22,21 +21,21 @@ defmodule Chronoscope.NTS.CertificateTest do
end end
test "converts certificate datetime to iso8601" do test "converts certificate datetime to iso8601" do
Chronoscope.NTS.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("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
Chronoscope.NTS.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
Chronoscope.NTS.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"

View File

@ -5,6 +5,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
setup :verify_on_exit! setup :verify_on_exit!
describe "/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
@ -14,6 +15,26 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
assert %{"error" => "missing host"} == response assert %{"error" => "missing host"} == response
end end
test "truncates the host name", %{conn: conn} do
Chronoscope.NTS.BehaviourMock
|> expect(
:key_establishment,
fn "test.example.com.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456",
4460 ->
{:ok, %{status: :ok}}
end
)
response =
conn
|> get(
~p"/api/v1/nts/key-establishment?host=test.example.com.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789"
)
|> json_response(200)
assert %{"status" => "ok", "response" => %{"cookies" => 0}} == response
end
test "uses the given port number", %{conn: conn} do test "uses the given port number", %{conn: conn} do
Chronoscope.NTS.BehaviourMock Chronoscope.NTS.BehaviourMock
|> expect(:key_establishment, fn "localhost", 4461 -> {:ok, %{status: :ok}} end) |> expect(:key_establishment, fn "localhost", 4461 -> {:ok, %{status: :ok}} end)
@ -78,4 +99,5 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
assert %{"status" => "ok", "response" => %{"cookies" => 3, "cookie_length" => 300}} == response assert %{"status" => "ok", "response" => %{"cookies" => 3, "cookie_length" => 300}} == response
end end
end
end end

8
test/support/case.ex Normal file
View File

@ -0,0 +1,8 @@
defmodule Chronoscope.Case do
use ExUnit.CaseTemplate
setup _tags do
Mox.stub_with(Chronoscope.NTS.DateTimeMock, Chronoscope.DateTime.Stub)
:ok
end
end

View File

@ -32,6 +32,7 @@ defmodule ChronoscopeWeb.ConnCase do
end end
setup _tags do setup _tags do
Mox.stub_with(Chronoscope.NTS.DateTimeMock, Chronoscope.DateTime.Stub)
{:ok, conn: Phoenix.ConnTest.build_conn()} {:ok, conn: Phoenix.ConnTest.build_conn()}
end end
end end

View File

@ -1,6 +1,11 @@
defmodule DateTime.Behaviour do defmodule Chronoscope.DateTime.Behaviour do
@callback utc_now :: DateTime.t() @callback utc_now :: DateTime.t()
end end
defmodule Chronoscope.DateTime.Stub do
@behaviour Chronoscope.DateTime.Behaviour
def utc_now(), do: DateTime.utc_now()
end
Mox.defmock(Chronoscope.NTS.BehaviourMock, for: Chronoscope.NTS.Behaviour) Mox.defmock(Chronoscope.NTS.BehaviourMock, for: Chronoscope.NTS.Behaviour)
Mox.defmock(Chronoscope.NTS.DateTimeMock, for: DateTime.Behaviour) Mox.defmock(Chronoscope.NTS.DateTimeMock, for: Chronoscope.DateTime.Behaviour)