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