Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / test / asn1_encode_test.c
index 763c427d1a1d4ab14ea2e0412a801c227c89fda1..335f24e1133fb70eddc6ee99276281bd8578db6b 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
@@ -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__
@@ -41,6 +41,9 @@ static unsigned char t_one[] = {
 static unsigned char t_one_neg[] = {
     0xff
 };
+static unsigned char t_minus_256[] = {
+    0xff, 0x00
+};
 static unsigned char t_longundef[] = {
     0x7f, 0xff, 0xff, 0xff
 };
@@ -99,6 +102,7 @@ static TEST_CUSTOM_DATA test_custom_data[] = {
     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),
@@ -140,7 +144,8 @@ 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 */
@@ -174,7 +179,7 @@ typedef struct {
     ENCDEC_DATA(-1, -1),                        \
     ENCDEC_DATA(0, ASN1_LONG_UNDEF)
 
-#if OPENSSL_API_COMPAT < 0x10200000L
+#ifndef OPENSSL_NO_DEPRECATED_3_0
 /***** LONG ******************************************************************/
 
 typedef struct {
@@ -185,7 +190,7 @@ typedef struct {
 } ASN1_LONG_DATA;
 
 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
-    ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_FBOOLEAN),
+    ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_BOOLEAN),
     ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
     ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
@@ -199,6 +204,7 @@ static ASN1_LONG_DATA long_expected_32bit[] = {
     { 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 */
@@ -219,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,
@@ -234,6 +240,7 @@ static ASN1_LONG_DATA long_expected_64bit[] = {
     { 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 */
@@ -254,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,
@@ -273,9 +280,9 @@ typedef struct {
 } ASN1_INT32_DATA;
 
 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_SIMPLE(ASN1_INT32_DATA, success, ASN1_BOOLEAN),
+    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)
@@ -286,6 +293,7 @@ static ASN1_INT32_DATA int32_expected[] = {
     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 */
@@ -304,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,
@@ -320,9 +328,9 @@ typedef struct {
 } ASN1_UINT32_DATA;
 
 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_SIMPLE(ASN1_UINT32_DATA, success, ASN1_BOOLEAN),
+    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)
@@ -333,6 +341,7 @@ static ASN1_UINT32_DATA uint32_expected[] = {
     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 */
@@ -351,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,
@@ -367,9 +376,9 @@ typedef struct {
 } ASN1_INT64_DATA;
 
 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_SIMPLE(ASN1_INT64_DATA, success, ASN1_BOOLEAN),
+    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)
@@ -380,13 +389,14 @@ static ASN1_INT64_DATA int64_expected[] = {
     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 */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad (illegal padding) */
     CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad (illegal padding) */
-    CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
+    CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */
     CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
@@ -399,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,
@@ -415,9 +425,9 @@ typedef struct {
 } ASN1_UINT64_DATA;
 
 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_SIMPLE(ASN1_UINT64_DATA, success, ASN1_BOOLEAN),
+    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)
@@ -428,6 +438,7 @@ static ASN1_UINT64_DATA uint64_expected[] = {
     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 */
@@ -435,7 +446,7 @@ static ASN1_UINT64_DATA uint64_expected[] = {
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_4_neg */
     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad */
-    CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
+    CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */
     CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
@@ -447,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,
@@ -566,14 +577,14 @@ static size_t der_encode_length(size_t len, unsigned char **pp)
 
     if (pp != NULL) {
         if (lenbytes == 1) {
-            *(*pp)++ = len;
+            *(*pp)++ = (unsigned char)len;
         } else {
-            *(*pp)++ = lenbytes - 1;
+            *(*pp)++ = (unsigned char)(lenbytes - 1);
             if (lenbytes == 2) {
-                *(*pp)++ = 0x80 | len;
+                *(*pp)++ = (unsigned char)(0x80 | len);
             } else {
-                *(*pp)++ = 0x80 | (len >> 8);
-                *(*pp)++ = len & 0xff;
+                *(*pp)++ = (unsigned char)(0x80 | (len >> 8));
+                *(*pp)++ = (unsigned char)(len);
             }
         }
     }
@@ -661,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);
@@ -695,6 +706,25 @@ static int do_encode_custom(EXPECTED *input,
     return ret;
 }
 
+static int do_print_item(const TEST_PACKAGE *package)
+{
+#define DATA_BUF_SIZE 256
+    const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
+    ASN1_VALUE *o;
+    int ret;
+
+    OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
+    if ((o = OPENSSL_malloc(DATA_BUF_SIZE)) == NULL)
+        return 0;
+
+    (void)RAND_bytes((unsigned char*)o,
+                     (int)package->encode_expectations_elem_size);
+    ret = ASN1_item_print(bio_err, o, 0, i, NULL);
+    OPENSSL_free(o);
+
+    return ret;
+}
+
 
 static int test_intern(const TEST_PACKAGE *package)
 {
@@ -712,22 +742,23 @@ static int test_intern(const TEST_PACKAGE *package)
                    sizeof(test_custom_data) / sizeof(test_custom_data[0]));
     for (i = 0; i < nelems; i++) {
         size_t pos = i * package->encode_expectations_elem_size;
-        switch (do_encode_custom((EXPECTED *)&((unsigned char *)package
-                                               ->encode_expectations)[pos],
-                                 &test_custom_data[i], package)) {
+        EXPECTED *expected
+            = (EXPECTED *)&((unsigned char *)package->encode_expectations)[pos];
+
+        switch (do_encode_custom(expected, &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);
-            fail++;
-            ERR_clear_error();
+            if (expected->success) {
+                TEST_error("Failed custom encode round trip %u of %s",
+                           i, package->name);
+                TEST_openssl_errors();
+                fail++;
+            }
             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;
@@ -735,24 +766,22 @@ static int test_intern(const TEST_PACKAGE *package)
             OPENSSL_die("do_encode_custom() return unknown value",
                         __FILE__, __LINE__);
         }
-        switch (do_decode_custom(&test_custom_data[i],
-                                 (EXPECTED *)&((unsigned char *)package
-                                               ->encode_expectations)[pos],
+        switch (do_decode_custom(&test_custom_data[i], expected,
                                  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);
-            fail++;
-            ERR_clear_error();
+            if (expected->success) {
+                TEST_error("Failed custom decode round trip %u of %s",
+                           i, package->name);
+                TEST_openssl_errors();
+                fail++;
+            }
             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;
@@ -766,20 +795,21 @@ static int test_intern(const TEST_PACKAGE *package)
     nelems = package->encdec_data_size / package->encdec_data_elem_size;
     for (i = 0; i < nelems; i++) {
         size_t pos = i * package->encdec_data_elem_size;
-        switch (do_enc_dec((EXPECTED *)&((unsigned char *)package
-                                         ->encdec_data)[pos],
-                           package->encdec_data_elem_size,
-                           package)) {
+        EXPECTED *expected
+            = (EXPECTED *)&((unsigned char *)package->encdec_data)[pos];
+
+        switch (do_enc_dec(expected, 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();
-            fail++;
+            if (expected->success) {
+                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:
@@ -790,10 +820,16 @@ 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;
 }
 
-#if OPENSSL_API_COMPAT < 0x10200000L
+#ifndef OPENSSL_NO_DEPRECATED_3_0
 static int test_long_32bit(void)
 {
     return test_intern(&long_test_package_32bit);
@@ -825,9 +861,41 @@ static int test_uint64(void)
     return test_intern(&uint64_test_package);
 }
 
-void register_tests(void)
+typedef struct {
+    ASN1_STRING *invalidDirString;
+} INVALIDTEMPLATE;
+
+ASN1_SEQUENCE(INVALIDTEMPLATE) = {
+    /*
+     * DirectoryString is a CHOICE type so it must use explicit tagging -
+     * but we deliberately use implicit here, which makes this template invalid.
+     */
+    ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
+} static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
+
+IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
+IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
+
+static int test_invalid_template(void)
+{
+    INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new();
+    int ret;
+
+    if (!TEST_ptr(temp))
+        return 0;
+
+    ret = i2d_INVALIDTEMPLATE(temp, NULL);
+
+    INVALIDTEMPLATE_free(temp);
+
+    /* We expect the i2d operation to fail */
+    return ret < 0;
+}
+
+
+int setup_tests(void)
 {
-#if OPENSSL_API_COMPAT < 0x10200000L
+#ifndef OPENSSL_NO_DEPRECATED_3_0
     ADD_TEST(test_long_32bit);
     ADD_TEST(test_long_64bit);
 #endif
@@ -835,4 +903,6 @@ void register_tests(void)
     ADD_TEST(test_uint32);
     ADD_TEST(test_int64);
     ADD_TEST(test_uint64);
+    ADD_TEST(test_invalid_template);
+    return 1;
 }