-hashmap for storing lockingrequests in lockmanager
[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 1
32
33 #define VERBOSE_ARM 1
34
35 #define LOG(kind,...) \
36   GNUNET_log_from (kind, "test-lockmanager-api",__VA_ARGS__)
37
38 #define TIME_REL_SECONDS(min) \
39   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
40
41 /**
42  * The testing result
43  */
44 static int result;
45
46 /**
47  * The process id of the GNUNET ARM process
48  */
49 static struct GNUNET_OS_Process *arm_pid = NULL;
50
51 /**
52  * Configuration Handle
53  */
54 static struct GNUNET_CONFIGURATION_Handle *config;
55
56 /**
57  * The handle to the lockmanager service
58  */
59 static struct GNUNET_LOCKMANAGER_Handle *handle;
60
61 /**
62  * The locking request
63  */
64 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
65
66 /**
67  * Shutdown nicely
68  *
69  * @param cls
70  * @param tc the task context
71  */
72 static void
73 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   if (NULL != request)
76     {
77       GNUNET_LOCKMANAGER_cancel_request (request);
78       request = NULL;
79     }
80   GNUNET_LOCKMANAGER_disconnect (handle);
81   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
82     {
83       LOG (GNUNET_ERROR_TYPE_DEBUG,
84            "Kill gnunet-service-arm manually\n");
85     }
86   GNUNET_OS_process_wait (arm_pid);
87   GNUNET_OS_process_close (arm_pid);
88   if (GNUNET_SYSERR != result)
89     result = GNUNET_OK;
90 }
91
92
93 /**
94  * Shutdown nicely
95  *
96  * @param cls
97  * @param tc the task context
98  */
99 static void
100 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   result = GNUNET_SYSERR;
103   do_shutdown (cls, tc);
104 }
105
106 /**
107  * Callback for lock status changes
108  *
109  * @param cls the closure from GNUNET_LOCKMANAGER_lock call
110  *
111  * @param domain_name the locking domain of the lock 
112  *
113  * @param lock the lock for which this status is relevant
114  *
115  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
116  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
117  */
118 static void 
119 status_cb (void *cls,
120            const char *domain_name,
121            uint32_t lock,
122            enum GNUNET_LOCKMANAGER_Status status)
123 {
124   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
125                                 &do_shutdown,
126                                 NULL);
127 }
128
129
130 /**
131  * Testing function
132  *
133  * @param cls NULL
134  * @param tc the task context
135  */
136 static void
137 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {  
139   handle = GNUNET_LOCKMANAGER_connect (config);
140   GNUNET_assert (NULL != handle);
141   
142   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
143                                              "GNUNET_LOCKMANAGER_TESTING",
144                                              99,
145                                              &status_cb,
146                                              NULL);
147   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
148                                 &do_abort,
149                                 NULL);
150 }
151
152
153 /**
154  * Main point of test execution
155  */
156 static void
157 run (void *cls, char *const *args, const char *cfgfile,
158      const struct GNUNET_CONFIGURATION_Handle *cfg)
159 {
160   config = GNUNET_CONFIGURATION_dup (cfg);
161   arm_pid = 
162     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
163                              "gnunet-service-arm",
164 #if VERBOSE_ARM
165                              "-L", "DEBUG",
166 #endif
167                              "-c", "test_lockmanager_api.conf", NULL);
168
169   GNUNET_assert (NULL != arm_pid);
170   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
171                                 &test,
172                                 NULL);
173 }
174
175
176 /**
177  * Main function
178  */
179 int main (int argc, char **argv)
180 {
181   int ret;
182
183   char *const argv2[] = { "test-lockmanager-api",
184                           "-c", "test_lockmanager_api.conf",
185 #if VERBOSE
186                           "-L", "DEBUG",
187 #endif
188                           NULL
189   };
190   
191   struct GNUNET_GETOPT_CommandLineOption options[] = {
192     GNUNET_GETOPT_OPTION_END
193   };
194
195   ret =
196     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
197                         "test-lockmanager-api", "nohelp", options, &run, NULL);
198
199   if (GNUNET_OK != ret)
200   {
201     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
202          ret);
203     return 1;
204   }
205   if (GNUNET_SYSERR == result)
206   {
207     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
208     return 1;
209   }
210   LOG (GNUNET_ERROR_TYPE_INFO, "test ok\n");
211   return 0;
212 }