aboutsummaryrefslogtreecommitdiffstats
path: root/test/diagnostic.c
blob: 35866df010f5cc5b64cbf8d121951d0fd335fc23 (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
/*
 * The Rin Library – library "conformance" tests
 *
 * Copyright (C) 2015-2017 Gediminas Jakutis
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; version 2.1
 * of the License.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "test.h"
#include "rin/diagnostic.h"

struct stdiocap {
	int pipefd[2];
	int origfd;
	int backupfd;
	FILE *pipe;
};

static int err_test(void);
static int warn_test(void);
static int fixme_test(void);
static int info_test(void);
static int prefix_test(void);
static int thread_test(void);
static int clean_test(void);
static struct stdiocap capture_stdio_start(FILE *stream);
static void capture_stdio_stop(struct stdiocap *cap);
static char *visible_newlines(const char * const in);
static int test_default_channels(const char * const in, const char * const expected, size_t i, int usearg, void (*func)(const char *, const char *, ...));

int diagnostic_test(char *testname)
{
	static const struct test tests[] = {
			{"err", err_test},
			{"warn", warn_test},
			{"fixme", fixme_test},
			{"info", info_test},
			{"prefix", prefix_test},
			{"thread", thread_test},
			{"clean", clean_test} };
	size_t i;

	for (i = 0; i < arrlen(tests); ++i) {
		if (!strcmp(testname, tests[i].name)) {
			return tests[i].testfunc();
		}
	}

	return EXIT_FAILURE;
}

static int err_test(void)
{
	size_t i;
	int usearg;
	int ret;

	static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"};
	static const char *expected[] = {"error:\n", "error:test\n", "error:test number is 3\n", "error:4 tests ran\n"};

	for (i = 0; i < arrlen(in); ++i) {
		usearg = i < (arrlen(in) / 2) ? 0 : 1;
		ret = test_default_channels(in[i], expected[i], i, usearg, __rin_err);
	}

	return ret;
}

static int warn_test(void)
{
	size_t i;
	int usearg;
	int ret;

	static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"};
	static const char *expected[] = {"warning:\n", "warning:test\n", "warning:test number is 3\n", "warning:4 tests ran\n"};

	for (i = 0; i < arrlen(in); ++i) {
		usearg = i < (arrlen(in) / 2) ? 0 : 1;
		ret = test_default_channels(in[i], expected[i], i, usearg, __rin_warn);
	}

	return ret;
}

static int fixme_test(void)
{
	size_t i;
	int usearg;
	int ret;

	static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"};
	static const char *expected[] = {"fixme:\n", "fixme:test\n", "fixme:test number is 3\n", "fixme:4 tests ran\n"};

	for (i = 0; i < arrlen(in); ++i) {
		usearg = i < (arrlen(in) / 2) ? 0 : 1;
		ret = test_default_channels(in[i], expected[i], i, usearg, __rin_fixme);
	}

	return ret;
}

static int info_test(void)
{
	size_t i;
	int usearg;
	int ret;

	static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"};
	static const char *expected[] = {"info:\n", "info:test\n", "info:test number is 3\n", "info:4 tests ran\n"};

	/* `ninja test` seems to be intercepting stdout, so we cannot test the default outstream */
	rin_diag_set_outstream(rin_diag_info, stderr);

	for (i = 0; i < arrlen(in); ++i) {
		usearg = i < (arrlen(in) / 2) ? 0 : 1;
		ret = test_default_channels(in[i], expected[i], i, usearg, __rin_info);
	}

	/* restore the default, kind of */
	rin_diag_set_outstream(rin_diag_info, NULL);

	return ret;
}

static int prefix_test(void)
{
	return 77;
}

static int thread_test(void)
{
	return 77;
}

static int clean_test(void)
{
	return 77;
}

static struct stdiocap capture_stdio_start(FILE *stream)
{
	struct stdiocap ret;

	ret.origfd = fileno(stream);
	ret.backupfd = dup(ret.origfd);

	if (pipe(ret.pipefd)) {
		close(ret.backupfd);
		ret.backupfd = -1;
		ret.origfd = -1;
		ret.pipefd[0] = -1;
		ret.pipefd[1] = -1;
		ret.pipe = NULL;
	}

	dup2(ret.pipefd[1], ret.origfd);
	ret.pipe = fdopen(ret.pipefd[0], "r");
	return ret;
}

static void capture_stdio_stop(struct stdiocap *cap)
{
	/* restore original fd to the stream */
	dup2(cap->backupfd, cap->origfd);
	close(cap->backupfd);
	close(cap->pipefd[1]);
	close(cap->pipefd[0]);

	/* close everything we opened and invalidate all the descriptors */
	fclose(cap->pipe);
	cap->backupfd = -1;
	cap->origfd = -1;
	cap->pipefd[0] = -1;
	cap->pipefd[1] = -1;
	cap->pipe = NULL;
}

static char *visible_newlines(const char * const in)
{
	char *ret;
	const char *newline;
	const char *readptr;
	char *writeptr;

	readptr = in;
	ret = malloc(strlen(readptr) * 2 + 1);
	writeptr = ret;
	newline = strchr(readptr, '\n');

	while (newline) {
		/* write chunk until the next newline */
		memcpy(writeptr, readptr, newline - readptr);
		writeptr += newline - readptr; /* advance writing position */
		readptr = newline;
		++readptr; /* skip the newline */
		/* toss in replacement */
		memcpy(writeptr, "\\n", 2);
		writeptr += 2;
		newline = strchr(readptr, '\n');
	}

	/* write all that remains */
	strcpy(writeptr, readptr);
	return ret;
}

static int test_default_channels(const char * const in, const char * const expected, size_t i, int usearg, void (*func)(const char *, const char *, ...))
{
	char buf[32] = {0};
	struct stdiocap cap;
	size_t len;
	int ret = 0;

	len = strlen(expected);
	++len;

	cap = capture_stdio_start(stderr);

	if (!cap.pipe) {
		ret = -1;
		goto fail;
	}

	if (usearg) {
		func(NULL, in, i + 1);
	} else {
		func(NULL, in);
	}

	if (!(fgets(buf, sizeof(buf) < len ? sizeof(buf) : len, cap.pipe))) {
		ret = 1;
		goto fail;
	}

fail:
	capture_stdio_stop(&cap);

	if (ret == -1) {
		ret = ok(1, "%s: unexpected test program failure on iteration %zu", __func__, i);
	} else {
		char *tmp[2];

		ret = strncmp(buf, expected, sizeof(buf) < len ? sizeof(buf) : len);

		tmp[0] = visible_newlines(buf);
		tmp[1] = visible_newlines(expected);

		ret = ok(ret, "%s: expected: \"%s\", got: \"%s\"", __func__, tmp[1], tmp[0]);

		free(tmp[0]);
		free(tmp[1]);
	}

	return ret;
}