Skip to content

Commit 2134d0e

Browse files
committed
added ssh key auth test
1 parent 0e810b4 commit 2134d0e

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

src-tauri/src/conn_pool/cmd.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ mod test {
103103
))
104104
.unwrap();
105105
let err = DeviceConnection::new(device, None).expect_err("Should have failed");
106-
assert!(
107-
matches!(err, Error::Authorization { message } if message == "Bad SSH password")
108-
);
106+
assert!(matches!(err, Error::Authorization { message } if message == "Bad SSH password"));
109107
}
110108

111109
#[test]
@@ -124,6 +122,23 @@ mod test {
124122
assert_eq!(b"root\n", output.stdout.as_ref());
125123
}
126124

125+
#[test]
126+
fn execute_command_whoami_keyauth() {
127+
let sshd = SshContainer::new();
128+
let port = sshd.wait();
129+
let device = serde_json::from_str::<Device>(&format!(
130+
"{{\"profile\":\"ose\",\"name\":\"test\",\"host\":\"127.0.0.1\",\
131+
\"port\": {port},\"username\": \"root\",\"privateKey\": {{\"openSsh\": \"id_root\"}}}}"
132+
))
133+
.unwrap();
134+
let conn = DeviceConnection::new(device, Some(&SshContainer::fixture_path("keys", false)))
135+
.expect("Failed to create connection");
136+
let output = conn
137+
.execute_command("whoami", None, Encoding::String)
138+
.expect("Failed to execute command");
139+
assert_eq!(b"root\n", output.stdout.as_ref());
140+
}
141+
127142
#[test]
128143
fn execute_command_false() {
129144
let sshd = SshContainer::new();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN OPENSSH PRIVATE KEY-----
2+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS
3+
1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQTqu3dj7L8aVOTlvR/+wFfzn/t7VA4H
4+
sA9wPRBtLxeOePcxYtXb+a/htuy0I7VKQxyNk6u3yz3AP5rImvc0po+4AAAAsBDlh8UQ5Y
5+
fFAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOq7d2PsvxpU5OW9
6+
H/7AV/Of+3tUDgewD3A9EG0vF4549zFi1dv5r+G27LQjtUpDHI2Tq7fLPcA/msia9zSmj7
7+
gAAAAgDz8yxLi2BFo+MQ9b4s6a8YZG2pXElVd/oP3kk3em8U4AAAAXbWFyaW90YWt1QE1B
8+
UklPVEFLVS1OVUMB
9+
-----END OPENSSH PRIVATE KEY-----
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOq7d2PsvxpU5OW9H/7AV/Of+3tUDgewD3A9EG0vF4549zFi1dv5r+G27LQjtUpDHI2Tq7fLPcA/msia9zSmj7g= mariotaku@MARIOTAKU-NUC

src-tauri/src/tests/common/test_server/mod.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ pub struct SshContainer {
1414

1515
impl SshContainer {
1616
pub fn new() -> Self {
17-
let mp = format!(
18-
"{}:/etc/entrypoint.d/",
19-
Self::entrypoint_d().to_string_lossy()
20-
);
2117
let output = Command::new("docker")
2218
.args(["run", "--rm", "-d", "-p", "22"])
2319
.args(["-e", "SSH_ENABLE_ROOT=true"])
@@ -30,7 +26,18 @@ impl SshContainer {
3026
"--health-interval",
3127
"3s",
3228
])
33-
.args(["-v", &mp])
29+
.args([
30+
"-v",
31+
&format!(
32+
"{}:/etc/entrypoint.d/",
33+
Self::fixture_path("entrypoint.d", true).to_string_lossy()
34+
),
35+
"-v",
36+
&format!(
37+
"{}:/root/.ssh/authorized_keys",
38+
Self::fixture_path("keys/id_root.pub", true).to_string_lossy()
39+
),
40+
])
3441
.arg("public.ecr.aws/panubo/sshd")
3542
.output()
3643
.expect("Failed to start sshd container");
@@ -75,9 +82,9 @@ impl SshContainer {
7582
panic!("Failed to start sshd container");
7683
}
7784

78-
fn entrypoint_d() -> PathBuf {
79-
let path = abs_file!().parent().unwrap().join("entrypoint.d");
80-
if cfg!(target_family = "windows") {
85+
pub fn fixture_path(name: &str, for_container: bool) -> PathBuf {
86+
let path = abs_file!().parent().unwrap().join(name);
87+
if for_container && cfg!(target_family = "windows") {
8188
let mut components = path.components();
8289
let drive: Component = components.next().unwrap().into();
8390
components.next();

0 commit comments

Comments
 (0)