|
| 1 | +package sshclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestUnknownHostErrorString(t *testing.T) { |
| 11 | + err := &UnknownHostError{Host: "example.com", Fingerprint: "SHA256:abc"} |
| 12 | + got := err.Error() |
| 13 | + want := "unknown host key for example.com (SHA256:abc)" |
| 14 | + if got != want { |
| 15 | + t.Fatalf("unexpected error string: got %q want %q", got, want) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func TestStripKnownHostPort(t *testing.T) { |
| 20 | + cases := []struct { |
| 21 | + in string |
| 22 | + want string |
| 23 | + }{ |
| 24 | + {in: "example.com:22", want: "example.com"}, |
| 25 | + {in: "[2001:db8::1]:22", want: "2001:db8::1"}, |
| 26 | + {in: "example.com", want: "example.com"}, |
| 27 | + } |
| 28 | + |
| 29 | + for _, tc := range cases { |
| 30 | + if got := stripKnownHostPort(tc.in); got != tc.want { |
| 31 | + t.Fatalf("stripKnownHostPort(%q) = %q, want %q", tc.in, got, tc.want) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestExpandUserPath(t *testing.T) { |
| 37 | + home, err := os.UserHomeDir() |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("os.UserHomeDir() failed: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + if got := expandUserPath("~"); got != home { |
| 43 | + t.Fatalf("expandUserPath(~) = %q, want %q", got, home) |
| 44 | + } |
| 45 | + |
| 46 | + want := filepath.Join(home, ".ssh", "id_ed25519") |
| 47 | + if got := expandUserPath("~/.ssh/id_ed25519"); got != want { |
| 48 | + t.Fatalf("expandUserPath(~/...) = %q, want %q", got, want) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestNeedsPassphraseWithMissingPath(t *testing.T) { |
| 53 | + missing := filepath.Join(t.TempDir(), "does-not-exist") |
| 54 | + if NeedsPassphrase(missing, nil) { |
| 55 | + t.Fatal("expected false when key path does not exist") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestSessionZeroMethods(t *testing.T) { |
| 60 | + s := &Session{ |
| 61 | + Password: []byte("pw"), |
| 62 | + KeyData: []byte("key"), |
| 63 | + KeyPassphrase: []byte("pass"), |
| 64 | + } |
| 65 | + |
| 66 | + s.zeroPassword() |
| 67 | + if s.Password != nil { |
| 68 | + t.Fatal("expected Password to be nil after zeroPassword") |
| 69 | + } |
| 70 | + |
| 71 | + s.zeroKeyData() |
| 72 | + if s.KeyData != nil { |
| 73 | + t.Fatal("expected KeyData to be nil after zeroKeyData") |
| 74 | + } |
| 75 | + |
| 76 | + s.zeroKeyPassphrase() |
| 77 | + if s.KeyPassphrase != nil { |
| 78 | + t.Fatal("expected KeyPassphrase to be nil after zeroKeyPassphrase") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestTrustHostKeyNilInput(t *testing.T) { |
| 83 | + if err := TrustHostKey(nil); err == nil { |
| 84 | + t.Fatal("expected error for nil UnknownHostError") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestFlattenAuthMethodsEmpty(t *testing.T) { |
| 89 | + if got := flattenAuthMethods(nil); !bytes.Equal([]byte{}, []byte{}) && len(got) != 0 { |
| 90 | + t.Fatal("expected empty auth method list") |
| 91 | + } |
| 92 | +} |
0 commit comments