summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSimponic <loganhunt@simponic.xyz>2022-12-27 23:50:22 -0700
committerSimponic <loganhunt@simponic.xyz>2022-12-27 23:50:22 -0700
commit10bc34245e8e1e3ba63fb0720d3bcfb1119db921 (patch)
tree0158f37dcda36f36327e45c4ce7543e77c26c8a0 /test
parentf7c2ccbe26dc808e4a7eae9a378e6c382220961a (diff)
downloadchessh-10bc34245e8e1e3ba63fb0720d3bcfb1119db921.tar.gz
chessh-10bc34245e8e1e3ba63fb0720d3bcfb1119db921.zip
Initial erlang stuff
Diffstat (limited to 'test')
-rw-r--r--test/auth/password_test.exs7
-rw-r--r--test/auth/pubkey_test.exs6
-rw-r--r--test/schema/register_test.exs3
-rw-r--r--test/ssh/ssh_auth_test.exs94
4 files changed, 100 insertions, 10 deletions
diff --git a/test/auth/password_test.exs b/test/auth/password_test.exs
index 974f2fa..1516bdf 100644
--- a/test/auth/password_test.exs
+++ b/test/auth/password_test.exs
@@ -1,11 +1,10 @@
defmodule Chessh.Auth.PasswordAuthenticatorTest do
use ExUnit.Case
- alias Chessh.Player
- alias Chessh.Repo
+ alias Chessh.{Player, Repo}
@valid_user %{username: "logan", password: "password"}
- setup do
+ setup_all do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Chessh.Repo)
{:ok, _user} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user))
@@ -13,7 +12,7 @@ defmodule Chessh.Auth.PasswordAuthenticatorTest do
:ok
end
- test "User can sign in with their password" do
+ test "Password can authenticate a hashed password" do
assert Chessh.Auth.PasswordAuthenticator.authenticate(
String.to_charlist(@valid_user.username),
String.to_charlist(@valid_user.password)
diff --git a/test/auth/pubkey_test.exs b/test/auth/pubkey_test.exs
index 78eecfb..d8236e3 100644
--- a/test/auth/pubkey_test.exs
+++ b/test/auth/pubkey_test.exs
@@ -1,8 +1,6 @@
defmodule Chessh.Auth.PublicKeyAuthenticatorTest do
use ExUnit.Case
- alias Chessh.Key
- alias Chessh.Repo
- alias Chessh.Player
+ alias Chessh.{Key, Repo, Player}
@valid_user %{username: "logan", password: "password"}
@valid_key %{
@@ -10,7 +8,7 @@ defmodule Chessh.Auth.PublicKeyAuthenticatorTest do
key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ/2LOJGGEd/dhFgRxJ5MMv0jJw4s4pA8qmMbZyulN44"
}
- setup do
+ setup_all do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Chessh.Repo)
{:ok, player} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user))
diff --git a/test/schema/register_test.exs b/test/schema/register_test.exs
index 5705d31..0e9fdf1 100644
--- a/test/schema/register_test.exs
+++ b/test/schema/register_test.exs
@@ -1,8 +1,7 @@
defmodule Chessh.Auth.UserRegistrationTest do
use Chessh.RepoCase
use ExUnit.Case
- alias Chessh.Player
- alias Chessh.Repo
+ alias Chessh.{Player, Repo}
@valid_user %{username: "logan", password: "password"}
@invalid_username %{username: "a", password: "password"}
diff --git a/test/ssh/ssh_auth_test.exs b/test/ssh/ssh_auth_test.exs
new file mode 100644
index 0000000..c3ced20
--- /dev/null
+++ b/test/ssh/ssh_auth_test.exs
@@ -0,0 +1,94 @@
+defmodule Chessh.SSH.AuthTest do
+ use ExUnit.Case
+ alias Chessh.{Player, Repo, Key}
+
+ @localhost '127.0.0.1'
+ @key_name "The Gamer Machine"
+ @valid_user %{username: "logan", password: "password"}
+ @client_test_keys_dir Path.join(Application.compile_env!(:chessh, :key_dir), "client_keys")
+ @client_pub_key 'id_ed25519.pub'
+
+ setup_all do
+ case Ecto.Adapters.SQL.Sandbox.checkout(Repo) do
+ :ok -> nil
+ {:already, :owner} -> nil
+ end
+
+ Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
+
+ {:ok, player} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user))
+
+ {:ok, key_text} = File.read(Path.join(@client_test_keys_dir, @client_pub_key))
+
+ {:ok, _key} =
+ Repo.insert(
+ Key.changeset(%Key{}, %{key: key_text, name: @key_name})
+ |> Ecto.Changeset.put_assoc(:player, player)
+ )
+
+ :ok
+ end
+
+ test "Fails to authenticate after configured max password attempt" do
+ assert :disconnect ==
+ Enum.reduce(
+ 1..Application.fetch_env!(:chessh, :max_password_attempts),
+ %{attempts: 0},
+ fn acc, _ ->
+ case Chessh.SSH.Daemon.pwd_authenticate(
+ @valid_user.username,
+ 'wrong_password',
+ @localhost,
+ acc
+ ) do
+ {false, state} -> state
+ x -> x
+ end
+ end
+ )
+ end
+
+ test "INTEGRATION TEST - Can ssh into daemon with password or public key" do
+ {:ok, sup} = Task.Supervisor.start_link()
+ test_pid = self()
+
+ Task.Supervisor.start_child(sup, fn ->
+ {:ok, _pid} =
+ :ssh.connect(@localhost, Application.fetch_env!(:chessh, :port),
+ user: String.to_charlist(@valid_user.username),
+ password: String.to_charlist(@valid_user.password),
+ auth_methods: 'password',
+ silently_accept_hosts: true
+ )
+
+ send(test_pid, :connected_via_password)
+ end)
+
+ Task.Supervisor.start_child(sup, fn ->
+ {:ok, _pid} =
+ :ssh.connect(@localhost, Application.fetch_env!(:chessh, :port),
+ user: String.to_charlist(@valid_user.username),
+ auth_methods: 'publickey',
+ silently_accept_hosts: true,
+ user_dir: String.to_charlist(@client_test_keys_dir)
+ )
+
+ send(test_pid, :connected_via_public_key)
+ end)
+
+ assert_receive(:connected_via_password, 500)
+ assert_receive(:connected_via_public_key, 500)
+ end
+
+ test "Hosts are rate limited via password attempts" do
+ :ok
+ end
+
+ test "Hosts are also rate limited with public keys" do
+ :ok
+ end
+
+ test "User cannot have more than one current session" do
+ :ok
+ end
+end