[gnome-keyring/wip/dueno/ecdsa-support: 6/10] Extend the asn1 test



commit 660b012dee1e807ff129764f407d4ad8588c3bbd
Author: Jakub Jelen <jjelen redhat com>
Date:   Tue Aug 8 18:54:19 2017 +0200

    Extend the asn1 test
    
    https://bugzilla.gnome.org/show_bug.cgi?id=641082

 pkcs11/gkm/test-data-asn1.c |  175 +++++++++++++++++++++++++++++++++++++++++++
 pkcs11/gkm/test.asn         |   16 ++++
 pkcs11/gkm/test.asn.h       |   11 +++-
 3 files changed, 201 insertions(+), 1 deletions(-)
---
diff --git a/pkcs11/gkm/test-data-asn1.c b/pkcs11/gkm/test-data-asn1.c
index 59da5d4..cc5b099 100644
--- a/pkcs11/gkm/test-data-asn1.c
+++ b/pkcs11/gkm/test-data-asn1.c
@@ -41,6 +41,10 @@ typedef struct _EggAsn1xDef ASN1_ARRAY_TYPE;
 typedef struct _EggAsn1xDef asn1_static_node;
 #include "test.asn.h"
 
+#define TEST_STRING "test data to write and read in the ASN1 structures"
+
+static GQuark OID_ANSI_SECP256R1;
+
 EGG_SECURE_DEFINE_GLIB_GLOBALS();
 
 typedef struct {
@@ -110,6 +114,173 @@ test_asn1_integers (Test *test, gconstpointer unused)
        gcry_mpi_release (mpt);
 }
 
+static void
+test_asn1_string_mpi (Test *test, gconstpointer unused)
+{
+       GNode *asn;
+       gcry_mpi_t mpi, mpt;
+       GBytes *data;
+       gboolean ret;
+
+       asn = egg_asn1x_create (test_asn1_tab, "TestStringMpi");
+       g_assert ("asn test structure is null" && asn != NULL);
+
+       /* Make a random number */
+       mpi = gcry_mpi_new (512);
+       g_return_if_fail (mpi);
+       gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM);
+
+       /* Write the mpi out */
+       ret = gkm_data_asn1_write_string_mpi (egg_asn1x_node (asn, "mpi", NULL), mpi);
+       g_assert ("couldn't write mpi to bit string in asn1" && ret);
+
+       /* Now encode the whole caboodle */
+       data = egg_asn1x_encode (asn, NULL);
+       g_assert ("encoding asn1 didn't work" && data != NULL);
+
+       egg_asn1x_destroy (asn);
+
+       /* Now decode it all nicely */
+       asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestStringMpi", data);
+       g_assert (asn != NULL);
+
+       ret = gkm_data_asn1_read_string_mpi (egg_asn1x_node (asn, "mpi", NULL), &mpt);
+       egg_asn1x_destroy (asn);
+       g_assert ("couldn't read mpi from octet string in asn1" && ret);
+       g_assert ("mpi returned is null" && mpt != NULL);
+       g_assert ("mpi is wrong number" && gcry_mpi_cmp (mpi, mpt) == 0);
+
+       g_bytes_unref (data);
+       gcry_mpi_release (mpi);
+       gcry_mpi_release (mpt);
+}
+
+static void
+test_asn1_bit_string (Test *test, gconstpointer unused)
+{
+       GNode *asn;
+       GBytes *data;
+       gboolean ret;
+       GBytes *source, *target;
+       gsize target_bits, source_bits;
+
+       asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
+       g_assert ("asn test structure is null" && asn != NULL);
+
+       /* Create a string */
+       source = g_bytes_new (TEST_STRING, strlen(TEST_STRING));
+       g_return_if_fail (source);
+       source_bits = g_bytes_get_size(source)*8;
+
+       /* Write the string out */
+       ret = gkm_data_asn1_write_bit_string (egg_asn1x_node (asn, "data", NULL),
+                                              source, source_bits);
+       g_assert ("couldn't write string to asn1" && ret);
+
+       /* Now encode the whole caboodle */
+       data = egg_asn1x_encode (asn, NULL);
+       g_assert ("encoding asn1 didn't work" && data != NULL);
+
+       egg_asn1x_destroy (asn);
+
+       /* Now decode it all nicely */
+       asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestBitString", data);
+       g_assert (asn != NULL);
+
+       ret = gkm_data_asn1_read_bit_string (egg_asn1x_node (asn, "data", NULL),
+                                             &target, &target_bits);
+       egg_asn1x_destroy (asn);
+       g_assert ("couldn't read bit string from asn1" && ret);
+       g_assert ("bit string returned is null" && target != NULL);
+       g_assert ("Source and target length differ" && target_bits == source_bits);
+       g_assert ("Bit strings differ" && g_bytes_equal (source, target));
+
+       g_bytes_unref (data);
+       g_bytes_unref (source);
+       g_bytes_unref (target);
+}
+/* XXX test some incomplete octets */
+
+static void
+test_asn1_string (Test *test, gconstpointer unused)
+{
+       GNode *asn;
+       GBytes *data;
+       gboolean ret;
+       GBytes *source, *target;
+
+       asn = egg_asn1x_create (test_asn1_tab, "TestString");
+       g_assert ("asn test structure is null" && asn != NULL);
+
+       /* Create a string */
+       source = g_bytes_new (TEST_STRING, strlen(TEST_STRING));
+       g_return_if_fail (source);
+
+       /* Write the string out */
+       ret = gkm_data_asn1_write_string (egg_asn1x_node (asn, "data", NULL),
+                                          source);
+       g_assert ("couldn't write string to asn1" && ret);
+
+       /* Now encode the whole caboodle */
+       data = egg_asn1x_encode (asn, NULL);
+       g_assert ("encoding asn1 didn't work" && data != NULL);
+
+       egg_asn1x_destroy (asn);
+
+       /* Now decode it all nicely */
+       asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestString", data);
+       g_assert (asn != NULL);
+
+       ret = gkm_data_asn1_read_string (egg_asn1x_node (asn, "data", NULL),
+                                         &target);
+       egg_asn1x_destroy (asn);
+       g_assert ("couldn't read string from asn1" && ret);
+       g_assert ("string returned is null" && target != NULL);
+       g_assert ("The strings differ" && g_bytes_equal (source, target));
+
+       g_bytes_unref (data);
+       g_bytes_unref (source);
+       g_bytes_unref (target);
+}
+
+static void
+test_asn1_oid (Test *test, gconstpointer unused)
+{
+       GNode *asn;
+       GBytes *data;
+       gboolean ret;
+       GQuark source, target;
+
+       asn = egg_asn1x_create (test_asn1_tab, "TestOid");
+       g_assert ("asn test structure is null" && asn != NULL);
+
+       /* Create a OID Quark */
+       OID_ANSI_SECP256R1 = g_quark_from_static_string("1.2.840.10045.3.1.7");
+       source = OID_ANSI_SECP256R1;
+
+       /* Write the OID out */
+       ret = gkm_data_asn1_write_oid (egg_asn1x_node (asn, "oid", NULL), source);
+       g_assert ("couldn't write OID to asn1" && ret);
+
+       /* Now encode the whole caboodle */
+       data = egg_asn1x_encode (asn, NULL);
+       g_assert ("encoding asn1 didn't work" && data != NULL);
+
+       egg_asn1x_destroy (asn);
+
+       /* Now decode it all nicely */
+       asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOid", data);
+       g_assert (asn != NULL);
+
+       ret = gkm_data_asn1_read_oid (egg_asn1x_node (asn, "oid", NULL), &target);
+       egg_asn1x_destroy (asn);
+       g_assert ("couldn't read oid from asn1" && ret);
+       g_assert ("oid returned is 0" && target != 0);
+       g_assert ("mpi is wrong number" && source == target);
+
+       g_bytes_unref (data);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -121,6 +292,10 @@ main (int argc, char **argv)
        g_test_init (&argc, &argv, NULL);
 
        g_test_add ("/gkm/data-asn1/integers", Test, NULL, setup, test_asn1_integers, teardown);
+       g_test_add ("/gkm/data-asn1/string_mpi", Test, NULL, setup, test_asn1_string_mpi, teardown);
+       g_test_add ("/gkm/data-asn1/bit_string", Test, NULL, setup, test_asn1_bit_string, teardown);
+       g_test_add ("/gkm/data-asn1/string", Test, NULL, setup, test_asn1_string, teardown);
+       g_test_add ("/gkm/data-asn1/oid", Test, NULL, setup, test_asn1_oid, teardown);
 
        return g_test_run ();
 }
