Add tests
This commit is contained in:
parent
31431bcf23
commit
68fb573d01
|
@ -12,8 +12,7 @@ defmodule Chronoscope.Gemini.ConnectionClient do
|
||||||
def connect(%{host: host, port: port, path: _} = resource) do
|
def connect(%{host: host, port: port, path: _} = resource) do
|
||||||
case ssl_connect(host, port) do
|
case ssl_connect(host, port) do
|
||||||
{:ok, socket} -> make_request(socket, resource)
|
{:ok, socket} -> make_request(socket, resource)
|
||||||
{:error, {:tls_alert, {:handshake_failure, error}}} -> {:error, String.trim("#{error}")}
|
{:error, {:tls_alert, {:handshake_failure, error}}} -> {:error, handshake_failure_message("#{error}")}
|
||||||
{:error, {:tls_alert, {:no_application_protocol, error}}} -> {:error, String.trim("#{error}")}
|
|
||||||
{:error, :timeout} -> {:error, :timeout}
|
{:error, :timeout} -> {:error, :timeout}
|
||||||
{:error, error} -> {:error, inspect(error)}
|
{:error, error} -> {:error, inspect(error)}
|
||||||
error -> {:error, inspect(error)}
|
error -> {:error, inspect(error)}
|
||||||
|
@ -73,4 +72,17 @@ defmodule Chronoscope.Gemini.ConnectionClient do
|
||||||
{:error, error}
|
{:error, error}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp handshake_failure_message(error) do
|
||||||
|
cond do
|
||||||
|
error =~ ~r/\{bad_cert,hostname_check_failed\}$/ ->
|
||||||
|
"The certificate is NOT trusted. The name in the certificate does not match the expected."
|
||||||
|
|
||||||
|
error =~ ~r/\{bad_cert,unable_to_match_altnames\}$/ ->
|
||||||
|
"The certificate is NOT trusted. The name in the certificate does not match the expected."
|
||||||
|
|
||||||
|
true ->
|
||||||
|
String.trim(error)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,7 +12,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do
|
||||||
def key_establishment(%{host: host, port: port}) do
|
def key_establishment(%{host: host, port: port}) do
|
||||||
case ssl_connect(host, port) do
|
case ssl_connect(host, port) do
|
||||||
{:ok, socket} -> perform_key_establishment(socket)
|
{:ok, socket} -> perform_key_establishment(socket)
|
||||||
{:error, {:tls_alert, {:handshake_failure, error}}} -> handshake_failure_message("#{error}")
|
{:error, {:tls_alert, {:handshake_failure, error}}} -> {:error, handshake_failure_message("#{error}")}
|
||||||
{:error, {:tls_alert, {:no_application_protocol, error}}} -> {:error, String.trim("#{error}")}
|
{:error, {:tls_alert, {:no_application_protocol, error}}} -> {:error, String.trim("#{error}")}
|
||||||
{:error, :timeout} -> {:error, :timeout}
|
{:error, :timeout} -> {:error, :timeout}
|
||||||
{:error, error} -> {:error, inspect(error)}
|
{:error, error} -> {:error, inspect(error)}
|
||||||
|
@ -64,10 +64,13 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do
|
||||||
defp handshake_failure_message(error) do
|
defp handshake_failure_message(error) do
|
||||||
cond do
|
cond do
|
||||||
error =~ ~r/\{bad_cert,hostname_check_failed\}$/ ->
|
error =~ ~r/\{bad_cert,hostname_check_failed\}$/ ->
|
||||||
{:error, "The certificate is NOT trusted. The name in the certificate does not match the expected."}
|
"The certificate is NOT trusted. The name in the certificate does not match the expected."
|
||||||
|
|
||||||
|
error =~ ~r/\{bad_cert,unable_to_match_altnames\}$/ ->
|
||||||
|
"The certificate is NOT trusted. The name in the certificate does not match the expected."
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
{:error, String.trim(error)}
|
String.trim(error)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -55,5 +55,51 @@ defmodule Chronoscope.Gemini.ConnectionClientTest do
|
||||||
|
|
||||||
assert {:error, "bad response: HTTP/1.1 400 Bad Request\r\nServer: nginx\r\n"} = ConnectionClient.connect(resource)
|
assert {:error, "bad response: HTTP/1.1 400 Bad Request\r\nServer: nginx\r\n"} = ConnectionClient.connect(resource)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "handles a bad certificate hostname failure", %{resource: resource} do
|
||||||
|
SSLMock
|
||||||
|
|> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout ->
|
||||||
|
{:error, {:tls_alert, {:handshake_failure, "connection failed {bad_cert,hostname_check_failed}"}}}
|
||||||
|
end)
|
||||||
|
|> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout ->
|
||||||
|
{:error, {:tls_alert, {:handshake_failure, "connection failed {bad_cert,unable_to_match_altnames}"}}}
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert ConnectionClient.connect(resource) ==
|
||||||
|
{:error, "The certificate is NOT trusted. The name in the certificate does not match the expected."}
|
||||||
|
|
||||||
|
assert ConnectionClient.connect(resource) ==
|
||||||
|
{:error, "The certificate is NOT trusted. The name in the certificate does not match the expected."}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "handles a handshake failure", %{resource: resource} do
|
||||||
|
SSLMock
|
||||||
|
|> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout ->
|
||||||
|
{:error, {:tls_alert, {:handshake_failure, "unsatisfactory handshake"}}}
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert ConnectionClient.connect(resource) == {:error, "unsatisfactory handshake"}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "handles a timeout failure", %{resource: resource} do
|
||||||
|
SSLMock
|
||||||
|
|> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout -> {:error, :timeout} end)
|
||||||
|
|
||||||
|
assert ConnectionClient.connect(resource) == {:error, :timeout}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "handles an unknown error", %{resource: resource} do
|
||||||
|
SSLMock
|
||||||
|
|> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout -> {:error, {:unsatisfactory, "client"}} end)
|
||||||
|
|
||||||
|
assert ConnectionClient.connect(resource) == {:error, "{:unsatisfactory, \"client\"}"}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "handles an unexpected error", %{resource: resource} do
|
||||||
|
SSLMock
|
||||||
|
|> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout -> {:unsatisfactory, "client"} end)
|
||||||
|
|
||||||
|
assert ConnectionClient.connect(resource) == {:error, "{:unsatisfactory, \"client\"}"}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -78,6 +78,12 @@ defmodule Chronoscope.NTS.KeyEstablishmentClientTest do
|
||||||
|> expect(:connect, fn ~c"localhost", 2222, _tls_options, @timeout ->
|
|> expect(:connect, fn ~c"localhost", 2222, _tls_options, @timeout ->
|
||||||
{:error, {:tls_alert, {:handshake_failure, "connection failed {bad_cert,hostname_check_failed}"}}}
|
{:error, {:tls_alert, {:handshake_failure, "connection failed {bad_cert,hostname_check_failed}"}}}
|
||||||
end)
|
end)
|
||||||
|
|> expect(:connect, fn ~c"localhost", 2222, _tls_options, @timeout ->
|
||||||
|
{:error, {:tls_alert, {:handshake_failure, "connection failed {bad_cert,unable_to_match_altnames}"}}}
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert KeyEstablishmentClient.key_establishment(%{host: "localhost", port: 2222}) ==
|
||||||
|
{:error, "The certificate is NOT trusted. The name in the certificate does not match the expected."}
|
||||||
|
|
||||||
assert KeyEstablishmentClient.key_establishment(%{host: "localhost", port: 2222}) ==
|
assert KeyEstablishmentClient.key_establishment(%{host: "localhost", port: 2222}) ==
|
||||||
{:error, "The certificate is NOT trusted. The name in the certificate does not match the expected."}
|
{:error, "The certificate is NOT trusted. The name in the certificate does not match the expected."}
|
||||||
|
|
Loading…
Reference in New Issue