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