mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-08-07 23:23:52 +08:00
- 修正 OpenFileDialog 默认目录,支持无扩展名密钥路径 - macOS 下取消错误文件名过滤,显示隐藏项以选择 ~/.ssh 密钥 - 私钥读/解析失败返回明确错误,示例改为 id_ed25519 - 补充选钥目录解析与私钥读取失败回归测试 Refs #711
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package ssh
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"GoNavi-Wails/internal/connection"
|
|
)
|
|
|
|
func TestConnectSSHReturnsErrorWhenPrivateKeyMissing(t *testing.T) {
|
|
t.Cleanup(CloseAllSSHClients)
|
|
|
|
missing := filepath.Join(t.TempDir(), "id_ed25519")
|
|
_, err := connectSSH(connection.SSHConfig{
|
|
Host: "127.0.0.1",
|
|
Port: 1,
|
|
User: "root",
|
|
KeyPath: missing,
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error for missing private key")
|
|
}
|
|
message := err.Error()
|
|
if !strings.Contains(message, "failed to read SSH private key") {
|
|
t.Fatalf("expected read failure message, got %q", message)
|
|
}
|
|
if !strings.Contains(message, missing) {
|
|
t.Fatalf("expected key path in error, got %q", message)
|
|
}
|
|
}
|
|
|
|
func TestConnectSSHReturnsErrorWhenPrivateKeyInvalid(t *testing.T) {
|
|
t.Cleanup(CloseAllSSHClients)
|
|
|
|
keyPath := filepath.Join(t.TempDir(), "custom_deploy_key")
|
|
if err := os.WriteFile(keyPath, []byte("not-a-private-key"), 0o600); err != nil {
|
|
t.Fatalf("write key: %v", err)
|
|
}
|
|
|
|
_, err := connectSSH(connection.SSHConfig{
|
|
Host: "127.0.0.1",
|
|
Port: 1,
|
|
User: "root",
|
|
KeyPath: keyPath,
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid private key")
|
|
}
|
|
message := err.Error()
|
|
if !strings.Contains(message, "failed to parse SSH private key") {
|
|
t.Fatalf("expected parse failure message, got %q", message)
|
|
}
|
|
if !strings.Contains(message, keyPath) {
|
|
t.Fatalf("expected key path in error, got %q", message)
|
|
}
|
|
}
|