feat: Add test case for publickey_simple_withmultiple_keyfile (#396)

This commit adds a new test case to cover the scenario where multiple key files are used for authentication with the publickey_simple user. It generates a temporary key file and attempts to SSH to the piper server using both the correct and incorrect key files. The test verifies that the correct key file allows the connection and writes a random text to a shared file.
This commit is contained in:
Boshi Lian 2024-05-30 11:25:35 -07:00 committed by GitHub
parent 6fe67e3f5c
commit 305e8164d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -289,4 +289,55 @@ func TestYaml(t *testing.T) {
checkSharedFileContent(t, targetfie, randtext)
})
t.Run("publickey_simple_withmultiple_keyfile", func(t *testing.T) {
randtext := uuid.New().String()
targetfie := uuid.New().String()
wrongkeydir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("failed to create temp key file: %v", err)
}
wrongkeyfile := path.Join(wrongkeydir, "key")
if err := runCmdAndWait(
"ssh-keygen",
"-N",
"",
"-f",
wrongkeyfile,
); err != nil {
t.Errorf("failed to generate key: %v", err)
}
c, _, _, err := runCmd(
"ssh",
"-v",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
piperport,
"-l",
"publickey_simple",
"-i",
wrongkeyfile,
"-i",
path.Join(yamldir, "id_rsa_simple"),
"127.0.0.1",
fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie),
)
if err != nil {
t.Errorf("failed to ssh to piper, %v", err)
}
defer killCmd(c)
time.Sleep(time.Second) // wait for file flush
checkSharedFileContent(t, targetfie, randtext)
})
}