2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
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.
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.
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/>.
19 * @file testbed-logger/test_testbed_logger_api.c
20 * @brief testcases for the testbed logger api
21 * @author Sree Harsha Totakura
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_testbed_logger_service.h"
29 * Generic logging shortcut
31 #define LOG(kind,...) \
32 GNUNET_log (kind, __VA_ARGS__)
35 * Relative time seconds shorthand
37 #define TIME_REL_SECS(sec) \
38 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
41 * Opaque handle for the logging service
43 static struct GNUNET_TESTBED_LOGGER_Handle *h;
45 static struct GNUNET_TESTING_Peer *peer;
47 static char *search_dir;
50 * Abort task identifier
52 static struct GNUNET_SCHEDULER_Task *abort_task;
53 static struct GNUNET_SCHEDULER_Task *write_task;
57 #define CANCEL_TASK(task) do { \
60 GNUNET_SCHEDULER_cancel (task); \
66 * shortcut to exit during failure
68 #define FAIL_TEST(cond, ret) do { \
71 CANCEL_TASK (abort_task); \
72 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
82 * @param tc the task context
87 CANCEL_TASK (abort_task);
88 CANCEL_TASK (write_task);
89 GNUNET_free_non_null (search_dir);
91 GNUNET_TESTBED_LOGGER_disconnect (h);
92 GNUNET_SCHEDULER_shutdown ();
99 LOG (GNUNET_ERROR_TYPE_WARNING,
110 * Function called to iterate over a directory.
113 * @param filename complete filename (absolute path)
114 * @return #GNUNET_OK to continue to iterate,
115 * #GNUNET_NO to stop iteration with no error,
116 * #GNUNET_SYSERR to abort iteration with error!
119 iterator_cb (void *cls,
120 const char *filename)
126 LOG (GNUNET_ERROR_TYPE_DEBUG,
127 "Iterator sees file %s\n",
129 len = strlen (filename);
131 if (0 != strcasecmp (".dat", fn - 4))
134 GNUNET_DISK_file_size (filename,
139 LOG (GNUNET_ERROR_TYPE_DEBUG,
140 "Failed to obtain file size for file %s\n",
142 return GNUNET_SYSERR;
144 if ((BSIZE * 2) != fs)
146 LOG (GNUNET_ERROR_TYPE_DEBUG,
147 "Unexpected file size for file %s\n",
149 /* The file size should be equal to what we
151 return GNUNET_SYSERR;
159 * Functions of this type are called to notify a successful
160 * transmission of the message to the logger service
162 * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
163 * @param size the amount of data sent
166 flush_comp (void *cls,
169 LOG (GNUNET_ERROR_TYPE_DEBUG,
171 FAIL_TEST (&write_task == cls,
173 FAIL_TEST ((BSIZE * 2) == size,
175 FAIL_TEST (GNUNET_OK ==
176 GNUNET_TESTING_peer_stop (peer),
178 LOG (GNUNET_ERROR_TYPE_DEBUG,
179 "Peer stopped, scanning %s\n",
181 FAIL_TEST (GNUNET_SYSERR !=
182 GNUNET_DISK_directory_scan (search_dir,
197 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "Write task running\n");
200 write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
203 (void) memset (buf, i, BSIZE);
204 GNUNET_TESTBED_LOGGER_write (h,
209 GNUNET_TESTBED_LOGGER_flush (h,
216 * Signature of the 'main' function for a (single-peer) testcase that
217 * is run using #GNUNET_TESTING_peer_run().
220 * @param cfg configuration of the peer that was started
221 * @param peer identity of the peer that was created
224 test_main (void *cls,
225 const struct GNUNET_CONFIGURATION_Handle *cfg,
226 struct GNUNET_TESTING_Peer *p)
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Connecting to logger\n");
230 FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)),
232 FAIL_TEST (GNUNET_OK ==
233 GNUNET_CONFIGURATION_get_value_filename (cfg,
239 write_task = GNUNET_SCHEDULER_add_now (&do_write,
241 abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
251 main (int argc, char **argv)
255 result = GNUNET_SYSERR;
256 GNUNET_log_setup ("test-testbed-logger-api",
259 GNUNET_break (GNUNET_OK ==
260 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
261 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
263 "test_testbed_logger_api.conf",
266 GNUNET_break (GNUNET_OK ==
267 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
270 if (GNUNET_OK != result)