-typo
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_servercrash.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_servercrash.c
23  * @brief Test cases for lockmanager_api where the server crashes
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_lockmanager_service.h"
30 #include "gnunet_testing_lib-new.h"
31
32 /**
33  * Generic logging shorthand
34  */
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 /**
39  * Relative seconds shorthand
40  */
41 #define TIME_REL_SECONDS(min)                                   \
42   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
43
44 /**
45  * Various steps of the test
46  */
47 enum Test
48   {
49     /**
50      * Signal test failure
51      */
52     TEST_FAIL,
53
54     /**
55      * Testing just began
56      */
57     TEST_INIT,
58
59     /**
60      * Client 1 has got the lock successfully; Client 2 should try to acquire
61      * the lock now; after some time client 1 has to release the lock
62      */
63     TEST_CLIENT1_LOCK_SUCCESS,
64
65     /**
66      * Client 2 has got the lock; Server should crash now;
67      */
68     TEST_CLIENT2_LOCK_SUCCESS,
69
70     /**
71      * Client 2 should get lock release due to server crash; Should call
72      * shutdown now
73      */
74     TEST_CLIENT2_SERVER_CRASH_SUCCESS
75   };
76
77 /**
78  * The testing result
79  */
80 static enum Test result;
81
82 /**
83  * Configuration Handle
84  */
85 static const struct GNUNET_CONFIGURATION_Handle *config;
86
87 /**
88  * The handle to the lockmanager service
89  */
90 static struct GNUNET_LOCKMANAGER_Handle *handle;
91
92 /**
93  * A second client handle to the lockmanager service
94  */
95 static struct GNUNET_LOCKMANAGER_Handle *handle2;
96
97 /**
98  * The locking request
99  */
100 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
101
102 /**
103  * The locking request of second client
104  */
105 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
106
107 /**
108  * Abort task identifier
109  */
110 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
111
112 /**
113  * Our peer
114  */
115 static struct GNUNET_TESTING_Peer *self;
116
117
118 /**
119  * Shutdown nicely
120  *
121  * @param cls
122  * @param tc the task context
123  */
124 static void
125 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
128   {
129     GNUNET_SCHEDULER_cancel (abort_task_id);
130     abort_task_id = GNUNET_SCHEDULER_NO_TASK;
131   }
132   if (NULL != handle)
133     GNUNET_LOCKMANAGER_disconnect (handle);
134   if (NULL != handle2)
135     GNUNET_LOCKMANAGER_disconnect (handle2);
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   switch (result)
177   {
178   case TEST_INIT:
179     GNUNET_assert (handle == cls);
180     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
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     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
194     result = TEST_CLIENT2_LOCK_SUCCESS;
195     /* We should stop our peer to simulate crash in lockmanager service */
196     GNUNET_TESTING_peer_stop (self);
197     break;
198   case TEST_CLIENT2_LOCK_SUCCESS:
199     GNUNET_assert (handle2 == cls);
200     GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
201     GNUNET_assert (99 == lock);
202     GNUNET_assert (0 == strcmp (domain_name, "GNUNET_LOCKMANAGER_TESTING"));
203     result = TEST_CLIENT2_SERVER_CRASH_SUCCESS;
204     GNUNET_LOCKMANAGER_cancel_request (request2);
205     request2 = NULL;
206     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
207                                   &do_shutdown,
208                                   NULL);
209     break;
210   default:
211     GNUNET_assert (0);          /* We should never reach here */
212   }
213 }
214
215
216 /**
217  * Main point of test execution
218  */
219 static void
220 run (void *cls,
221      const struct GNUNET_CONFIGURATION_Handle *cfg,
222      struct GNUNET_TESTING_Peer *peer)
223 {
224   config = cfg;
225   self = peer;
226   result = TEST_INIT;
227   handle = GNUNET_LOCKMANAGER_connect (config);
228   GNUNET_assert (NULL != handle);
229   handle2 = GNUNET_LOCKMANAGER_connect (config);
230   
231   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
232                                              "GNUNET_LOCKMANAGER_TESTING",
233                                              99,
234                                              &status_cb,
235                                              handle);
236   GNUNET_assert (NULL != request);
237   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
238                                                 &do_abort,
239                                                 NULL);
240 }
241
242
243 /**
244  * Main function
245  */
246 int main (int argc, char **argv)
247 {
248   if (0 != GNUNET_TESTING_peer_run ("test_lockmanager_api_servercrash",
249                                     "test_lockmanager_api.conf",
250                                     &run, NULL))
251     return 1;
252   return (TEST_CLIENT2_SERVER_CRASH_SUCCESS != result) ? 1 : 0;
253 }