aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_common_logging_runtime_loglevels.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_common_logging_runtime_loglevels.c')
-rw-r--r--src/lib/util/test_common_logging_runtime_loglevels.c457
1 files changed, 457 insertions, 0 deletions
diff --git a/src/lib/util/test_common_logging_runtime_loglevels.c b/src/lib/util/test_common_logging_runtime_loglevels.c
new file mode 100644
index 000000000..f3f2fd64f
--- /dev/null
+++ b/src/lib/util/test_common_logging_runtime_loglevels.c
@@ -0,0 +1,457 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/test_common_logging_runtime_loglevels.c
23 * @brief testcase for the logging module (runtime log level adjustment)
24 * @author LRN
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30#define VERBOSE GNUNET_NO
31
32/**
33 * How much time the child is allowed to waste on skipped log calls, at most.
34 * Raspberry Pi takes 113 microseconds tops, this is 3x that value.
35 */
36#define MAX_SKIP_DELAY GNUNET_TIME_relative_multiply ( \
37 GNUNET_TIME_UNIT_MICROSECONDS, 400).rel_value_us
38
39/**
40 * How much time non-skipped log call should take, at least.
41 * Keep in sync with the value in the dummy!
42 */
43#define OUTPUT_DELAY GNUNET_TIME_relative_multiply ( \
44 GNUNET_TIME_UNIT_MICROSECONDS, 1000).rel_value_us
45
46static int ok;
47
48static int phase = 0;
49
50static struct GNUNET_OS_Process *proc;
51
52/* Pipe to read from started processes stdout (on read end) */
53static struct GNUNET_DISK_PipeHandle *pipe_stdout;
54
55static struct GNUNET_SCHEDULER_Task *die_task;
56
57static struct GNUNET_SCHEDULER_Task *read_task;
58
59static void
60runone (void);
61
62
63static void
64end_task (void *cls)
65{
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
67 ok);
68 if (NULL != proc)
69 {
70 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
71 {
72 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
73 }
74 GNUNET_OS_process_wait (proc);
75 GNUNET_OS_process_destroy (proc);
76 proc = NULL;
77 }
78 if (NULL != read_task)
79 {
80 GNUNET_SCHEDULER_cancel (read_task);
81 read_task = NULL;
82 }
83 GNUNET_DISK_pipe_close (pipe_stdout);
84 if (ok == 1)
85 {
86 if (phase < 9)
87 {
88 phase += 1;
89 runone ();
90 }
91 else
92 ok = 0;
93 }
94 else
95 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
96}
97
98
99static char *
100read_output_line (int phase_from1, int phase_to1, int phase_from2,
101 int phase_to2, char c, const char *expect_level,
102 long delay_morethan, long delay_lessthan, int phase,
103 char *p,
104 int *len, long *delay, char level[8])
105{
106 char *r = p;
107 char t[7];
108 int i, j, stop = 0;
109 int level_matches;
110 int delay_is_sane;
111 int delay_is_a_dummy;
112 int delay_outside_of_range;
113
114 j = 0;
115 int stage = 0;
116
117 if (! ((phase >= phase_from1) && (phase <= phase_to1)) &&
118 ! ((phase >= phase_from2) && (phase <= phase_to2)))
119 return p;
120#if 0
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
123#endif
124 for (i = 0; i < *len && ! stop; i++)
125 {
126 switch (stage)
127 {
128 case 0: /* read first char */
129 if (r[i] != c)
130 {
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c,
132 r[i]);
133 GNUNET_break (0);
134 return NULL;
135 }
136 stage += 1;
137 break;
138
139 case 1: /* read at most 7 char-long error level string, finished by ' ' */
140 if (r[i] == ' ')
141 {
142 level[j] = '\0';
143 stage += 1;
144 j = 0;
145 }
146 else if (i == 8)
147 {
148 GNUNET_break (0);
149 ok = 2;
150 return NULL;
151 }
152 else
153 level[j++] = r[i];
154 break;
155
156 case 2: /* read the delay, finished by '\n' */
157 t[j++] = r[i];
158 if (r[i] == '\n')
159 {
160 t[j - 1] = '\0';
161 *delay = strtol (t, NULL, 10);
162 stop = 1;
163 }
164 break;
165 }
166 }
167 level_matches = (strcmp (expect_level, level) == 0);
168 delay_is_sane = (*delay >= 0) && (*delay <= 1000000);
169 delay_is_a_dummy = (c == 'L');
170 /* Delay must be either less than 'lessthan' (log call is skipped)
171 * or more than 'morethan' (log call is not skipped)
172 */
173 delay_outside_of_range = ((*delay < delay_lessthan) || (*delay >=
174 delay_morethan));
175 if (delay_is_a_dummy)
176 delay_outside_of_range = 1;
177
178 if (! stop)
179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180 "Wrong log format?\n");
181 if (! level_matches)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "Wrong log level\n");
184 if (! delay_is_sane)
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 "Delay %ld is insane\n",
187 *delay);
188 if (! delay_outside_of_range)
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "Delay %ld is not outside of range (%ld ; %ld)\n",
191 *delay,
192 delay_lessthan,
193 delay_morethan);
194 if (! stop || ! level_matches || ! delay_is_sane || ! delay_outside_of_range)
195 return NULL;
196 *len = *len - i;
197 return &r[i];
198}
199
200
201/**
202 * Up to 8 non-skipped GNUNET_log() calls
203 * + extra line with delay for each one
204 */
205#define LOG_MAX_NUM_LINES (8 * 2)
206/**
207 * Actual message is 17 chars at most
208 */
209#define LOG_MAX_LINE_LENGTH (17)
210
211#define LOG_BUFFER_SIZE LOG_MAX_NUM_LINES *LOG_MAX_LINE_LENGTH
212
213static char buf[LOG_BUFFER_SIZE];
214
215static char *buf_ptr;
216
217static int bytes;
218
219
220static void
221read_call (void *cls)
222{
223 const struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
224 char level[8];
225 long delay;
226 long delays[8];
227 int rd;
228
229 read_task = NULL;
230 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr,
231 sizeof(buf) - bytes);
232 if (rd > 0)
233 {
234 buf_ptr += rd;
235 bytes += rd;
236#if VERBOSE
237 fprintf (stderr, "got %d bytes, reading more\n", rd);
238#endif
239 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
240 stdout_read_handle,
241 &read_call,
242 (void *) stdout_read_handle);
243 return;
244 }
245
246#if VERBOSE
247 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
248#endif
249
250 /* +------CHILD OUTPUT--
251 * | SOFT HARD
252 * | E W I D E W I D
253 * | 0E * * *
254 * | 1W * * * *
255 * P 2I * * * * *
256 * H 3D * * * * * *
257 * A
258 * S 4E * *
259 * E 5W * * * *
260 * | 6I * * * * * *
261 * | 7D * * * * * * * *
262 * | 8 * * * *
263 * | 9 * * * *
264 */char *p = buf;
265
266 if ((bytes == LOG_BUFFER_SIZE) ||
267 ! (p =
268 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1,
269 1, phase, p,
270 &bytes, &delay, level)) ||
271 ! (p =
272 read_output_line (0, 3, 4, 9, '1', "ERROR", OUTPUT_DELAY,
273 MAX_SKIP_DELAY, phase, p,
274 &bytes, &delays[0], level)) ||
275 ! (p =
276 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1,
277 1, phase, p,
278 &bytes, &delay, level)) ||
279 ! (p =
280 read_output_line (0, 3, 4, 9, '1', "WARNING", OUTPUT_DELAY,
281 MAX_SKIP_DELAY, phase, p,
282 &bytes, &delays[1], level)) ||
283 ! (p =
284 read_output_line (2, 3, 6, 7, 'L', "INFO", -1,
285 1, phase, p,
286 &bytes, &delay, level)) ||
287 ! (p =
288 read_output_line (0, 3, 4, 9, '1', "INFO", OUTPUT_DELAY,
289 MAX_SKIP_DELAY, phase, p,
290 &bytes, &delays[2], level)) ||
291 ! (p =
292 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1,
293 1, phase, p,
294 &bytes, &delay, level)) ||
295 ! (p =
296 read_output_line (0, 3, 4, 9, '1', "DEBUG", OUTPUT_DELAY,
297 MAX_SKIP_DELAY, phase, p,
298 &bytes, &delays[3], level)) ||
299 ! (p =
300 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1,
301 1, phase, p,
302 &bytes, &delay, level)) ||
303 ! (p =
304 read_output_line (0, 3, 4, 9, '2', "ERROR", OUTPUT_DELAY,
305 MAX_SKIP_DELAY, phase, p,
306 &bytes, &delays[4], level)) ||
307 ! (p =
308 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1,
309 1, phase, p,
310 &bytes, &delay, level)) ||
311 ! (p =
312 read_output_line (0, 3, 4, 9, '2', "WARNING", OUTPUT_DELAY,
313 MAX_SKIP_DELAY, phase, p,
314 &bytes, &delays[5], level)) ||
315 ! (p =
316 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1,
317 1, phase, p,
318 &bytes, &delay, level)) ||
319 ! (p =
320 read_output_line (0, 3, 4, 9, '2', "INFO", OUTPUT_DELAY,
321 MAX_SKIP_DELAY, phase, p,
322 &bytes, &delays[6], level)) ||
323 ! (p =
324 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1,
325 1, phase, p,
326 &bytes, &delay, level)) ||
327 ! (p =
328 read_output_line (0, 3, 4, 9, '2', "DEBUG", OUTPUT_DELAY,
329 MAX_SKIP_DELAY, phase, p,
330 &bytes, &delays[7], level)))
331 {
332 if (bytes == LOG_BUFFER_SIZE)
333 fprintf (stderr, "%s", "Ran out of buffer space!\n");
334 GNUNET_break (0);
335 ok = 2;
336 GNUNET_SCHEDULER_cancel (die_task);
337 GNUNET_SCHEDULER_add_now (&end_task, NULL);
338 return;
339 }
340
341 GNUNET_SCHEDULER_cancel (die_task);
342 GNUNET_SCHEDULER_add_now (&end_task, NULL);
343}
344
345
346static void
347runone ()
348{
349 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
350
351 pipe_stdout = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW);
352
353 if (pipe_stdout == NULL)
354 {
355 GNUNET_break (0);
356 ok = 2;
357 return;
358 }
359
360 putenv ("GNUNET_LOG=");
361 putenv ("GNUNET_FORCE_LOG=");
362 putenv ("GNUNET_FORCE_LOGFILE=");
363 switch (phase)
364 {
365 case 0:
366 putenv ("GNUNET_LOG=;;;;ERROR");
367 break;
368
369 case 1:
370 putenv ("GNUNET_LOG=;;;;WARNING");
371 break;
372
373 case 2:
374 putenv ("GNUNET_LOG=;;;;INFO");
375 break;
376
377 case 3:
378 putenv ("GNUNET_LOG=;;;;DEBUG");
379 break;
380
381 case 4:
382 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
383 break;
384
385 case 5:
386 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
387 break;
388
389 case 6:
390 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
391 break;
392
393 case 7:
394 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
395 break;
396
397 case 8:
398 putenv ("GNUNET_LOG=blah;;;;ERROR");
399 break;
400
401 case 9:
402 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
403 break;
404 }
405
406 proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
407 NULL, pipe_stdout, NULL,
408 "./test_common_logging_dummy",
409 "test_common_logging_dummy", NULL);
410 GNUNET_assert (NULL != proc);
411 putenv ("GNUNET_FORCE_LOG=");
412 putenv ("GNUNET_LOG=");
413
414 /* Close the write end of the read pipe */
415 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
416
417 stdout_read_handle =
418 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
419
420 die_task =
421 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
422 (GNUNET_TIME_UNIT_SECONDS, 10),
423 &end_task,
424 NULL);
425
426 bytes = 0;
427 buf_ptr = buf;
428 memset (&buf, 0, sizeof(buf));
429
430 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
431 stdout_read_handle,
432 &read_call,
433 (void *) stdout_read_handle);
434}
435
436
437static void
438task (void *cls)
439{
440 phase = 0;
441 runone ();
442}
443
444
445int
446main (int argc, char *argv[])
447{
448 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
449 "WARNING",
450 NULL);
451 ok = 1;
452 GNUNET_SCHEDULER_run (&task, &ok);
453 return ok;
454}
455
456
457/* end of test_common_logging_runtime_loglevels.c */