Add test cases and docs for ASN1_STRING_TABLE_* functions
authorPaul Yang <yang.yang@baishancloud.com>
Wed, 26 Jul 2017 17:18:50 +0000 (01:18 +0800)
committerAndy Polyakov <appro@openssl.org>
Wed, 26 Jul 2017 18:06:51 +0000 (20:06 +0200)
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Andy Polyakov <appro@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3998)

doc/man3/ASN1_STRING_TABLE_add.pod [new file with mode: 0644]
test/asn1_string_table_test.c [new file with mode: 0644]
test/build.info
test/recipes/04-test_asn1_string_table.t [new file with mode: 0644]
util/private.num

diff --git a/doc/man3/ASN1_STRING_TABLE_add.pod b/doc/man3/ASN1_STRING_TABLE_add.pod
new file mode 100644 (file)
index 0000000..e1786bf
--- /dev/null
@@ -0,0 +1,65 @@
+=pod
+
+=head1 NAME
+
+ASN1_STRING_TABLE, ASN1_STRING_TABLE_add, ASN1_STRING_TABLE_get,
+ASN1_STRING_TABLE_cleanup - ASN1_STRING_TABLE manipulation functions
+
+=head1 SYNOPSIS
+
+ #include <openssl/asn1.h>
+
+ typedef struct asn1_string_table_st ASN1_STRING_TABLE;
+
+ int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
+                           unsigned long mask, unsigned long flags);
+ ASN1_STRING_TABLE * ASN1_STRING_TABLE_get(int nid);
+ void ASN1_STRING_TABLE_cleanup(void);
+
+=head1 DESCRIPTION
+
+=head2 Types
+
+B<ASN1_STRING_TABLE> is a table which holds string information
+(basically minimum size, maximum size, type and etc) for a NID object.
+
+=head2 Functions
+
+ASN1_STRING_TABLE_add() adds a new B<ASN1_STRING_TABLE> item into the
+local ASN1 string table based on the B<nid> along with other parameters.
+
+If the item is already in the table, fields of B<ASN1_STRING_TABLE> are
+updated (depending on the values of those parameters, e.g., B<minsize>
+and B<maxsize> >= 0, B<mask> and B<flags> != 0). If the B<nid> is standard,
+a copy of the standard B<ASN1_STRING_TABLE> is created and updated with
+other parameters.
+
+ASN1_STRING_TABLE_get() searches for an B<ASN1_STRING_TABLE> item based
+on B<nid>. It will search the local table first, then the standard one.
+
+ASN1_STRING_TABLE_cleanup() frees all B<ASN1_STRING_TABLE> items added
+by ASN1_STRING_TABLE_add().
+
+=head1 RETURN VALUES
+
+ASN1_STRING_TABLE_add() returns 1 on success, 0 if an error occurred.
+
+ASN1_STRING_TABLE_get() returns a valid B<ASN1_STRING_TABLE> structure
+or B<NULL> if nothing is found.
+
+ASN1_STRING_TABLE_cleanup() does not return a value.
+
+=head1 SEE ALSO
+
+L<ERR_get_error(3)>
+
+=head1 COPYRIGHT
+
+Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+
+Licensed under the OpenSSL license (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
+L<https://www.openssl.org/source/license.html>.
+
+=cut
diff --git a/test/asn1_string_table_test.c b/test/asn1_string_table_test.c
new file mode 100644 (file)
index 0000000..7e542b9
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (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
+ */
+
+/* Tests for the ANS1_STRING_TABLE_* functions */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <openssl/asn1.h>
+#include "testutil.h"
+
+static int test_string_tbl()
+{
+    const ASN1_STRING_TABLE *tmp = NULL;
+    int nid = 12345678, nid2 = 87654321, rv = 0, ret = 0;
+
+    tmp = ASN1_STRING_TABLE_get(nid);
+    if (!TEST_ptr_null(tmp)) {
+        TEST_info("asn1 string table: ASN1_STRING_TABLE_get non-exist nid");
+        goto out;
+    }
+
+    ret = ASN1_STRING_TABLE_add(nid, -1, -1, MBSTRING_ASC, 0);
+    if (!TEST_true(ret)) {
+        TEST_info("asn1 string table: add NID(%d) failed", nid);
+        goto out;
+    }
+
+    ret = ASN1_STRING_TABLE_add(nid2, -1, -1, MBSTRING_ASC, 0);
+    if (!TEST_true(ret)) {
+        TEST_info("asn1 string table: add NID(%d) failed", nid2);
+        goto out;
+    }
+
+    tmp = ASN1_STRING_TABLE_get(nid);
+    if (!TEST_ptr(tmp)) {
+        TEST_info("asn1 string table: get NID(%d) failed", nid);
+        goto out;
+    }
+
+    tmp = ASN1_STRING_TABLE_get(nid2);
+    if (!TEST_ptr(tmp)) {
+        TEST_info("asn1 string table: get NID(%d) failed", nid2);
+        goto out;
+    }
+
+    ASN1_STRING_TABLE_cleanup();
+
+    /* check if all newly added NIDs are cleaned up */
+    tmp = ASN1_STRING_TABLE_get(nid);
+    if (!TEST_ptr_null(tmp)) {
+        TEST_info("asn1 string table: get NID(%d) failed", nid);
+        goto out;
+    }
+
+    tmp = ASN1_STRING_TABLE_get(nid2);
+    if (!TEST_ptr_null(tmp)) {
+        TEST_info("asn1 string table: get NID(%d) failed", nid2);
+        goto out;
+    }
+
+    rv = 1;
+ out:
+    return rv;
+}
+
+void register_tests(void)
+{
+    ADD_TEST(test_string_tbl);
+}
index a92ff18..7b8f654 100644 (file)
@@ -41,7 +41,7 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN
           dtlsv1listentest ct_test threadstest afalgtest d2i_test \
           ssl_test_ctx_test ssl_test x509aux cipherlist_test asynciotest \
           bioprinttest sslapitest dtlstest sslcorrupttest bio_enc_test \
-          pkey_meth_test uitest cipherbytes_test asn1_encode_test \
+          pkey_meth_test uitest cipherbytes_test asn1_encode_test asn1_string_table_test \
           x509_time_test x509_dup_cert_test x509_check_cert_pkey_test \
           recordlentest drbgtest sslbuffertest \
           time_offset_test pemtest ssl_cert_table_internal_test ciphername_test
@@ -361,6 +361,10 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN
   INCLUDE[asn1_encode_test]=../include
   DEPEND[asn1_encode_test]=../libcrypto libtestutil.a
 
+  SOURCE[asn1_string_table_test]=asn1_string_table_test.c
+  INCLUDE[asn1_string_table_test]=../include
+  DEPEND[asn1_string_table_test]=../libcrypto libtestutil.a
+
   SOURCE[time_offset_test]=time_offset_test.c
   INCLUDE[time_offset_test]=.. ../include
   DEPEND[time_offset_test]=../libcrypto libtestutil.a
diff --git a/test/recipes/04-test_asn1_string_table.t b/test/recipes/04-test_asn1_string_table.t
new file mode 100644 (file)
index 0000000..041f372
--- /dev/null
@@ -0,0 +1,12 @@
+#! /usr/bin/env perl
+# Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the OpenSSL license (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
+
+
+use OpenSSL::Test::Simple;
+
+simple_test("test_asn1_string_table", "asn1_string_table_test");
index 203b531..ff45e56 100644 (file)
@@ -8,6 +8,7 @@ OPENSSL_MALLOC_FAILURES                 environment
 OPENSSL_instrument_bus                  assembler
 OPENSSL_instrument_bus2                 assembler
 #
+ASN1_STRING_TABLE                       datatype
 BIO_ADDR                                datatype
 BIO_ADDRINFO                            datatype
 BIO_callback_fn                         datatype