]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/init.c
Add tracing capability in test utilities
[thirdparty/openssl.git] / test / testutil / init.c
1 /*
2 * Copyright 2017 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 <string.h>
11 #include <openssl/opensslconf.h>
12 #include <openssl/trace.h>
13 #include "apps.h"
14 #include "../testutil.h"
15
16 #ifndef OPENSSL_NO_TRACE
17 typedef struct tracedata_st {
18 BIO *bio;
19 unsigned int ingroup:1;
20 } tracedata;
21
22 static size_t internal_trace_cb(const char *buf, size_t cnt,
23 int category, int cmd, void *vdata)
24 {
25 int ret = 0;
26 tracedata *trace_data = vdata;
27 union {
28 CRYPTO_THREAD_ID tid;
29 unsigned long ltid;
30 } tid;
31 char buffer[256];
32
33 switch (cmd) {
34 case OSSL_TRACE_CTRL_BEGIN:
35 trace_data->ingroup = 1;
36
37 tid.ltid = 0;
38 tid.tid = CRYPTO_THREAD_get_current_id();
39
40 BIO_snprintf(buffer, sizeof(buffer), "TRACE[%lx]:%s: ", tid.ltid,
41 OSSL_trace_get_category_name(category));
42 BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX,
43 strlen(buffer), buffer);
44 break;
45 case OSSL_TRACE_CTRL_WRITE:
46 ret = BIO_write(trace_data->bio, buf, cnt);
47 break;
48 case OSSL_TRACE_CTRL_END:
49 trace_data->ingroup = 0;
50
51 BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL);
52 break;
53 }
54
55 return ret < 0 ? 0 : ret;
56 }
57
58 DEFINE_STACK_OF(tracedata)
59 static STACK_OF(tracedata) *trace_data_stack;
60
61 static void tracedata_free(tracedata *data)
62 {
63 BIO_free_all(data->bio);
64 OPENSSL_free(data);
65 }
66
67 static STACK_OF(tracedata) *trace_data_stack;
68
69 static void cleanup_trace(void)
70 {
71 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
72 }
73
74 static void setup_trace_category(int category)
75 {
76 BIO *channel;
77 tracedata *trace_data;
78
79 if (OSSL_trace_enabled(category))
80 return;
81
82 channel = BIO_push(BIO_new(apps_bf_prefix()),
83 BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
84 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
85
86 if (trace_data == NULL
87 || (trace_data->bio = channel) == NULL
88 || OSSL_trace_set_callback(category, internal_trace_cb,
89 trace_data) == 0
90 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
91
92 fprintf(stderr,
93 "warning: unable to setup trace callback for category '%s'.\n",
94 OSSL_trace_get_category_name(category));
95
96 OSSL_trace_set_callback(category, NULL, NULL);
97 BIO_free_all(channel);
98 }
99 }
100
101 static void setup_trace(const char *str)
102 {
103 char *val;
104
105 trace_data_stack = sk_tracedata_new_null();
106 val = OPENSSL_strdup(str);
107
108 if (val != NULL) {
109 char *valp = val;
110 char *item;
111
112 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
113 int category = OSSL_trace_get_category_num(item);
114
115 if (category == OSSL_TRACE_CATEGORY_ALL) {
116 while (++category < OSSL_TRACE_CATEGORY_NUM)
117 setup_trace_category(category);
118 break;
119 } else if (category > 0) {
120 setup_trace_category(category);
121 } else {
122 fprintf(stderr,
123 "warning: unknown trace category: '%s'.\n", item);
124 }
125 }
126 }
127
128 OPENSSL_free(val);
129 atexit(cleanup_trace);
130 }
131 #endif /* OPENSSL_NO_TRACE */
132
133 int global_init(void)
134 {
135 #ifndef OPENSSL_NO_TRACE
136 setup_trace(getenv("OPENSSL_TRACE"));
137 #endif
138
139 return 1;
140 }