2 This file is part of GNUnet
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier abort_task;
57 static GNUNET_SCHEDULER_TaskIdentifier write_task;
61 #define CANCEL_TASK(task) do { \
62 if (GNUNET_SCHEDULER_NO_TASK != task) \
64 GNUNET_SCHEDULER_cancel (task); \
65 task = GNUNET_SCHEDULER_NO_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 ();
100 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
103 abort_task = GNUNET_SCHEDULER_NO_TASK;
112 * Function called to iterate over a directory.
115 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
116 * get called on the next entry (or finish cleanly);
117 * NULL on error (will be the last call in that case)
118 * @param filename complete filename (absolute path)
119 * @param dirname directory name (absolute path)
122 iterator_cb (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
123 const char *filename, const char *dirname)
131 if (NULL == filename)
133 len = strlen (filename);
134 if (len < 5) /* log file: `pid'.dat */
137 if (0 != strcasecmp (".dat", fn - 4))
139 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs,
140 GNUNET_NO, GNUNET_YES))
142 if ((BSIZE * 2) != fs) /* The file size should be equal to what we
151 (GNUNET_YES == GNUNET_DISK_directory_iterator_next (di, cancel)) )
157 * Functions of this type are called to notify a successful transmission of the
158 * message to the logger service
160 * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
161 * @param size the amount of data sent
164 flush_comp (void *cls, size_t size)
166 FAIL_TEST (&write_task == cls, return);
167 FAIL_TEST ((BSIZE * 2) == size, return);
168 FAIL_TEST (GNUNET_OK == GNUNET_TESTING_peer_stop (peer), return);
169 FAIL_TEST (GNUNET_YES == GNUNET_DISK_directory_iterator_start
170 (GNUNET_SCHEDULER_PRIORITY_DEFAULT, search_dir,
171 &iterator_cb, NULL), return);
176 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 write_task = GNUNET_SCHEDULER_NO_TASK;
183 write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_write, NULL);
184 (void) memset (buf, i, BSIZE);
185 GNUNET_TESTBED_LOGGER_write (h, buf, BSIZE);
188 GNUNET_TESTBED_LOGGER_flush (h, GNUNET_TIME_UNIT_FOREVER_REL,
189 &flush_comp, &write_task);
194 * Signature of the 'main' function for a (single-peer) testcase that
195 * is run using 'GNUNET_TESTING_peer_run'.
198 * @param cfg configuration of the peer that was started
199 * @param peer identity of the peer that was created
202 test_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
203 struct GNUNET_TESTING_Peer *p)
205 FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)), return);
206 FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename
207 (cfg, "testbed-logger", "dir", &search_dir), return);
209 write_task = GNUNET_SCHEDULER_add_now (&do_write, NULL);
210 abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
219 main (int argc, char **argv)
223 result = GNUNET_SYSERR;
224 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
226 "test_testbed_logger_api.conf",
231 if (GNUNET_OK != result)