xref: /freebsd/crypto/openssl/test/trace_api_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <openssl/trace.h>
11*e0c4386eSCy Schubert 
12*e0c4386eSCy Schubert #include "testutil.h"
13*e0c4386eSCy Schubert 
test_trace_categories(void)14*e0c4386eSCy Schubert static int test_trace_categories(void)
15*e0c4386eSCy Schubert {
16*e0c4386eSCy Schubert     int cat_num;
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert     for (cat_num = -1; cat_num <= OSSL_TRACE_CATEGORY_NUM + 1; ++cat_num) {
19*e0c4386eSCy Schubert         const char *cat_name = OSSL_trace_get_category_name(cat_num);
20*e0c4386eSCy Schubert         int is_cat_name_eq = 0;
21*e0c4386eSCy Schubert         int ret_cat_num;
22*e0c4386eSCy Schubert         int expected_ret;
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert         switch (cat_num) {
25*e0c4386eSCy Schubert #define CASE(name) \
26*e0c4386eSCy Schubert         case OSSL_TRACE_CATEGORY_##name: \
27*e0c4386eSCy Schubert             is_cat_name_eq = TEST_str_eq(cat_name, #name); \
28*e0c4386eSCy Schubert             break
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert         CASE(ALL);
31*e0c4386eSCy Schubert         CASE(TRACE);
32*e0c4386eSCy Schubert         CASE(INIT);
33*e0c4386eSCy Schubert         CASE(TLS);
34*e0c4386eSCy Schubert         CASE(TLS_CIPHER);
35*e0c4386eSCy Schubert         CASE(CONF);
36*e0c4386eSCy Schubert         CASE(ENGINE_TABLE);
37*e0c4386eSCy Schubert         CASE(ENGINE_REF_COUNT);
38*e0c4386eSCy Schubert         CASE(PKCS5V2);
39*e0c4386eSCy Schubert         CASE(PKCS12_KEYGEN);
40*e0c4386eSCy Schubert         CASE(PKCS12_DECRYPT);
41*e0c4386eSCy Schubert         CASE(X509V3_POLICY);
42*e0c4386eSCy Schubert         CASE(BN_CTX);
43*e0c4386eSCy Schubert         CASE(CMP);
44*e0c4386eSCy Schubert         CASE(STORE);
45*e0c4386eSCy Schubert         CASE(DECODER);
46*e0c4386eSCy Schubert         CASE(ENCODER);
47*e0c4386eSCy Schubert         CASE(REF_COUNT);
48*e0c4386eSCy Schubert #undef CASE
49*e0c4386eSCy Schubert         default:
50*e0c4386eSCy Schubert             is_cat_name_eq = TEST_ptr_null(cat_name);
51*e0c4386eSCy Schubert             break;
52*e0c4386eSCy Schubert         }
53*e0c4386eSCy Schubert 
54*e0c4386eSCy Schubert         if (!TEST_true(is_cat_name_eq))
55*e0c4386eSCy Schubert             return 0;
56*e0c4386eSCy Schubert         ret_cat_num =
57*e0c4386eSCy Schubert             OSSL_trace_get_category_num(cat_name);
58*e0c4386eSCy Schubert         expected_ret = cat_name != NULL ? cat_num : -1;
59*e0c4386eSCy Schubert         if (!TEST_int_eq(expected_ret, ret_cat_num))
60*e0c4386eSCy Schubert             return 0;
61*e0c4386eSCy Schubert     }
62*e0c4386eSCy Schubert 
63*e0c4386eSCy Schubert     return 1;
64*e0c4386eSCy Schubert }
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TRACE
put_trace_output(void)67*e0c4386eSCy Schubert static void put_trace_output(void)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert     OSSL_TRACE_BEGIN(REF_COUNT) {
70*e0c4386eSCy Schubert         BIO_printf(trc_out, "Hello World\n");
71*e0c4386eSCy Schubert         BIO_printf(trc_out, "Good Bye Universe\n");
72*e0c4386eSCy Schubert     } OSSL_TRACE_END(REF_COUNT);
73*e0c4386eSCy Schubert }
74*e0c4386eSCy Schubert 
test_trace_channel(void)75*e0c4386eSCy Schubert static int test_trace_channel(void)
76*e0c4386eSCy Schubert {
77*e0c4386eSCy Schubert     static const char expected[] = "xyz-\nHello World\nGood Bye Universe\n-abc\n";
78*e0c4386eSCy Schubert     static const char expected_len = sizeof(expected) - 1;
79*e0c4386eSCy Schubert     BIO *bio = NULL;
80*e0c4386eSCy Schubert     char *p_buf = NULL;
81*e0c4386eSCy Schubert     long len = 0;
82*e0c4386eSCy Schubert     int ret = 0;
83*e0c4386eSCy Schubert 
84*e0c4386eSCy Schubert     bio = BIO_new(BIO_s_mem());
85*e0c4386eSCy Schubert     if (!TEST_ptr(bio))
86*e0c4386eSCy Schubert         goto end;
87*e0c4386eSCy Schubert 
88*e0c4386eSCy Schubert     if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_REF_COUNT, bio), 1))
89*e0c4386eSCy Schubert         goto end;
90*e0c4386eSCy Schubert 
91*e0c4386eSCy Schubert     if (!TEST_true(OSSL_trace_enabled(OSSL_TRACE_CATEGORY_REF_COUNT)))
92*e0c4386eSCy Schubert         goto end;
93*e0c4386eSCy Schubert 
94*e0c4386eSCy Schubert     if (!TEST_int_eq(OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_REF_COUNT, "xyz-"), 1))
95*e0c4386eSCy Schubert         goto end;
96*e0c4386eSCy Schubert     if (!TEST_int_eq(OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_REF_COUNT, "-abc"), 1))
97*e0c4386eSCy Schubert         goto end;
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert     put_trace_output();
100*e0c4386eSCy Schubert     len = BIO_get_mem_data(bio, &p_buf);
101*e0c4386eSCy Schubert     if (!TEST_strn2_eq(p_buf, len, expected, expected_len))
102*e0c4386eSCy Schubert         goto end;
103*e0c4386eSCy Schubert     if (!TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_REF_COUNT, NULL), 1))
104*e0c4386eSCy Schubert         goto end;
105*e0c4386eSCy Schubert     bio = NULL;
106*e0c4386eSCy Schubert 
107*e0c4386eSCy Schubert     ret = 1;
108*e0c4386eSCy Schubert  end:
109*e0c4386eSCy Schubert     BIO_free(bio);
110*e0c4386eSCy Schubert     return ret;
111*e0c4386eSCy Schubert }
112*e0c4386eSCy Schubert 
113*e0c4386eSCy Schubert static int trace_cb_failure;
114*e0c4386eSCy Schubert static int trace_cb_called;
115*e0c4386eSCy Schubert 
trace_cb(const char * buffer,size_t count,int category,int cmd,void * data)116*e0c4386eSCy Schubert static size_t trace_cb(const char *buffer, size_t count,
117*e0c4386eSCy Schubert                        int category, int cmd, void *data)
118*e0c4386eSCy Schubert {
119*e0c4386eSCy Schubert     trace_cb_called = 1;
120*e0c4386eSCy Schubert     if (!TEST_true(category == OSSL_TRACE_CATEGORY_TRACE))
121*e0c4386eSCy Schubert         trace_cb_failure = 1;
122*e0c4386eSCy Schubert     return count;
123*e0c4386eSCy Schubert }
124*e0c4386eSCy Schubert 
test_trace_callback(void)125*e0c4386eSCy Schubert static int test_trace_callback(void)
126*e0c4386eSCy Schubert {
127*e0c4386eSCy Schubert     int ret = 0;
128*e0c4386eSCy Schubert 
129*e0c4386eSCy Schubert     if (!TEST_true(OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_TRACE, trace_cb,
130*e0c4386eSCy Schubert                                            NULL)))
131*e0c4386eSCy Schubert         goto end;
132*e0c4386eSCy Schubert 
133*e0c4386eSCy Schubert     put_trace_output();
134*e0c4386eSCy Schubert 
135*e0c4386eSCy Schubert     if (!TEST_false(trace_cb_failure) || !TEST_true(trace_cb_called))
136*e0c4386eSCy Schubert         goto end;
137*e0c4386eSCy Schubert 
138*e0c4386eSCy Schubert     ret = 1;
139*e0c4386eSCy Schubert  end:
140*e0c4386eSCy Schubert     return ret;
141*e0c4386eSCy Schubert }
142*e0c4386eSCy Schubert #endif
143*e0c4386eSCy Schubert 
144*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("\n")
145*e0c4386eSCy Schubert 
setup_tests(void)146*e0c4386eSCy Schubert int setup_tests(void)
147*e0c4386eSCy Schubert {
148*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
149*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
150*e0c4386eSCy Schubert         return 0;
151*e0c4386eSCy Schubert     }
152*e0c4386eSCy Schubert 
153*e0c4386eSCy Schubert     ADD_TEST(test_trace_categories);
154*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TRACE
155*e0c4386eSCy Schubert     ADD_TEST(test_trace_channel);
156*e0c4386eSCy Schubert     ADD_TEST(test_trace_callback);
157*e0c4386eSCy Schubert #endif
158*e0c4386eSCy Schubert     return 1;
159*e0c4386eSCy Schubert }
160*e0c4386eSCy Schubert 
cleanup_tests(void)161*e0c4386eSCy Schubert void cleanup_tests(void)
162*e0c4386eSCy Schubert {
163*e0c4386eSCy Schubert }
164