summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Fiala <daniel@openssl.org>2022-08-05 04:44:51 +0200
committerTomas Mraz <tomas@openssl.org>2022-09-16 08:31:56 +0200
commitfcff5bd43c85418cc4aa8052e3dc3dba344d763e (patch)
tree58094e308b5f5009f0607c138586b6c3e0681480
parent181167b6d0e5cd896847f7538adf28878b81b0b2 (diff)
Add tests for trace_api.
Fixes openssl#17422 Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/19096)
-rw-r--r--crypto/trace.c14
-rw-r--r--test/build.info5
-rw-r--r--test/recipes/90-test_traceapi.t12
-rw-r--r--test/trace_api_test.c135
4 files changed, 166 insertions, 0 deletions
diff --git a/crypto/trace.c b/crypto/trace.c
index 09a08a6512..0d58932825 100644
--- a/crypto/trace.c
+++ b/crypto/trace.c
@@ -146,9 +146,19 @@ const char *OSSL_trace_get_category_name(int num)
{
size_t i;
+ /*
+ * Partial check that OSSL_TRACE_CATEGORY_... macros
+ * are synced with trace_categories array
+ */
+#ifndef OPENSSL_NO_ENGINE
+ if (!ossl_assert(OSSL_TRACE_CATEGORY_NUM == OSSL_NELEM(trace_categories)))
+ return NULL;
+#endif
+
for (i = 0; i < OSSL_NELEM(trace_categories); i++)
if (trace_categories[i].num == num)
return trace_categories[i].name;
+
return NULL; /* not found */
}
@@ -156,9 +166,13 @@ int OSSL_trace_get_category_num(const char *name)
{
size_t i;
+ if (name == NULL)
+ return -1;
+
for (i = 0; i < OSSL_NELEM(trace_categories); i++)
if (OPENSSL_strcasecmp(name, trace_categories[i].name) == 0)
return trace_categories[i].num;
+
return -1; /* not found */
}
diff --git a/test/build.info b/test/build.info
index bab28035a8..27117a71a3 100644
--- a/test/build.info
+++ b/test/build.info
@@ -926,6 +926,11 @@ IF[{- !$disabled{tests} -}]
INCLUDE[hexstr_test]=.. ../include ../apps/include
DEPEND[hexstr_test]=../libcrypto.a libtestutil.a
+ PROGRAMS{noinst}=trace_api_test
+ SOURCE[trace_api_test]=trace_api_test.c
+ INCLUDE[trace_api_test]=.. ../include ../apps/include
+ DEPEND[trace_api_test]=../libcrypto.a libtestutil.a
+
PROGRAMS{noinst}=endecode_test
SOURCE[endecode_test]=endecode_test.c helpers/predefined_dhparams.c
INCLUDE[endecode_test]=.. ../include ../apps/include
diff --git a/test/recipes/90-test_traceapi.t b/test/recipes/90-test_traceapi.t
new file mode 100644
index 0000000000..f2c64795d3
--- /dev/null
+++ b/test/recipes/90-test_traceapi.t
@@ -0,0 +1,12 @@
+#! /usr/bin/env perl
+# Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+#
+# 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
+
+
+use OpenSSL::Test::Simple;
+
+simple_test("test_traceapi", "trace_api_test");
diff --git a/test/trace_api_test.c b/test/trace_api_test.c
new file mode 100644
index 0000000000..32a8ad94a3
--- /dev/null
+++ b/test/trace_api_test.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * 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
+ */
+
+#include <openssl/trace.h>
+
+#include "testutil.h"
+
+static int test_trace_categories(void)
+{
+ int cat_num;
+
+ for (cat_num = -1; cat_num <= OSSL_TRACE_CATEGORY_NUM + 1; ++cat_num) {
+ const char *cat_name = OSSL_trace_get_category_name(cat_num);
+ int is_cat_name_eq = 0;
+ int ret_cat_num;
+ int expected_ret;
+
+ switch (cat_num) {
+#define CASE(name) \
+ case OSSL_TRACE_CATEGORY_##name: \
+ is_cat_name_eq = TEST_str_eq(cat_name, #name); \
+ break
+
+ CASE(ALL);
+ CASE(TRACE);
+ CASE(INIT);
+ CASE(TLS);
+ CASE(TLS_CIPHER);
+ CASE(CONF);
+#ifndef OPENSSL_NO_ENGINE
+ CASE(ENGINE_TABLE);
+ CASE(ENGINE_REF_COUNT);
+#endif
+ CASE(PKCS5V2);
+ CASE(PKCS12_KEYGEN);
+ CASE(PKCS12_DECRYPT);
+ CASE(X509V3_POLICY);
+ CASE(BN_CTX);
+ CASE(CMP);
+ CASE(STORE);
+ CASE(DECODER);
+ CASE(ENCODER);
+ CASE(REF_COUNT);
+ CASE(HTTP);
+#undef CASE
+ default:
+ is_cat_name_eq = TEST_ptr_null(cat_name);
+ break;
+ }
+
+ if (!TEST_true(is_cat_name_eq))
+ return 0;
+ ret_cat_num =
+ OSSL_trace_get_category_num(cat_name);
+ expected_ret = cat_name != NULL ? cat_num : -1;
+ if (!TEST_int_eq(expected_ret, ret_cat_num))
+ return 0;
+ }
+
+ return 1;
+}
+
+#ifndef OPENSSL_NO_TRACE
+static void put_trace_output()
+{
+ OSSL_TRACE_BEGIN(TLS) {
+ BIO_printf(trc_out, "Hello World\n");
+ BIO_printf(trc_out, "Good Bye Universe\n");
+ } OSSL_TRACE_END(TLS);
+}
+
+static int test_trace_channel()
+{
+ static const char expected[] = "xyz-\nHello World\nGood Bye Universe\n-abc\n";
+ static const char expected_len = sizeof(expected) - 1;
+ BIO *bio = NULL;
+ char *p_buf = NULL;
+ long len = 0;
+ int ret = 0;
+
+ bio = BIO_new(BIO_s_mem());
+ if (!TEST_ptr(bio))
+ goto end;
+
+ if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_TLS, bio), 1))
+ goto end;
+
+ if (!TEST_true(OSSL_trace_enabled(OSSL_TRACE_CATEGORY_TLS)))
+ goto end;
+
+ if (!TEST_int_eq(OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_TLS, "xyz-"), 1))
+ goto end;
+ if (!TEST_int_eq(OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_TLS, "-abc"), 1))
+ goto end;
+
+ put_trace_output();
+ len = BIO_get_mem_data(bio, &p_buf);
+ if (!TEST_strn2_eq(p_buf, len, expected, expected_len))
+ goto end;
+ if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_TLS, NULL), 1))
+ goto end;
+ bio = NULL;
+
+ ret = 1;
+end:
+ BIO_free(bio);
+ return ret;
+}
+#endif
+
+OPT_TEST_DECLARE_USAGE("\n")
+
+int setup_tests(void)
+{
+ if (!test_skip_common_options()) {
+ TEST_error("Error parsing test options\n");
+ return 0;
+ }
+
+ ADD_TEST(test_trace_categories);
+#ifndef OPENSSL_NO_TRACE
+ ADD_TEST(test_trace_channel);
+#endif
+ return 1;
+}
+
+void cleanup_tests(void)
+{
+}