Clean up code
This commit is contained in:
parent
bb33c29c44
commit
c1c46707ba
|
@ -27,13 +27,14 @@ defmodule Chronoscope.NTS do
|
||||||
GenServer.call(pid, :key_establishment)
|
GenServer.call(pid, :key_establishment)
|
||||||
|
|
||||||
[] ->
|
[] ->
|
||||||
NTS.DynamicSupervisor
|
{:ok, pid} =
|
||||||
|> DynamicSupervisor.start_child({
|
NTS.DynamicSupervisor
|
||||||
NTS.Client,
|
|> DynamicSupervisor.start_child({
|
||||||
host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}
|
NTS.Client,
|
||||||
})
|
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
|
||||||
|
|
|
@ -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])
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -5,77 +5,99 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
|
||||||
|
|
||||||
setup :verify_on_exit!
|
setup :verify_on_exit!
|
||||||
|
|
||||||
test "requires a host name", %{conn: conn} do
|
describe "/api/v1/nts/key-establishment" do
|
||||||
response =
|
test "requires a host name", %{conn: conn} do
|
||||||
conn
|
response =
|
||||||
|> get(~p"/api/v1/nts/key-establishment")
|
conn
|
||||||
|> json_response(400)
|
|> get(~p"/api/v1/nts/key-establishment")
|
||||||
|
|> json_response(400)
|
||||||
|
|
||||||
assert %{"error" => "missing host"} == response
|
assert %{"error" => "missing host"} == response
|
||||||
end
|
end
|
||||||
|
|
||||||
test "uses the given port number", %{conn: conn} do
|
test "truncates the host name", %{conn: conn} do
|
||||||
Chronoscope.NTS.BehaviourMock
|
Chronoscope.NTS.BehaviourMock
|
||||||
|> expect(:key_establishment, fn "localhost", 4461 -> {:ok, %{status: :ok}} end)
|
|> 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 =
|
response =
|
||||||
conn
|
conn
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=4461")
|
|> get(
|
||||||
|> json_response(200)
|
~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
|
assert %{"status" => "ok", "response" => %{"cookies" => 0}} == response
|
||||||
end
|
end
|
||||||
|
|
||||||
test "handles an out of range port number", %{conn: conn} do
|
test "uses the given port number", %{conn: conn} do
|
||||||
response =
|
Chronoscope.NTS.BehaviourMock
|
||||||
conn
|
|> expect(:key_establishment, fn "localhost", 4461 -> {:ok, %{status: :ok}} end)
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=65536")
|
|
||||||
|> json_response(400)
|
|
||||||
|
|
||||||
assert %{"error" => "port out of range"} == response
|
response =
|
||||||
end
|
conn
|
||||||
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=4461")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
test "handles a negative port number", %{conn: conn} do
|
assert %{"status" => "ok", "response" => %{"cookies" => 0}} == response
|
||||||
response =
|
end
|
||||||
conn
|
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=-4460")
|
|
||||||
|> json_response(400)
|
|
||||||
|
|
||||||
assert %{"error" => "port out of range"} == response
|
test "handles an out of range port number", %{conn: conn} do
|
||||||
end
|
response =
|
||||||
|
conn
|
||||||
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=65536")
|
||||||
|
|> json_response(400)
|
||||||
|
|
||||||
test "handles a bad port number", %{conn: conn} do
|
assert %{"error" => "port out of range"} == response
|
||||||
response =
|
end
|
||||||
conn
|
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=AA60")
|
|
||||||
|> json_response(400)
|
|
||||||
|
|
||||||
assert %{"error" => "invalid port"} == response
|
test "handles a negative port number", %{conn: conn} do
|
||||||
end
|
response =
|
||||||
|
conn
|
||||||
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=-4460")
|
||||||
|
|> json_response(400)
|
||||||
|
|
||||||
test "returns an empty successful response", %{conn: conn} do
|
assert %{"error" => "port out of range"} == response
|
||||||
Chronoscope.NTS.BehaviourMock
|
end
|
||||||
|> expect(:key_establishment, fn "localhost", 4460 -> {:ok, %{status: :ok}} end)
|
|
||||||
|
|
||||||
response =
|
test "handles a bad port number", %{conn: conn} do
|
||||||
conn
|
response =
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost")
|
conn
|
||||||
|> json_response(200)
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=AA60")
|
||||||
|
|> json_response(400)
|
||||||
|
|
||||||
assert %{"status" => "ok", "response" => %{"cookies" => 0}} == response
|
assert %{"error" => "invalid port"} == response
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns a full successful response", %{conn: conn} do
|
test "returns an empty successful response", %{conn: conn} do
|
||||||
Chronoscope.NTS.BehaviourMock
|
Chronoscope.NTS.BehaviourMock
|
||||||
|> expect(:key_establishment, fn "localhost", 4460 ->
|
|> expect(:key_establishment, fn "localhost", 4460 -> {:ok, %{status: :ok}} end)
|
||||||
{:ok, %{cookies: [[], [], []], cookie_length: 300}}
|
|
||||||
end)
|
|
||||||
|
|
||||||
response =
|
response =
|
||||||
conn
|
conn
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost")
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost")
|
||||||
|> json_response(200)
|
|> json_response(200)
|
||||||
|
|
||||||
assert %{"status" => "ok", "response" => %{"cookies" => 3, "cookie_length" => 300}} == response
|
assert %{"status" => "ok", "response" => %{"cookies" => 0}} == response
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns a full successful response", %{conn: conn} do
|
||||||
|
Chronoscope.NTS.BehaviourMock
|
||||||
|
|> expect(:key_establishment, fn "localhost", 4460 ->
|
||||||
|
{:ok, %{cookies: [[], [], []], cookie_length: 300}}
|
||||||
|
end)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert %{"status" => "ok", "response" => %{"cookies" => 3, "cookie_length" => 300}} == response
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue