tests: added reproducer of cert re-encoding issue

Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
diff --git a/tests/Makefile.am b/tests/Makefile.am
index be10bbb..db077d4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,14 +60,14 @@
 	Test_errors Test_simple Test_overflow Test_strings Test_choice \
 	Test_encdec copynode coding-decoding2 strict-der Test_choice_ocsp \
 	ocsp-basic-response octet-string coding-long-oid object-id-decoding \
-	spc_pe_image_data
+	spc_pe_image_data cert-reenc
 
 TESTS = Test_parser Test_tree Test_encoding Test_indefinite	\
 	Test_errors Test_simple Test_overflow crlf threadsafety \
 	Test_strings Test_choice Test_encdec copynode coding-decoding2 \
 	strict-der Test_choice_ocsp decoding decoding-invalid-x509 \
 	ocsp-basic-response octet-string coding-long-oid object-id-decoding \
-	spc_pe_image_data decoding-invalid-pkcs7 coding
+	spc_pe_image_data decoding-invalid-pkcs7 coding cert-reenc
 
 TESTS_ENVIRONMENT = \
 	ASN1PARSER=$(srcdir)/Test_parser.asn \
diff --git a/tests/cert-reenc.c b/tests/cert-reenc.c
new file mode 100644
index 0000000..563f7de
--- /dev/null
+++ b/tests/cert-reenc.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2002-2014 Free Software Foundation, Inc.
+ *
+ * This file is part of LIBTASN1.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+/*****************************************************/
+/* File: Test_tree.c                                 */
+/* Description: Test sequences for these functions:  */
+/*     asn1_visit_tree,                              */
+/*     asn1_create_element,                          */
+/*     asn1_delete_structure,                        */
+/*     asn1_write_value,                             */
+/*     asn1_read_value,                              */
+/*****************************************************/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "libtasn1.h"
+
+unsigned char _cert_der[] = {
+  0x30, 0x82, 0x03, 0x24, 0x30, 0x82, 0x02, 0x0c, 0xa0, 0x03, 0x02, 0x01,
+  0x02, 0x02, 0x01, 0x08, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
+  0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x26, 0x31, 0x24, 0x30,
+  0x22, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1b, 0x6f, 0x70, 0x65, 0x6e,
+  0x73, 0x68, 0x69, 0x66, 0x74, 0x2d, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x72,
+  0x40, 0x31, 0x35, 0x31, 0x38, 0x31, 0x39, 0x35, 0x31, 0x34, 0x38, 0x30,
+  0x1e, 0x17, 0x0d, 0x31, 0x38, 0x30, 0x32, 0x30, 0x39, 0x31, 0x36, 0x35,
+  0x32, 0x32, 0x38, 0x5a, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x32, 0x30, 0x39,
+  0x31, 0x36, 0x35, 0x32, 0x32, 0x39, 0x5a, 0x30, 0x4e, 0x31, 0x35, 0x30,
+  0x1c, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x15, 0x73, 0x79, 0x73, 0x74,
+  0x65, 0x6d, 0x3a, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2d, 0x61,
+  0x64, 0x6d, 0x69, 0x6e, 0x73, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0a,
+  0x13, 0x0e, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x3a, 0x6d, 0x61, 0x73,
+  0x74, 0x65, 0x72, 0x73, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04,
+  0x03, 0x13, 0x0c, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x3a, 0x61, 0x64,
+  0x6d, 0x69, 0x6e, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a,
+  0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82,
+  0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00,
+  0xb4, 0xe5, 0x5e, 0xaf, 0xe3, 0xbc, 0x83, 0xcb, 0x8e, 0xe2, 0xf5, 0xad,
+  0x91, 0xd9, 0x59, 0x06, 0x83, 0xcf, 0x29, 0x5f, 0x78, 0x81, 0x7a, 0x95,
+  0xac, 0xce, 0x17, 0x7a, 0xb7, 0xbd, 0x66, 0x74, 0x02, 0x3e, 0xdb, 0xed,
+  0xa7, 0xb6, 0xce, 0x38, 0xe6, 0x7f, 0xe5, 0x2e, 0x4d, 0x8d, 0x8b, 0xf0,
+  0x0b, 0x67, 0xc0, 0x95, 0x9b, 0xdd, 0xe7, 0x2b, 0xf1, 0xa2, 0xba, 0xb7,
+  0xf9, 0xae, 0x39, 0xc9, 0x39, 0x1d, 0xd9, 0xea, 0x7c, 0x1c, 0x52, 0xc9,
+  0xdb, 0x79, 0xc0, 0x1f, 0xcf, 0xd6, 0x1b, 0x65, 0x48, 0x03, 0x14, 0x58,
+  0x07, 0x6e, 0x38, 0x43, 0x3a, 0xff, 0x18, 0x19, 0x9b, 0x81, 0x1f, 0x6e,
+  0x80, 0x93, 0x04, 0x41, 0x14, 0x0d, 0xab, 0x9b, 0x92, 0x89, 0xf4, 0xa7,
+  0x63, 0x18, 0x75, 0x33, 0xc5, 0x1c, 0x13, 0xdd, 0x29, 0xf4, 0xc8, 0x40,
+  0x25, 0x5f, 0x57, 0x28, 0x83, 0xf1, 0x46, 0xb2, 0x7a, 0xf1, 0x2a, 0x0a,
+  0xf0, 0x5d, 0x33, 0x67, 0x3a, 0x5a, 0x83, 0x85, 0x1f, 0xb6, 0xf5, 0x5e,
+  0xe0, 0xd6, 0xf3, 0x7c, 0x4d, 0x72, 0x56, 0x81, 0x07, 0x41, 0x61, 0x68,
+  0x03, 0x8f, 0x35, 0x89, 0xaa, 0x77, 0x67, 0x66, 0xf2, 0x90, 0x83, 0xd6,
+  0x35, 0x4b, 0xa7, 0xfd, 0xf5, 0x66, 0x79, 0x81, 0xcc, 0x2f, 0xdd, 0xf5,
+  0xaa, 0xa2, 0x93, 0x96, 0x1a, 0xf1, 0xcf, 0x00, 0xe7, 0xc6, 0x89, 0xad,
+  0xfc, 0xb1, 0xcc, 0xb6, 0x67, 0x46, 0xbd, 0xed, 0x62, 0xe5, 0x8b, 0x94,
+  0x2e, 0xad, 0xae, 0xee, 0xdf, 0x28, 0x7c, 0xf2, 0xd1, 0x57, 0x47, 0x33,
+  0x0d, 0xf2, 0x61, 0xc1, 0xf9, 0xa7, 0x71, 0x8e, 0xe7, 0xf7, 0x2e, 0x60,
+  0x6b, 0x42, 0xd1, 0x84, 0xe1, 0x08, 0xdb, 0x12, 0xac, 0xad, 0x70, 0x3d,
+  0x62, 0xf9, 0x2e, 0x8b, 0x78, 0x74, 0x4f, 0x3b, 0xaf, 0x16, 0xd0, 0xaa,
+  0x33, 0x4d, 0x97, 0x45, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x35, 0x30,
+  0x33, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04,
+  0x04, 0x03, 0x02, 0x05, 0xa0, 0x30, 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25,
+  0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
+  0x03, 0x02, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
+  0x04, 0x02, 0x30, 0x00, 0x30, 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 0x3e, 0xd6, 0xc1, 0x15, 0x4d, 0x0b, 0x6c,
+  0x1a, 0xc7, 0x58, 0xf7, 0x4b, 0xe8, 0xf1, 0x72, 0xe6, 0x18, 0x00, 0xc0,
+  0xe5, 0x6e, 0xb9, 0x3b, 0xb7, 0xcb, 0x6f, 0x93, 0x9e, 0x9d, 0x82, 0xab,
+  0x63, 0xbd, 0x67, 0x02, 0x74, 0x51, 0xae, 0x0d, 0x9b, 0xb5, 0xc9, 0x56,
+  0xb8, 0x5d, 0x48, 0xc0, 0x14, 0x3d, 0x81, 0x35, 0xb7, 0x0d, 0x5d, 0xf6,
+  0x79, 0xf1, 0xb9, 0x17, 0xda, 0x6b, 0x3c, 0x86, 0xac, 0x12, 0x96, 0xd5,
+  0x52, 0x18, 0x12, 0x1f, 0x15, 0x57, 0x06, 0xbf, 0xfd, 0x48, 0x0a, 0x25,
+  0xe1, 0xbe, 0x38, 0x16, 0xf3, 0x11, 0x85, 0x97, 0x37, 0x63, 0x06, 0xc6,
+  0x8c, 0x55, 0x9d, 0x1d
+};
+unsigned int _cert_der_len = 808;
+
+
+int
+main (int argc, char *argv[])
+{
+  int result;
+  char buffer2[5 * 1024];
+  asn1_node definitions = NULL;
+  asn1_node asn1_element = NULL;
+  char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
+  FILE *out;
+  int size2;
+  const char *treefile = getenv ("ASN1PKIX");
+  int verbose = 0;
+
+  if (argc > 1)
+    verbose = 1;
+
+  if (!treefile)
+    treefile = "pkix.asn";
+
+  if (verbose)
+    {
+      printf ("\n\n/****************************************/\n");
+      printf ("/*     Test sequence : Test_indefinite  */\n");
+      printf ("/****************************************/\n\n");
+      printf ("ASN1TREE: %s\n", treefile);
+    }
+
+  /* Check version */
+  if (asn1_check_version ("0.3.3") == NULL)
+    printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
+	    asn1_check_version (NULL));
+
+  result = asn1_parser2tree (treefile, &definitions, errorDescription);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("ErrorDescription = %s\n\n", errorDescription);
+      exit (1);
+    }
+
+  out = stdout;
+
+  result =
+    asn1_create_element (definitions, "PKIX1.Certificate", &asn1_element);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot create CRL element\n");
+      exit (1);
+    }
+
+  result = asn1_der_decoding (&asn1_element, _cert_der, _cert_der_len, errorDescription);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot decode DER data (size %ld)\n", (long) _cert_der_len);
+      exit (1);
+    }
+
+  size2 = sizeof(buffer2);
+  result = asn1_der_coding (asn1_element, "", buffer2, &size2, NULL);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot encode data (size %ld)\n", (long) size2);
+      exit (1);
+    }
+ 
+  if (size2 != (int)_cert_der_len || memcmp(buffer2, _cert_der, _cert_der_len) != 0) 
+    {
+      printf("DER encoded data differ!\n");
+      exit(1);
+    }
+
+  /* Clear the definition structures */
+  asn1_delete_structure (&definitions);
+  asn1_delete_structure (&asn1_element);
+
+  if (out != stdout)
+    fclose (out);
+
+  exit (0);
+}