-fix
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_lockrelease.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_lockrelease.c
23  * @brief Test cases for lockmanager_api where client disconnects abruptly
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  * Various steps of the test
43  */
44 enum Test
45   {
46     /**
47      * Signal test failure
48      */
49     TEST_FAIL,
50
51     /**
52      * Testing just began
53      */
54     TEST_INIT,
55
56     /**
57      * Client 1 has got the lock successfully; Client 2 should try to acquire
58      * the lock now; after some time client 1 has to release the lock
59      */
60     TEST_CLIENT1_LOCK_SUCCESS,
61
62     /**
63      * Client 2 has got the lock; Should release it and call shutdown
64      */
65     TEST_CLIENT2_LOCK_SUCCESS,
66   };
67
68 /**
69  * The testing result
70  */
71 static enum Test result;
72
73 /**
74  * The process id of the GNUNET ARM process
75  */
76 static struct GNUNET_OS_Process *arm_pid = NULL;
77
78 /**
79  * Configuration Handle
80  */
81 static struct GNUNET_CONFIGURATION_Handle *config;
82
83 /**
84  * The handle to the lockmanager service
85  */
86 static struct GNUNET_LOCKMANAGER_Handle *handle;
87
88 /**
89  * A second client handle to the lockmanager service
90  */
91 static struct GNUNET_LOCKMANAGER_Handle *handle2;
92
93 /**
94  * The locking request
95  */
96 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
97
98 /**
99  * The locking request of second client
100  */
101 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
102
103 /**
104  * Abort task identifier
105  */
106 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
107
108
109 /**
110  * Shutdown nicely
111  *
112  * @param cls
113  * @param tc the task context
114  */
115 static void
116 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
119   {
120     GNUNET_SCHEDULER_cancel (abort_task_id);
121     abort_task_id = GNUNET_SCHEDULER_NO_TASK;
122   }
123   
124   GNUNET_LOCKMANAGER_disconnect (handle);
125   GNUNET_LOCKMANAGER_disconnect (handle2);
126   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
127   {
128     LOG (GNUNET_ERROR_TYPE_DEBUG,
129          "Kill gnunet-service-arm manually\n");
130   }
131   GNUNET_OS_process_wait (arm_pid);
132   GNUNET_OS_process_destroy (arm_pid);
133
134   if (NULL != config)
135     GNUNET_CONFIGURATION_destroy (config);
136 }
137
138
139 /**
140  * Abort
141  *
142  * @param cls
143  * @param tc the task context
144  */
145 static void
146 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
149   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
150   result = TEST_FAIL;
151   do_shutdown (cls, tc);
152 }
153
154
155 /**
156  * Callback for lock status changes
157  *
158  * @param cls the handle
159  *
160  * @param domain_name the locking domain of the lock 
161  *
162  * @param lock the lock for which this status is relevant
163  *
164  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
165  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
166  */
167 static void 
168 status_cb (void *cls,
169            const char *domain_name,
170            uint32_t lock,
171            enum GNUNET_LOCKMANAGER_Status status)
172 {
173   LOG (GNUNET_ERROR_TYPE_DEBUG,
174        "Status change callback called on lock: %d of domain: %s\n",
175        lock, domain_name);
176   GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
177   switch (result)
178   {
179   case TEST_INIT:
180     GNUNET_assert (handle == cls);
181     result = TEST_CLIENT1_LOCK_SUCCESS;
182     request2 = GNUNET_LOCKMANAGER_acquire_lock (handle2,
183                                                 "GNUNET_LOCKMANAGER_TESTING",
184                                                 99,
185                                                 &status_cb,
186                                                 handle2);
187     GNUNET_assert (NULL != request2);
188     GNUNET_LOCKMANAGER_cancel_request (request);
189     request = NULL;
190     break;
191   case TEST_CLIENT1_LOCK_SUCCESS:
192     GNUNET_assert (handle2 == cls);
193     result = TEST_CLIENT2_LOCK_SUCCESS;
194     GNUNET_LOCKMANAGER_cancel_request (request2);
195     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
196                                   &do_shutdown,
197                                   NULL);
198     break;
199   default:
200     GNUNET_assert (0);          /* We should never reach here */
201   }
202
203 }
204
205
206 /**
207  * Testing function
208  *
209  * @param cls NULL
210  * @param tc the task context
211  */
212 static void
213 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214
215   result = TEST_INIT;
216   handle = GNUNET_LOCKMANAGER_connect (config);
217   GNUNET_assert (NULL != handle);
218   handle2 = GNUNET_LOCKMANAGER_connect (config);
219   
220   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
221                                              "GNUNET_LOCKMANAGER_TESTING",
222                                              99,
223                                              &status_cb,
224                                              handle);
225   GNUNET_assert (NULL != request);
226   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
227                                                 &do_abort,
228                                                 NULL);
229 }
230
231
232 /**
233  * Main point of test execution
234  */
235 static void
236 run (void *cls, char *const *args, const char *cfgfile,
237      const struct GNUNET_CONFIGURATION_Handle *cfg)
238 {
239   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
240   config = GNUNET_CONFIGURATION_dup (cfg);
241   arm_pid = 
242     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
243                              "gnunet-service-arm",
244 #if VERBOSE_ARM
245                              "-L", "DEBUG",
246 #endif
247                              "-c", "test_lockmanager_api.conf", NULL);
248
249   GNUNET_assert (NULL != arm_pid);
250   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (3),
251                                 &test,
252                                 NULL);
253 }
254
255
256 /**
257  * Main function
258  */
259 int main (int argc, char **argv)
260 {
261   int ret;
262
263   char *const argv2[] = { "test-lockmanager-api-lockrelease",
264                           "-c", "test_lockmanager_api.conf",
265 #if VERBOSE
266                           "-L", "DEBUG",
267 #endif
268                           NULL
269   };
270   
271   struct GNUNET_GETOPT_CommandLineOption options[] = {
272     GNUNET_GETOPT_OPTION_END
273   };
274   
275   GNUNET_log_setup ("test-lockmanager-api-lockrelease",
276 #if VERBOSE
277                     "DEBUG",
278 #else
279                     "WARNING",
280 #endif
281                     NULL);
282
283   ret =
284     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
285                         "test-lockmanager-api-lockrelease",
286                         "nohelp", options, &run, NULL);
287
288   if (GNUNET_OK != ret)
289   {
290     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
291          ret);
292     return 1;
293   }
294   if (TEST_CLIENT2_LOCK_SUCCESS != result)
295   {
296     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
297     return 1;
298   }
299   LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");
300   return 0;
301 }