From a50f24c854845da3021ff5b0b70f9a3cd25ae6ce Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sat, 2 Oct 2021 10:30:34 -0400 Subject: [PATCH] Test that built-in ASN1_STRING_TABLEs are sorted. There's a test in the file under ifdef, but that is not wired up into the build. Change-Id: Iec09277c7ce948c33303d12c325207de2188d908 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/49766 Reviewed-by: Adam Langley --- crypto/asn1/a_strnid.c | 37 ++++++++----------------------------- crypto/asn1/asn1_test.cc | 10 ++++++++++ crypto/asn1/internal.h | 5 +++++ 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/crypto/asn1/a_strnid.c b/crypto/asn1/a_strnid.c index f7ad08448..299d03b3f 100644 --- a/crypto/asn1/a_strnid.c +++ b/crypto/asn1/a_strnid.c @@ -64,6 +64,10 @@ #include #include +#include "../internal.h" +#include "internal.h" + + DEFINE_STACK_OF(ASN1_STRING_TABLE) static STACK_OF(ASN1_STRING_TABLE) *stable = NULL; @@ -244,33 +248,8 @@ static void st_free(ASN1_STRING_TABLE *tbl) OPENSSL_free(tbl); } -#ifdef STRING_TABLE_TEST - -int main(void) -{ - ASN1_STRING_TABLE *tmp; - int i, last_nid = -1; - - for (tmp = tbl_standard, i = 0; - i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) { - if (tmp->nid < last_nid) { - last_nid = 0; - break; - } - last_nid = tmp->nid; - } - - if (last_nid != 0) { - printf("Table order OK\n"); - exit(0); - } - - for (tmp = tbl_standard, i = 0; - i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) - printf("Index %d, NID %d, Name=%s\n", i, tmp->nid, - OBJ_nid2ln(tmp->nid)); - - return 0; +void asn1_get_string_table_for_testing(const ASN1_STRING_TABLE **out_ptr, + size_t *out_len) { + *out_ptr = tbl_standard; + *out_len = OPENSSL_ARRAY_SIZE(tbl_standard); } - -#endif diff --git a/crypto/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc index 9119deab2..49a0c27b6 100644 --- a/crypto/asn1/asn1_test.cc +++ b/crypto/asn1/asn1_test.cc @@ -30,6 +30,7 @@ #include #include "../test/test_util.h" +#include "internal.h" // kTag128 is an ASN.1 structure with a universal tag with number 128. @@ -1119,6 +1120,15 @@ TEST(ASN1Test, InvalidMSTRING) { EXPECT_EQ(-1, i2d_DIRECTORYSTRING(obj.get(), nullptr)); } +TEST(ASN1Test, StringTableSorted) { + const ASN1_STRING_TABLE *table; + size_t table_len; + asn1_get_string_table_for_testing(&table, &table_len); + for (size_t i = 1; i < table_len; i++) { + EXPECT_LT(table[i-1].nid, table[i].nid); + } +} + // The ASN.1 macros do not work on Windows shared library builds, where usage of // |OPENSSL_EXPORT| is a bit stricter. #if !defined(OPENSSL_WINDOWS) || !defined(BORINGSSL_SHARED_LIBRARY) diff --git a/crypto/asn1/internal.h b/crypto/asn1/internal.h index a4bd34e5b..cab0c7735 100644 --- a/crypto/asn1/internal.h +++ b/crypto/asn1/internal.h @@ -175,6 +175,11 @@ const void *asn1_type_value_as_pointer(const ASN1_TYPE *a); * ASN.1 PrintableString, and zero otherwise. */ int asn1_is_printable(uint32_t value); +/* asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table + * of built-in |ASN1_STRING_TABLE| values. It is exported for testing. */ +OPENSSL_EXPORT void asn1_get_string_table_for_testing( + const ASN1_STRING_TABLE **out_ptr, size_t *out_len); + #if defined(__cplusplus) } /* extern C */