From cf9a875b09cbb6aa6e9bcff0fafc7ee1fe7259ed Mon Sep 17 00:00:00 2001 From: tb <> Date: Mon, 5 May 2025 06:33:35 +0000 Subject: merge the x509name test into x509_name_test.c Remove the old x509name test and its Makefile rule. Its logic has been fully integrated into x509_name_test.c using a new table-driven approach. Each x509 name entry is added and validated step by step, checking both the string representation produced by X509_NAME_print_ex() and the internal RDN set structure. This makes the test easier to extend and maintain, and eliminates the need for an external .expected file or output diff. From Kenjiro Nakayama (with tiny tweaks) --- src/regress/lib/libcrypto/x509/Makefile | 10 +- src/regress/lib/libcrypto/x509/x509_name_test.c | 123 ++++++++++++++++++++++- src/regress/lib/libcrypto/x509/x509name.c | 62 ------------ src/regress/lib/libcrypto/x509/x509name.expected | 3 - 4 files changed, 125 insertions(+), 73 deletions(-) delete mode 100644 src/regress/lib/libcrypto/x509/x509name.c delete mode 100644 src/regress/lib/libcrypto/x509/x509name.expected (limited to 'src/regress/lib') diff --git a/src/regress/lib/libcrypto/x509/Makefile b/src/regress/lib/libcrypto/x509/Makefile index 19e65efddd..94e9e476a0 100644 --- a/src/regress/lib/libcrypto/x509/Makefile +++ b/src/regress/lib/libcrypto/x509/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.24 2025/03/15 06:37:49 tb Exp $ +# $OpenBSD: Makefile,v 1.25 2025/05/05 06:33:34 tb Exp $ -PROGS = constraints verify x509attribute x509name x509req_ext callback +PROGS = constraints verify x509attribute x509req_ext callback PROGS += expirecallback callbackfailures x509_asn1 x509_extensions_test PROGS += x509_name_test LDADD = -lcrypto @@ -16,7 +16,7 @@ CFLAGS += -I${.CURDIR}/../../../../lib/libcrypto/bytestring SUBDIR += bettertls policy rfc3779 -CLEANFILES += x509name.result callback.out +CLEANFILES += callback.out .if make(clean) || make(cleandir) . if ${.OBJDIR} != ${.CURDIR} @@ -29,10 +29,6 @@ run-regress-verify: verify perl ${.CURDIR}/make-dir-roots.pl ${.CURDIR}/../certs . ./verify ${.CURDIR}/../certs -run-regress-x509name: x509name - ./x509name > x509name.result - diff -u ${.CURDIR}/x509name.expected x509name.result - run-regress-callback: callback ./callback ${.CURDIR}/../certs perl ${.CURDIR}/callback.pl callback.out diff --git a/src/regress/lib/libcrypto/x509/x509_name_test.c b/src/regress/lib/libcrypto/x509/x509_name_test.c index eaf7076d74..24e62cc766 100644 --- a/src/regress/lib/libcrypto/x509/x509_name_test.c +++ b/src/regress/lib/libcrypto/x509/x509_name_test.c @@ -1,7 +1,9 @@ -/* $OpenBSD: x509_name_test.c,v 1.2 2025/03/19 11:19:17 tb Exp $ */ +/* $OpenBSD: x509_name_test.c,v 1.3 2025/05/05 06:33:34 tb Exp $ */ /* * Copyright (c) 2025 Theo Buehler + * Copyright (c) 2025 Kenjiro Nakayama + * Copyright (c) 2018 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -288,12 +290,131 @@ x509_name_compat_test(void) return failed; } +static const struct x509_name_entry_test { + const char *field; + const char *value; + int loc; + int set; + const char *expected_str; + const int expected_set[4]; + const int expected_count; +} entry_tests[] = { + { + .field = "ST", + .value = "BaWue", + .loc = -1, + .set = 0, + .expected_str = "ST=BaWue", + .expected_set = { 0 }, + .expected_count = 1, + }, + { + .field = "O", + .value = "KIT", + .loc = -1, + .set = 0, + .expected_str = "ST=BaWue, O=KIT", + .expected_set = { 0, 1 }, + .expected_count = 2, + }, + { + .field = "L", + .value = "Karlsruhe", + .loc = 1, + .set = 0, + .expected_str = "ST=BaWue, L=Karlsruhe, O=KIT", + .expected_set = { 0, 1, 2 }, + .expected_count = 3, + }, + { + .field = "C", + .value = "DE", + .loc = 0, + .set = 1, + .expected_str = "C=DE + ST=BaWue, L=Karlsruhe, O=KIT", + .expected_set = { 0, 0, 1, 2 }, + .expected_count = 4, + }, +}; + +#define N_ENTRY_TESTS (sizeof(entry_tests) / sizeof(entry_tests[0])) + +static int +verify_x509_name_output(X509_NAME *name, const struct x509_name_entry_test *tc) +{ + BIO *bio; + char *got; + long got_len; + int loc, ret; + int failed = 1; + + if ((bio = BIO_new(BIO_s_mem())) == NULL) + goto fail; + + if ((ret = X509_NAME_print_ex(bio, name, 0, XN_FLAG_SEP_CPLUS_SPC)) == -1) + goto fail; + + if ((got_len = BIO_get_mem_data(bio, &got)) < 0) + goto fail; + + if (ret != got_len || strlen(tc->expected_str) != (size_t)ret) + goto fail; + + if (strncmp(tc->expected_str, got, got_len) != 0) + goto fail; + + if (X509_NAME_entry_count(name) != tc->expected_count) + goto fail; + + for (loc = 0; loc < X509_NAME_entry_count(name); loc++) { + X509_NAME_ENTRY *e = X509_NAME_get_entry(name, loc); + if (e == NULL || X509_NAME_ENTRY_set(e) != tc->expected_set[loc]) + goto fail; + } + + failed = 0; + + fail: + BIO_free(bio); + + return failed; +} + +static int +x509_name_add_entry_test(void) +{ + X509_NAME *name; + int failed = 1; + + if ((name = X509_NAME_new()) == NULL) + goto done; + + for (size_t i = 0; i < N_ENTRY_TESTS; i++) { + const struct x509_name_entry_test *t = &entry_tests[i]; + + if (!X509_NAME_add_entry_by_txt(name, t->field, MBSTRING_ASC, + (const unsigned char *)t->value, -1, t->loc, t->set)) + goto done; + + if (verify_x509_name_output(name, t)) + goto done; + } + + failed = 0; + + done: + X509_NAME_free(name); + + return failed; +} + int main(void) { int failed = 0; failed |= x509_name_compat_test(); + failed |= x509_name_add_entry_test(); return failed; } diff --git a/src/regress/lib/libcrypto/x509/x509name.c b/src/regress/lib/libcrypto/x509/x509name.c deleted file mode 100644 index 9deeeb2986..0000000000 --- a/src/regress/lib/libcrypto/x509/x509name.c +++ /dev/null @@ -1,62 +0,0 @@ -/* $OpenBSD: x509name.c,v 1.3 2021/10/31 08:27:15 tb Exp $ */ -/* - * Copyright (c) 2018 Ingo Schwarze - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include - -#include - -static void debug_print(X509_NAME *); - -static void -debug_print(X509_NAME *name) -{ - int loc; - - for (loc = 0; loc < X509_NAME_entry_count(name); loc++) - printf("%d:", - X509_NAME_ENTRY_set(X509_NAME_get_entry(name, loc))); - putchar(' '); - X509_NAME_print_ex_fp(stdout, name, 0, XN_FLAG_SEP_CPLUS_SPC); - putchar('\n'); -} - -int -main(void) -{ - X509_NAME *name; - - if ((name = X509_NAME_new()) == NULL) - err(1, NULL); - X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_ASC, - "BaWue", -1, -1, 0); - X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, - "KIT", -1, -1, 0); - debug_print(name); - - X509_NAME_add_entry_by_txt(name, "L", MBSTRING_ASC, - "Karlsruhe", -1, 1, 0); - debug_print(name); - - X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, - "DE", -1, 0, 1); - debug_print(name); - - X509_NAME_free(name); - - return 0; -} diff --git a/src/regress/lib/libcrypto/x509/x509name.expected b/src/regress/lib/libcrypto/x509/x509name.expected deleted file mode 100644 index 6cee7cc435..0000000000 --- a/src/regress/lib/libcrypto/x509/x509name.expected +++ /dev/null @@ -1,3 +0,0 @@ -0:1: ST=BaWue, O=KIT -0:1:2: ST=BaWue, L=Karlsruhe, O=KIT -0:0:1:2: C=DE + ST=BaWue, L=Karlsruhe, O=KIT -- cgit v1.2.3-55-g6feb