44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
|
|
package sshutil
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestIsHostKeyErrorDetectsMismatch(t *testing.T) {
|
||
|
|
out := "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!"
|
||
|
|
if !IsHostKeyError(out, errors.New("ssh failed")) {
|
||
|
|
t.Fatalf("expected host-key mismatch to be detected")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestIsHostKeyErrorIgnoresGenericFailures(t *testing.T) {
|
||
|
|
out := "connection timed out"
|
||
|
|
if IsHostKeyError(out, errors.New("ssh failed")) {
|
||
|
|
t.Fatalf("did not expect host-key mismatch for generic timeout")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestKnownHostsFilesIncludesDerivedPaths(t *testing.T) {
|
||
|
|
configFile := "/home/atlas/.ssh/config"
|
||
|
|
identityFile := "/home/tethys/.ssh/id_ed25519"
|
||
|
|
files := KnownHostsFiles(configFile, identityFile)
|
||
|
|
set := map[string]struct{}{}
|
||
|
|
for _, f := range files {
|
||
|
|
set[f] = struct{}{}
|
||
|
|
}
|
||
|
|
|
||
|
|
want := []string{
|
||
|
|
"/home/atlas/.ssh/known_hosts",
|
||
|
|
"/home/tethys/.ssh/known_hosts",
|
||
|
|
filepath.Join(filepath.Dir(configFile), "known_hosts"),
|
||
|
|
filepath.Join(filepath.Dir(identityFile), "known_hosts"),
|
||
|
|
}
|
||
|
|
for _, path := range want {
|
||
|
|
if _, ok := set[path]; !ok {
|
||
|
|
t.Fatalf("expected known_hosts candidate %s", path)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|