From 5208fc8539aabc626c1699f181e1191d6bb1c787 Mon Sep 17 00:00:00 2001
From: Stef Walter <stefw@gnome.org>
Date: Thu, 14 Mar 2013 11:14:52 +0100
Subject: asn1: Implement a parsed ASN.1 tree cache

In order to unmarry the parser from the future builder, but still retain
efficiency, we need to be able to cache parsed ASN.1 trees. The ASN.1
cache provides this. In addition it carries around the loaded ASN.1
definitions.

https://bugs.freedesktop.org/show_bug.cgi?id=62329
---
 common/tests/test-asn1.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

(limited to 'common/tests')

diff --git a/common/tests/test-asn1.c b/common/tests/test-asn1.c
index 0034623..0335fa6 100644
--- a/common/tests/test-asn1.c
+++ b/common/tests/test-asn1.c
@@ -89,6 +89,51 @@ test_tlv_length (CuTest *cu)
 	teardown (cu);
 }
 
+static const unsigned char test_eku_server_and_client[] = {
+	0x30, 0x14, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b, 0x06,
+	0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
+};
+
+static void
+test_asn1_cache (CuTest *cu)
+{
+	p11_asn1_cache *cache;
+	p11_dict *defs;
+	node_asn *asn;
+	node_asn *check;
+
+	cache = p11_asn1_cache_new ();
+	CuAssertPtrNotNull (cu, cache);
+
+	defs = p11_asn1_cache_defs (cache);
+	CuAssertPtrNotNull (cu, defs);
+
+	asn = p11_asn1_decode (defs, "PKIX1.ExtKeyUsageSyntax",
+	                       test_eku_server_and_client,
+	                       sizeof (test_eku_server_and_client), NULL);
+	CuAssertPtrNotNull (cu, defs);
+
+	/* Place the parsed data in the cache */
+	p11_asn1_cache_take (cache, asn, "PKIX1.ExtKeyUsageSyntax",
+	                     test_eku_server_and_client,
+	                     sizeof (test_eku_server_and_client));
+
+	/* Get it back out */
+	check = p11_asn1_cache_get (cache, "PKIX1.ExtKeyUsageSyntax",
+	                            test_eku_server_and_client,
+	                            sizeof (test_eku_server_and_client));
+	CuAssertPtrEquals (cu, asn, check);
+
+	/* Flush should remove it */
+	p11_asn1_cache_flush (cache);
+	check = p11_asn1_cache_get (cache, "PKIX1.ExtKeyUsageSyntax",
+	                            test_eku_server_and_client,
+	                            sizeof (test_eku_server_and_client));
+	CuAssertPtrEquals (cu, NULL, check);
+
+	p11_asn1_cache_free (cache);
+}
+
 int
 main (void)
 {
@@ -100,6 +145,7 @@ main (void)
 	p11_debug_init ();
 
 	SUITE_ADD_TEST (suite, test_tlv_length);
+	SUITE_ADD_TEST (suite, test_asn1_cache);
 
 	CuSuiteRun (suite);
 	CuSuiteSummary (suite, output);
-- 
cgit v1.1