- test cases for testbed logger
[oweals/gnunet.git] / src / testbed / test_testbed_logger_api.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file testbed/test_testbed_logger_api.c
23  * @brief testcases for the testbed logger api
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_testbed_logger_service.h"
31
32 /**
33  * Generic logging shortcut
34  */
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 /**
39  * Relative time seconds shorthand
40  */
41 #define TIME_REL_SECS(sec) \
42   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
43
44 /**
45  * Opaque handle for the logging service
46  */
47 struct GNUNET_TESTBED_LOGGER_Handle *h;
48
49 /**
50  * Abort task identifier
51  */
52 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
53 static GNUNET_SCHEDULER_TaskIdentifier write_task;
54
55 static int result;
56
57 #define CANCEL_TASK(task) do {                  \
58     if (GNUNET_SCHEDULER_NO_TASK != task) \
59     {                                           \
60       GNUNET_SCHEDULER_cancel (task);     \
61       task = GNUNET_SCHEDULER_NO_TASK;    \
62     }                                           \
63   } while (0)
64
65 /**
66  * shortcut to exit during failure
67  */
68 #define FAIL_TEST(cond, ret) do {                               \
69     if (!(cond)) {                                              \
70       GNUNET_break(0);                                          \
71       CANCEL_TASK (abort_task);                                 \
72       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
73       ret;                                                      \
74     }                                                           \
75   } while (0)
76
77 /**
78  * Shutdown nicely
79  *
80  * @param cls NULL
81  * @param tc the task context
82  */
83 static void
84 shutdown_now ()
85 {
86   CANCEL_TASK (abort_task);
87   CANCEL_TASK (write_task);
88   GNUNET_SCHEDULER_shutdown ();
89 }
90
91
92 static void
93 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 {
95   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
96   abort_task = GNUNET_SCHEDULER_NO_TASK;
97   shutdown_now ();
98 }
99
100
101 #define BSIZE 1024
102
103 /**
104  * Functions of this type are called to notify a successful transmission of the
105  * message to the logger service
106  *
107  * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
108  * @param size the amount of data sent
109  */
110 static void
111 flush_comp (void *cls, size_t size)
112 {
113   FAIL_TEST (&write_task == cls, return);
114   FAIL_TEST ((BSIZE * 2) == size, return);
115   result = GNUNET_OK;
116   shutdown_now ();
117 }
118
119
120 static void
121 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 {
123   static int i;
124   char buf[BSIZE];
125
126   write_task = GNUNET_SCHEDULER_NO_TASK;
127   if (0 == i)
128     write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_write, NULL);
129   (void) memset (buf, i, BSIZE);
130   GNUNET_TESTBED_LOGGER_write (h, buf, BSIZE);
131   if (0 == i++)
132     return;
133   GNUNET_TESTBED_LOGGER_flush (h, &flush_comp, &write_task);
134 }
135
136
137 /**
138  * Signature of the 'main' function for a (single-peer) testcase that
139  * is run using 'GNUNET_TESTING_peer_run'.
140  * 
141  * @param cls closure
142  * @param cfg configuration of the peer that was started
143  * @param peer identity of the peer that was created
144  */
145 static void
146 test_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
147            struct GNUNET_TESTING_Peer *peer)
148 {
149   FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)), return);
150   write_task = GNUNET_SCHEDULER_add_now (&do_write, NULL);
151   abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
152                                              &do_abort, NULL);
153 }
154
155
156 /**
157  * Main function
158  */
159 int
160 main (int argc, char **argv)
161 {
162   int ret;
163
164   result = GNUNET_SYSERR;
165   ret = GNUNET_TESTING_service_run ("test-testbed-logger",
166                                     "testbed-logger",
167                                     "test_testbed_logger_api.conf",
168                                     &test_main,
169                                     NULL);
170   if (0 != ret)
171     return 1;
172   if (GNUNET_OK != result)
173     return 2;
174   return 0;
175 }