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
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.
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.
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.
21 * @file testbed/test_testbed_logger_api.c
22 * @brief testcases for the testbed logger api
23 * @author Sree Harsha Totakura
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_testbed_logger_service.h"
31 * Generic logging shortcut
33 #define LOG(kind,...) \
34 GNUNET_log (kind, __VA_ARGS__)
37 * Relative time seconds shorthand
39 #define TIME_REL_SECS(sec) \
40 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
43 * Opaque handle for the logging service
45 static struct GNUNET_TESTBED_LOGGER_Handle *h;
47 static struct GNUNET_TESTING_Peer *peer;
49 static char *search_dir;
52 * Abort task identifier
54 static struct GNUNET_SCHEDULER_Task *abort_task;
55 static struct GNUNET_SCHEDULER_Task *write_task;
59 #define CANCEL_TASK(task) do { \
62 GNUNET_SCHEDULER_cancel (task); \
68 * shortcut to exit during failure
70 #define FAIL_TEST(cond, ret) do { \
73 CANCEL_TASK (abort_task); \
74 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
84 * @param tc the task context
89 CANCEL_TASK (abort_task);
90 CANCEL_TASK (write_task);
91 GNUNET_free_non_null (search_dir);
93 GNUNET_TESTBED_LOGGER_disconnect (h);
94 GNUNET_SCHEDULER_shutdown ();
101 LOG (GNUNET_ERROR_TYPE_WARNING,
112 * Function called to iterate over a directory.
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!
121 iterator_cb (void *cls,
122 const char *filename)
128 LOG (GNUNET_ERROR_TYPE_DEBUG,
129 "Iterator sees file %s\n",
131 len = strlen (filename);
133 if (0 != strcasecmp (".dat", fn - 4))
136 GNUNET_DISK_file_size (filename,
141 LOG (GNUNET_ERROR_TYPE_DEBUG,
142 "Failed to obtain file size for file %s\n",
144 return GNUNET_SYSERR;
146 if ((BSIZE * 2) != fs)
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "Unexpected file size for file %s\n",
151 /* The file size should be equal to what we
153 return GNUNET_SYSERR;
161 * Functions of this type are called to notify a successful
162 * transmission of the message to the logger service
164 * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
165 * @param size the amount of data sent
168 flush_comp (void *cls,
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
173 FAIL_TEST (&write_task == cls,
175 FAIL_TEST ((BSIZE * 2) == size,
177 FAIL_TEST (GNUNET_OK ==
178 GNUNET_TESTING_peer_stop (peer),
180 LOG (GNUNET_ERROR_TYPE_DEBUG,
181 "Peer stopped, scanning %s\n",
183 FAIL_TEST (GNUNET_SYSERR !=
184 GNUNET_DISK_directory_scan (search_dir,
199 LOG (GNUNET_ERROR_TYPE_DEBUG,
200 "Write task running\n");
202 write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
205 (void) memset (buf, i, BSIZE);
206 GNUNET_TESTBED_LOGGER_write (h,
211 GNUNET_TESTBED_LOGGER_flush (h,
212 GNUNET_TIME_UNIT_FOREVER_REL,
219 * Signature of the 'main' function for a (single-peer) testcase that
220 * is run using #GNUNET_TESTING_peer_run().
223 * @param cfg configuration of the peer that was started
224 * @param peer identity of the peer that was created
227 test_main (void *cls,
228 const struct GNUNET_CONFIGURATION_Handle *cfg,
229 struct GNUNET_TESTING_Peer *p)
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 "Connecting to logger\n");
233 FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)),
235 FAIL_TEST (GNUNET_OK ==
236 GNUNET_CONFIGURATION_get_value_filename (cfg,
242 write_task = GNUNET_SCHEDULER_add_now (&do_write,
244 abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
254 main (int argc, char **argv)
258 result = GNUNET_SYSERR;
259 GNUNET_log_setup ("test-testbed-logger-api",
262 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
264 "test_testbed_logger_api.conf",
269 if (GNUNET_OK != result)