unit-tests: Added crypter tests
authorAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 6 Dec 2013 09:09:36 +0000 (10:09 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 6 Dec 2013 09:09:36 +0000 (10:09 +0100)
src/libstrongswan/tests/Makefile.am
src/libstrongswan/tests/suites/test_crypter.c [new file with mode: 0644]
src/libstrongswan/tests/suites/test_hasher.c
src/libstrongswan/tests/tests.h

index d81bef3..cff3cd8 100644 (file)
@@ -39,6 +39,7 @@ tests_SOURCES = tests.h tests.c \
   suites/test_rsa.c \
   suites/test_host.c \
   suites/test_hasher.c \
+  suites/test_crypter.c \
   suites/test_pen.c \
   suites/test_asn1.c \
   suites/test_printf.c \
diff --git a/src/libstrongswan/tests/suites/test_crypter.c b/src/libstrongswan/tests/suites/test_crypter.c
new file mode 100644 (file)
index 0000000..4e7550a
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2013 Andreas Steffen
+ * HSR Hochschule fuer Technik Rapperswil
+ *
+ * This program 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 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program 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.
+ */
+
+#include "test_suite.h"
+
+#include <crypto/crypters/crypter.h>
+#include <asn1/oid.h>
+#include <utils/test.h>
+
+typedef struct {
+       int oid;
+       encryption_algorithm_t alg;
+       size_t key_size;
+}crypter_oid_t;
+
+static crypter_oid_t oids[] = {
+       { OID_UNKNOWN, ENCR_AES_CBC, 0 },
+       { OID_UNKNOWN, ENCR_CAMELLIA_CBC, 0 },
+       { OID_UNKNOWN, ENCR_UNDEFINED, 0 },
+       { OID_DES_CBC, ENCR_DES, 0 },
+       { OID_3DES_EDE_CBC, ENCR_3DES, 0 },
+       { OID_AES128_CBC, ENCR_AES_CBC, 128 },
+       { OID_AES192_CBC, ENCR_AES_CBC, 192 },
+       { OID_AES256_CBC, ENCR_AES_CBC, 256 },
+       { OID_CAMELLIA128_CBC, ENCR_CAMELLIA_CBC, 128 },
+       { OID_CAMELLIA192_CBC, ENCR_CAMELLIA_CBC, 192 },
+       { OID_CAMELLIA256_CBC, ENCR_CAMELLIA_CBC, 256 }
+};
+
+START_TEST(test_crypter_from_oid)
+{
+       size_t key_size;
+
+       ck_assert(encryption_algorithm_from_oid(oids[_i].oid, NULL) ==
+                                                                                   oids[_i].alg);
+       ck_assert(encryption_algorithm_from_oid(oids[_i].oid, &key_size) ==
+                                                                                   oids[_i].alg);
+       ck_assert(key_size == oids[_i].key_size);
+}
+END_TEST
+
+START_TEST(test_crypter_to_oid)
+{
+       ck_assert(encryption_algorithm_to_oid(oids[_i].alg,
+                                                                             oids[_i].key_size) == oids[_i].oid);
+}
+END_TEST
+
+typedef struct {
+       encryption_algorithm_t alg;
+       bool is_aead;
+}crypter_aead_t;
+
+static crypter_aead_t aead[] = {
+       { ENCR_AES_CCM_ICV8, TRUE },
+       { ENCR_AES_CCM_ICV12, TRUE },
+       { ENCR_AES_CCM_ICV16, TRUE },
+       { ENCR_AES_GCM_ICV8, TRUE },
+       { ENCR_AES_GCM_ICV12, TRUE },
+       { ENCR_AES_GCM_ICV16, TRUE },
+       { ENCR_NULL_AUTH_AES_GMAC, TRUE },
+       { ENCR_CAMELLIA_CCM_ICV8, TRUE },
+       { ENCR_CAMELLIA_CCM_ICV12, TRUE },
+       { ENCR_CAMELLIA_CCM_ICV16, TRUE },
+       { ENCR_AES_CBC, FALSE },
+       { ENCR_CAMELLIA_CBC, FALSE }
+};
+     
+START_TEST(test_crypter_is_aead)
+{
+       ck_assert(encryption_algorithm_is_aead(aead[_i].alg) == aead[_i].is_aead);
+}
+END_TEST
+
+Suite *crypter_suite_create()
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("crypter");
+
+       tc = tcase_create("from_oid");
+       tcase_add_loop_test(tc, test_crypter_from_oid, 2, countof(oids));
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("to_oid");
+       tcase_add_loop_test(tc, test_crypter_to_oid, 0, countof(oids));
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("is_aead");
+       tcase_add_loop_test(tc, test_crypter_is_aead, 0, countof(aead));
+       suite_add_tcase(s, tc);
+
+       return s;
+}
index a8afa7a..41a9d64 100644 (file)
@@ -27,7 +27,7 @@ typedef struct {
        key_type_t key;
 }hasher_oid_t;
 
-hasher_oid_t oids[] = {
+static hasher_oid_t oids[] = {
        { OID_MD2, HASH_MD2, KEY_ANY },
        { OID_MD5, HASH_MD5, KEY_ANY },
        { OID_SHA1, HASH_SHA1, KEY_ANY },
@@ -75,7 +75,7 @@ typedef struct {
        hash_algorithm_t alg;
 }hasher_prf_t;
 
-hasher_prf_t prfs[] = {
+static hasher_prf_t prfs[] = {
        { PRF_HMAC_MD5, HASH_MD5 },
        { PRF_HMAC_SHA1, HASH_SHA1 },
        { PRF_FIPS_SHA1_160, HASH_SHA1 },
@@ -104,7 +104,7 @@ typedef struct {
        size_t length;
 }hasher_auth_t;
 
-hasher_auth_t auths[] = {
+static hasher_auth_t auths[] = {
        { AUTH_UNDEFINED, HASH_MD2, 0 },
        { AUTH_UNDEFINED, HASH_MD4, 0 },
        { AUTH_UNDEFINED, HASH_SHA224, 0 },
index b4e3d03..84578ee 100644 (file)
@@ -33,6 +33,7 @@ TEST_SUITE_DEPEND(rsa_suite_create, PRIVKEY_GEN, KEY_RSA)
 TEST_SUITE(host_suite_create)
 TEST_SUITE(printf_suite_create)
 TEST_SUITE(hasher_suite_create)
+TEST_SUITE(crypter_suite_create)
 TEST_SUITE(pen_suite_create)
 TEST_SUITE(asn1_suite_create)
 TEST_SUITE(test_rng_suite_create)