3
* Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
37
#define TEMPL "/tmp/test-logging-error.XXXXXXXXXX"
40
* Actually run the test pair of child and parent.
41
* Child must log whatever it wants to log and parent processes the log
43
* Return zero on failure, non-zero on success.
46
test_runner(void (*child)(void), int (*parent)(FILE *))
53
/* Create the temporary file we'll use. */
55
strlcpy(sfn, TEMPL, sizeof(sfn));
56
if ((fd = mkstemp(sfn)) == -1)
57
err(EXIT_FAILURE, "%s", sfn);
59
/* Start the child that will emit the message. */
61
if ((pid = fork()) == -1)
62
err(EXIT_FAILURE, "fork");
66
if (!kutil_openlog(sfn))
67
errx(EXIT_FAILURE, "kutil_openlog");
73
/* Wait for child to do its printing, then cleanup. */
75
if (waitpid(pid, &st, 0) == -1) {
82
/* Make sure it exited properly. */
84
if (!WIFEXITED(st) || WEXITSTATUS(st) != EXIT_FAILURE)
85
errx(EXIT_FAILURE, "child failure (%d)", WIFEXITED(st));
87
/* Now run the parent test component. */
89
if ((f = fdopen(fd, "r")) == NULL)
90
err(EXIT_FAILURE, "%s", sfn);
98
* Wrap around getline().
107
if ((linelen = getline(&line, &linesize, f)) == -1)
108
errx(EXIT_FAILURE, "getline");
117
kutil_err(NULL, NULL, "%s", "foo");
124
struct log_line line;
128
if (!log_line_parse(buf, &line))
130
if (strcmp(line.addr, "-") ||
131
strcmp(line.ident, "-") ||
132
strcmp(line.level, "ERROR") ||
133
strcmp(line.umsg, "foo: Invalid argument\n") ||
146
kutil_errx(NULL, NULL, "%s", "foo");
153
struct log_line line;
157
if (!log_line_parse(buf, &line))
159
if (strcmp(line.addr, "-") ||
160
strcmp(line.ident, "-") ||
161
strcmp(line.level, "ERROR") ||
162
strcmp(line.umsg, "foo\n") ||
175
kutil_errx(NULL, "foo", NULL);
182
struct log_line line;
186
if (!log_line_parse(buf, &line))
188
if (strcmp(line.addr, "-") ||
189
strcmp(line.ident, "foo") ||
190
strcmp(line.level, "ERROR") ||
191
strcmp(line.umsg, "-\n") ||
204
kutil_errx(NULL, NULL, NULL);
211
struct log_line line;
215
if (!log_line_parse(buf, &line))
217
if (strcmp(line.addr, "-") ||
218
strcmp(line.ident, "-") ||
219
strcmp(line.level, "ERROR") ||
220
strcmp(line.umsg, "-\n") ||
233
if (!test_runner(child1, parent1))
235
if (!test_runner(child2, parent2))
237
if (!test_runner(child3, parent3))
239
if (!test_runner(child4, parent4))