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 <agl@google.com>
diff --git a/crypto/asn1/a_strnid.c b/crypto/asn1/a_strnid.c
index f7ad084..299d03b 100644
--- a/crypto/asn1/a_strnid.c
+++ b/crypto/asn1/a_strnid.c
@@ -64,6 +64,10 @@
 #include <openssl/obj.h>
 #include <openssl/stack.h>
 
+#include "../internal.h"
+#include "internal.h"
+
+
 DEFINE_STACK_OF(ASN1_STRING_TABLE)
 
 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
@@ -244,33 +248,8 @@
         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 9119dea..49a0c27 100644
--- a/crypto/asn1/asn1_test.cc
+++ b/crypto/asn1/asn1_test.cc
@@ -30,6 +30,7 @@
 #include <openssl/x509v3.h>
 
 #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 @@
   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 a4bd34e..cab0c77 100644
--- a/crypto/asn1/internal.h
+++ b/crypto/asn1/internal.h
@@ -175,6 +175,11 @@
  * 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 */