summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2017-04-08 15:50:16 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2017-04-08 17:17:40 +0200
commit4eb84579da07bed96b96f8fd1746acadc9cb2caf (patch)
treee190ffd401f238637070ce464707e37ea6e40325
parent83683fde0b5305fcf229b70464b878feff88d101 (diff)
downloadgnutls-4eb84579da07bed96b96f8fd1746acadc9cb2caf.tar.gz
tests: added basic unit test on gnutls_random_art()
Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/random-art.c141
2 files changed, 142 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0a72e09b24..f3633e9548 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -140,7 +140,7 @@ ctests = mini-record-2 simple gc set_pkcs12_cred cert certuniqueid \
hostname-check-utf8 pkcs8-key-decode-encrypted priority-mix pkcs7 \
send-data-before-handshake recv-data-before-handshake crt_inv_write \
x509sign-verify-error rng-op-nonce rng-op-random rng-op-key x509-dn-decode-compat \
- ip-check mini-x509-ipaddr trust-store base64-raw
+ ip-check mini-x509-ipaddr trust-store base64-raw random-art
if HAVE_SECCOMP_TESTS
ctests += dtls-with-seccomp tls-with-seccomp dtls-client-with-seccomp tls-client-with-seccomp
diff --git a/tests/random-art.c b/tests/random-art.c
new file mode 100644
index 0000000000..05206696e0
--- /dev/null
+++ b/tests/random-art.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2016, 2017 Red Hat, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * GnuTLS is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuTLS is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GnuTLS; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/* Parts copied from GnuTLS example programs. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <string.h>
+#include <gnutls/gnutls.h>
+
+#include "utils.h"
+
+/* This checks random art encoding */
+
+static void encode(const char *test_name, const char *type, unsigned key_size, unsigned char *input, unsigned input_size, const char *expected)
+{
+ int ret;
+ gnutls_datum_t out;
+
+ ret = gnutls_random_art(GNUTLS_RANDOM_ART_OPENSSH, type, key_size, input, input_size, &out);
+ if (ret < 0) {
+ fail("%s: gnutls_random_art: %s\n", test_name, gnutls_strerror(ret));
+ exit(1);
+ }
+
+ if (strlen(expected)!=out.size) {
+ fail("%s: gnutls_random_art: output has incorrect size (%d, expected %d)\n%s\n", test_name, (int)out.size, (int)strlen(expected), out.data);
+ exit(1);
+ }
+
+ if (strncasecmp(expected, (char*)out.data, out.size) != 0) {
+ fail("%s: gnutls_random_art: output does not match the expected:\n%s\n", test_name, out.data);
+ exit(1);
+ }
+
+ gnutls_free(out.data);
+
+ return;
+}
+
+
+struct encode_tests_st {
+ const char *name;
+ unsigned char *raw;
+ unsigned raw_size;
+ const char *key_type;
+ unsigned key_size;
+ const char *art;
+};
+
+struct encode_tests_st encode_tests[] = {
+ {
+ .name = "key1",
+ .raw = (void*)"\x38\x17\x0c\x08\xcb\x45\x8f\xd4\x87\x9c\x34\xb6\xf6\x08\x29\x4c\x50\x31\x2b\xbb",
+ .raw_size = 20,
+ .key_type = "RSA",
+ .key_size = 2048,
+ .art = "+--[ RSA 2048]----+\n"
+ "|.o*++==o |\n"
+ "| + *.===. |\n"
+ "|. * + +.o |\n"
+ "| o . o + . |\n"
+ "|. + S |\n"
+ "| . o |\n"
+ "|E |\n"
+ "| |\n"
+ "| |\n"
+ "+-----------------+"
+ },
+ {
+ .name = "key2",
+ .raw = (void*)"\xf8\xa7\x1c\x08\x76\x47\x2c\x08\x38\x17\x0c\x08\x38\x17\x0c\x08\xcb\x45\x8f\xd4\x87\x9c\xa4\xb6\xf6\xf8\x29\xfc\x50\x3f\x2b\xbb",
+ .raw_size = 32,
+ .key_type = "RSA",
+ .key_size = 3072,
+ .art = "+--[ RSA 3072]----+\n"
+ "|@*=*+.o |\n"
+ "|O.B.+* o |\n"
+ "|.* +..o o |\n"
+ "| . . + |\n"
+ "| oo.o S |\n"
+ "| ..+o.+ |\n"
+ "| .o ..oo . |\n"
+ "| oo...o+ |\n"
+ "| oE+.o |\n"
+ "+-----------------+"
+ },
+ {
+ .name = "key3",
+ .raw = (void*)"\x38\xf7\x0c\x08\xcb\x34\x8a\xd4\xb7\x9c\x34\xb4\xf6\x08\x29\x4c\x50\x3f\x2b\xbb",
+ .raw_size = 20,
+ .key_type = "ECDSA",
+ .key_size = 256,
+ .art = "+--[ECDSA 256]---+\n"
+ "|oo. . |\n"
+ "|o ..o . |\n"
+ "| + +** |\n"
+ "|...+***o |\n"
+ "|. o +=+.S |\n"
+ "| o o + |\n"
+ "| . o |\n"
+ "| . |\n"
+ "| E |\n"
+ "+-----------------+"
+ }
+};
+
+void doit(void)
+{
+ unsigned i;
+
+ for (i=0;i<sizeof(encode_tests)/sizeof(encode_tests[0]);i++) {
+ encode(encode_tests[i].name, encode_tests[i].key_type, encode_tests[i].key_size,
+ encode_tests[i].raw, encode_tests[i].raw_size, encode_tests[i].art);
+ }
+}
+