@@ -16,6 +16,17 @@ github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAA
16
16
github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl
17
17
`
18
18
19
+ // To fetch latest knownhosts for source.developers.google.com run:
20
+ // ssh-keyscan -p 2022 source.developers.google.com
21
+ //
22
+ // Expected hash (used in the cases) can get found with:
23
+ // ssh-keyscan -p 2022 source.developers.google.com | ssh-keygen -l -f -
24
+ var knownHostsFixtureWithPort = `[source.developers.google.com]:2022 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBB5Iy4/cq/gt/fPqe3uyMy4jwv1Alc94yVPxmnwNhBzJqEV5gRPiRk5u4/JJMbbu9QUVAguBABxL7sBZa5PH/xY=`
25
+
26
+ // This is an incorrect known hosts entry, that does not aligned with
27
+ // the normalized format and therefore won't match.
28
+ var knownHostsFixtureUnormalized = `source.developers.google.com:2022 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBB5Iy4/cq/gt/fPqe3uyMy4jwv1Alc94yVPxmnwNhBzJqEV5gRPiRk5u4/JJMbbu9QUVAguBABxL7sBZa5PH/xY=`
29
+
19
30
func TestKnownHostsCallback (t * testing.T ) {
20
31
tests := []struct {
21
32
name string
@@ -25,6 +36,38 @@ func TestKnownHostsCallback(t *testing.T) {
25
36
hostkey git2go.HostkeyCertificate
26
37
want error
27
38
}{
39
+ {
40
+ name : "Empty" ,
41
+ host : "source.developers.google.com" ,
42
+ knownHosts : []byte ("" ),
43
+ hostkey : git2go.HostkeyCertificate {Kind : git2go .HostkeySHA256 , HashSHA256 : sha256Fingerprint ("AGvEpqYNMqsRNIviwyk4J4HM0lEylomDBKOWZsBn434" )},
44
+ expectedHost : "source.developers.google.com:2022" ,
45
+ want : fmt .Errorf ("hostkey verification aborted: no known_hosts found" ),
46
+ },
47
+ {
48
+ name : "Mismatch incorrect known_hosts" ,
49
+ host : "source.developers.google.com" ,
50
+ knownHosts : []byte (knownHostsFixtureUnormalized ),
51
+ hostkey : git2go.HostkeyCertificate {Kind : git2go .HostkeySHA256 , HashSHA256 : sha256Fingerprint ("AGvEpqYNMqsRNIviwyk4J4HM0lEylomDBKOWZsBn434" )},
52
+ expectedHost : "source.developers.google.com:2022" ,
53
+ want : fmt .Errorf ("no entries in known_hosts match host '[source.developers.google.com]:2022' with fingerprint 'AGvEpqYNMqsRNIviwyk4J4HM0lEylomDBKOWZsBn434'" ),
54
+ },
55
+ {
56
+ name : "Match when host has port" ,
57
+ host : "source.developers.google.com:2022" ,
58
+ knownHosts : []byte (knownHostsFixtureWithPort ),
59
+ hostkey : git2go.HostkeyCertificate {Kind : git2go .HostkeySHA256 , HashSHA256 : sha256Fingerprint ("AGvEpqYNMqsRNIviwyk4J4HM0lEylomDBKOWZsBn434" )},
60
+ expectedHost : "source.developers.google.com:2022" ,
61
+ want : nil ,
62
+ },
63
+ {
64
+ name : "Match even when host does not have port" ,
65
+ host : "source.developers.google.com" ,
66
+ knownHosts : []byte (knownHostsFixtureWithPort ),
67
+ hostkey : git2go.HostkeyCertificate {Kind : git2go .HostkeySHA256 , HashSHA256 : sha256Fingerprint ("AGvEpqYNMqsRNIviwyk4J4HM0lEylomDBKOWZsBn434" )},
68
+ expectedHost : "source.developers.google.com:2022" ,
69
+ want : nil ,
70
+ },
28
71
{
29
72
name : "Match" ,
30
73
host : "github.com" ,
@@ -66,7 +109,7 @@ func TestKnownHostsCallback(t *testing.T) {
66
109
knownHosts : []byte (knownHostsFixture ),
67
110
hostkey : git2go.HostkeyCertificate {Kind : git2go .HostkeySHA256 , HashSHA256 : sha256Fingerprint ("ROQFvPThGrW4RuWLoL9tq9I9zJ42fK4XywyRtbOz/EQ" )},
68
111
expectedHost : "github.com" ,
69
- want : fmt .Errorf ("hostkey could not be verified " ),
112
+ want : fmt .Errorf ("no entries in known_hosts match host 'github.com' with fingerprint 'ROQFvPThGrW4RuWLoL9tq9I9zJ42fK4XywyRtbOz/EQ' " ),
70
113
},
71
114
}
72
115
for _ , tt := range tests {
0 commit comments