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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file testbed-logger/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,
218 * Signature of the 'main' function for a (single-peer) testcase that
219 * is run using #GNUNET_TESTING_peer_run().
222 * @param cfg configuration of the peer that was started
223 * @param peer identity of the peer that was created
226 test_main (void *cls,
227 const struct GNUNET_CONFIGURATION_Handle *cfg,
228 struct GNUNET_TESTING_Peer *p)
230 LOG (GNUNET_ERROR_TYPE_DEBUG,
231 "Connecting to logger\n");
232 FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)),
234 FAIL_TEST (GNUNET_OK ==
235 GNUNET_CONFIGURATION_get_value_filename (cfg,
241 write_task = GNUNET_SCHEDULER_add_now (&do_write,
243 abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
253 main (int argc, char **argv)
257 result = GNUNET_SYSERR;
258 GNUNET_log_setup ("test-testbed-logger-api",
261 GNUNET_break (GNUNET_OK ==
262 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
263 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
265 "test_testbed_logger_api.conf",
268 GNUNET_break (GNUNET_OK ==
269 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
272 if (GNUNET_OK != result)