summaryrefslogtreecommitdiff
path: root/test/basictests.c
blob: d74df8777996b257e3d2b5768ba26e9b8222521e (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
/* test/basictests.c
 *
 * Basic tests for the test suite for libnslog
 *
 * Copyright 2009,2017 The NetSurf Browser Project
 *                Daniel Silverstone <dsilvers@netsurf-browser.org>
 */

#include <check.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include "tests.h"

#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif

NSLOG_DEFINE_CATEGORY(test, "Top level test category");

static void *captured_render_context = NULL;
static nslog_entry_context_t captured_context = { 0 };
static char captured_rendered_message[4096] = { 0 };
static int captured_rendered_message_length = 0;
static int captured_message_count = 0;

static const char* anchor_context_1 = "1";

static void
nslog__test__render_function(void *_ctx, nslog_entry_context_t *ctx,
			     const char *fmt, va_list args)
{
	captured_context = *ctx;
	captured_render_context = _ctx;
	captured_rendered_message_length =
		vsnprintf(captured_rendered_message,
			  sizeof(captured_rendered_message),
			  fmt, args);
	captured_message_count++;
}

/**** The next set of tests need a fixture set ****/

static void
with_simple_context_setup(void)
{
	captured_render_context = NULL;
	memset(&captured_context, 0, sizeof(captured_context));
	memset(captured_rendered_message, 0, sizeof(captured_rendered_message));
	captured_rendered_message_length = 0;
	captured_message_count = 0;
	fail_unless(nslog_set_render_callback(
			    nslog__test__render_function,
			    (void *)anchor_context_1) == NSLOG_NO_ERROR,
		    "Unable to set up render callback");
}

static void
with_simple_context_teardown(void)
{
        /* Nothing to do to tear down */
}

START_TEST (test_nslog_trivial_corked_message)
{
	NSLOG(test, INFO, "Hello %s", "world");
	fail_unless(nslog_uncork() == NSLOG_NO_ERROR,
		    "Unable to uncork");
	fail_unless(captured_message_count == 1,
		    "Captured message count was wrong");
	fail_unless(captured_render_context == anchor_context_1,
		    "Captured context wasn't passed through");
	fail_unless(strcmp(captured_context.category->name, "test") == 0,
		    "Captured context category wasn't normalised");
	fail_unless(captured_context.category == &__nslog_category_test,
		    "Captured context category wasn't the one we wanted");
	fail_unless(captured_rendered_message_length == 11,
		    "Captured message wasn't correct length");
	fail_unless(strcmp(captured_rendered_message, "Hello world") == 0,
		    "Captured message wasn't correct");
	fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0,
		    "Captured message wasn't correct filename");
	fail_unless(strcmp(captured_context.funcname, "test_nslog_trivial_corked_message") == 0,
		    "Captured message wasn't correct function name");
}
END_TEST

START_TEST (test_nslog_trivial_uncorked_message)
{
	fail_unless(nslog_uncork() == NSLOG_NO_ERROR,
		    "Unable to uncork");
	fail_unless(captured_message_count == 0,
		    "Unusual, we had messages from before uncorking");
	NSLOG(test, INFO, "Hello %s", "world");
	fail_unless(captured_message_count == 1,
		    "Captured message count was wrong");
	fail_unless(captured_render_context == anchor_context_1,
		    "Captured context wasn't passed through");
	fail_unless(strcmp(captured_context.category->name, "test") == 0,
		    "Captured context category wasn't normalised");
	fail_unless(captured_context.category == &__nslog_category_test,
		    "Captured context category wasn't the one we wanted");
	fail_unless(captured_rendered_message_length == 11,
		    "Captured message wasn't correct length");
	fail_unless(strcmp(captured_rendered_message, "Hello world") == 0,
		    "Captured message wasn't correct");
	fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0,
		    "Captured message wasn't correct filename");
	fail_unless(strcmp(captured_context.funcname, "test_nslog_trivial_uncorked_message") == 0,
		    "Captured message wasn't correct function name");
}
END_TEST

/**** The next set of tests need a fixture set for filters ****/

static nslog_filter_t *cat_test = NULL;
static nslog_filter_t *cat_another = NULL;

static const char *anchor_context_2 = "2";

static void
with_simple_filter_context_setup(void)
{
	captured_render_context = NULL;
	memset(&captured_context, 0, sizeof(captured_context));
	memset(captured_rendered_message, 0, sizeof(captured_rendered_message));
	captured_rendered_message_length = 0;
	captured_message_count = 0;
	fail_unless(nslog_set_render_callback(
			    nslog__test__render_function,
			    (void *)anchor_context_2) == NSLOG_NO_ERROR,
		    "Unable to set up render callback");
	fail_unless(nslog_filter_category_new("test", &cat_test) == NSLOG_NO_ERROR,
		    "Unable to create a category filter for 'test'");
	fail_unless(nslog_filter_category_new("another", &cat_another) == NSLOG_NO_ERROR,
		    "Unable to create a category filter for 'another'");
}

