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 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.
16 * @file testbed-logger/test_testbed_logger_api.c
17 * @brief testcases for the testbed logger api
18 * @author Sree Harsha Totakura
21 #include "gnunet_util_lib.h"
22 #include "gnunet_testing_lib.h"
23 #include "gnunet_testbed_logger_service.h"
26 * Generic logging shortcut
28 #define LOG(kind,...) \
29 GNUNET_log (kind, __VA_ARGS__)
32 * Relative time seconds shorthand
34 #define TIME_REL_SECS(sec) \
35 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
38 * Opaque handle for the logging service
40 static struct GNUNET_TESTBED_LOGGER_Handle *h;
42 static struct GNUNET_TESTING_Peer *peer;
44 static char *search_dir;
47 * Abort task identifier
49 static struct GNUNET_SCHEDULER_Task *abort_task;
50 static struct GNUNET_SCHEDULER_Task *write_task;
54 #define CANCEL_TASK(task) do { \
57 GNUNET_SCHEDULER_cancel (task); \
63 * shortcut to exit during failure
65 #define FAIL_TEST(cond, ret) do { \
68 CANCEL_TASK (abort_task); \
69 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
79 * @param tc the task context
84 CANCEL_TASK (abort_task);
85 CANCEL_TASK (write_task);
86 GNUNET_free_non_null (search_dir);
88 GNUNET_TESTBED_LOGGER_disconnect (h);
89 GNUNET_SCHEDULER_shutdown ();
96 LOG (GNUNET_ERROR_TYPE_WARNING,
107 * Function called to iterate over a directory.
110 * @param filename complete filename (absolute path)
111 * @return #GNUNET_OK to continue to iterate,
112 * #GNUNET_NO to stop iteration with no error,
113 * #GNUNET_SYSERR to abort iteration with error!
116 iterator_cb (void *cls,
117 const char *filename)
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Iterator sees file %s\n",
126 len = strlen (filename);
128 if (0 != strcasecmp (".dat", fn - 4))
131 GNUNET_DISK_file_size (filename,
136 LOG (GNUNET_ERROR_TYPE_DEBUG,
137 "Failed to obtain file size for file %s\n",
139 return GNUNET_SYSERR;
141 if ((BSIZE * 2) != fs)
143 LOG (GNUNET_ERROR_TYPE_DEBUG,
144 "Unexpected file size for file %s\n",
146 /* The file size should be equal to what we
148 return GNUNET_SYSERR;
156 * Functions of this type are called to notify a successful
157 * transmission of the message to the logger service
159 * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
160 * @param size the amount of data sent
163 flush_comp (void *cls,
166 LOG (GNUNET_ERROR_TYPE_DEBUG,
168 FAIL_TEST (&write_task == cls,
170 FAIL_TEST ((BSIZE * 2) == size,
172 FAIL_TEST (GNUNET_OK ==
173 GNUNET_TESTING_peer_stop (peer),
175 LOG (GNUNET_ERROR_TYPE_DEBUG,
176 "Peer stopped, scanning %s\n",
178 FAIL_TEST (GNUNET_SYSERR !=
179 GNUNET_DISK_directory_scan (search_dir,
194 LOG (GNUNET_ERROR_TYPE_DEBUG,
195 "Write task running\n");
197 write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
200 (void) memset (buf, i, BSIZE);
201 GNUNET_TESTBED_LOGGER_write (h,
206 GNUNET_TESTBED_LOGGER_flush (h,
213 * Signature of the 'main' function for a (single-peer) testcase that
214 * is run using #GNUNET_TESTING_peer_run().
217 * @param cfg configuration of the peer that was started
218 * @param peer identity of the peer that was created
221 test_main (void *cls,
222 const struct GNUNET_CONFIGURATION_Handle *cfg,
223 struct GNUNET_TESTING_Peer *p)
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "Connecting to logger\n");
227 FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)),
229 FAIL_TEST (GNUNET_OK ==
230 GNUNET_CONFIGURATION_get_value_filename (cfg,
236 write_task = GNUNET_SCHEDULER_add_now (&do_write,
238 abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
248 main (int argc, char **argv)
252 result = GNUNET_SYSERR;
253 GNUNET_log_setup ("test-testbed-logger-api",
256 GNUNET_break (GNUNET_OK ==
257 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
258 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
260 "test_testbed_logger_api.conf",
263 GNUNET_break (GNUNET_OK ==
264 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
267 if (GNUNET_OK != result)