[gnome-keyring/asn1-work: 18/18] [egg] Implement some new asn1 tests, and fix problems.



commit e76c4996a36f2811f4f852573b655e7b284af2f3
Author: Stef Walter <stef memberwebs com>
Date:   Thu Dec 31 01:22:59 2009 +0000

    [egg] Implement some new asn1 tests, and fix problems.

 egg/.gitignore        |    3 +-
 egg/Makefile.am       |    1 +
 egg/egg-asn1x.c       |   23 ++++--
 egg/tests/Makefile.am |   10 ++-
 egg/tests/test-asn1.c |  202 +++++++++++++++++++++++++++++++++++++++++++++++++
 egg/tests/tests.asn   |   19 +++++
 6 files changed, 247 insertions(+), 11 deletions(-)
---
diff --git a/egg/.gitignore b/egg/.gitignore
index 1142560..7061f83 100644
--- a/egg/.gitignore
+++ b/egg/.gitignore
@@ -1,4 +1,3 @@
-/asn1-def-pk.h
-/asn1-def-pkix.h
+asn1-def-*.h
 /tests/test-asn1x
 
diff --git a/egg/Makefile.am b/egg/Makefile.am
index 5cb86d6..e47c560 100644
--- a/egg/Makefile.am
+++ b/egg/Makefile.am
@@ -22,6 +22,7 @@ libegg_la_CFLAGS = \
 
 libegg_la_SOURCES = \
 	egg-asn1.c egg-asn1.h \
+	egg-asn1x.c egg-asn1x.h \
 	egg-buffer.c egg-buffer.h \
 	egg-cleanup.c egg-cleanup.h \
 	egg-dh.c egg-dh.h \
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index b1ecc59..1c71e8f 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -931,6 +931,9 @@ egg_asn1x_decode (GNode *asn, gconstpointer data, gsize n_data)
 	if (!anode_decode_anything (asn, &tlv))
 		return FALSE;
 
+	if (tlv.end - tlv.buf != n_data)
+		return FALSE;
+
 	return egg_asn1x_validate (asn);
 }
 
@@ -1820,7 +1823,7 @@ anode_read_boolean (GNode *node, Atlv *tlv, gboolean *value)
 		return FALSE;
 	if (tlv->buf[tlv->off] == 0x00)
 		*value = FALSE;
-	if (tlv->buf[tlv->off] == 0xFF)
+	else if (tlv->buf[tlv->off] == 0xFF)
 		*value = TRUE;
 	else
 		return FALSE;
@@ -2003,7 +2006,8 @@ egg_asn1x_get_boolean (GNode *node, gboolean *value)
 	g_return_val_if_fail (anode_def_type (node) == TYPE_BOOLEAN, FALSE);
 
 	tlv = anode_get_tlv_data (node);
-	g_return_val_if_fail (tlv, FALSE);
+	if (tlv == NULL)
+		return FALSE;
 
 	return anode_read_boolean (node, tlv, value);
 }
@@ -2035,7 +2039,8 @@ egg_asn1x_get_integer_as_ulong (GNode *node, gulong *value)
 	g_return_val_if_fail (anode_def_type (node) == TYPE_INTEGER, FALSE);
 
 	tlv = anode_get_tlv_data (node);
-	g_return_val_if_fail (tlv, FALSE);
+	if (tlv == NULL)
+		return FALSE;
 
 	return anode_read_integer_as_ulong(node, tlv, value);
 }
@@ -2066,7 +2071,8 @@ egg_asn1x_get_raw_value (GNode *node, gsize *n_content)
 	g_return_val_if_fail (n_content, NULL);
 
 	tlv = anode_get_tlv_data (node);
-	g_return_val_if_fail (tlv, NULL);
+	if (tlv == NULL)
+		return FALSE;
 	g_return_val_if_fail (!(tlv->cls & ASN1_CLASS_STRUCTURED), NULL);
 
 	*n_content = tlv->len;
