@@ -251,7 +251,7 @@ func Test_knownHostsCallback(t *testing.T) {
251
251
}
252
252
}
253
253
254
- func Test_parseKnownHosts (t * testing.T ) {
254
+ func Test_parseKnownHosts_matches (t * testing.T ) {
255
255
tests := []struct {
256
256
name string
257
257
hostkey git2go.HostkeyCertificate
@@ -280,6 +280,72 @@ func Test_parseKnownHosts(t *testing.T) {
280
280
}
281
281
}
282
282
283
+ func Test_parseKnownHosts (t * testing.T ) {
284
+ tests := []struct {
285
+ name string
286
+ fixture string
287
+ wantErr bool
288
+ }{
289
+ {
290
+ name : "empty file" ,
291
+ fixture : "" ,
292
+ wantErr : false ,
293
+ },
294
+ {
295
+ name : "single host" ,
296
+ fixture : `github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==` ,
297
+ wantErr : false ,
298
+ },
299
+ {
300
+ name : "single host with comment" ,
301
+ fixture : `# github.com
302
+ github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==` ,
303
+ wantErr : false ,
304
+ },
305
+ {
306
+ name : "multiple hosts with comments" ,
307
+ fixture : `# github.com
308
+ github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
309
+ # gitlab.com
310
+ gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf` ,
311
+ },
312
+ {
313
+ name : "no host key, only comments" ,
314
+ fixture : `# example.com
315
+ #github.com
316
+ # gitlab.com` ,
317
+ wantErr : false ,
318
+ },
319
+ {
320
+ name : "invalid host entry" ,
321
+ fixture : `github.com ssh-rsa` ,
322
+ wantErr : true ,
323
+ },
324
+ {
325
+ name : "invalid content" ,
326
+ fixture : `some random text` ,
327
+ wantErr : true ,
328
+ },
329
+ {
330
+ name : "invalid line with valid host key" ,
331
+ fixture : `some random text
332
+ gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf` ,
333
+ wantErr : true ,
334
+ },
335
+ }
336
+ for _ , tt := range tests {
337
+ t .Run (tt .name , func (t * testing.T ) {
338
+ g := NewWithT (t )
339
+ _ , err := parseKnownHosts (tt .fixture )
340
+ if tt .wantErr {
341
+ g .Expect (err ).To (HaveOccurred ())
342
+ } else {
343
+ g .Expect (err ).ToNot (HaveOccurred ())
344
+ }
345
+ })
346
+ }
347
+ }
348
+
283
349
func md5Fingerprint (in string ) [16 ]byte {
284
350
var out [16 ]byte
285
351
copy (out [:], in )
0 commit comments