summaryrefslogtreecommitdiffstats
path: root/test/trace_api_test.c
blob: 0effee421ab0b6818c220fc736b83eb2fae9c9c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright 2022-2023 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);
        const char *expected_cat_name = NULL;
        int ret_cat_num;

#define SET_EXPECTED_CAT_NAME(name) expected_cat_name = #name; break
        switch (cat_num) {
        case OSSL_TRACE_CATEGORY_ALL:
            SET_EXPECTED_CAT_NAME(ALL);
        case OSSL_TRACE_CATEGORY_TRACE:
            SET_EXPECTED_CAT_NAME(TRACE);
        case OSSL_TRACE_CATEGORY_INIT:
            SET_EXPECTED_CAT_NAME(INIT);
        case OSSL_TRACE_CATEGORY_TLS:
            SET_EXPECTED_CAT_NAME(TLS);
        case OSSL_TRACE_CATEGORY_TLS_CIPHER:
            SET_EXPECTED_CAT_NAME(TLS_CIPHER);
        case OSSL_TRACE_CATEGORY_CONF:
            SET_EXPECTED_CAT_NAME(CONF);
        case OSSL_TRACE_CATEGORY_ENGINE_TABLE:
            SET_EXPECTED_CAT_NAME(ENGINE_TABLE);
        case OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT:
            SET_EXPECTED_CAT_NAME(ENGINE_REF_COUNT);
        case OSSL_TRACE_CATEGORY_PKCS5V2:
            SET_EXPECTED_CAT_NAME(PKCS5V2);
        case OSSL_TRACE_CATEGORY_PKCS12_KEYGEN:
            SET_EXPECTED_CAT_NAME(PKCS12_KEYGEN);
        case OSSL_TRACE_CATEGORY_PKCS12_DECRYPT:
            SET_EXPECTED_CAT_NAME(PKCS12_DECRYPT);
        case OSSL_TRACE_CATEGORY_X509V3_POLICY:
            SET_EXPECTED_CAT_NAME(X509V3_POLICY);
        case OSSL_TRACE_CATEGORY_BN_CTX:
            SET_EXPECTED_CAT_NAME(BN_CTX);
        case OSSL_TRACE_CATEGORY_CMP:
            SET_EXPECTED_CAT_NAME(CMP);
        case OSSL_TRACE_CATEGORY_STORE:
            SET_EXPECTED_CAT_NAME(STORE);
        case OSSL_TRACE_CATEGORY_DECODER:
            SET_EXPECTED_CAT_NAME(DECODER);
        case OSSL_TRACE_CATEGORY_ENCODER:
            SET_EXPECTED_CAT_NAME(ENCODER);
        case OSSL_TRACE_CATEGORY_REF_COUNT:
            SET_EXPECTED_CAT_NAME(REF_COUNT);
        case OSSL_TRACE_CATEGORY_HTTP:
            SET_EXPECTED_CAT_NAME(HTTP);
        case OSSL_TRACE_CATEGORY_PROVIDER:
            SET_EXPECTED_CAT_NAME(PROVIDER);
        case OSSL_TRACE_CATEGORY_QUERY:
            SET_EXPECTED_CAT_NAME(QUERY);
        default:
            if (cat_num == -1 || cat_num >= OSSL_TRACE_CATEGORY_NUM)
                expected_cat_name = NULL;
            break;
        }
#undef SET_EXPECTED_CAT_NAME

        if (!TEST_str_eq(cat_name, expected_cat_name))
            return 0;
        ret_cat_num =
            OSSL_trace_get_category_num(cat_name);
        if (cat_num < OSSL_TRACE_CATEGORY_NUM)
            if (!TEST_int_eq(cat_num, ret_cat_num))
                return 0;
    }

    return 1;
}

#ifndef OPENSSL_NO_TRACE

# define OSSL_START "xyz-"
# define OSSL_HELLO "Hello World\n"
/* OSSL_STR80 must have length OSSL_TRACE_STRING_MAX */
# define OSSL_STR80 "1234567890123456789012345678901234567890123456789012345678901234567890123456789\n"
# define OSSL_STR81 (OSSL_STR80"x")
# define OSSL_CTRL "A\xfe\nB"
# define OSSL_MASKED "A \nB"
# define OSSL_BYE "Good Bye Universe\n"
# define OSSL_END "-abc"

# define trace_string(text, full, str) \
    OSSL_trace_string(trc_out, text, full, (unsigned char *)(str), strlen(str))

static int put_trace_output(void)
{
    int res = 1;

    OSSL_TRACE_BEGIN(HTTP) {
        res = TEST_int_eq(BIO_printf(trc_out, OSSL_HELLO), strlen(OSSL_HELLO));
        res += TEST_int_eq(trace_string(0, 0, OSSL_STR80), strlen(OSSL_STR80));
        res += TEST_int_eq(trace_string(0, 0, OSSL_STR81), strlen(OSSL_STR80));
        res += TEST_int_eq(trace_string(1, 1, OSSL_CTRL), strlen(OSSL_CTRL));
        res += TEST_int_eq(trace_string(0, 1, OSSL_MASKED), strlen(OSSL_MASKED)
                           + 1); /* newline added */
        res += TEST_int_eq(BIO_printf(trc_out, OSSL_BYE), strlen(OSSL_BYE));
        res = res == 6;
        /* not using '&&' but '+' to catch potentially multiple test failures */
    } OSSL_TRACE_END(HTTP);
    return res;
}

static int test_trace_channel(void)
{
    static const char expected[] =
        OSSL_START"\n" OSSL_HELLO
        OSSL_STR80 "[len 81 limited to 80]: "OSSL_STR80
        OSSL_CTRL OSSL_MASKED"\n" OSSL_BYE OSSL_END"\n";
    static const size_t 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_HTTP, bio), 1)) {
        BIO_free(bio);
        goto end;
    }

    if (!TEST_true(OSSL_trace_enabled(OSSL_TRACE_CATEGORY_HTTP)))
        goto end;

    if (!TEST_int_eq(OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_HTTP,
                                           OSSL_START), 1))
        goto end;
    if (!TEST_int_eq(OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_HTTP,
                                           OSSL_END), 1))
        goto end;

    ret = put_trace_output();
    len = BIO_get_mem_data(bio, &p_buf);
    if (!TEST_strn2_eq(p_buf, len, expected, expected_len))
        ret = 0;
    ret = TEST_int_eq(OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_HTTP, NULL), 1)
        && ret;

 end:
    return ret;
}

static int trace_cb_failure;
static int trace_cb_called;

static size_t trace_cb(const char *buffer, size_t count,
                       int category, int cmd, void *data)
{
    trace_cb_called = 1;
    if (!TEST_true(category == OSSL_TRACE_CATEGORY_TRACE))
        trace_cb_failure = 1;
    return count;
}

static int test_trace_callback(void)
{
    int ret = 0;

    if (!TEST_true(OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_TRACE, trace_cb,
                                           NULL)))
        goto end;

    put_trace_output();

    if (!TEST_false(trace_cb_failure) || !TEST_true(trace_cb_called))
        goto end;

    ret = 1;
 end:
    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);
    ADD_TEST(test_trace_callback);
#endif
    return 1;
}

void cleanup_tests(void)
{
}