@@ -16,6 +16,17 @@ github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAA
1616github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl
1717`
1818
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+
1930func TestKnownHostsCallback (t * testing.T ) {
2031 tests := []struct {
2132 name string
@@ -25,6 +36,38 @@ func TestKnownHostsCallback(t *testing.T) {
2536 hostkey git2go.HostkeyCertificate
2637 want error
2738 }{
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+ },
2871 {
2972 name : "Match" ,
3073 host : "github.com" ,
@@ -66,7 +109,7 @@ func TestKnownHostsCallback(t *testing.T) {
66109 knownHosts : []byte (knownHostsFixture ),
67110 hostkey : git2go.HostkeyCertificate {Kind : git2go .HostkeySHA256 , HashSHA256 : sha256Fingerprint ("ROQFvPThGrW4RuWLoL9tq9I9zJ42fK4XywyRtbOz/EQ" )},
68111 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' " ),
70113 },
71114 }
72115 for _ , tt := range tests {
0 commit comments