-fix channel data range to make -1 legal value
[oweals/gnunet.git] / src / testbed / test_testbed_logger_api.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 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 testbed/test_testbed_logger_api.c
23  * @brief testcases for the testbed logger api
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_testbed_logger_service.h"
31
32 /**
33  * Generic logging shortcut
34  */
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 /**
39  * Relative time seconds shorthand
40  */
41 #define TIME_REL_SECS(sec) \
42   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
43
44 /**
45  * Opaque handle for the logging service
46  */
47 static struct GNUNET_TESTBED_LOGGER_Handle *h;
48
49 static struct GNUNET_TESTING_Peer *peer;
50
51 static char *search_dir;
52
53 /**
54  * Abort task identifier
55  */
56 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
57 static GNUNET_SCHEDULER_TaskIdentifier write_task;
58
59 static int result;
60
61 #define CANCEL_TASK(task) do {                  \
62     if (GNUNET_SCHEDULER_NO_TASK != task) \
63     {                                           \
64       GNUNET_SCHEDULER_cancel (task);     \
65       task = GNUNET_SCHEDULER_NO_TASK;    \
66     }                                           \
67   } while (0)
68
69 /**
70  * shortcut to exit during failure
71  */
72 #define FAIL_TEST(cond, ret) do {                               \
73     if (!(cond)) {                                              \
74       GNUNET_break(0);                                          \
75       CANCEL_TASK (abort_task);                                 \
76       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
77       ret;                                                      \
78     }                                                           \
79   } while (0)
80
81 /**
82  * Shutdown nicely
83  *
84  * @param cls NULL
85  * @param tc the task context
86  */
87 static void
88 shutdown_now ()
89 {
90   CANCEL_TASK (abort_task);
91   CANCEL_TASK (write_task);
92   GNUNET_free_non_null (search_dir);
93   if (NULL != h)
94     GNUNET_TESTBED_LOGGER_disconnect (h);
95   GNUNET_SCHEDULER_shutdown ();
96 }
97
98
99 static void
100 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
103   abort_task = GNUNET_SCHEDULER_NO_TASK;
104   shutdown_now ();
105 }
106
107
108 #define BSIZE 1024
109
110
111 /**
112  * Function called to iterate over a directory.
113  *
114  * @param cls closure
115  * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
116  *           get called on the next entry (or finish cleanly);
117  *           NULL on error (will be the last call in that case)
118  * @param filename complete filename (absolute path)
119  * @param dirname directory name (absolute path)
120  */
121 static void
122 iterator_cb (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
123              const char *filename, const char *dirname)
124 {
125   const char *fn;
126   size_t len;
127   uint64_t fs;
128   int cancel;
129
130   cancel = GNUNET_NO;
131   if (NULL == filename)
132     goto iteration_cont;
133   len = strlen (filename);
134   if (len < 5)                  /* log file: `pid'.dat */
135     goto iteration_cont;
136   fn = filename + len;
137   if (0 != strcasecmp (".dat", fn - 4))
138     goto iteration_cont;
139   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs,
140                                           GNUNET_NO, GNUNET_YES))
141     goto iteration_cont;
142   if ((BSIZE * 2) != fs)        /* The file size should be equal to what we
143                                    have written */
144     goto iteration_cont;
145
146   cancel = GNUNET_YES;
147   result = GNUNET_OK;
148
149  iteration_cont:
150   if ( (NULL != di) &&
151        (GNUNET_YES == GNUNET_DISK_directory_iterator_next (di, cancel)) )
152     return;
153   shutdown_now ();
154 }
155
156 /**
157  * Functions of this type are called to notify a successful transmission of the
158  * message to the logger service
159  *
160  * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
161  * @param size the amount of data sent
162  */
163 static void
164 flush_comp (void *cls, size_t size)
165 {
166   FAIL_TEST (&write_task == cls, return);
167   FAIL_TEST ((BSIZE * 2) == size, return);
168   FAIL_TEST (GNUNET_OK == GNUNET_TESTING_peer_stop (peer), return);
169   FAIL_TEST (GNUNET_YES == GNUNET_DISK_directory_iterator_start
170              (GNUNET_SCHEDULER_PRIORITY_DEFAULT, search_dir,
171               &iterator_cb, NULL), return);
172 }
173
174
175 static void
176 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 {
178   static int i;
179   char buf[BSIZE];
180
181   write_task = GNUNET_SCHEDULER_NO_TASK;
182   if (0 == i)
183     write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_write, NULL);
184   (void) memset (buf, i, BSIZE);
185   GNUNET_TESTBED_LOGGER_write (h, buf, BSIZE);
186   if (0 == i++)
187     return;
188   GNUNET_TESTBED_LOGGER_flush (h, GNUNET_TIME_UNIT_FOREVER_REL,
189                                &flush_comp, &write_task);
190 }
191
192
193 /**
194  * Signature of the 'main' function for a (single-peer) testcase that
195  * is run using 'GNUNET_TESTING_peer_run'.
196  *
197  * @param cls closure
198  * @param cfg configuration of the peer that was started
199  * @param peer identity of the peer that was created
200  */
201 static void
202 test_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
203            struct GNUNET_TESTING_Peer *p)
204 {
205   FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)), return);
206   FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename
207              (cfg, "testbed-logger", "dir", &search_dir), return);
208   peer = p;
209   write_task = GNUNET_SCHEDULER_add_now (&do_write, NULL);
210   abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
211                                              &do_abort, NULL);
212 }
213
214
215 /**
216  * Main function
217  */
218 int
219 main (int argc, char **argv)
220 {
221   int ret;
222
223   result = GNUNET_SYSERR;
224   ret = GNUNET_TESTING_service_run ("test-testbed-logger",
225                                     "testbed-logger",
226                                     "test_testbed_logger_api.conf",
227                                     &test_main,
228                                     NULL);
229   if (0 != ret)
230     return 1;
231   if (GNUNET_OK != result)
232     return 2;
233   return 0;
234 }