aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelen <jjelen@redhat.com>2018-10-18 16:15:35 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-10-19 17:43:20 +0200
commit32e502a79d335de9309aecfa6bfba4580044f60a (patch)
tree28e55eff6be3648b1ef99656381157edc435eb03
parent6ec5a0863975d4f5b574460f9b010afbbe61b1a0 (diff)
downloadlibssh-32e502a79d335de9309aecfa6bfba4580044f60a.tar.gz
libssh-32e502a79d335de9309aecfa6bfba4580044f60a.tar.xz
libssh-32e502a79d335de9309aecfa6bfba4580044f60a.zip
tests: Generate valid known_hosts file, fixing the current test
Previously, the file contained the known_hosts strings separated by NULL bytes which somehow magically worked. The test was also expecting all the keys from the file will have the same key type, which was not indeed true. Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--tests/unittests/torture_knownhosts_parsing.c43
1 files changed, 28 insertions, 15 deletions
diff --git a/tests/unittests/torture_knownhosts_parsing.c b/tests/unittests/torture_knownhosts_parsing.c
index 86752bd9..a4ed14d8 100644
--- a/tests/unittests/torture_knownhosts_parsing.c
+++ b/tests/unittests/torture_knownhosts_parsing.c
@@ -42,18 +42,24 @@ static int setup_knownhosts_file(void **state)
nwritten = fwrite(LOCALHOST_PATTERN_ED25519,
sizeof(char),
- sizeof(LOCALHOST_PATTERN_ED25519),
+ strlen(LOCALHOST_PATTERN_ED25519),
fp);
- if (nwritten != sizeof(LOCALHOST_PATTERN_ED25519)) {
+ if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) {
+ fclose(fp);
+ return -1;
+ }
+
+ nwritten = fwrite("\n", sizeof(char), 1, fp);
+ if (nwritten != 1) {
fclose(fp);
return -1;
}
nwritten = fwrite(LOCALHOST_RSA_LINE,
sizeof(char),
- sizeof(LOCALHOST_RSA_LINE),
+ strlen(LOCALHOST_RSA_LINE),
fp);
- if (nwritten != sizeof(LOCALHOST_RSA_LINE)) {
+ if (nwritten != strlen(LOCALHOST_RSA_LINE)) {
fclose(fp);
return -1;
}
@@ -210,6 +216,8 @@ static void torture_knownhosts_read_file(void **state)
const char *knownhosts_file = *state;
struct ssh_list *entry_list = NULL;
struct ssh_iterator *it = NULL;
+ struct ssh_knownhosts_entry *entry = NULL;
+ enum ssh_keytypes_e type;
int rc;
rc = ssh_known_hosts_read_entries("localhost",
@@ -219,22 +227,27 @@ static void torture_knownhosts_read_file(void **state)
assert_non_null(entry_list);
it = ssh_list_get_iterator(entry_list);
assert_non_null(it);
- for (;it != NULL; it = it->next) {
- struct ssh_knownhosts_entry *entry = NULL;
- enum ssh_keytypes_e type;
- entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
- assert_non_null(entry);
+ /* First key in known hosts file is ED25519 */
+ entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
+ assert_non_null(entry);
- assert_string_equal(entry->hostname, "localhost");
- type = ssh_key_type(entry->publickey);
- assert_int_equal(type, SSH_KEYTYPE_ED25519);
- }
+ assert_string_equal(entry->hostname, "localhost");
+ type = ssh_key_type(entry->publickey);
+ assert_int_equal(type, SSH_KEYTYPE_ED25519);
+
+ it = it->next;
+
+ /* Second key in known hosts file is RSA */
+ entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
+ assert_non_null(entry);
+
+ assert_string_equal(entry->hostname, "localhost");
+ type = ssh_key_type(entry->publickey);
+ assert_int_equal(type, SSH_KEYTYPE_RSA);
it = ssh_list_get_iterator(entry_list);
for (;it != NULL; it = it->next) {
- struct ssh_knownhosts_entry *entry = NULL;
-
entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
SSH_KNOWNHOSTS_ENTRY_FREE(entry);
}