chessh/test/ssh/ssh_auth_test.exs

84 lines
2.5 KiB
Elixir
Raw Normal View History

2022-12-28 01:50:22 -05:00
defmodule Chessh.SSH.AuthTest do
use ExUnit.Case
alias Chessh.{Player, Repo, Key}
@localhost '127.0.0.1'
2022-12-29 19:49:42 -05:00
@localhost_inet {{127, 0, 0, 1}, 1}
2022-12-28 01:50:22 -05:00
@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
2022-12-29 19:49:42 -05:00
Ecto.Adapters.SQL.Sandbox.checkout(Repo)
2022-12-28 01:50:22 -05:00
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
2022-12-29 19:21:20 -05:00
test "Password attempts are rate limited" do
2022-12-29 19:49:42 -05:00
jail_attempt_threshold =
Application.get_env(:chessh, RateLimits)
|> Keyword.get(:jail_attempt_threshold)
2022-12-28 01:50:22 -05:00
assert :disconnect ==
Enum.reduce(
2022-12-29 19:49:42 -05:00
0..(jail_attempt_threshold + 1),
2022-12-29 19:21:20 -05:00
fn _, _ ->
Chessh.SSH.Daemon.pwd_authenticate(
2022-12-29 19:49:42 -05:00
@valid_user.username,
"wrong_password",
@localhost_inet
)
2022-12-28 01:50:22 -05:00
end
)
end
2022-12-29 19:21:20 -05:00
test "INTEGRATION - Can ssh into daemon with password or public key" do
2022-12-28 01:50:22 -05:00
{: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
2022-12-29 19:49:42 -05:00
# TODO
# test "INTEGRATION - User cannot have more than specified concurrent sessions" do
# :ok
# end
2022-12-28 01:50:22 -05:00
end