@@ -2099,7 +2105,8 @@ egg_asn1x_get_string_as_raw (GNode *node, EggAllocator allocator, gsize *n_strin
 	g_return_val_if_fail (type == TYPE_OCTET_STRING || type == TYPE_GENERALSTRING, NULL);
 
 	tlv = anode_get_tlv_data (node);
-	g_return_val_if_fail (tlv, NULL);
+	if (tlv == NULL)
+		return NULL;
 
 	if (!anode_read_string (node, tlv, NULL, &length))
 		return NULL;
@@ -2182,7 +2189,8 @@ egg_asn1x_get_time_as_long (GNode *node)
 	g_return_val_if_fail (anode_def_type (node) == TYPE_TIME, -1);
 
 	tlv = anode_get_tlv_data (node);
-	g_return_val_if_fail (tlv, -1);
+	if (tlv == NULL)
+		return -1;
 
 	if (!anode_read_time (node, tlv, &time))
 		return -1;
@@ -2199,7 +2207,8 @@ egg_asn1x_get_oid_as_string (GNode *node)
 	g_return_val_if_fail (anode_def_type (node) == TYPE_OBJECT_ID, NULL);
 
 	tlv = anode_get_tlv_data (node);
-	g_return_val_if_fail (tlv, NULL);
+	if (tlv == NULL)
+		return NULL;
 
 	if (!anode_read_object_id (node, tlv, &oid))
 		return NULL;
diff --git a/egg/tests/Makefile.am b/egg/tests/Makefile.am
index d42a92e..eddb066 100644
--- a/egg/tests/Makefile.am
+++ b/egg/tests/Makefile.am
@@ -1,8 +1,12 @@
 asn1-def-test.h: test.asn
-	$(ASN1PARSER) -o asn1-def-test.h $(srcdir)/test.asn 
+	$(ASN1PARSER) -o asn1-def-test.h $(srcdir)/test.asn
+
+asn1-def-tests.h: tests.asn
+	$(ASN1PARSER) -o asn1-def-tests.h $(srcdir)/tests.asn
 
 # Test files should be listed in order they need to run
 TESTING_FILES = \
+	test-asn1.c \
 	unit-test-asn1.c \
 	test-dn.c \
 	unit-test-cleanup.c \
@@ -14,7 +18,8 @@ TESTING_FILES = \
 	unit-test-openssl.c \
 	unit-test-dh.c \
 	unit-test-spawn.c \
-	asn1-def-test.h
+	asn1-def-test.h \
+	asn1-def-tests.h
 
 UNIT_PROMPT = 
 
@@ -23,6 +28,7 @@ TESTING_LIBS =  \
 
 EXTRA_DIST = \
 	test.asn \
+	tests.asn \
 	test-data
 
 include $(top_srcdir)/testing/testing.make
diff --git a/egg/tests/test-asn1.c b/egg/tests/test-asn1.c
new file mode 100644
index 0000000..d550984
--- /dev/null
+++ b/egg/tests/test-asn1.c
@@ -0,0 +1,202 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* test-asn1.c: Test ASN1 stuf
+
+   Copyright (C) 2009 Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#include "config.h"
+
+#include "test-suite.h"
+
+#include "egg/egg-asn1x.h"
+
+#include <glib.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "asn1-def-tests.h"
+
+const gchar I33[] =           "\x02\x01\x2A";
+const gchar BFALSE[] =        "\x01\x01\x00";
+const gchar BTRUE[] =         "\x01\x01\xFF";
+const gchar SFARNSWORTH[] =   "\x04\x0A""farnsworth";
+const gchar SIMPLICIT[] =     "\x85\x08""implicit";
+const gchar SEXPLICIT[] =     "\xE5\x0A\x04\x08""explicit";
+const gchar TGENERALIZED[] =  "\x18\x0F""20070725130528Z";
+
+#define XL(x) G_N_ELEMENTS (x) - 1
+
+DEFINE_TEST(asn1_boolean)
+{
+	GNode *asn;
+	gboolean value;
+
+	asn = egg_asn1x_create (tests_asn1_tab, "TestBoolean");
+	g_assert (asn);
+
+	/* Shouldn't succeed */
+	if (egg_asn1x_get_boolean (asn, &value))
+		g_assert_not_reached ();
+
+	/* Decode a false */
+	if (!egg_asn1x_decode (asn, BFALSE, XL (BFALSE)))
+		g_assert_not_reached ();
+	value = TRUE;
+	if (!egg_asn1x_get_boolean (asn, &value))
+		g_assert_not_reached ();
+	g_assert (value == FALSE);
+
+	/* Decode a true */
+	if (!egg_asn1x_decode (asn, BTRUE, XL (BTRUE)))
+		g_assert_not_reached ();
+	value = FALSE;
+	if (!egg_asn1x_get_boolean (asn, &value))
+		g_assert_not_reached ();
+	g_assert (value == TRUE);
+
+	egg_asn1x_clear (asn);
+
+	/* Shouldn't suceed after clear */
+	if (egg_asn1x_get_boolean (asn, &value))
+		g_assert_not_reached ();
+
+	egg_asn1x_destroy (asn);
+}
+
+DEFINE_TEST(asn1_integer)
+{
+	GNode *asn;
+	gulong value;
+
+	asn = egg_asn1x_create (tests_asn1_tab, "TestInteger");
+	g_assert (asn);
+
+	/* Shouldn't succeed */
+	if (egg_asn1x_get_integer_as_ulong (asn, &value))
+		g_assert_not_reached ();
+
+	/* Should suceed now */
+	if (!egg_asn1x_decode (asn, I33, XL (I33)))
+		g_assert_not_reached ();
+	if (!egg_asn1x_get_integer_as_ulong (asn, &value))
+		g_assert_not_reached ();
+	g_assert (value == 42);
+
+	egg_asn1x_clear (asn);
+
+	/* Shouldn't suceed after clear */
+	if (egg_asn1x_get_integer_as_ulong (asn, &value))
+		g_assert_not_reached ();
+
+	egg_asn1x_destroy (asn);
+}
+
+DEFINE_TEST(asn1_octet_string)
+{
+	GNode *asn;
+	gchar *value;
+
+	asn = egg_asn1x_create (tests_asn1_tab, "TestOctetString");
+	g_assert (asn);
+
+	/* Shouldn't succeed */
+	if (egg_asn1x_get_string_as_utf8 (asn, NULL))
+		g_assert_not_reached ();
+
+	/* Should work */
+	if (!egg_asn1x_decode (asn, SFARNSWORTH, XL (SFARNSWORTH)))
+		g_assert_not_reached ();
+	value = egg_asn1x_get_string_as_utf8 (asn, NULL);
+	g_assert_cmpstr (value, ==, "farnsworth");
+	g_free (value);
+
+	egg_asn1x_clear (asn);
+
+	/* Shouldn't succeed */
+	if (egg_asn1x_get_string_as_utf8 (asn, NULL))
+		g_assert_not_reached ();
+
+	egg_asn1x_destroy (asn);
+}
+
+DEFINE_TEST(asn1_generalized_time)
+{
+	GNode *asn;
+	glong value;
+
+	asn = egg_asn1x_create (tests_asn1_tab, "TestGeneralized");
+	g_assert (asn);
+
+	/* Shouldn't succeed */
+	value = egg_asn1x_get_time_as_long (asn);
+	g_assert (value == -1);
+
+	/* Should work */
+	if (!egg_asn1x_decode (asn, TGENERALIZED, XL (TGENERALIZED)))
+		g_assert_not_reached ();
+	value = egg_asn1x_get_time_as_long (asn);
+	g_assert (value == 1185368728);
+
+	egg_asn1x_clear (asn);
+
+	/* Shouldn't succeed */
+	value = egg_asn1x_get_time_as_long (asn);
+	g_assert (value == -1);
+
+	egg_asn1x_destroy (asn);
+}
+
+DEFINE_TEST(asn1_implicit)
+{
+	GNode *asn;
+	gchar *value;
+
+	asn = egg_asn1x_create (tests_asn1_tab, "TestImplicit");
+	g_assert (asn);
+
+	/* Should work */
+	if (!egg_asn1x_decode (asn, SIMPLICIT, XL (SIMPLICIT)))
+		g_assert_not_reached ();
+	value = egg_asn1x_get_string_as_utf8 (asn, NULL);
+	g_assert_cmpstr (value, ==, "implicit");
+	g_free (value);
+
+	egg_asn1x_destroy (asn);
+}
+
+DEFINE_TEST(asn1_explicit)
+{
+	GNode *asn;
+	gchar *value;
+
+	asn = egg_asn1x_create (tests_asn1_tab, "TestExplicit");
+	g_assert (asn);
+
+	/* Should work */
+	if (!egg_asn1x_decode (asn, SEXPLICIT, XL (SEXPLICIT)))
+		g_assert_not_reached ();
+	value = egg_asn1x_get_string_as_utf8 (asn, NULL);
+	g_assert_cmpstr (value, ==, "explicit");
+	g_free (value);
+
+	egg_asn1x_destroy (asn);
+}
diff --git a/egg/tests/tests.asn b/egg/tests/tests.asn
new file mode 100644
index 0000000..ad23d5b
--- /dev/null
+++ b/egg/tests/tests.asn
@@ -0,0 +1,19 @@
+TESTS { }
+
+DEFINITIONS EXPLICIT TAGS ::=
+
+BEGIN
+
+TestInteger ::= INTEGER
+
+TestBoolean ::= BOOLEAN
+
+TestOctetString ::= OCTET STRING
+
+TestGeneralized ::= GeneralizedTime
+
+TestImplicit ::= [5] IMPLICIT OCTET STRING
+
+TestExplicit ::= [5] EXPLICIT OCTET STRING
+
+END



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