- fix
[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   GNUNET_SCHEDULER_shutdown ();
94 }
95
96
97 static void
98 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
101   abort_task = GNUNET_SCHEDULER_NO_TASK;
102   shutdown_now ();
103 }
104
105
106 #define BSIZE 1024
107
108
109 /**
110  * Function called to iterate over a directory.
111  *
112  * @param cls closure
113  * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
114  *           get called on the next entry (or finish cleanly);
115  *           NULL on error (will be the last call in that case)
116  * @param filename complete filename (absolute path)
117  * @param dirname directory name (absolute path)
118  */
119 static void
120 iterator_cb (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
121              const char *filename, const char *dirname)
122 {
123   const char *fn;
124   size_t len;
125   uint64_t fs;
126   int cancel;
127
128   cancel = GNUNET_NO;
129   if (NULL == filename)
130     goto iteration_cont;
131   len = strlen (filename);
132   if (len < 5)                  /* log file: `pid'.dat */
133     goto iteration_cont;
134   fn = filename + len;
135   if (0 != strcasecmp (".dat", fn - 4))
136     goto iteration_cont;
137   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs,
138                                           GNUNET_NO, GNUNET_YES))
139     goto iteration_cont;
140   if ((BSIZE * 2) != fs)        /* The file size should be equal to what we
141                                    have written */
142     goto iteration_cont;
143   
144   cancel = GNUNET_YES;
145   result = GNUNET_OK;
146     
147  iteration_cont:
148   if ( (NULL != di) && 
149        (GNUNET_YES == GNUNET_DISK_directory_iterator_next (di, cancel)) )
150     return;
151   shutdown_now ();
152 }
153
154 /**
155  * Functions of this type are called to notify a successful transmission of the
156  * message to the logger service
157  *
158  * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
159  * @param size the amount of data sent
160  */
161 static void
162 flush_comp (void *cls, size_t size)
163 {
164   FAIL_TEST (&write_task == cls, return);
165   FAIL_TEST ((BSIZE * 2) == size, return);
166   FAIL_TEST (GNUNET_OK == GNUNET_TESTING_peer_stop (peer), return);
167   FAIL_TEST (GNUNET_YES == GNUNET_DISK_directory_iterator_start
168              (GNUNET_SCHEDULER_PRIORITY_DEFAULT, search_dir,
169               &iterator_cb, NULL), return);
170 }
171
172
173 static void
174 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 {
176   static int i;
177   char buf[BSIZE];
178
179   write_task = GNUNET_SCHEDULER_NO_TASK;
180   if (0 == i)
181     write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_write, NULL);
182   (void) memset (buf, i, BSIZE);
183   GNUNET_TESTBED_LOGGER_write (h, buf, BSIZE);
184   if (0 == i++)
185     return;
186   GNUNET_TESTBED_LOGGER_flush (h, &flush_comp, &write_task);
187 }
188
189
190 /**
191  * Signature of the 'main' function for a (single-peer) testcase that
192  * is run using 'GNUNET_TESTING_peer_run'.
193  * 
194  * @param cls closure
195  * @param cfg configuration of the peer that was started
196  * @param peer identity of the peer that was created
197  */
198 static void
199 test_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
200            struct GNUNET_TESTING_Peer *p)
201 {
202   FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)), return);
203   FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
204              (cfg, "testbed-logger", "dir", &search_dir), return);
205   search_dir = GNUNET_CONFIGURATION_expand_dollar (cfg, search_dir);
206   peer = p;
207   write_task = GNUNET_SCHEDULER_add_now (&do_write, NULL);
208   abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
209                                              &do_abort, NULL);
210 }
211
212
213 /**
214  * Main function
215  */
216 int
217 main (int argc, char **argv)
218 {
219   int ret;
220
221   result = GNUNET_SYSERR;
222   ret = GNUNET_TESTING_service_run ("test-testbed-logger",
223                                     "testbed-logger",
224                                     "test_testbed_logger_api.conf",
225                                     &test_main,
226                                     NULL);
227   if (0 != ret)
228     return 1;
229   if (GNUNET_OK != result)
230     return 2;
231   return 0;
232 }