summaryrefslogtreecommitdiff
path: root/pkcs11/gkm/test-data-asn1.c
diff options
context:
space:
mode:
authorJakub Jelen <jjelen@redhat.com>2017-08-08 18:54:19 +0200
committerDaiki Ueno <ueno@gnu.org>2017-10-13 08:51:46 +0200
commit660b012dee1e807ff129764f407d4ad8588c3bbd (patch)
tree0bde43b31a9d77cd649aed19b87f9f99f93d21d5 /pkcs11/gkm/test-data-asn1.c
parent44c4205701dda8c24f9ab78a3b8f09ab600d1a11 (diff)
downloadgnome-keyring-660b012dee1e807ff129764f407d4ad8588c3bbd.tar.gz
Extend the asn1 test
https://bugzilla.gnome.org/show_bug.cgi?id=641082
Diffstat (limited to 'pkcs11/gkm/test-data-asn1.c')
-rw-r--r--pkcs11/gkm/test-data-asn1.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/pkcs11/gkm/test-data-asn1.c b/pkcs11/gkm/test-data-asn1.c
index 59da5d4f..cc5b0990 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 ();
}