Fix some typo and comments
[openssl.git] / test / asn1_encode_test.c
index 293613a44fd93faf2386771af5b85268106ae1cf..3b8d4184be74e9adee0928ce3bf0e1acf09f7255 100644 (file)
@@ -10,9 +10,9 @@
 #include <stdio.h>
 #include <string.h>
 
+#include <openssl/rand.h>
 #include <openssl/asn1t.h>
 #include "internal/numbers.h"
-#include "test_main.h"
 #include "testutil.h"
 
 #ifdef __GNUC__
@@ -38,6 +38,12 @@ static unsigned char t_zero[] = {
 static unsigned char t_one[] = {
     0x01
 };
+static unsigned char t_one_neg[] = {
+    0xff
+};
+static unsigned char t_minus_256[] = {
+    0xff, 0x00
+};
 static unsigned char t_longundef[] = {
     0x7f, 0xff, 0xff, 0xff
 };
@@ -95,6 +101,8 @@ static TEST_CUSTOM_DATA test_custom_data[] = {
     CUSTOM_DATA(t_zero),
     CUSTOM_DATA(t_longundef),
     CUSTOM_DATA(t_one),
+    CUSTOM_DATA(t_one_neg),
+    CUSTOM_DATA(t_minus_256),
     CUSTOM_DATA(t_9bytes_1),
     CUSTOM_DATA(t_8bytes_1),
     CUSTOM_DATA(t_8bytes_2),
@@ -132,11 +140,12 @@ static TEST_CUSTOM_DATA test_custom_data[] = {
  * A structure to collect all test information in.  There MUST be one instance
  * of this for each test
  */
-typedef int i2d_fn(void **a, unsigned char **pp);
+typedef int i2d_fn(void *a, unsigned char **pp);
 typedef void *d2i_fn(void **a, unsigned char **pp, long length);
 typedef void ifree_fn(void *a);
 typedef struct {
-    char *name;
+    ASN1_ITEM_EXP *asn1_type;
+    const char *name;
     int skip;                    /* 1 if this package should be skipped */
 
     /* An array of structures to compare decoded custom data with */
@@ -194,6 +203,8 @@ static ASN1_LONG_DATA long_expected_32bit[] = {
     { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
     { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
+    CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
+    CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_2 */
@@ -214,7 +225,7 @@ static ASN1_LONG_DATA long_encdec_data_32bit[] = {
 };
 
 static TEST_PACKAGE long_test_package_32bit = {
-    "LONG", sizeof(long) != 4,
+    ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4,
     long_expected_32bit,
     sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
     long_encdec_data_32bit,
@@ -228,6 +239,8 @@ static ASN1_LONG_DATA long_expected_64bit[] = {
     { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
     { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
+    CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
+    CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
     CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
@@ -248,7 +261,7 @@ static ASN1_LONG_DATA long_encdec_data_64bit[] = {
 };
 
 static TEST_PACKAGE long_test_package_64bit = {
-    "LONG", sizeof(long) != 8,
+    ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8,
     long_expected_64bit,
     sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
     long_encdec_data_64bit,
@@ -268,8 +281,8 @@ typedef struct {
 
 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
     ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_FBOOLEAN),
-    ASN1_SIMPLE(ASN1_INT32_DATA, test_int32, INT32),
-    ASN1_EXP_OPT(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
+    ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
+    ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
 
 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
@@ -279,6 +292,8 @@ static ASN1_INT32_DATA int32_expected[] = {
     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
+    CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
+    CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_2 */
@@ -297,7 +312,7 @@ static ASN1_INT32_DATA int32_encdec_data[] = {
 };
 
 static TEST_PACKAGE int32_test_package = {
-    "INT32", 0,
+    ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0,
     int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
     int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
     (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
@@ -314,8 +329,8 @@ typedef struct {
 
 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
     ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_FBOOLEAN),
-    ASN1_SIMPLE(ASN1_UINT32_DATA, test_uint32, UINT32),
-    ASN1_EXP_OPT(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
+    ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
+    ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
 
 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
@@ -325,6 +340,8 @@ static ASN1_UINT32_DATA uint32_expected[] = {
     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
+    CUSTOM_EXPECTED_FAILURE,     /* t_one_neg (illegal negative value) */
+    CUSTOM_EXPECTED_FAILURE,     /* t_minus_256 (illegal negative value) */
     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_2 */
@@ -343,7 +360,7 @@ static ASN1_UINT32_DATA uint32_encdec_data[] = {
 };
 
 static TEST_PACKAGE uint32_test_package = {
-    "UINT32", 0,
+    ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0,
     uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
     uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
     (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
@@ -360,8 +377,8 @@ typedef struct {
 
 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
     ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_FBOOLEAN),
-    ASN1_SIMPLE(ASN1_INT64_DATA, test_int64, INT64),
-    ASN1_EXP_OPT(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
+    ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
+    ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
 
 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
@@ -371,6 +388,8 @@ static ASN1_INT64_DATA int64_expected[] = {
     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
+    CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
+    CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 (too large positive) */
     CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
@@ -390,7 +409,7 @@ static ASN1_INT64_DATA int64_encdec_data[] = {
 };
 
 static TEST_PACKAGE int64_test_package = {
-    "INT64", 0,
+    ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0,
     int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
     int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
     (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
@@ -407,8 +426,8 @@ typedef struct {
 
 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
     ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_FBOOLEAN),
-    ASN1_SIMPLE(ASN1_UINT64_DATA, test_uint64, UINT64),
-    ASN1_EXP_OPT(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
+    ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
+    ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
 
 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
@@ -418,6 +437,8 @@ static ASN1_UINT64_DATA uint64_expected[] = {
     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
+    CUSTOM_EXPECTED_FAILURE,     /* t_one_neg (illegal negative value) */
+    CUSTOM_EXPECTED_FAILURE,     /* t_minus_256 (illegal negative value) */
     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
     CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
                                  /* t_8bytes_1 */
@@ -437,7 +458,7 @@ static ASN1_UINT64_DATA uint64_encdec_data[] = {
 };
 
 static TEST_PACKAGE uint64_test_package = {
-    "UINT64", 0,
+    ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0,
     uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
     uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
     (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
@@ -651,7 +672,7 @@ static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
 {
     unsigned char *encoding = NULL;
     /*
-     * We force the defaults to be explicitely encoded to make sure we test
+     * We force the defaults to be explicitly encoded to make sure we test
      * for defaults that shouldn't be present (i.e. we check for failure)
      */
     size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
@@ -685,6 +706,22 @@ static int do_encode_custom(EXPECTED *input,
     return ret;
 }
 
+static int do_print_item(const TEST_PACKAGE *package)
+{
+#define DATA_BUF_SIZE 256
+    unsigned char buf[DATA_BUF_SIZE];
+    const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
+    ASN1_VALUE *o = (ASN1_VALUE *)&buf;
+    int ret;
+
+    OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
+
+    (void)RAND_bytes(buf, (int)package->encode_expectations_elem_size);
+    ret = ASN1_item_print(bio_err, o, 0, i, NULL);
+
+    return ret;
+}
+
 
 static int test_intern(const TEST_PACKAGE *package)
 {
@@ -706,18 +743,16 @@ static int test_intern(const TEST_PACKAGE *package)
                                                ->encode_expectations)[pos],
                                  &test_custom_data[i], package)) {
         case -1:
-            fprintf(stderr, "Failed custom encode round trip %u of %s\n",
-                    i, package->name);
-            ERR_print_errors_fp(stderr);
+            TEST_error("Failed custom encode round trip %u of %s",
+                       i, package->name);
+            TEST_openssl_errors();
             fail++;
-            ERR_clear_error();
             break;
         case 0:
-            fprintf(stderr, "Custom encode round trip %u of %s mismatch\n",
-                    i, package->name);
-            ERR_print_errors_fp(stderr);
+            TEST_error("Custom encode round trip %u of %s mismatch",
+                       i, package->name);
+            TEST_openssl_errors();
             fail++;
-            ERR_clear_error();
             break;
         case 1:
             break;
@@ -731,18 +766,16 @@ static int test_intern(const TEST_PACKAGE *package)
                                  package->encode_expectations_elem_size,
                                  package)) {
         case -1:
-            fprintf(stderr, "Failed custom decode round trip %u of %s\n",
-                    i, package->name);
-            ERR_print_errors_fp(stderr);
+            TEST_error("Failed custom decode round trip %u of %s",
+                       i, package->name);
+            TEST_openssl_errors();
             fail++;
-            ERR_clear_error();
             break;
         case 0:
-            fprintf(stderr, "Custom decode round trip %u of %s mismatch\n",
-                    i, package->name);
-            ERR_print_errors_fp(stderr);
+            TEST_error("Custom decode round trip %u of %s mismatch",
+                       i, package->name);
+            TEST_openssl_errors();
             fail++;
-            ERR_clear_error();
             break;
         case 1:
             break;
@@ -761,15 +794,14 @@ static int test_intern(const TEST_PACKAGE *package)
                            package->encdec_data_elem_size,
                            package)) {
         case -1:
-            fprintf(stderr, "Failed encode/decode round trip %u of %s\n",
-                    i, package->name);
-            ERR_print_errors_fp(stderr);
-            ERR_clear_error();
+            TEST_error("Failed encode/decode round trip %u of %s",
+                       i, package->name);
+            TEST_openssl_errors();
             fail++;
             break;
         case 0:
-            fprintf(stderr, "Encode/decode round trip %u of %s mismatch\n",
-                    i, package->name);
+            TEST_error("Encode/decode round trip %u of %s mismatch",
+                       i, package->name);
             fail++;
             break;
         case 1:
@@ -780,6 +812,12 @@ static int test_intern(const TEST_PACKAGE *package)
         }
     }
 
+    if (!do_print_item(package)) {
+        TEST_error("Printing of %s failed", package->name);
+        TEST_openssl_errors();
+        fail++;
+    }
+
     return fail == 0;
 }
 
@@ -815,7 +853,7 @@ static int test_uint64(void)
     return test_intern(&uint64_test_package);
 }
 
-void register_tests(void)
+int setup_tests(void)
 {
 #if OPENSSL_API_COMPAT < 0x10200000L
     ADD_TEST(test_long_32bit);
@@ -825,4 +863,5 @@ void register_tests(void)
     ADD_TEST(test_uint32);
     ADD_TEST(test_int64);
     ADD_TEST(test_uint64);
+    return 1;
 }