static void
with_simple_filter_context_teardown(void)
{
        /* Nothing to do to tear down */
	fail_unless(nslog_filter_set_active(NULL, NULL) == NSLOG_NO_ERROR,
		    "Unable to clear active filter");
	cat_test = nslog_filter_unref(cat_test);
	cat_another = nslog_filter_unref(cat_another);
}

START_TEST (test_nslog_simple_filter_corked_message)
{
	NSLOG(test, INFO, "Hello world");
	fail_unless(nslog_filter_set_active(cat_test, NULL) == NSLOG_NO_ERROR,
		    "Unable to set active filter to cat:test");
	fail_unless(nslog_uncork() == NSLOG_NO_ERROR,
		    "Unable to uncork");
	fail_unless(captured_message_count == 1,
		    "Captured message count was wrong");
	fail_unless(captured_render_context == anchor_context_2,
		    "Captured context wasn't passed through");
	fail_unless(strcmp(captured_context.category->name, "test") == 0,
		    "Captured context category wasn't normalised");
	fail_unless(captured_context.category == &__nslog_category_test,
		    "Captured context category wasn't the one we wanted");
	fail_unless(captured_rendered_message_length == 11,
		    "Captured message wasn't correct length");
	fail_unless(strcmp(captured_rendered_message, "Hello world") == 0,
		    "Captured message wasn't correct");
	fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0,
		    "Captured message wasn't correct filename");
	fail_unless(strcmp(captured_context.funcname, "test_nslog_simple_filter_corked_message") == 0,
		    "Captured message wasn't correct function name");

}
END_TEST

START_TEST (test_nslog_simple_filter_uncorked_message)
{
	fail_unless(nslog_filter_set_active(cat_test, NULL) == NSLOG_NO_ERROR,
		    "Unable to set active filter to cat:test");
	fail_unless(nslog_uncork() == NSLOG_NO_ERROR,
		    "Unable to uncork");
	NSLOG(test, INFO, "Hello world");
	fail_unless(captured_message_count == 1,
		    "Captured message count was wrong");
	fail_unless(captured_render_context == anchor_context_2,
		    "Captured context wasn't passed through");
	fail_unless(strcmp(captured_context.category->name, "test") == 0,
		    "Captured context category wasn't normalised");
	fail_unless(captured_context.category == &__nslog_category_test,
		    "Captured context category wasn't the one we wanted");
	fail_unless(captured_rendered_message_length == 11,
		    "Captured message wasn't correct length");
	fail_unless(strcmp(captured_rendered_message, "Hello world") == 0,
		    "Captured message wasn't correct");
	fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0,
		    "Captured message wasn't correct filename");
	fail_unless(strcmp(captured_context.funcname, "test_nslog_simple_filter_uncorked_message") == 0,
		    "Captured message wasn't correct function name");

}
END_TEST

START_TEST (test_nslog_basic_filter_sprintf)
{
	char *ct = nslog_filter_sprintf(cat_test);
	fail_unless(ct != NULL, "Unable to sprintf");
	fail_unless(strcmp(ct, "cat:test") == 0,
		    "Printed category test is wrong");
	free(ct);
	ct = nslog_filter_sprintf(cat_another);
	fail_unless(ct != NULL, "Unable to sprintf");
	fail_unless(strcmp(ct, "cat:another") == 0,
		    "Printed category another is wrong");
	free(ct);
}
END_TEST

START_TEST (test_nslog_parse_and_sprintf)
{
	nslog_filter_t *filt;
	fail_unless(nslog_filter_from_text("cat:test", &filt) == NSLOG_NO_ERROR,
		    "Unable to parse cat:test");
	char *ct = nslog_filter_sprintf(filt);
	nslog_filter_unref(filt);
	fail_unless(strcmp(ct, "cat:test") == 0,
		    "Printed parsed cat:test not right");
}
END_TEST

/**** And the suites are set up here ****/

void
nslog_basic_suite(SRunner *sr)
{
        Suite *s = suite_create("libnslog: Basic tests");
        TCase *tc_basic = NULL;

	tc_basic = tcase_create("Simple log checks, no filters");

        tcase_add_checked_fixture(tc_basic, with_simple_context_setup,
                                  with_simple_context_teardown);
        tcase_add_test(tc_basic, test_nslog_trivial_corked_message);
        tcase_add_test(tc_basic, test_nslog_trivial_uncorked_message);
        suite_add_tcase(s, tc_basic);

        tc_basic = tcase_create("Simple filter checks");

        tcase_add_checked_fixture(tc_basic, with_simple_filter_context_setup,
                                  with_simple_filter_context_teardown);
        tcase_add_test(tc_basic, test_nslog_simple_filter_corked_message);
        tcase_add_test(tc_basic, test_nslog_simple_filter_uncorked_message);
        tcase_add_test(tc_basic, test_nslog_basic_filter_sprintf);
        tcase_add_test(tc_basic, test_nslog_parse_and_sprintf);
        suite_add_tcase(s, tc_basic);

        srunner_add_suite(sr, s);
}