unit-tests: 12 asn1 functions tested
authorAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 2 Nov 2013 20:20:04 +0000 (21:20 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 2 Nov 2013 20:20:04 +0000 (21:20 +0100)
src/libstrongswan/tests/Makefile.am
src/libstrongswan/tests/test_asn1.c [new file with mode: 0644]
src/libstrongswan/tests/test_runner.c

index 0336ec8..994619e 100644 (file)
@@ -7,7 +7,8 @@ test_runner_SOURCES = \
   test_linked_list.c test_enumerator.c test_linked_list_enumerator.c \
   test_bio_reader.c test_bio_writer.c test_chunk.c test_enum.c test_hashtable.c \
   test_identification.c test_threading.c test_utils.c test_vectors.c \
-  test_array.c test_ecdsa.c test_rsa.c test_host.c test_printf.c test_pen.c
+  test_array.c test_ecdsa.c test_rsa.c test_host.c test_printf.c test_pen.c \
+  test_asn1.c
 
 test_runner_CFLAGS = \
   -I$(top_srcdir)/src/libstrongswan \
diff --git a/src/libstrongswan/tests/test_asn1.c b/src/libstrongswan/tests/test_asn1.c
new file mode 100644 (file)
index 0000000..6d85136
--- /dev/null
@@ -0,0 +1,538 @@
+/*
+ * 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 <asn1/asn1.h>
+#include <asn1/oid.h>
+#include <utils/chunk.h>
+
+/*******************************************************************************
+ * algorithm_identifier
+ */
+
+START_TEST(test_asn1_algorithmIdentifier)
+{
+       typedef struct {
+               int n;
+               chunk_t algid;
+       } testdata_t;
+
+       testdata_t test[] = {
+               { OID_ECDSA_WITH_SHA1, chunk_from_chars(0x30, 0x09, 0x06, 0x07,
+                       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
+               { OID_SHA1_WITH_RSA,   chunk_from_chars(0x30, 0x0d, 0x06, 0x09,
+                       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00) },
+       };
+
+       chunk_t algid;
+       int i;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               algid = asn1_algorithmIdentifier(test[i].n);
+               ck_assert(chunk_equals(algid, test[i].algid));
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * known_oid
+ */
+
+START_TEST(test_asn1_known_oid)
+{
+       typedef struct {
+               int n;
+               chunk_t oid;
+       } testdata_t;
+
+       testdata_t test[] = {
+               { OID_UNKNOWN,    chunk_empty },
+               { OID_UNKNOWN,    chunk_from_chars(0x55, 0x04, 0x02) },
+               { OID_COUNTRY,    chunk_from_chars(0x55, 0x04, 0x06) },
+               { OID_STRONGSWAN, chunk_from_chars(0x2b, 0x06, 0x01, 0x04, 0x01,
+                                                                                  0x82, 0xa0, 0x2a, 0x01) }
+       };
+
+       int i;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               ck_assert(asn1_known_oid(test[i].oid) == test[i].n);
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * build_known_oid
+ */
+
+START_TEST(test_asn1_build_known_oid)
+{
+       typedef struct {
+               int n;
+               chunk_t oid;
+       } testdata_t;
+
+       testdata_t test[] = {
+               { OID_UNKNOWN,    chunk_empty },
+               { OID_MAX,        chunk_empty },
+               { OID_COUNTRY,    chunk_from_chars(0x06, 0x03, 0x55, 0x04, 0x06) },
+               { OID_STRONGSWAN, chunk_from_chars(0x06, 0x09, 0x2b, 0x06, 0x01, 0x04,
+                                                                                  0x01, 0x82, 0xa0, 0x2a, 0x01) }
+       };
+
+       int i;
+       chunk_t oid = chunk_empty;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               oid = asn1_build_known_oid(test[i].n);
+               if (test[i].oid.len == 0)
+               {
+                       ck_assert(oid.len == 0 && oid.ptr == NULL);
+               }
+               else
+               {
+                       ck_assert(chunk_equals(oid, test[i].oid));
+                       chunk_free(&oid);
+               }
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * oid_from_string
+ */
+
+START_TEST(test_asn1_oid_from_string)
+{
+       typedef struct {
+               char *string;
+               chunk_t oid;
+       } testdata_t;
+
+       testdata_t test[] = {
+               { "",  chunk_empty },
+               { " ", chunk_empty },
+               { "0.2.262.1", chunk_from_chars(
+                       0x02, 0x82, 0x06, 0x01) },
+               { "1.2.840.10045.4.1", chunk_from_chars(
+                       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
+               { "1.3.6.1.4.1.36906.1", chunk_from_chars(
+                       0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
+               { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
+                       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
+               { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
+                 "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
+                 "2097153", chunk_from_chars(
+                       0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
+                       0x81, 0x00, 0x81, 0x01, 0x81, 0x02, 0x81, 0x03, 0x81, 0x04,
+                       0x81, 0x05, 0x81, 0x06, 0x81, 0x07, 0x81, 0x08, 0x81, 0x09,
+                       0x82, 0x00, 0x82, 0x01, 0x82, 0x02, 0x82, 0x03, 0x82, 0x04,
+                       0x82, 0x05, 0x82, 0x06, 0x82, 0x07, 0x82, 0x08, 0x82, 0x09,
+                       0x83, 0x00, 0x83, 0x01, 0x83, 0x02, 0x83, 0x03, 0x83, 0x04,
+                       0x81, 0x80, 0x80, 0x01) },
+               { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
+                       chunk_from_chars(0x0a,0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
+                       0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
+                       0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
+               { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
+                 "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
+                 "1.2097153", chunk_empty },
+               { "1.a.2.b.3", chunk_empty }
+       };
+
+       int i;
+       chunk_t oid = chunk_empty;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               oid = asn1_oid_from_string(test[i].string);
+               if (test[i].oid.len == 0)
+               {
+                       ck_assert(oid.len == 0 && oid.ptr == NULL);
+               }
+               else
+               {
+                       ck_assert(chunk_equals(oid, test[i].oid));
+                       chunk_free(&oid);
+               }
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * oid_to_string
+ */
+
+START_TEST(test_asn1_oid_to_string)
+{
+       typedef struct {
+               char *string;
+               chunk_t oid;
+       } testdata_t;
+
+       testdata_t test[] = {
+               {  NULL,  chunk_empty },
+               { "0.2.262.1", chunk_from_chars(
+                       0x02, 0x82, 0x06, 0x01) },
+               { "1.2.840.10045.4.1", chunk_from_chars(
+                       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
+               { "1.3.6.1.4.1.36906.1", chunk_from_chars(
+                       0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
+               { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
+                       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
+               { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
+                       chunk_from_chars( 0x0a, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
+                       0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
+                       0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
+               { NULL, chunk_from_chars(
+                       0x0a, 0x02, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d, 0x20,
+                       0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00, 0xaf, 0xd7, 0xc2, 0x00,
+                   0xff, 0xff, 0xff, 0x7f) }
+       };
+
+       int i;
+       char *string = NULL;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               string = asn1_oid_to_string(test[i].oid);
+               if (test[i].string == NULL)
+               {
+                       ck_assert(string == NULL);
+               }
+               else
+               {
+                       ck_assert(streq(string, test[i].string));
+                       free(string);
+               }
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * length
+ */
+
+START_TEST(test_asn1_length)
+{
+       chunk_t a;
+
+       a = chunk_empty;
+       ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
+
+       a = chunk_from_chars(0x04);
+       ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
+
+       a = chunk_from_chars(0x04, 0x00);
+       ck_assert(asn1_length(&a) == 0);
+
+       a = chunk_from_chars(0x04, 0x01);
+       ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
+
+       a = chunk_from_chars(0x04, 0x01, 0xaa);
+       ck_assert(asn1_length(&a) == 1);
+
+       a = chunk_from_chars(0x04, 0x7f, 0xaa);
+       a.len = 2 + 127;
+       ck_assert(asn1_length(&a) == 127);
+
+       a = chunk_from_chars(0x04, 0x80, 0xaa);
+       a.len = 2 + 128;
+       ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
+
+       a = chunk_from_chars(0x04, 0x81);
+       ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
+
+       a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
+       ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
+
+       a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
+       a.len = 3 + 128;
+       ck_assert(asn1_length(&a) == 128);
+
+       a = chunk_from_chars(0x04, 0x82, 0x01, 0x02, 0xaa);
+       a.len = 4 + 258;
+       ck_assert(asn1_length(&a) == 258);
+
+       a = chunk_from_chars(0x04, 0x83, 0x01, 0x02, 0x03, 0xaa);
+       a.len = 5 + 66051;
+       ck_assert(asn1_length(&a) == 66051);
+}
+END_TEST
+
+/*******************************************************************************
+ * is_asn1
+ */
+
+START_TEST(test_is_asn1)
+{
+       typedef struct {
+               bool asn1;
+               chunk_t chunk;
+       } testdata_t;
+
+       u_char buf[8];
+       chunk_t chunk_zero = { buf, 0 };
+
+       testdata_t test[] = {
+               { FALSE, chunk_zero },
+               { FALSE, chunk_empty },
+               { TRUE,  chunk_from_chars(0x30, 0x00) },
+               { TRUE,  chunk_from_chars(0x31, 0x00) },
+               { TRUE,  chunk_from_chars(0x04, 0x00) },
+               { FALSE, chunk_from_chars(0x02, 0x00) },
+               { FALSE, chunk_from_chars(0x30, 0x01) },
+               { FALSE, chunk_from_chars(0x30, 0x80) },
+               { TRUE,  chunk_from_chars(0x30, 0x01, 0xa1) },
+               { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2) },
+               { TRUE,  chunk_from_chars(0x30, 0x01, 0xa1, 0x0a) },
+       };
+
+       int i;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               ck_assert(is_asn1(test[i].chunk) == test[i].asn1);
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * is_printablestring
+ */
+
+START_TEST(test_asn1_is_printablestring)
+{
+       typedef struct {
+               bool printable;
+               char *string;
+       } testdata_t;
+
+
+       testdata_t test[] = {
+               { TRUE,  "" },
+               { TRUE,  "Z" },
+               { FALSE, "Z#" },
+               { FALSE, "&Z" },
+               { FALSE, "Z@z" },
+               { FALSE, "!" },  { FALSE, "*" },  { FALSE, "$" },  { FALSE, "%" },
+               { FALSE, "[" },  { FALSE, "]" },  { FALSE, "{" },  { FALSE, "}" },
+               { FALSE, "|" },  { FALSE, "~" },  { FALSE, "^" },  { FALSE, "_" },
+               { FALSE, "\"" }, { FALSE, "\\" }, { FALSE, "ä" },  { FALSE, "à" },
+               { TRUE,  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+                                "0123456789 '()+,-./:=?" },
+       };
+
+       chunk_t chunk;
+       int i;
+
+       ck_assert(asn1_is_printablestring(chunk_empty));
+
+       for (i = 0; i < countof(test); i++)
+       {
+               chunk = chunk_from_str(test[i].string);
+               ck_assert(asn1_is_printablestring(chunk) == test[i].printable);
+       }
+}
+END_TEST
+
+
+/*******************************************************************************
+ * build_object
+ */
+
+START_TEST(test_asn1_build_object)
+{
+       typedef struct {
+               size_t len;
+               size_t size;
+               u_char *b;
+       } testdata_t;
+
+       u_char b0[] = { 0x05, 0x00 };
+       u_char b1[] = { 0x04, 0x7f };
+       u_char b2[] = { 0x04, 0x81, 0x80 };
+       u_char b3[] = { 0x04, 0x81, 0xff };
+       u_char b4[] = { 0x04, 0x82, 0x01, 0x00 };
+       u_char b5[] = { 0x04, 0x82, 0xff, 0xff };
+       u_char b6[] = { 0x04, 0x83, 0x01, 0x00, 0x00 };
+
+       testdata_t test[] = {
+               {     0, sizeof(b0), b0 },
+               {   127, sizeof(b1), b1 },
+               {   128, sizeof(b2), b2 },
+               {   255, sizeof(b3), b3 },
+               {   256, sizeof(b4), b4 },
+               { 65535, sizeof(b5), b5 },
+               { 65536, sizeof(b6), b6 }
+       };
+
+       chunk_t a = chunk_empty;
+       u_char *pos;
+       int i;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               pos = asn1_build_object(&a, test[i].b[0], test[i].len);
+               ck_assert(pos == (a.ptr + test[i].size));
+               ck_assert(a.len == test[i].size + test[i].len);
+               ck_assert(memeq(a.ptr, test[i].b, test[i].size)); 
+               chunk_free(&a);
+       }
+}
+END_TEST
+
+/*******************************************************************************
+ * simple_object
+ */
+
+START_TEST(test_asn1_simple_object)
+{
+       chunk_t a = chunk_empty;
+       chunk_t b = chunk_from_chars(0x04, 0x05, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
+       chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
+
+       a = asn1_simple_object(0x04, c);
+       ck_assert(chunk_equals(a, b));
+       chunk_free(&a);
+}
+END_TEST
+
+/*******************************************************************************
+ * bitstring
+ */
+
+START_TEST(test_asn1_bitstring)
+{
+       chunk_t a = chunk_empty;
+       chunk_t b = chunk_from_chars(0x03, 0x05, 0x00, 0xa1, 0xa2, 0xa3, 0xa4);
+       chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4);
+       chunk_t d = chunk_clone(c);
+
+       a = asn1_bitstring("c", c);
+       ck_assert(chunk_equals(a, b));
+       chunk_free(&a);
+
+       a = asn1_bitstring("m", d);
+       ck_assert(chunk_equals(a, b));
+       chunk_free(&a);
+}
+END_TEST
+
+/*******************************************************************************
+ * integer
+ */
+
+START_TEST(test_asn1_integer)
+{
+       typedef struct {
+               chunk_t b;
+               chunk_t c;
+       } testdata_t;
+
+       chunk_t b0 = chunk_from_chars(0x02, 0x01, 0x00);
+       chunk_t b1 = chunk_from_chars(0x02, 0x01, 0x7f);
+       chunk_t b2 = chunk_from_chars(0x02, 0x02, 0x00, 0x80);
+
+       chunk_t c0 = chunk_empty;
+       chunk_t c1 = chunk_from_chars(0x7f);
+       chunk_t c2 = chunk_from_chars(0x80);
+       chunk_t c3 = chunk_from_chars(0x00, 0x80);
+
+       testdata_t test[] = {
+               { b0, c0 },
+               { b1, c1 },
+               { b2, c2 },
+               { b2, c3 }
+       };
+
+       chunk_t a = chunk_empty;
+       int i;
+
+       for (i = 0; i < countof(test); i++)
+       {
+               a = asn1_integer("c", test[i].c);
+               ck_assert(chunk_equals(a, test[i].b));
+               chunk_free(&a);
+
+               a = asn1_integer("m", chunk_clone(test[i].c));
+               ck_assert(chunk_equals(a, test[i].b));
+               chunk_free(&a);
+       }
+}
+END_TEST
+
+Suite *asn1_suite_create()
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("asn1");
+
+       tc = tcase_create("algorithmIdentifier");
+       tcase_add_test(tc, test_asn1_algorithmIdentifier);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("known_oid");
+       tcase_add_test(tc, test_asn1_known_oid);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("build_known_oid");
+       tcase_add_test(tc, test_asn1_build_known_oid);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("oid_from_string");
+       tcase_add_test(tc, test_asn1_oid_from_string);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("oid_to_string");
+       tcase_add_test(tc, test_asn1_oid_to_string);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("length");
+       tcase_add_test(tc, test_asn1_length);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("is_asn1");
+       tcase_add_test(tc, test_is_asn1);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("is_printablestring");
+       tcase_add_test(tc, test_asn1_is_printablestring);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("build_object");
+       tcase_add_test(tc, test_asn1_build_object);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("simple_object");
+       tcase_add_test(tc, test_asn1_simple_object);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("bitstring");
+       tcase_add_test(tc, test_asn1_bitstring);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("integer");
+       tcase_add_test(tc, test_asn1_integer);
+       suite_add_tcase(s, tc);
+
+       return s;
+}
index e1269f2..e0e7e2b 100644 (file)
@@ -86,6 +86,7 @@ int main()
        srunner_add_suite(sr, vectors_suite_create());
        srunner_add_suite(sr, printf_suite_create());
        srunner_add_suite(sr, pen_suite_create());
+       srunner_add_suite(sr, asn1_suite_create());
        if (lib->plugins->has_feature(lib->plugins,
                                                                  PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA)))
        {