Added self-check for recursive choices.
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 14 Mar 2014 14:09:11 +0000 (15:09 +0100)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 14 Mar 2014 14:09:11 +0000 (15:09 +0100)
tests/Makefile.am
tests/Test_choice.c [new file with mode: 0644]
tests/choice.asn [new file with mode: 0644]

index 10bdba1..8f8beb8 100644 (file)
@@ -23,7 +23,7 @@ AM_LDFLAGS = -no-install
 LDADD = ../lib/libtasn1.la ../gl/libgnu.la
 
 EXTRA_DIST = Test_parser.asn Test_tree.asn Test_tree_asn1_tab.c        \
-       Test_encoding.asn pkix.asn TestIndef.p12
+       Test_encoding.asn pkix.asn TestIndef.p12 choice.asn
 
 # For crlf.
 EXTRA_DIST += crlf.cer
@@ -34,11 +34,11 @@ dist_check_SCRIPTS += threadsafety
 MOSTLYCLEANFILES = Test_parser_ERROR.asn
 
 check_PROGRAMS = Test_parser Test_tree Test_encoding Test_indefinite \
-       Test_errors Test_simple Test_overflow Test_strings
+       Test_errors Test_simple Test_overflow Test_strings Test_choice
 
 TESTS = Test_parser Test_tree Test_encoding Test_indefinite    \
        Test_errors Test_simple Test_overflow crlf threadsafety \
-       Test_strings
+       Test_strings Test_choice
 
 TESTS_ENVIRONMENT = \
        ASN1PARSER=$(srcdir)/Test_parser.asn \
diff --git a/tests/Test_choice.c b/tests/Test_choice.c
new file mode 100644 (file)
index 0000000..0c6742e
--- /dev/null
@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libtasn1.h>
+
+int
+main ()
+{
+  int result = 0;
+  asn1_node definitions = NULL, node1 = NULL, node2 = NULL;
+  char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
+  unsigned i;
+
+  char data[1024];
+  int data_size = sizeof (data);
+
+  /* Encode */
+  result = asn1_parser2tree ("choice.asn", &definitions, errorDescription);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  result = asn1_create_element (definitions, "TEST.Choice0", &node1);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  result = asn1_write_value (node1, "", "choice1", 1);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  result = asn1_write_value (node1, "choice1", "choice2", 1);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  result = asn1_write_value (node1, "choice1.choice2", "int1", 1);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  result = asn1_write_value (node1, "choice1.choice2.int1", "1234", 0);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  result = asn1_der_coding (node1, "", data, &data_size, errorDescription);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  asn1_delete_structure (&node1);
+
+  /* Decode */
+  result = asn1_create_element (definitions, "TEST.Choice0", &node2);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+#if 0
+  printf ("der:");
+  for (i = 0; i < data_size; i++)
+    printf ("%.2x ", (unsigned char) (data[i]));
+  printf ("\n");
+#endif
+
+  result = asn1_der_decoding (&node2, data, data_size, errorDescription);
+  if (result != ASN1_SUCCESS)
+    {
+      printf ("error in %d\n", __LINE__);
+      exit (1);
+    }
+
+  asn1_delete_structure (&node2);
+
+  return 0;
+}
diff --git a/tests/choice.asn b/tests/choice.asn
new file mode 100644 (file)
index 0000000..b309204
--- /dev/null
@@ -0,0 +1,23 @@
+TEST {}
+DEFINITIONS IMPLICIT TAGS ::=
+BEGIN
+
+Choice2 ::= CHOICE {
+ oct1 OCTET STRING,
+ int1 [3] INTEGER,
+ oct2 OCTET STRING
+}
+
+Choice1 ::= CHOICE {
+ int4 [0] INTEGER,
+ choice2 Choice2,
+ int5 [1] INTEGER
+}
+
+Choice0 ::= CHOICE {
+ int6 [0] INTEGER,
+ choice1 Choice1,
+ int7 [1] INTEGER
+}
+
+END