-fix dht flags
[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, const char *domain_name, uint32_t lock,
139            enum GNUNET_LOCKMANAGER_Status status)
140 {
141   LOG (GNUNET_ERROR_TYPE_DEBUG,
142        "Status change callback called on lock: %d of domain: %s\n", lock,
143        domain_name);
144   switch (result)
145   {
146   case LOCK1_ACQUIRE:
147     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
148     GNUNET_assert (NULL != request);
149     //GNUNET_LOCKMANAGER_cancel_request (request);
150     //request = NULL;
151     result = LOCK2_ACQUIRE;
152     request2 =
153         GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING",
154                                          100, &status_cb, NULL);
155     GNUNET_assert (NULL != request2);
156     break;
157   case LOCK2_ACQUIRE:
158     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
159     GNUNET_assert (NULL != request);
160     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), &do_shutdown, NULL);
161     break;
162   default:
163     GNUNET_break (0);
164   }
165 }
166
167
168 /**
169  * Main point of test execution
170  */
171 static void
172 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
173      struct GNUNET_TESTING_Peer *peer)
174 {
175   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
176   config = cfg;
177   handle = GNUNET_LOCKMANAGER_connect (config);
178   GNUNET_assert (NULL != handle);
179   result = LOCK1_ACQUIRE;
180   request =
181       GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING", 99,
182                                        &status_cb, NULL);
183   abort_task_id =
184       GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (30), &do_abort, NULL);
185 }
186
187
188 /**
189  * Main function
190  */
191 int
192 main (int argc, char **argv)
193 {
194
195   if (0 !=
196       GNUNET_TESTING_peer_run ("test_lockmanager_api",
197                                "test_lockmanager_api.conf", &run, NULL))
198     return 1;
199   return (TEST_FAIL == result) ? 1 : 0;
200 }
201
202 /* end of test_lockmanager_api.c */