-check return value
[oweals/gnunet.git] / src / util / test_common_logging_runtime_loglevels.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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_NO
34
35 static int ok;
36 static int phase = 0;
37
38 static struct GNUNET_OS_Process *proc;
39
40 /* Pipe to read from started processes stdout (on read end) */
41 static struct GNUNET_DISK_PipeHandle *pipe_stdout;
42
43 static GNUNET_SCHEDULER_TaskIdentifier die_task;
44
45 static void
46 runone (void);
47
48 static void
49 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
50 {
51   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
52               ok);
53   if (NULL != proc)
54   {
55     if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
56     {
57       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
58     }
59     GNUNET_OS_process_wait (proc);
60     GNUNET_OS_process_destroy (proc);
61     proc = NULL;
62   }
63   GNUNET_DISK_pipe_close (pipe_stdout);
64   if (ok == 1)
65   {
66     if (phase < 9)
67     {
68       phase += 1;
69       runone ();
70     }
71     else
72       ok = 0;
73   }
74   else
75     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
76 }
77
78 static char *
79 read_output_line (int phase_from1, int phase_to1, int phase_from2,
80                   int phase_to2, char c, char *expect_level,
81                   long delay_morethan, long delay_lessthan, int phase, char *p,
82                   int *len, long *delay, char level[8])
83 {
84   char *r = p;
85   char t[7];
86   int i, j, stop = 0;
87
88   j = 0;
89   int stage = 0;
90
91   if (!(phase >= phase_from1 && phase <= phase_to1) &&
92       !(phase >= phase_from2 && phase <= phase_to2))
93     return p;
94 #if 0
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96               "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
97 #endif
98   for (i = 0; i < *len && !stop; i++)
99   {
100     switch (stage)
101     {
102     case 0:                    /* read first char */
103       if (r[i] != c)
104       {
105         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c,
106                     r[i]);
107         GNUNET_break (0);
108         return NULL;
109       }
110       stage += 1;
111       break;
112     case 1:                    /* read at most 7 char-long error level string, finished by ' ' */
113       if (r[i] == ' ')
114       {
115         level[j] = '\0';
116         stage += 1;
117         j = 0;
118       }
119       else if (i == 8)
120       {
121         GNUNET_break (0);
122         ok = 2;
123         return NULL;
124       }
125       else
126         level[j++] = r[i];
127       break;
128     case 2:                    /* read the delay, finished by '\n' */
129       t[j++] = r[i];
130 #if WINDOWS
131       if (r[i] == '\r' && r[i + 1] == '\n')
132       {
133         i += 1;
134         t[j - 1] = '\0';
135         *delay = strtol (t, NULL, 10);
136         stop = 1;
137       }
138 #else
139       if (r[i] == '\n')
140       {
141         t[j - 1] = '\0';
142         *delay = strtol (t, NULL, 10);
143         stop = 1;
144       }
145 #endif
146       break;
147     }
148   }
149   if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 || *delay > 1000
150       || (!((*delay < delay_lessthan) || !(*delay > delay_morethan)) && c != '1'
151           && c != '2'))
152     return NULL;
153   *len = *len - i;
154   return &r[i];
155 }
156
157 char buf[20 * 16];
158 char *buf_ptr;
159 int bytes;
160
161 static void
162 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 {
164   struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
165   char level[8];
166   long delay;
167   long delays[8];
168   int rd;
169
170   rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr,
171                               sizeof (buf) - bytes);
172   if (rd > 0)
173   {
174     buf_ptr += rd;
175     bytes += rd;
176 #if VERBOSE
177     FPRINTF (stderr, "got %d bytes, reading more\n", rd);
178 #endif
179     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
180                                     stdout_read_handle, &read_call,
181                                     (void *) stdout_read_handle);
182     return;
183   }
184
185 #if VERBOSE
186   FPRINTF (stderr, "bytes is %d:%s\n", bytes, buf);
187 #endif
188
189   /* +------CHILD OUTPUT--
190    * |      SOFT     HARD
191    * |    E W I D  E W I D
192    * | 0E *        * *
193    * | 1W * *      * *
194    * P 2I * * *    * *
195    * H 3D * * * *  * *
196    * A
197    * S 4E *        *
198    * E 5W * *      * *
199    * | 6I * * *    * * *
200    * | 7D * * * *  * * * *
201    * | 8  * *      * *
202    * | 9  * *      * *
203    */
204   char *p = buf;
205
206   if (bytes == 20 * 16 ||
207       !(p =
208         read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
209                           &delay, level)) ||
210       !(p =
211         read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, &bytes,
212                           &delays[0], level)) ||
213       !(p =
214         read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes,
215                           &delay, level)) ||
216       !(p =
217         read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p,
218                           &bytes, &delays[1], level)) ||
219       !(p =
220         read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
221                           &delay, level)) ||
222       !(p =
223         read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, &bytes,
224                           &delays[2], level)) ||
225       !(p =
226         read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes,
227                           &delay, level)) ||
228       !(p =
229         read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, &bytes,
230                           &delays[3], level)) ||
231       !(p =
232         read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
233                           &delay, level)) ||
234       !(p =
235         read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, &bytes,
236                           &delays[4], level)) ||
237       !(p =
238         read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes,
239                           &delay, level)) ||
240       !(p =
241         read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p,
242                           &bytes, &delays[5], level)) ||
243       !(p =
244         read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
245                           &delay, level)) ||
246       !(p =
247         read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, &bytes,
248                           &delays[6], level)) ||
249       !(p =
250         read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes,
251                           &delay, level)) ||
252       !(p =
253         read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, &bytes,
254                           &delays[7], level)))
255   {
256     if (bytes == 20 * 16)
257       FPRINTF (stderr, "%s",  "Ran out of buffer space!\n");
258     GNUNET_break (0);
259     ok = 2;
260     GNUNET_SCHEDULER_cancel (die_task);
261     GNUNET_SCHEDULER_add_now (&end_task, NULL);
262     return;
263   }
264
265   GNUNET_SCHEDULER_cancel (die_task);
266   GNUNET_SCHEDULER_add_now (&end_task, NULL);
267 }
268
269 static void
270 runone ()
271 {
272   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
273
274   pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
275
276   if (pipe_stdout == NULL)
277   {
278     GNUNET_break (0);
279     ok = 2;
280     return;
281   }
282
283   putenv ("GNUNET_LOG=");
284   putenv ("GNUNET_FORCE_LOG=");
285   putenv ("GNUNET_FORCE_LOGFILE=");
286   switch (phase)
287   {
288   case 0:
289     putenv ("GNUNET_LOG=;;;;ERROR");
290     break;
291   case 1:
292     putenv ("GNUNET_LOG=;;;;WARNING");
293     break;
294   case 2:
295     putenv ("GNUNET_LOG=;;;;INFO");
296     break;
297   case 3:
298     putenv ("GNUNET_LOG=;;;;DEBUG");
299     break;
300   case 4:
301     putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
302     break;
303   case 5:
304     putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
305     break;
306   case 6:
307     putenv ("GNUNET_FORCE_LOG=;;;;INFO");
308     break;
309   case 7:
310     putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
311     break;
312   case 8:
313     putenv ("GNUNET_LOG=blah;;;;ERROR");
314     break;
315   case 9:
316     putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
317     break;
318   }
319
320   proc = GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, pipe_stdout,
321 #if MINGW
322                                   "test_common_logging_dummy",
323 #else
324                                   "./test_common_logging_dummy",
325 #endif
326                                   "test_common_logging_dummy", NULL);
327   GNUNET_assert (NULL != proc);
328   putenv ("GNUNET_FORCE_LOG=");
329   putenv ("GNUNET_LOG=");
330
331   /* Close the write end of the read pipe */
332   GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
333
334   stdout_read_handle =
335       GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
336
337   die_task =
338       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
339                                     (GNUNET_TIME_UNIT_SECONDS, 10), &end_task,
340                                     NULL);
341
342   bytes = 0;
343   buf_ptr = buf;
344   memset (&buf, 0, sizeof (buf));
345
346   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
347                                   stdout_read_handle, &read_call,
348                                   (void *) stdout_read_handle);
349 }
350
351 static void
352 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 {
354   phase = 0;
355   runone ();
356 }
357
358
359 int
360 main (int argc, char *argv[])
361 {
362   GNUNET_log_setup ("test-common-logging-runtime-loglevels",
363                     "WARNING",
364                     NULL);
365   ok = 1;
366   GNUNET_SCHEDULER_run (&task, &ok);
367   return ok;
368 }
369
370 /* end of test_common_logging_runtime_loglevels.c */