lockmanager testcase new testing library
[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 #include "gnunet_testing_lib-new.h"
31
32 /**
33  * Generic logging shortcut
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  * Configuration Handle
64  */
65 static const struct GNUNET_CONFIGURATION_Handle *config;
66
67 /**
68  * The handle to the lockmanager service
69  */
70 static struct GNUNET_LOCKMANAGER_Handle *handle;
71
72 /**
73  * The locking request
74  */
75 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
76
77 /**
78  * The second locking request
79  */
80 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
81
82 /**
83  * Abort task identifier
84  */
85 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
86
87 /**
88  * Shutdown nicely
89  *
90  * @param cls
91  * @param tc the task context
92  */
93 static void
94 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
97     {
98       GNUNET_SCHEDULER_cancel (abort_task_id);
99       abort_task_id = GNUNET_SCHEDULER_NO_TASK;
100     }
101   if (NULL != request)
102     GNUNET_LOCKMANAGER_cancel_request (request);
103   if (NULL != request2)
104     GNUNET_LOCKMANAGER_cancel_request (request2);
105   GNUNET_LOCKMANAGER_disconnect (handle);
106   GNUNET_SCHEDULER_shutdown ();
107 }
108
109
110 /**
111  * Shutdown nicely
112  *
113  * @param cls
114  * @param tc the task context
115  */
116 static void
117 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
120   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
121   result = TEST_FAIL;
122   do_shutdown (cls, tc);
123 }
124
125 /**
126  * Callback for lock status changes
127  *
128  * @param cls the closure from GNUNET_LOCKMANAGER_lock call
129  *
130  * @param domain_name the locking domain of the lock 
131  *
132  * @param lock the lock for which this status is relevant
133  *
134  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
135  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
136  */
137 static void 
138 status_cb (void *cls,
139            const char *domain_name,
140            uint32_t lock,
141            enum GNUNET_LOCKMANAGER_Status status)
142 {
143   LOG (GNUNET_ERROR_TYPE_DEBUG,
144        "Status change callback called on lock: %d of domain: %s\n",
145        lock, domain_name);
146   switch (result)
147   {
148   case LOCK1_ACQUIRE:
149     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
150     GNUNET_assert (NULL != request);
151     //GNUNET_LOCKMANAGER_cancel_request (request);
152     //request = NULL;
153     result = LOCK2_ACQUIRE;
154     request2 = GNUNET_LOCKMANAGER_acquire_lock (handle,
155                                                 "GNUNET_LOCKMANAGER_TESTING",
156                                                 100,
157                                                 &status_cb,
158                                                 NULL);
159     GNUNET_assert (NULL != request2);
160     break;
161   case LOCK2_ACQUIRE:
162     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
163     GNUNET_assert (NULL != request);
164     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
165                                   &do_shutdown,
166                                   NULL);
167     break;
168   default:
169     GNUNET_break (0);
170   } 
171 }
172
173
174 /**
175  * Main point of test execution
176  */
177 static void
178 run (void *cls,
179      const struct GNUNET_CONFIGURATION_Handle *cfg,
180      struct GNUNET_TESTING_Peer *peer)
181 {
182   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
183   config = cfg;
184   handle = GNUNET_LOCKMANAGER_connect (config);
185   GNUNET_assert (NULL != handle);
186   result = LOCK1_ACQUIRE;
187   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
188                                              "GNUNET_LOCKMANAGER_TESTING",
189                                              99,
190                                              &status_cb,
191                                              NULL);
192   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (30),
193                                                 &do_abort,
194                                                 NULL);
195 }
196
197
198 /**
199  * Main function
200  */
201 int main (int argc, char **argv)
202 {
203
204   if (0 != GNUNET_TESTING_peer_run ("test_lockmanager_api",
205                                     "test_lockmanager_api.conf",
206                                     &run, NULL))
207     return 1;
208   return (TEST_FAIL == result) ? 1 : 0;
209 }
210
211 /* end of test_lockmanager_api.c */