|
@@ -304,3 +304,134 @@ func TestInvalidEntry(t *testing.T) {
|
|
|
t.Errorf("got valid entry for %q", authInvalid)
|
|
t.Errorf("got valid entry for %q", authInvalid)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+var knownHostsParseTests = []struct {
|
|
|
|
|
+ input string
|
|
|
|
|
+ err string
|
|
|
|
|
+
|
|
|
|
|
+ marker string
|
|
|
|
|
+ comment string
|
|
|
|
|
+ hosts []string
|
|
|
|
|
+ rest string
|
|
|
|
|
+} {
|
|
|
|
|
+ {
|
|
|
|
|
+ "",
|
|
|
|
|
+ "EOF",
|
|
|
|
|
+
|
|
|
|
|
+ "", "", nil, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "# Just a comment",
|
|
|
|
|
+ "EOF",
|
|
|
|
|
+
|
|
|
|
|
+ "", "", nil, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ " \t ",
|
|
|
|
|
+ "EOF",
|
|
|
|
|
+
|
|
|
|
|
+ "", "", nil, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost ssh-rsa {RSAPUB}",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "", []string{"localhost"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost\tssh-rsa {RSAPUB}",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "", []string{"localhost"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost\tssh-rsa {RSAPUB}\tcomment comment",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "comment comment", []string{"localhost"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost\tssh-rsa {RSAPUB}\tcomment comment\n",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "comment comment", []string{"localhost"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost\tssh-rsa {RSAPUB}\tcomment comment\r\n",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "comment comment", []string{"localhost"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost\tssh-rsa {RSAPUB}\tcomment comment\r\nnext line",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "comment comment", []string{"localhost"}, "next line",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "localhost,[host2:123]\tssh-rsa {RSAPUB}\tcomment comment",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "", "comment comment", []string{"localhost","[host2:123]"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "@marker \tlocalhost,[host2:123]\tssh-rsa {RSAPUB}",
|
|
|
|
|
+ "",
|
|
|
|
|
+
|
|
|
|
|
+ "marker", "", []string{"localhost","[host2:123]"}, "",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "@marker \tlocalhost,[host2:123]\tssh-rsa aabbccdd",
|
|
|
|
|
+ "short read",
|
|
|
|
|
+
|
|
|
|
|
+ "", "", nil, "",
|
|
|
|
|
+ },
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestKnownHostsParsing(t *testing.T) {
|
|
|
|
|
+ rsaPub, rsaPubSerialized := getTestKey()
|
|
|
|
|
+
|
|
|
|
|
+ for i, test := range knownHostsParseTests {
|
|
|
|
|
+ var expectedKey PublicKey
|
|
|
|
|
+ const rsaKeyToken = "{RSAPUB}"
|
|
|
|
|
+
|
|
|
|
|
+ input := test.input
|
|
|
|
|
+ if strings.Contains(input, rsaKeyToken) {
|
|
|
|
|
+ expectedKey = rsaPub
|
|
|
|
|
+ input = strings.Replace(test.input, rsaKeyToken, rsaPubSerialized, -1)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ marker, hosts, pubKey, comment, rest, err := ParseKnownHosts([]byte(input))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ if len(test.err) == 0 {
|
|
|
|
|
+ t.Errorf("#%d: unexpectedly failed with %q", i, err)
|
|
|
|
|
+ } else if !strings.Contains(err.Error(), test.err) {
|
|
|
|
|
+ t.Errorf("#%d: expected error containing %q, but got %q", i, test.err, err)
|
|
|
|
|
+ }
|
|
|
|
|
+ continue
|
|
|
|
|
+ } else if len(test.err) != 0 {
|
|
|
|
|
+ t.Errorf("#%d: succeeded but expected error including %q", i, test.err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !reflect.DeepEqual(expectedKey, pubKey) {
|
|
|
|
|
+ t.Errorf("#%d: expected key %#v, but got %#v", i, expectedKey, pubKey)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if marker != test.marker {
|
|
|
|
|
+ t.Errorf("#%d: expected marker %q, but got %q", i, test.marker, marker)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if comment != test.comment {
|
|
|
|
|
+ t.Errorf("#%d: expected comment %q, but got %q", i, test.comment, comment)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !reflect.DeepEqual(test.hosts, hosts) {
|
|
|
|
|
+ t.Errorf("#%d: expected hosts %#v, but got %#v", i, test.hosts, hosts)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if rest := string(rest); rest != test.rest {
|
|
|
|
|
+ t.Errorf("#%d: expected remaining input to be %q, but got %q", i, test.rest, rest)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|