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.
22 * @file testbed/test_testbed_logger_api.c
23 * @brief testcases for the testbed logger api
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_testbed_logger_service.h"
33 * Generic logging shortcut
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
39 * Relative time seconds shorthand
41 #define TIME_REL_SECS(sec) \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
45 * Opaque handle for the logging service
47 static struct GNUNET_TESTBED_LOGGER_Handle *h;
49 static struct GNUNET_TESTING_Peer *peer;
51 static char *search_dir;
54 * Abort task identifier
56 static struct GNUNET_SCHEDULER_Task * abort_task;
57 static struct GNUNET_SCHEDULER_Task * write_task;
61 #define CANCEL_TASK(task) do { \
64 GNUNET_SCHEDULER_cancel (task); \
70 * shortcut to exit during failure
72 #define FAIL_TEST(cond, ret) do { \
75 CANCEL_TASK (abort_task); \
76 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
85 * @param tc the task context
90 CANCEL_TASK (abort_task);
91 CANCEL_TASK (write_task);
92 GNUNET_free_non_null (search_dir);
94 GNUNET_TESTBED_LOGGER_disconnect (h);
95 GNUNET_SCHEDULER_shutdown ();
102 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
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 len = strlen (filename);
129 if (len < 5) /* log file: `pid'.dat */
133 if (0 != strcasecmp (".dat", fn - 4))
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
141 return GNUNET_SYSERR;
147 * Functions of this type are called to notify a successful
148 * transmission of the message to the logger service
150 * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
151 * @param size the amount of data sent
154 flush_comp (void *cls, size_t size)
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,
176 write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
179 (void) memset (buf, i, BSIZE);
180 GNUNET_TESTBED_LOGGER_write (h, buf, BSIZE);
183 GNUNET_TESTBED_LOGGER_flush (h,
184 GNUNET_TIME_UNIT_FOREVER_REL,
185 &flush_comp, &write_task);
190 * Signature of the 'main' function for a (single-peer) testcase that
191 * is run using #GNUNET_TESTING_peer_run().
194 * @param cfg configuration of the peer that was started
195 * @param peer identity of the peer that was created
198 test_main (void *cls,
199 const struct GNUNET_CONFIGURATION_Handle *cfg,
200 struct GNUNET_TESTING_Peer *p)
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);
206 write_task = GNUNET_SCHEDULER_add_now (&do_write, NULL);
207 abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
216 main (int argc, char **argv)
220 result = GNUNET_SYSERR;
221 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
223 "test_testbed_logger_api.conf",
228 if (GNUNET_OK != result)