allow using -e as filter on ego list
[oweals/gnunet.git] / src / testbed-logger / test_testbed_logger_api.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 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  * @file testbed-logger/test_testbed_logger_api.c
22  * @brief testcases for the testbed logger api
23  * @author Sree Harsha Totakura
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_testbed_logger_service.h"
29
30 /**
31  * Generic logging shortcut
32  */
33 #define LOG(kind,...)                           \
34   GNUNET_log (kind, __VA_ARGS__)
35
36 /**
37  * Relative time seconds shorthand
38  */
39 #define TIME_REL_SECS(sec) \
40   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
41
42 /**
43  * Opaque handle for the logging service
44  */
45 static struct GNUNET_TESTBED_LOGGER_Handle *h;
46
47 static struct GNUNET_TESTING_Peer *peer;
48
49 static char *search_dir;
50
51 /**
52  * Abort task identifier
53  */
54 static struct GNUNET_SCHEDULER_Task *abort_task;
55 static struct GNUNET_SCHEDULER_Task *write_task;
56
57 static int result;
58
59 #define CANCEL_TASK(task) do {                  \
60     if (NULL != task) \
61     {                                           \
62       GNUNET_SCHEDULER_cancel (task);     \
63       task = NULL;    \
64     }                                           \
65   } while (0)
66
67 /**
68  * shortcut to exit during failure
69  */
70 #define FAIL_TEST(cond, ret) do {                               \
71     if (!(cond)) {                                              \
72       GNUNET_break(0);                                          \
73       CANCEL_TASK (abort_task);                                 \
74       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
75       ret;                                                      \
76     }                                                           \
77   } while (0)
78
79
80 /**
81  * Shutdown nicely
82  *
83  * @param cls NULL
84  * @param tc the task context
85  */
86 static void
87 shutdown_now ()
88 {
89   CANCEL_TASK (abort_task);
90   CANCEL_TASK (write_task);
91   GNUNET_free_non_null (search_dir);
92   if (NULL != h)
93     GNUNET_TESTBED_LOGGER_disconnect (h);
94   GNUNET_SCHEDULER_shutdown ();
95 }
96
97
98 static void
99 do_abort (void *cls)
100 {
101   LOG (GNUNET_ERROR_TYPE_WARNING,
102        "Aborting\n");
103   abort_task = NULL;
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 filename complete filename (absolute path)
116  * @return #GNUNET_OK to continue to iterate,
117  *  #GNUNET_NO to stop iteration with no error,
118  *  #GNUNET_SYSERR to abort iteration with error!
119  */
120 static int
121 iterator_cb (void *cls,
122              const char *filename)
123 {
124   const char *fn;
125   size_t len;
126   uint64_t fs;
127
128   LOG (GNUNET_ERROR_TYPE_DEBUG,
129        "Iterator sees file %s\n",
130        filename);
131   len = strlen (filename);
132   fn = filename + len;
133   if (0 != strcasecmp (".dat", fn - 4))
134     return GNUNET_OK;
135   if (GNUNET_OK !=
136       GNUNET_DISK_file_size (filename,
137                              &fs,
138                              GNUNET_NO,
139                              GNUNET_YES))
140   {
141     LOG (GNUNET_ERROR_TYPE_DEBUG,
142          "Failed to obtain file size for file %s\n",
143          filename);
144     return GNUNET_SYSERR;
145   }
146   if ((BSIZE * 2) != fs)
147   {
148     LOG (GNUNET_ERROR_TYPE_DEBUG,
149          "Unexpected file size for file %s\n",
150          filename);
151     /* The file size should be equal to what we
152        have written */
153     return GNUNET_SYSERR;
154   }
155   result = GNUNET_OK;
156   return GNUNET_OK;
157 }
158
159
160 /**
161  * Functions of this type are called to notify a successful
162  * transmission of the message to the logger service
163  *
164  * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
165  * @param size the amount of data sent
166  */
167 static void
168 flush_comp (void *cls,
169             size_t size)
170 {
171   LOG (GNUNET_ERROR_TYPE_DEBUG,
172        "Flush running\n");
173   FAIL_TEST (&write_task == cls,
174              return);
175   FAIL_TEST ((BSIZE * 2) == size,
176              return);
177   FAIL_TEST (GNUNET_OK ==
178              GNUNET_TESTING_peer_stop (peer),
179              return);
180   LOG (GNUNET_ERROR_TYPE_DEBUG,
181        "Peer stopped, scanning %s\n",
182        search_dir);
183   FAIL_TEST (GNUNET_SYSERR !=
184              GNUNET_DISK_directory_scan (search_dir,
185                                          &iterator_cb,
186                                          NULL),
187              return);
188   shutdown_now ();
189 }
190
191
192 static void
193 do_write (void *cls)
194 {
195   static int i;
196   char buf[BSIZE];
197
198   write_task = NULL;
199   LOG (GNUNET_ERROR_TYPE_DEBUG,
200        "Write task running\n");
201   if (0 == i)
202     write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
203                                                &do_write,
204                                                NULL);
205   (void) memset (buf, i, BSIZE);
206   GNUNET_TESTBED_LOGGER_write (h,
207                                buf,
208                                BSIZE);
209   if (0 == i++)
210     return;
211   GNUNET_TESTBED_LOGGER_flush (h,
212                                &flush_comp,
213                                &write_task);
214 }
215
216
217 /**
218  * Signature of the 'main' function for a (single-peer) testcase that
219  * is run using #GNUNET_TESTING_peer_run().
220  *
221  * @param cls closure
222  * @param cfg configuration of the peer that was started
223  * @param peer identity of the peer that was created
224  */
225 static void
226 test_main (void *cls,
227            const struct GNUNET_CONFIGURATION_Handle *cfg,
228            struct GNUNET_TESTING_Peer *p)
229 {
230   LOG (GNUNET_ERROR_TYPE_DEBUG,
231        "Connecting to logger\n");
232   FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)),
233              return);
234   FAIL_TEST (GNUNET_OK ==
235              GNUNET_CONFIGURATION_get_value_filename (cfg,
236                                                       "testbed-logger",
237                                                       "dir",
238                                                       &search_dir),
239              return);
240   peer = p;
241   write_task = GNUNET_SCHEDULER_add_now (&do_write,
242                                          NULL);
243   abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
244                                              &do_abort,
245                                              NULL);
246 }
247
248
249 /**
250  * Main function
251  */
252 int
253 main (int argc, char **argv)
254 {
255   int ret;
256
257   result = GNUNET_SYSERR;
258   GNUNET_log_setup ("test-testbed-logger-api",
259                     "WARNING",
260                     NULL);
261   GNUNET_break (GNUNET_OK ==
262                 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
263   ret = GNUNET_TESTING_service_run ("test-testbed-logger",
264                                     "testbed-logger",
265                                     "test_testbed_logger_api.conf",
266                                     &test_main,
267                                     NULL);
268   GNUNET_break (GNUNET_OK ==
269                 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
270   if (0 != ret)
271     return 1;
272   if (GNUNET_OK != result)
273     return 2;
274   return 0;
275 }