2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file lockmanager/test_lockmanager_api.c
23 * @brief Test cases for lockmanager_api.c
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_lockmanager_service.h"
35 #define LOG(kind,...) \
36 GNUNET_log_from (kind, "test-lockmanager-api",__VA_ARGS__)
38 #define TIME_REL_SECONDS(min) \
39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
47 * The process id of the GNUNET ARM process
49 static struct GNUNET_OS_Process *arm_pid = NULL;
52 * Configuration Handle
54 static struct GNUNET_CONFIGURATION_Handle *config;
57 * The handle to the lockmanager service
59 static struct GNUNET_LOCKMANAGER_Handle *handle;
64 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
67 * Abort task identifier
69 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
75 * @param tc the task context
78 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
80 if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
82 GNUNET_SCHEDULER_cancel (abort_task_id);
83 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
86 GNUNET_LOCKMANAGER_disconnect (handle);
87 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
89 LOG (GNUNET_ERROR_TYPE_DEBUG,
90 "Kill gnunet-service-arm manually\n");
92 GNUNET_OS_process_wait (arm_pid);
93 GNUNET_OS_process_close (arm_pid);
96 GNUNET_CONFIGURATION_destroy (config);
98 if (GNUNET_SYSERR != result)
107 * @param tc the task context
110 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
112 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
113 result = GNUNET_SYSERR;
114 do_shutdown (cls, tc);
118 * Callback for lock status changes
120 * @param cls the closure from GNUNET_LOCKMANAGER_lock call
122 * @param domain_name the locking domain of the lock
124 * @param lock the lock for which this status is relevant
126 * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
127 * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
130 status_cb (void *cls,
131 const char *domain_name,
133 enum GNUNET_LOCKMANAGER_Status status)
137 GNUNET_LOCKMANAGER_cancel_request (request);
140 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
150 * @param tc the task context
153 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 handle = GNUNET_LOCKMANAGER_connect (config);
156 GNUNET_assert (NULL != handle);
158 request = GNUNET_LOCKMANAGER_acquire_lock (handle,
159 "GNUNET_LOCKMANAGER_TESTING",
163 abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
170 * Main point of test execution
173 run (void *cls, char *const *args, const char *cfgfile,
174 const struct GNUNET_CONFIGURATION_Handle *cfg)
176 config = GNUNET_CONFIGURATION_dup (cfg);
178 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
179 "gnunet-service-arm",
183 "-c", "test_lockmanager_api.conf", NULL);
185 GNUNET_assert (NULL != arm_pid);
186 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
195 int main (int argc, char **argv)
199 char *const argv2[] = { "test-lockmanager-api",
200 "-c", "test_lockmanager_api.conf",
207 struct GNUNET_GETOPT_CommandLineOption options[] = {
208 GNUNET_GETOPT_OPTION_END
212 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
213 "test-lockmanager-api", "nohelp", options, &run, NULL);
215 if (GNUNET_OK != ret)
217 LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
221 if (GNUNET_SYSERR == result)
223 LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
226 LOG (GNUNET_ERROR_TYPE_INFO, "test ok\n");