ksgi

Форк
0
/
test-logging-errors.c 
242 строки · 4.5 Кб
1
/*	$Id$ */
2
/*
3
 * Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
#include "../config.h"
18

19
#include <sys/wait.h>
20

21
#include <ctype.h>
22
#include <errno.h>
23
#if HAVE_ERR
24
# include <err.h>
25
#endif
26
#include <stdarg.h>
27
#include <stdint.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <unistd.h>
31

32
#include <curl/curl.h>
33

34
#include "../kcgi.h"
35
#include "regress.h"
36

37
#define	TEMPL "/tmp/test-logging-error.XXXXXXXXXX"
38

39
/*
40
 * Actually run the test pair of child and parent.
41
 * Child must log whatever it wants to log and parent processes the log
42
 * messages (if any).
43
 * Return zero on failure, non-zero on success.
44
 */
45
static int
46
test_runner(void (*child)(void), int (*parent)(FILE *))
47
{
48
	char	 sfn[32];
49
	int	 fd, st, rc;
50
	FILE	*f;
51
	pid_t	 pid;
52

53
	/* Create the temporary file we'll use. */
54

55
	strlcpy(sfn, TEMPL, sizeof(sfn));
56
	if ((fd = mkstemp(sfn)) == -1)
57
		err(EXIT_FAILURE, "%s", sfn);
58

59
	/* Start the child that will emit the message. */
60

61
	if ((pid = fork()) == -1)
62
		err(EXIT_FAILURE, "fork");
63

64
	if (pid == 0) {
65
		close(fd);
66
		if (!kutil_openlog(sfn))
67
			errx(EXIT_FAILURE, "kutil_openlog");
68
		child();
69
		abort();
70
		/* NOTREACHED */
71
	}
72

73
	/* Wait for child to do its printing, then cleanup. */
74

75
	if (waitpid(pid, &st, 0) == -1) {
76
		warn("waitpid");
77
		unlink(sfn);
78
		exit(EXIT_FAILURE);
79
	}
80
	unlink(sfn);
81

82
	/* Make sure it exited properly. */
83

84
	if (!WIFEXITED(st) || WEXITSTATUS(st) != EXIT_FAILURE)
85
		errx(EXIT_FAILURE, "child failure (%d)", WIFEXITED(st));
86

87
	/* Now run the parent test component. */
88

89
	if ((f = fdopen(fd, "r")) == NULL)
90
		err(EXIT_FAILURE, "%s", sfn);
91

92
	rc = parent(f);
93
	fclose(f);
94
	return rc;
95
}
96

97
/*
98
 * Wrap around getline().
99
 */
100
static char *
101
get_line(FILE *f)
102
{
103
           char		*line = NULL;
104
           size_t	 linesize = 0;
105
           ssize_t	 linelen;
106

107
           if ((linelen = getline(&line, &linesize, f)) == -1)
108
		   errx(EXIT_FAILURE, "getline");
109
	   return line;
110
}
111

112
static void
113
child4(void)
114
{
115

116
	errno = EINVAL;
117
	kutil_err(NULL, NULL, "%s", "foo");
118
}
119

120
static int
121
parent4(FILE *f)
122
{
123
	char		*buf;
124
	struct log_line	 line;
125
	int		 rc = 0;
126

127
	buf = get_line(f);
128
	if (!log_line_parse(buf, &line))
129
		goto out;
130
	if (strcmp(line.addr, "-") ||
131
	    strcmp(line.ident, "-") ||
132
	    strcmp(line.level, "ERROR") ||
133
	    strcmp(line.umsg, "foo: Invalid argument\n") ||
134
	    line.errmsg == NULL)
135
		goto out;
136
	rc = 1;
137
out:
138
	free(buf);
139
	return rc;
140
}
141

142
static void
143
child3(void)
144
{
145

146
	kutil_errx(NULL, NULL, "%s", "foo");
147
}
148

149
static int
150
parent3(FILE *f)
151
{
152
	char		*buf;
153
	struct log_line	 line;
154
	int		 rc = 0;
155

156
	buf = get_line(f);
157
	if (!log_line_parse(buf, &line))
158
		goto out;
159
	if (strcmp(line.addr, "-") ||
160
	    strcmp(line.ident, "-") ||
161
	    strcmp(line.level, "ERROR") ||
162
	    strcmp(line.umsg, "foo\n") ||
163
	    line.errmsg != NULL)
164
		goto out;
165
	rc = 1;
166
out:
167
	free(buf);
168
	return rc;
169
}
170

171
static void
172
child2(void)
173
{
174

175
	kutil_errx(NULL, "foo", NULL);
176
}
177

178
static int
179
parent2(FILE *f)
180
{
181
	char		*buf;
182
	struct log_line	 line;
183
	int		 rc = 0;
184

185
	buf = get_line(f);
186
	if (!log_line_parse(buf, &line))
187
		goto out;
188
	if (strcmp(line.addr, "-") ||
189
	    strcmp(line.ident, "foo") ||
190
	    strcmp(line.level, "ERROR") ||
191
	    strcmp(line.umsg, "-\n") ||
192
	    line.errmsg != NULL)
193
		goto out;
194
	rc = 1;
195
out:
196
	free(buf);
197
	return rc;
198
}
199

200
static void
201
child1(void)
202
{
203

204
	kutil_errx(NULL, NULL, NULL);
205
}
206

207
static int
208
parent1(FILE *f)
209
{
210
	char		*buf;
211
	struct log_line	 line;
212
	int		 rc = 0;
213

214
	buf = get_line(f);
215
	if (!log_line_parse(buf, &line))
216
		goto out;
217
	if (strcmp(line.addr, "-") ||
218
	    strcmp(line.ident, "-") ||
219
	    strcmp(line.level, "ERROR") ||
220
	    strcmp(line.umsg, "-\n") ||
221
	    line.errmsg != NULL)
222
		goto out;
223
	rc = 1;
224
out:
225
	free(buf);
226
	return rc;
227
}
228

229
int
230
main(void)
231
{
232

233
	if (!test_runner(child1, parent1))
234
		return EXIT_FAILURE;
235
	if (!test_runner(child2, parent2))
236
		return EXIT_FAILURE;
237
	if (!test_runner(child3, parent3))
238
		return EXIT_FAILURE;
239
	if (!test_runner(child4, parent4))
240
		return EXIT_FAILURE;
241
	return EXIT_SUCCESS;
242
}
243

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.