aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-10-06 07:45:59 +0000
committerChristian Grothoff <christian@grothoff.org>2011-10-06 07:45:59 +0000
commit7c761db5dc34daba09b587c6b5abc3090b253010 (patch)
tree96ad5463d81ce5452c54b172214f3715fd29ab0b /src/util
parent09ba4f230cd95676753a791c30db18489dca5c49 (diff)
downloadgnunet-7c761db5dc34daba09b587c6b5abc3090b253010.tar.gz
gnunet-7c761db5dc34daba09b587c6b5abc3090b253010.zip
testcase for runtime loglevels form LRN
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Makefile.am17
-rw-r--r--src/util/test_common_logging_dummy.c92
-rw-r--r--src/util/test_common_logging_runtime_loglevels.c349
3 files changed, 456 insertions, 2 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 02f823442..d816bc42c 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -26,7 +26,8 @@ if USE_COVERAGE
26endif 26endif
27 27
28noinst_PROGRAMS = \ 28noinst_PROGRAMS = \
29 gnunet-config-diff 29 gnunet-config-diff \
30 test_common_logging_dummy
30 31
31gnunet_config_diff_SOURCES = \ 32gnunet_config_diff_SOURCES = \
32 gnunet-config-diff.c 33 gnunet-config-diff.c
@@ -35,6 +36,12 @@ gnunet_config_diff_LDADD = \
35gnunet_config_diff_DEPENDENCIES = \ 36gnunet_config_diff_DEPENDENCIES = \
36 libgnunetutil.la 37 libgnunetutil.la
37 38
39test_common_logging_dummy_SOURCES = \
40 test_common_logging_dummy.c
41test_common_logging_dummy_LDADD = \
42 $(top_builddir)/src/util/libgnunetutil.la
43test_common_logging_dummy_DEPENDENCIES = \
44 libgnunetutil.la
38 45
39lib_LTLIBRARIES = libgnunetutil.la 46lib_LTLIBRARIES = libgnunetutil.la
40 47
@@ -175,7 +182,8 @@ check_PROGRAMS = \
175 test_strings \ 182 test_strings \
176 test_time \ 183 test_time \
177 $(BENCHMARKS) \ 184 $(BENCHMARKS) \
178 test_os_start_process 185 test_os_start_process \
186 test_common_logging_runtime_loglevels
179 187
180if ENABLE_TEST_RUN 188if ENABLE_TEST_RUN
181TESTS = $(check_PROGRAMS) 189TESTS = $(check_PROGRAMS)
@@ -212,6 +220,11 @@ test_common_logging_SOURCES = \
212test_common_logging_LDADD = \ 220test_common_logging_LDADD = \
213 $(top_builddir)/src/util/libgnunetutil.la 221 $(top_builddir)/src/util/libgnunetutil.la
214 222
223test_common_logging_runtime_loglevels_SOURCES = \
224 test_common_logging_runtime_loglevels.c
225test_common_logging_runtime_loglevels_LDADD = \
226 $(top_builddir)/src/util/libgnunetutil.la
227
215test_configuration_SOURCES = \ 228test_configuration_SOURCES = \
216 test_configuration.c 229 test_configuration.c
217test_configuration_LDADD = \ 230test_configuration_LDADD = \
diff --git a/src/util/test_common_logging_dummy.c b/src/util/test_common_logging_dummy.c
new file mode 100644
index 000000000..64d164a13
--- /dev/null
+++ b/src/util/test_common_logging_dummy.c
@@ -0,0 +1,92 @@
1/*
2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file util/test_common_logging_dummy.c
23 * @brief dummy labrat for the testcase for the logging module (runtime
24 * log level adjustment)
25 * @author LRN
26 */
27#include "platform.h"
28#include "gnunet_common.h"
29#include "gnunet_time_lib.h"
30#include "gnunet_network_lib.h"
31
32#define MS200 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200)
33
34static void
35my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component,
36 const char *date, const char *msg)
37{
38 if (strncmp ("test-common-logging-dummy", component, 25) != 0)
39 return;
40 fprintf (stdout, "%s", msg);
41 fflush (stdout);
42}
43
44static int
45expensive_func ()
46{
47 return GNUNET_NETWORK_socket_select (NULL, NULL, NULL, MS200);
48}
49
50#define pr(kind,lvl) {\
51 struct GNUNET_TIME_Absolute t1, t2;\
52 t1 = GNUNET_TIME_absolute_get ();\
53 GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\
54 t2 = GNUNET_TIME_absolute_get ();\
55 printf ("1%s %llu\n", lvl,\
56 (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value); \
57}
58
59#define pr2(kind,lvl) {\
60 struct GNUNET_TIME_Absolute t1, t2;\
61 t1 = GNUNET_TIME_absolute_get ();\
62 GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\
63 t2 = GNUNET_TIME_absolute_get ();\
64 printf ("2%s %llu\n", lvl,\
65 (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value); \
66}
67
68int
69main (int argc, char *argv[])
70{
71 /* We set up logging with NULL level - will be overrided by
72 * GNUNET_LOG or GNUNET_FORCE_LOG at runtime.
73 */
74 GNUNET_log_setup ("test-common-logging-dummy", NULL, "/dev/null");
75 GNUNET_logger_add (&my_log, NULL);
76 pr (GNUNET_ERROR_TYPE_ERROR, "ERROR");
77 pr (GNUNET_ERROR_TYPE_WARNING, "WARNING");
78 pr (GNUNET_ERROR_TYPE_INFO, "INFO");
79 pr (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
80
81 /* We set up logging with WARNING level - will onle be overrided by
82 * GNUNET_FORCE_LOG at runtime.
83 */
84 GNUNET_log_setup ("test-common-logging-dummy", "WARNING", "/dev/null");
85 pr2 (GNUNET_ERROR_TYPE_ERROR, "ERROR");
86 pr2 (GNUNET_ERROR_TYPE_WARNING, "WARNING");
87 pr2 (GNUNET_ERROR_TYPE_INFO, "INFO");
88 pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
89 return 0;
90} /* end of main */
91
92/* end of test_common_logging_dummy.c */
diff --git a/src/util/test_common_logging_runtime_loglevels.c b/src/util/test_common_logging_runtime_loglevels.c
new file mode 100644
index 000000000..e2864165a
--- /dev/null
+++ b/src/util/test_common_logging_runtime_loglevels.c
@@ -0,0 +1,349 @@
1/*
2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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#include "platform.h"
27#include "gnunet_common.h"
28#include "gnunet_scheduler_lib.h"
29#include "gnunet_network_lib.h"
30#include "gnunet_disk_lib.h"
31#include "gnunet_os_lib.h"
32
33#define VERBOSE GNUNET_EXTRA_LOGGING
34
35static int ok;
36static int phase = 0;
37
38static struct GNUNET_OS_Process *proc;
39
40/* Pipe to read from started processes stdout (on read end) */
41static struct GNUNET_DISK_PipeHandle *pipe_stdout;
42
43static GNUNET_SCHEDULER_TaskIdentifier die_task;
44
45static void runone ();
46
47static void
48end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
49{
50
51 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
52 {
53 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
54 }
55 GNUNET_OS_process_wait (proc);
56 GNUNET_OS_process_close (proc);
57 proc = NULL;
58 GNUNET_DISK_pipe_close (pipe_stdout);
59 if (ok == 1)
60 {
61 if (phase < 9)
62 {
63 phase += 1;
64 runone ();
65 }
66 else
67 ok = 0;
68 }
69 else
70 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
71}
72
73static char *
74read_output_line (int phase_from1, int phase_to1, int phase_from2,
75 int phase_to2, char c, char *expect_level,
76 long delay_morethan, long delay_lessthan, int phase, char *p,
77 int *len, long *delay, char level[8])
78{
79 char *r = p;
80 char t[7];
81 int i, j, stop = 0;
82 j = 0;
83 int stage = 0;
84 if (!(phase >= phase_from1 && phase <= phase_to1) &&
85 !(phase >= phase_from2 && phase <= phase_to2))
86 return p;
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to match '%c%s \\d\\r\\n' on %s", c, expect_level, p);
88 for (i = 0; i < *len && !stop; i++)
89 {
90 switch (stage)
91 {
92 case 0: /* read first char */
93 if (r[i] != c)
94 {
95 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c, r[i]);
96 GNUNET_break (0);
97 return NULL;
98 }
99 stage += 1;
100 break;
101 case 1: /* read at most 7 char-long error level string, finished by ' ' */
102 if (r[i] == ' ')
103 {
104 level[j] = '\0';
105 stage += 1;
106 j = 0;
107 }
108 else if (i == 8)
109 {
110 GNUNET_break (0);
111 ok = 2;
112 return NULL;
113 }
114 else
115 level[j++] = r[i];
116 break;
117 case 2: /* read the delay, finished by '\n' */
118 t[j++] = r[i];
119#if WINDOWS
120 if (r[i] == '\r' && r[i + 1] == '\n')
121 {
122 i += 1;
123 t[j - 1] = '\0';
124 *delay = strtol (t, NULL, 10);
125 stop = 1;
126 }
127#else
128 if (r[i] == '\n')
129 {
130 t[j - 1] = '\0';
131 *delay = strtol (t, NULL, 10);
132 stop = 1;
133 }
134#endif
135 break;
136 }
137 }
138 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 || *delay > 1000 ||
139 (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) && c != '1' && c != '2'))
140 return NULL;
141 *len = *len - i;
142 return &r[i];
143}
144
145char buf[20*16];
146char *buf_ptr;
147int bytes;
148
149static void
150read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151{
152 struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
153 char level[8];
154 long delay;
155 long delays[8];
156 int rd;
157
158 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes);
159 if (rd > 0)
160 {
161 buf_ptr += rd;
162 bytes += rd;
163 while (rd > 0)
164 {
165 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes);
166 if (rd == -1 && errno == EWOULDBLOCK)
167 {
168 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
169 stdout_read_handle, &read_call,
170 (void *) stdout_read_handle);
171 return;
172 }
173 else if (rd == -1)
174 rd = 0;
175 buf_ptr += rd;
176 bytes += rd;
177 }
178 }
179
180#if VERBOSE
181 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
182#endif
183
184 /* +------CHILD OUTPUT--
185 * | SOFT HARD
186 * | E W I D E W I D
187 * | 0E * * *
188 * | 1W * * * *
189 * P 2I * * * * *
190 * H 3D * * * * * *
191 * A
192 * S 4E * *
193 * E 5W * * * *
194 * | 6I * * * * * *
195 * | 7D * * * * * * * *
196 * | 8 * * * *
197 * | 9 * * * *
198 */
199 char *p = buf;
200 if (bytes == 20*16 ||
201 !(p = read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, &delay, level)) ||
202 !(p = read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, &bytes, &delays[0], level)) ||
203 !(p = read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes, &delay, level)) ||
204 !(p = read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p, &bytes, &delays[1], level)) ||
205 !(p = read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, &delay, level)) ||
206 !(p = read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, &bytes, &delays[2], level)) ||
207 !(p = read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes, &delay, level)) ||
208 !(p = read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, &bytes, &delays[3], level)) ||
209 !(p = read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes, &delay, level)) ||
210 !(p = read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, &bytes, &delays[4], level)) ||
211 !(p = read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes, &delay, level)) ||
212 !(p = read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p, &bytes, &delays[5], level)) ||
213 !(p = read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes, &delay, level)) ||
214 !(p = read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, &bytes, &delays[6], level)) ||
215 !(p = read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes, &delay, level)) ||
216 !(p = read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, &bytes, &delays[7], level)))
217 {
218 if (bytes == 20*16)
219 fprintf (stderr, "Ran out of buffer space!\n");
220 GNUNET_break (0);
221 ok = 2;
222 GNUNET_SCHEDULER_cancel (die_task);
223 GNUNET_SCHEDULER_add_now (&end_task, NULL);
224 return;
225 }
226
227 GNUNET_SCHEDULER_cancel (die_task);
228 GNUNET_SCHEDULER_add_now (&end_task, NULL);
229}
230
231static void
232runone ()
233{
234 char *fn;
235 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
236
237 GNUNET_asprintf (&fn, "test_common_logging_dummy");
238
239 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
240
241 if (pipe_stdout == NULL)
242 {
243 GNUNET_break (0);
244 ok = 2;
245 GNUNET_free (fn);
246 return;
247 }
248
249 putenv ("GNUNET_LOG=");
250 putenv ("GNUNET_FORCE_LOG=");
251 switch (phase)
252 {
253 case 0:
254 putenv ("GNUNET_LOG=;;;;ERROR");
255 break;
256 case 1:
257 putenv ("GNUNET_LOG=;;;;WARNING");
258 break;
259 case 2:
260 putenv ("GNUNET_LOG=;;;;INFO");
261 break;
262 case 3:
263 putenv ("GNUNET_LOG=;;;;DEBUG");
264 break;
265 case 4:
266 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
267 break;
268 case 5:
269 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
270 break;
271 case 6:
272 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
273 break;
274 case 7:
275 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
276 break;
277 case 8:
278 putenv ("GNUNET_LOG=blah;;;;ERROR");
279 break;
280 case 9:
281 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
282 break;
283 }
284
285 proc =
286 GNUNET_OS_start_process (NULL, pipe_stdout, fn,
287 "test_common_logging_dummy", NULL);
288 GNUNET_free (fn);
289 putenv ("GNUNET_FORCE_LOG=");
290 putenv ("GNUNET_LOG=");
291
292 /* Close the write end of the read pipe */
293 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
294
295 stdout_read_handle =
296 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
297
298 die_task =
299 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
300 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task,
301 NULL);
302
303 bytes = 0;
304 buf_ptr = buf;
305 memset (&buf, 0, sizeof (buf));
306
307 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
308 stdout_read_handle, &read_call,
309 (void *) stdout_read_handle);
310}
311
312static void
313task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
314{
315 phase = 0;
316 runone ();
317}
318
319/**
320 * Main method, starts scheduler with task1,
321 * checks that "ok" is correct at the end.
322 */
323static int
324check ()
325{
326 ok = 1;
327 GNUNET_SCHEDULER_run (&task, &ok);
328 return ok;
329}
330
331
332int
333main (int argc, char *argv[])
334{
335 int ret;
336
337 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
338#if VERBOSE
339 "DEBUG",
340#else
341 "WARNING",
342#endif
343 NULL);
344 ret = check ();
345
346 return ret;
347}
348
349/* end of test_common_logging_runtime_loglevels.c */