2 This file is part of GNUnet.
3 (C) 2009, 2011 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 3, 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.
21 * @file arm/test_arm_api.c
22 * @brief testcase for arm_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_client_lib.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_resolver_service.h"
33 #define START_ARM GNUNET_YES
35 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1500)
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41 static struct GNUNET_ARM_Handle *arm;
48 arm_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
50 /* (6), a stop request should be sent to ARM successfully */
51 /* ARM should report that it is stopping */
52 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
53 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPING);
54 GNUNET_break (phase == 6);
56 FPRINTF (stderr, "Sent 'STOP' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
60 resolver_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
62 /* (5), a stop request should be sent to ARM successfully.
63 * ARM should report that resolver is stopped.
65 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
66 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
67 GNUNET_break (phase == 5);
68 FPRINTF (stderr, "Sent 'STOP' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
71 GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
73 arm_stop_cb (NULL, GNUNET_ARM_STATUS_SENT_OK, "arm", GNUNET_ARM_SERVICE_STOPPING);
74 arm_conn (NULL, GNUNET_NO);
79 dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
83 /* (4), resolver should finish resolving localhost */
84 GNUNET_break (phase == 4);
86 FPRINTF (stderr, "%s", "Finished resolving localhost\n");
89 GNUNET_ARM_request_service_stop (arm, "resolver", TIMEOUT, resolver_stop_cb, NULL);
92 /* (3), resolver should resolve localhost */
93 GNUNET_break (phase == 3);
94 FPRINTF (stderr, "%s", "Resolved localhost\n");
96 GNUNET_break (addr != NULL);
101 resolver_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
103 /* (2), the start request for resolver should be sent successfully
104 * ARM should report that resolver service is starting.
106 GNUNET_assert (status == GNUNET_ARM_REQUEST_SENT_OK);
107 GNUNET_break (phase == 2);
108 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
109 FPRINTF (stderr, "Sent 'START' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
111 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
115 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_ARM_disconnect_and_free ((struct GNUNET_ARM_Handle *) cls);
122 arm_conn (void *cls, struct GNUNET_ARM_Handle *arm, char connected)
124 if (GNUNET_SYSERR == connected)
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 _("Fatal error initializing ARM API.\n"));
128 GNUNET_SCHEDULER_shutdown ();
132 if (GNUNET_YES == connected)
134 /* (1), arm connection should be established */
135 FPRINTF (stderr, "%s", "Connected to ARM\n");
136 GNUNET_break (phase == 1);
138 GNUNET_ARM_request_service_start (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, resolver_start_cb, NULL);
142 /* (7), ARM should stop (we disconnect from it) */
143 FPRINTF (stderr, "%s", "Disconnected from ARM\n");
144 GNUNET_break (phase == 7);
149 GNUNET_SCHEDULER_add_now (trigger_disconnect, arm);
154 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
156 FPRINTF (stderr, "Service %s is %u\n", service, status);
160 FPRINTF (stderr, "Unexpectedly got status %u for service %s\n", status,
165 GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, NULL, NULL);
171 arm_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
173 /* (0) The request should be "sent" successfully
174 * ("sent", because it isn't going anywhere, ARM API starts ARM service
176 * ARM API should report that ARM service is starting.
178 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
179 GNUNET_break (phase == 0);
180 FPRINTF (stderr, "Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
181 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
186 task (void *cls, char *const *args, const char *cfgfile,
187 const struct GNUNET_CONFIGURATION_Handle *c)
194 GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
197 GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
198 *) cfg, "arm", "CONFIG",
202 GNUNET_free (armconfig);
204 arm = GNUNET_ARM_connect (cfg, arm_conn, NULL);
208 GNUNET_ARM_request_service_start (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
210 arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_STARTING);
211 arm_conn (NULL, arm, GNUNET_YES);
220 char *const argv[] = {
222 "-c", "test_arm_api_data.conf",
225 struct GNUNET_GETOPT_CommandLineOption options[] = {
226 GNUNET_GETOPT_OPTION_END
228 GNUNET_assert (GNUNET_OK ==
229 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
230 argv, "test-arm-api", "nohelp", options,
236 main (int argc, char *argv[])
241 GNUNET_log_setup ("test-arm-api",
249 /* end of test_arm_api.c */