| 1 | package remote |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "golang.org/x/crypto/ssh/knownhosts" |
| 9 | ) |
| 10 | |
| 11 | func TestHostKeyMismatchErrorKeepsStructuredSecurityDetails(t *testing.T) { |
| 12 | err := newHostKeyMismatchError("dev@example.test:2222", "SHA256:new", &knownhosts.KeyError{ |
| 13 | Want: []knownhosts.KnownKey{ |
| 14 | {Filename: "/home/dev/.ssh/known_hosts", Line: 7}, |
| 15 | {Filename: "/etc/ssh/ssh_known_hosts", Line: 3}, |
| 16 | }, |
| 17 | }) |
| 18 | if !errors.Is(err, ErrHostKeyMismatch) { |
| 19 | t.Fatalf("error does not unwrap to ErrHostKeyMismatch: %v", err) |
| 20 | } |
| 21 | var mismatch *HostKeyMismatchError |
| 22 | if !errors.As(err, &mismatch) { |
| 23 | t.Fatalf("error is not HostKeyMismatchError: %T", err) |
| 24 | } |
| 25 | if mismatch.PresentedFingerprint != "SHA256:new" || len(mismatch.Locations) != 2 { |
| 26 | t.Fatalf("structured mismatch details = %+v", mismatch) |
| 27 | } |
| 28 | if mismatch.Locations[0].Filename != "/home/dev/.ssh/known_hosts" || mismatch.Locations[0].Line != 7 { |
| 29 | t.Fatalf("first known_hosts record = %+v", mismatch.Locations[0]) |
| 30 | } |
| 31 | for _, want := range []string{"remote: host key mismatch", "SHA256:new", "/home/dev/.ssh/known_hosts:7"} { |
| 32 | if !strings.Contains(err.Error(), want) { |
| 33 | t.Fatalf("legacy error text %q missing %q", err, want) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 |