diff --git a/pkcs11/gkm/test.asn b/pkcs11/gkm/test.asn
index d6ca54d..820d1ae 100644
--- a/pkcs11/gkm/test.asn
+++ b/pkcs11/gkm/test.asn
@@ -13,4 +13,20 @@ TestData ::= SEQUENCE {
        boolean                 BOOLEAN DEFAULT FALSE
 }
 
+TestStringMpi ::= SEQUENCE {
+       mpi                     OCTET STRING
+}
+
+TestBitString ::= SEQUENCE {
+       data                    BIT STRING
+}
+
+TestString ::= SEQUENCE {
+       data                    OCTET STRING
+}
+
+TestOid ::= SEQUENCE {
+       oid                     OBJECT IDENTIFIER
+}
+
 END
diff --git a/pkcs11/gkm/test.asn.h b/pkcs11/gkm/test.asn.h
index 9a6381e..87cdc75 100644
--- a/pkcs11/gkm/test.asn.h
+++ b/pkcs11/gkm/test.asn.h
@@ -2,6 +2,7 @@
 # include "config.h"
 #endif
 
+/* Generated using  asn1Parser test.asn -o test.asn.h */
 /* #include <libtasn1.h> */
 
 const ASN1_ARRAY_TYPE test_asn1_tab[] = {
@@ -9,9 +10,17 @@ const ASN1_ARRAY_TYPE test_asn1_tab[] = {
   { NULL, 1073741836, NULL },
   { "TestIntegers", 1610612741, NULL },
   { "mpi", 3, NULL },
-  { "TestData", 536870917, NULL },
+  { "TestData", 1610612741, NULL },
   { "data", 1073741831, NULL },
   { "boolean", 536903684, NULL },
   { NULL, 131081, NULL },
+  { "TestStringMpi", 1610612741, NULL },
+  { "mpi", 7, NULL },
+  { "TestBitString", 1610612741, NULL },
+  { "data", 6, NULL },
+  { "TestString", 1610612741, NULL },
+  { "data", 7, NULL },
+  { "TestOid", 536870917, NULL },
+  { "oid", 12, NULL },
   { NULL, 0, NULL }
 };


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]