summaryrefslogtreecommitdiff
path: root/lib/util/tests/test_json_logging.c
blob: 42e6206a04144781df571c7e305a30d1fd0e0711 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * cmocka unit tests for the DEBUGJSON and DEBUGJSONC debug macros
 *
 *  Copyright (C) Gary Lockyer 2026 <gary@catalyst.net.nz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * from cmocka.c:
 * These headers or their equivalents should be included prior to
 * including
 * this header file.
 *
 * #include <stdarg.h>
 * #include <stddef.h>
 * #include <setjmp.h>
 *
 * This allows test applications to use custom definitions of C standard
 * library functions and types.
 *
 */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include "lib/util/debug.h"
#include <talloc.h>
#include <string.h>

#define MAX_CALLS 4

/*
 * This needs to be the same as the value set in lib/util/debug.c
 */
#define FORMAT_BUFR_SIZE 4096

#define FORMAT_STRING_SIZE (FORMAT_BUFR_SIZE - 1)

/*
 * Test context
 */
struct test_ctx {
	size_t calls;  /* number of times callback fn called */
	char **data;   /* data passed in each call           */
};

/*
* debug logging call back function.
*
* NOTE: The debug code calling this function debug.c:debug_callback_log
*       will replace a trailing '\n' with a '\0'
*/
static void debug_callback(void *state, int level, const char *msg) {
	struct test_ctx *test_ctx = talloc_get_type_abort(state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	assert_int_not_equal(MAX_CALLS, test_ctx->calls);
	test_ctx->data[test_ctx->calls] = talloc_strdup(test_ctx, msg);
	assert_non_null(test_ctx->data[test_ctx->calls]);

	test_ctx->calls++;
}

/*
* Test set-up.
*
* creates and initializes the test context
*/
static int setup(void **state)
{
	struct test_ctx *test_ctx;

	test_ctx = talloc_zero(NULL, struct test_ctx);
	assert_non_null(test_ctx);

	test_ctx->data = talloc_array(test_ctx, char *, MAX_CALLS);

	debug_set_callback(test_ctx, debug_callback);
	debuglevel_set_class(DBGC_ALL, DBGLVL_NOTICE);
	debuglevel_set_class(DBGC_DSDB_AUDIT_JSON, DBGLVL_INFO);

	*state = test_ctx;
	return 0;
}

/*
* Test clean up
*
* deallocate any memory used by the test context
*
*/
static int teardown(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);

	talloc_free(test_ctx);
	return 0;
}
/*
* Test DEBUGJSONC with an empty message
*/
static void test_empty_message(void **state)
{
	const char *message = "";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT_JSON, DBGLVL_INFO, (message));
	assert_int_equal(1, test_ctx->calls);
	assert_string_equal("", test_ctx->data[0]);
}

/*
* Test DEBUGJSONC with a short message
*/
static void test_short_message(void **state)
{
	const char *message = "A message";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT_JSON, DBGLVL_INFO, (message));
	assert_int_equal(2, test_ctx->calls);
	assert_string_equal(message, test_ctx->data[0]);
	assert_string_equal("", test_ctx->data[1]);
}

/*
* Test DEBUGJSONC honours the debug class levels
*/
static void test_log_class(void **state)
{
	const char *message = "A message";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT, DBGLVL_INFO, (message));
	assert_int_equal(0, test_ctx->calls);
}

/*
* Test DEBUGJSONC honours its debug level
*/
static void test_log_level(void **state)
{
	const char *message = "A message";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT_JSON, DBGLVL_DEBUG, (message));
	assert_int_equal(0, test_ctx->calls);
}
/*
* Test DEBUGJSONC with a message larger than the buffer.
*/
static void test_large_message(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	char *message = talloc_zero_size(test_ctx, 5112);
	memset(message, 'x', 5111);

	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT_JSON, DBGLVL_INFO, (message));
	assert_int_equal(3, test_ctx->calls);
	assert_int_equal(4095, strlen(test_ctx->data[0]));
	assert_int_equal(1016, strlen(test_ctx->data[1]));
	assert_int_equal(5111, (4095+1016));
	assert_string_equal("", test_ctx->data[2]);
}

/*
* Test DEBUGJSONC with a message equal to the buffer size.
*/
static void test_buffer_size_message(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	char *message = talloc_zero_size(test_ctx, FORMAT_BUFR_SIZE);
	memset(message, 'x', FORMAT_STRING_SIZE);

	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT_JSON, DBGLVL_INFO, (message));
	assert_int_equal(2, test_ctx->calls);
	assert_int_equal(FORMAT_STRING_SIZE, strlen(test_ctx->data[0]));
	assert_string_equal("", test_ctx->data[1]);
}

/*
* Test DEBUGJSONC replaces '\n' with spaces
*/
static void test_embedded_new_lines(void **state)
{
	const char *message = "A \nmessage\n with new lines\n";
	const char *expected_message = "A  message  with new lines ";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSONC(DBGC_DSDB_AUDIT_JSON, DBGLVL_INFO, (message));
	assert_int_equal(2, test_ctx->calls);
	assert_string_equal(expected_message, test_ctx->data[0]);
	assert_string_equal("", test_ctx->data[1]);
}

/*
* Test DEBUGJSON with a short message
*/
static void test_debugjson(void **state)
{
	const char *message = "A message";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSON(DBGLVL_NOTICE, (message));
	assert_int_equal(2, test_ctx->calls);
	assert_string_equal(message, test_ctx->data[0]);
	assert_string_equal("", test_ctx->data[1]);
}

/*
* Test DEBUGJSON honours the DBGC_ALL log level
*/
static void test_debugjson_log_level(void **state)
{
	const char *message = "A message";
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);
	assert_non_null(test_ctx);

	DEBUGJSON(DBGLVL_INFO, (message));
	assert_int_equal(0, test_ctx->calls);
}

int main(int argc, const char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(
			test_empty_message, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_short_message, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_large_message, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_buffer_size_message, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_embedded_new_lines, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_log_class, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_log_level, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_debugjson, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_debugjson_log_level, setup, teardown),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);

	return cmocka_run_group_tests(tests, NULL, NULL);

}