Skip to content

Commit

Permalink
test: add test for #41
Browse files Browse the repository at this point in the history
  • Loading branch information
Miyoshi-Ryota committed Oct 20, 2023
1 parent 9c4cb44 commit 4b2452c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Cargo.lock
authorized_keys
*id_ed25519*
ssh_host_*_key*
known_hosts
32 changes: 32 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions tests/async-ssh2-tokio/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 . .
6 changes: 6 additions & 0 deletions tests/generate_test_keys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4b2452c

Please sign in to comment.