handling replies continuously from server
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 2, 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 lockmanager/test_lockmanager_api.c
23  * @brief Test cases for lockmanager_api.c
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_lockmanager_service.h"
30
31 #define VERBOSE GNUNET_YES
32
33 #define VERBOSE_ARM 1
34
35 #define LOG(kind,...) \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 #define TIME_REL_SECONDS(min) \
39   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
40
41
42 /**
43  * Enumeration of testing steps
44  */
45 enum Test
46   {
47     TEST_FAIL,
48
49     TEST_INIT,
50
51     LOCK1_ACQUIRE,
52
53     LOCK2_ACQUIRE
54   };
55
56
57 /**
58  * The testing result
59  */
60 static enum Test result;
61
62 /**
63  * The process id of the GNUNET ARM process
64  */
65 static struct GNUNET_OS_Process *arm_pid = NULL;
66
67 /**
68  * Configuration Handle
69  */
70 static struct GNUNET_CONFIGURATION_Handle *config;
71
72 /**
73  * The handle to the lockmanager service
74  */
75 static struct GNUNET_LOCKMANAGER_Handle *handle;
76
77 /**
78  * The locking request
79  */
80 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
81
82 /**
83  * The second locking request
84  */
85 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
86
87 /**
88  * Abort task identifier
89  */
90 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
91
92 /**
93  * Shutdown nicely
94  *
95  * @param cls
96  * @param tc the task context
97  */
98 static void
99 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
102     {
103       GNUNET_SCHEDULER_cancel (abort_task_id);
104       abort_task_id = GNUNET_SCHEDULER_NO_TASK;
105     }
106   if (NULL != request)
107     GNUNET_LOCKMANAGER_cancel_request (request);
108   if (NULL != request2)
109     GNUNET_LOCKMANAGER_cancel_request (request2);
110   GNUNET_LOCKMANAGER_disconnect (handle);
111   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
112     {
113       LOG (GNUNET_ERROR_TYPE_DEBUG,
114            "Kill gnunet-service-arm manually\n");
115     }
116   GNUNET_OS_process_wait (arm_pid);
117   GNUNET_OS_process_destroy (arm_pid);
118
119   if (NULL != config)
120     GNUNET_CONFIGURATION_destroy (config);
121 }
122
123
124 /**
125  * Shutdown nicely
126  *
127  * @param cls
128  * @param tc the task context
129  */
130 static void
131 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
132 {
133   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
134   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
135   result = TEST_FAIL;
136   do_shutdown (cls, tc);
137 }
138
139 /**
140  * Callback for lock status changes
141  *
142  * @param cls the closure from GNUNET_LOCKMANAGER_lock call
143  *
144  * @param domain_name the locking domain of the lock 
145  *
146  * @param lock the lock for which this status is relevant
147  *
148  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
149  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
150  */
151 static void 
152 status_cb (void *cls,
153            const char *domain_name,
154            uint32_t lock,
155            enum GNUNET_LOCKMANAGER_Status status)
156 {
157   LOG (GNUNET_ERROR_TYPE_DEBUG,
158        "Status change callback called on lock: %d of domain: %s\n",
159        lock, domain_name);
160   switch (result)
161   {
162   case LOCK1_ACQUIRE:
163     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
164     GNUNET_assert (NULL != request);
165     //GNUNET_LOCKMANAGER_cancel_request (request);
166     //request = NULL;
167     result = LOCK2_ACQUIRE;
168     request2 = GNUNET_LOCKMANAGER_acquire_lock (handle,
169                                                 "GNUNET_LOCKMANAGER_TESTING",
170                                                 100,
171                                                 &status_cb,
172                                                 NULL);
173     GNUNET_assert (NULL != request2);
174     break;
175   case LOCK2_ACQUIRE:
176     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
177     GNUNET_assert (NULL != request);
178     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
179                                   &do_shutdown,
180                                   NULL);
181     break;
182   default:
183     GNUNET_break (0);
184   } 
185 }
186
187
188 /**
189  * Testing function
190  *
191  * @param cls NULL
192  * @param tc the task context
193  */
194 static void
195 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {  
197   handle = GNUNET_LOCKMANAGER_connect (config);
198   GNUNET_assert (NULL != handle);
199   result = LOCK1_ACQUIRE;
200   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
201                                              "GNUNET_LOCKMANAGER_TESTING",
202                                              99,
203                                              &status_cb,
204                                              NULL);
205   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
206                                                 &do_abort,
207                                                 NULL);
208 }
209
210
211 /**
212  * Main point of test execution
213  */
214 static void
215 run (void *cls, char *const *args, const char *cfgfile,
216      const struct GNUNET_CONFIGURATION_Handle *cfg)
217 {
218   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
219   config = GNUNET_CONFIGURATION_dup (cfg);
220   arm_pid = 
221     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
222                              "gnunet-service-arm",
223 #if VERBOSE_ARM
224                              "-L", "DEBUG",
225 #endif
226                              "-c", "test_lockmanager_api.conf", NULL);
227
228   GNUNET_assert (NULL != arm_pid);
229   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
230                                 &test,
231                                 NULL);
232 }
233
234
235 /**
236  * Main function
237  */
238 int main (int argc, char **argv)
239 {
240   int ret;
241
242   char *const argv2[] = { "test-lockmanager-api",
243                           "-c", "test_lockmanager_api.conf",
244 #if VERBOSE
245                           "-L", "DEBUG",
246 #endif
247                           NULL
248   };
249   
250   struct GNUNET_GETOPT_CommandLineOption options[] = {
251     GNUNET_GETOPT_OPTION_END
252   };
253   
254   GNUNET_log_setup ("test-lockmanager-api",
255 #if VERBOSE
256                     "DEBUG",
257 #else
258                     "WARNING",
259 #endif
260                     NULL);
261
262   ret =
263     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
264                         "test-lockmanager-api", "nohelp", options, &run, NULL);
265
266   if (GNUNET_OK != ret)
267   {
268     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
269          ret);
270     return 1;
271   }
272   if (TEST_FAIL == result)
273   {
274     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
275     return 1;
276   }
277   LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");
278   return 0;
279 }