-logging conventions
[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 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  * 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  * Abort task identifier
68  */
69 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
70
71 /**
72  * Shutdown nicely
73  *
74  * @param cls
75  * @param tc the task context
76  */
77 static void
78 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
81     {
82       GNUNET_SCHEDULER_cancel (abort_task_id);
83       abort_task_id = GNUNET_SCHEDULER_NO_TASK;
84     }
85   
86   GNUNET_LOCKMANAGER_disconnect (handle);
87   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
88     {
89       LOG (GNUNET_ERROR_TYPE_DEBUG,
90            "Kill gnunet-service-arm manually\n");
91     }
92   GNUNET_OS_process_wait (arm_pid);
93   GNUNET_OS_process_close (arm_pid);
94
95   if (NULL != config)
96     GNUNET_CONFIGURATION_destroy (config);
97
98   if (GNUNET_SYSERR != result)
99     result = GNUNET_OK;
100 }
101
102
103 /**
104  * Shutdown nicely
105  *
106  * @param cls
107  * @param tc the task context
108  */
109 static void
110 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
113   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
114   result = GNUNET_SYSERR;
115   do_shutdown (cls, tc);
116 }
117
118 /**
119  * Callback for lock status changes
120  *
121  * @param cls the closure from GNUNET_LOCKMANAGER_lock call
122  *
123  * @param domain_name the locking domain of the lock 
124  *
125  * @param lock the lock for which this status is relevant
126  *
127  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
128  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
129  */
130 static void 
131 status_cb (void *cls,
132            const char *domain_name,
133            uint32_t lock,
134            enum GNUNET_LOCKMANAGER_Status status)
135 {
136   LOG (GNUNET_ERROR_TYPE_DEBUG,
137        "Status change callback called on lock: %d of domain: %s\n",
138        lock, domain_name);
139   GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
140   GNUNET_assert (NULL != request);
141   GNUNET_LOCKMANAGER_cancel_request (request);
142   request = NULL;
143   
144   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
145                                 &do_shutdown,
146                                 NULL);
147 }
148
149
150 /**
151  * Testing function
152  *
153  * @param cls NULL
154  * @param tc the task context
155  */
156 static void
157 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 {  
159   handle = GNUNET_LOCKMANAGER_connect (config);
160   GNUNET_assert (NULL != handle);
161   
162   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
163                                              "GNUNET_LOCKMANAGER_TESTING",
164                                              99,
165                                              &status_cb,
166                                              NULL);
167   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
168                                                 &do_abort,
169                                                 NULL);
170 }
171
172
173 /**
174  * Main point of test execution
175  */
176 static void
177 run (void *cls, char *const *args, const char *cfgfile,
178      const struct GNUNET_CONFIGURATION_Handle *cfg)
179 {
180   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
181   config = GNUNET_CONFIGURATION_dup (cfg);
182   arm_pid = 
183     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
184                              "gnunet-service-arm",
185 #if VERBOSE_ARM
186                              "-L", "DEBUG",
187 #endif
188                              "-c", "test_lockmanager_api.conf", NULL);
189
190   GNUNET_assert (NULL != arm_pid);
191   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
192                                 &test,
193                                 NULL);
194 }
195
196
197 /**
198  * Main function
199  */
200 int main (int argc, char **argv)
201 {
202   int ret;
203
204   char *const argv2[] = { "test-lockmanager-api",
205                           "-c", "test_lockmanager_api.conf",
206 #if VERBOSE
207                           "-L", "DEBUG",
208 #endif
209                           NULL
210   };
211   
212   struct GNUNET_GETOPT_CommandLineOption options[] = {
213     GNUNET_GETOPT_OPTION_END
214   };
215   
216   GNUNET_log_setup ("test-lockmanager-api",
217 #if VERBOSE
218                     "DEBUG",
219 #else
220                     "WARNING",
221 #endif
222                     NULL);
223
224   ret =
225     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
226                         "test-lockmanager-api", "nohelp", options, &run, NULL);
227
228   if (GNUNET_OK != ret)
229   {
230     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
231          ret);
232     return 1;
233   }
234   if (GNUNET_SYSERR == result)
235   {
236     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
237     return 1;
238   }
239   LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");
240   return 0;
241 }