-rps: open channel when inserting peer in view
[oweals/gnunet.git] / src / testbed / 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
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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * abort_task;
57 static struct GNUNET_SCHEDULER_Task * write_task;
58
59 static int result;
60
61 #define CANCEL_TASK(task) do {                  \
62     if (NULL != task) \
63     {                                           \
64       GNUNET_SCHEDULER_cancel (task);     \
65       task = NULL;    \
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)
101 {
102   LOG (GNUNET_ERROR_TYPE_WARNING, "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   len = strlen (filename);
129   if (len < 5)                  /* log file: `pid'.dat */
130     return GNUNET_OK;
131
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, &fs,
137                              GNUNET_NO, GNUNET_YES))
138     return GNUNET_SYSERR;
139   if ((BSIZE * 2) != fs)        /* The file size should be equal to what we
140                                    have written */
141     return GNUNET_SYSERR;
142   return GNUNET_OK;
143 }
144
145
146 /**
147  * Functions of this type are called to notify a successful
148  * transmission of the message to the logger service
149  *
150  * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
151  * @param size the amount of data sent
152  */
153 static void
154 flush_comp (void *cls, size_t size)
155 {
156   FAIL_TEST (&write_task == cls, return);
157   FAIL_TEST ((BSIZE * 2) == size, return);
158   FAIL_TEST (GNUNET_OK == GNUNET_TESTING_peer_stop (peer), return);
159   FAIL_TEST (GNUNET_SYSERR !=
160              GNUNET_DISK_directory_scan (search_dir,
161                                          &iterator_cb,
162                                          NULL),
163              return);
164   shutdown_now ();
165 }
166
167
168 static void
169 do_write (void *cls)
170 {
171   static int i;
172   char buf[BSIZE];
173
174   write_task = NULL;
175   if (0 == i)
176     write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
177                                                &do_write,
178                                                NULL);
179   (void) memset (buf, i, BSIZE);
180   GNUNET_TESTBED_LOGGER_write (h, buf, BSIZE);
181   if (0 == i++)
182     return;
183   GNUNET_TESTBED_LOGGER_flush (h,
184                                GNUNET_TIME_UNIT_FOREVER_REL,
185                                &flush_comp, &write_task);
186 }
187
188
189 /**
190  * Signature of the 'main' function for a (single-peer) testcase that
191  * is run using #GNUNET_TESTING_peer_run().
192  *
193  * @param cls closure
194  * @param cfg configuration of the peer that was started
195  * @param peer identity of the peer that was created
196  */
197 static void
198 test_main (void *cls,
199            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_filename
204              (cfg, "testbed-logger", "dir", &search_dir), return);
205   peer = p;
206   write_task = GNUNET_SCHEDULER_add_now (&do_write, NULL);
207   abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
208                                              &do_abort, NULL);
209 }
210
211
212 /**
213  * Main function
214  */
215 int
216 main (int argc, char **argv)
217 {
218   int ret;
219
220   result = GNUNET_SYSERR;
221   ret = GNUNET_TESTING_service_run ("test-testbed-logger",
222                                     "testbed-logger",
223                                     "test_testbed_logger_api.conf",
224                                     &test_main,
225                                     NULL);
226   if (0 != ret)
227     return 1;
228   if (GNUNET_OK != result)
229     return 2;
230   return 0;
231 }