From 4b2452c8036a3c224d2e340b75ee02a4fb829e44 Mon Sep 17 00:00:00 2001 From: miyoshi-ryota Date: Fri, 20 Oct 2023 22:22:44 +0900 Subject: [PATCH] test: add test for #41 --- .gitignore | 1 + src/client.rs | 32 +++++++++++++++++++++++++++++++ tests/async-ssh2-tokio/Dockerfile | 3 +++ tests/generate_test_keys.sh | 6 ++++++ 4 files changed, 42 insertions(+) diff --git a/.gitignore b/.gitignore index 2727617..f20f7a8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ Cargo.lock authorized_keys *id_ed25519* ssh_host_*_key* +known_hosts diff --git a/src/client.rs b/src/client.rs index 31511f7..3ca3bbe 100644 --- a/src/client.rs +++ b/src/client.rs @@ -401,6 +401,13 @@ ASYNC_SSH2_TEST_SERVER_PUB .unwrap() } + fn test_hostname() -> impl ToSocketAddrsWithHostname { + ( + env("ASYNC_SSH2_TEST_HOST_NAME"), + env("ASYNC_SSH2_TEST_HOST_PORT").parse().unwrap(), + ) + } + async fn establish_test_host_connection() -> Client { Client::connect( ( @@ -483,6 +490,7 @@ ASYNC_SSH2_TEST_SERVER_PUB } #[tokio::test] + #[ignore = "This require over 1 minute to run, thus it is ignored by default"] async fn sequential_commands() { let client = establish_test_host_connection().await; @@ -670,4 +678,28 @@ ASYNC_SSH2_TEST_SERVER_PUB .await; assert!(client.is_ok()); } + + #[tokio::test] + async fn server_check_by_known_hosts_for_ip() { + let client = Client::connect( + test_address(), + &env("ASYNC_SSH2_TEST_HOST_USER"), + AuthMethod::with_password(&env("ASYNC_SSH2_TEST_HOST_PW")), + ServerCheckMethod::with_known_hosts_file(&env("ASYNC_SSH2_TEST_KNOWN_HOSTS")), + ) + .await; + assert!(client.is_ok()); + } + + #[tokio::test] + async fn server_check_by_known_hosts_for_hostname() { + let client = Client::connect( + test_hostname(), + &env("ASYNC_SSH2_TEST_HOST_USER"), + AuthMethod::with_password(&env("ASYNC_SSH2_TEST_HOST_PW")), + ServerCheckMethod::with_known_hosts_file(&env("ASYNC_SSH2_TEST_KNOWN_HOSTS")), + ) + .await; + assert!(client.is_ok()); + } } diff --git a/tests/async-ssh2-tokio/Dockerfile b/tests/async-ssh2-tokio/Dockerfile index 3311cc7..67b8736 100644 --- a/tests/async-ssh2-tokio/Dockerfile +++ b/tests/async-ssh2-tokio/Dockerfile @@ -8,12 +8,15 @@ ENV ASYNC_SSH2_TEST_CLIENT_PROT_PRIV=/root/.ssh/prot.id_ed25519 ENV ASYNC_SSH2_TEST_CLIENT_PROT_PASS=test ENV ASYNC_SSH2_TEST_SERVER_PUB=/root/server.ed25519.pub ENV ASYNC_SSH2_TEST_HOST_PORT=22 +ENV ASYNC_SSH2_TEST_HOST_NAME=ssh-server +ENV ASYNC_SSH2_TEST_KNOWN_HOSTS=/root/.ssh/known_hosts COPY tests/async-ssh2-tokio/id_ed25519 /root/.ssh/id_ed25519 COPY tests/async-ssh2-tokio/id_ed25519.pub /root/.ssh/id_ed25519.pub COPY tests/async-ssh2-tokio/prot.id_ed25519 /root/.ssh/prot.id_ed25519 COPY tests/async-ssh2-tokio/prot.id_ed25519.pub /root/.ssh/prot.id_ed25519.pub COPY tests/async-ssh2-tokio/server.ed25519.pub /root/server.ed25519.pub +COPY tests/async-ssh2-tokio/known_hosts /root/.ssh/known_hosts WORKDIR /async-ssh2-tokio COPY . . diff --git a/tests/generate_test_keys.sh b/tests/generate_test_keys.sh index e573c01..c7b9ed4 100755 --- a/tests/generate_test_keys.sh +++ b/tests/generate_test_keys.sh @@ -22,3 +22,9 @@ rm -f authorized_keys cat client.ed25519.pub >> authorized_keys cat client.prot.ed25519.pub >> authorized_keys mv authorized_keys sshd-test + +# setup known_hosts +export ASYNC_SSH2_TEST_HOST_IP=10.10.10.2 +export ASYNC_SSH2_TEST_HOST_NAME=ssh-server +awk -v IP=$ASYNC_SSH2_TEST_HOST_IP '{print IP, $1, $2}' server.ed25519.pub > async-ssh2-tokio/known_hosts +awk -v HOST=$ASYNC_SSH2_TEST_HOST_NAME '{print HOST, $1, $2}' server.ed25519.pub >> async-ssh2-tokio/known_hosts