Fix arm service message handler declaration
[oweals/gnunet.git] / src / arm / test_arm_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2011 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 3, 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  * @file arm/test_arm_api.c
22  * @brief testcase for arm_api.c
23  */
24 #include "platform.h"
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"
32
33 #define START_ARM GNUNET_YES
34
35 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1500)
36
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
38
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 static struct GNUNET_ARM_Handle *arm;
42
43 static int ok = 1;
44
45 static int phase = 0;
46
47 static void
48 arm_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
49 {
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);
55   phase++;
56   FPRINTF (stderr, "Sent 'STOP' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
57 }
58
59 static void
60 resolver_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
61 {
62   /* (5), a stop request should be sent to ARM successfully.
63    * ARM should report that resolver is stopped.
64    */
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");
69   phase++;
70 #if START_ARM
71   GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
72 #else
73   arm_stop_cb (NULL, GNUNET_ARM_STATUS_SENT_OK, "arm", GNUNET_ARM_SERVICE_STOPPING);
74   arm_conn (NULL, GNUNET_NO, GNUNET_NO);
75 #endif
76 }
77
78 static void
79 dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
80 {
81   if (addr == NULL)
82     {
83       /* (4), resolver should finish resolving localhost */
84       GNUNET_break (phase == 4);
85       phase++;
86       FPRINTF (stderr, "%s", "Finished resolving localhost\n");
87       if (ok != 0)
88         ok = 2;
89       GNUNET_ARM_request_service_stop (arm, "resolver", TIMEOUT, resolver_stop_cb, NULL);
90       return;
91     }
92   /* (3), resolver should resolve localhost */
93   GNUNET_break (phase == 3);
94   FPRINTF (stderr, "%s", "Resolved localhost\n");
95   phase++;
96   GNUNET_break (addr != NULL);
97   ok = 0;
98 }
99
100 static void
101 resolver_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
102 {
103   /* (2), the start request for resolver should be sent successfully
104    * ARM should report that resolver service is starting.
105    */
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");
110   phase++;
111   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
112 }
113
114 static void
115 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   GNUNET_ARM_disconnect ((struct GNUNET_ARM_Handle *) cls);
118 }
119
120
121 void
122 arm_conn (void *cls, struct GNUNET_ARM_Handle *arm, unsigned char connected, unsigned char error)
123 {
124   if (GNUNET_YES == error)
125   {
126     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127                 _("Fatal error initializing ARM API.\n"));
128     GNUNET_SCHEDULER_shutdown ();
129     GNUNET_assert (0);
130     return;
131   }
132   if (connected)
133   {
134     /* (1), arm connection should be established */
135     FPRINTF (stderr, "%s", "Connected to ARM\n");
136     GNUNET_break (phase == 1);
137     phase++;
138     GNUNET_ARM_request_service_start (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, resolver_start_cb, NULL);
139   }
140   else
141   {
142     /* (7), ARM should stop (we disconnect from it) */
143     FPRINTF (stderr, "%s", "Disconnected from ARM\n");
144     GNUNET_break (phase == 7);
145     if (phase != 7)
146       ok = 3;
147     else if (ok == 1)
148       ok = 0;
149     GNUNET_SCHEDULER_add_now (trigger_disconnect, arm);
150   }
151 }
152
153 void
154 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
155 {
156   FPRINTF (stderr, "Service %s is %u\n", service, status);
157   switch (phase)
158   {
159   default:
160     FPRINTF (stderr, "Unexpectedly got status %u for service %s\n", service);
161     GNUNET_break (0);
162     ok = 2;
163 #if START_ARM
164     GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, NULL, NULL);
165 #endif
166   }
167 }
168
169 static void
170 arm_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
171 {
172   /* (0) The request should be "sent" successfully
173    * ("sent", because it isn't going anywhere, ARM API starts ARM service
174    * by itself).
175    * ARM API should report that ARM service is starting.
176    */
177   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
178   GNUNET_break (phase == 0);
179   FPRINTF (stderr, "Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
180   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
181   phase++;
182 }
183
184 static void
185 task (void *cls, char *const *args, const char *cfgfile,
186       const struct GNUNET_CONFIGURATION_Handle *c)
187 {
188   char *armconfig;
189   cfg = c;
190   if (NULL != cfgfile)
191   {
192     if (GNUNET_OK !=
193         GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
194                                                &armconfig))
195     {
196       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
197                                               *) cfg, "arm", "CONFIG",
198                                              cfgfile);
199     }
200     else
201       GNUNET_free (armconfig);
202   }
203   arm = GNUNET_ARM_alloc (cfg);
204   GNUNET_ARM_connect (arm, arm_conn, NULL);
205 #if START_ARM
206   GNUNET_ARM_request_service_start (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
207 #else
208   arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_STARTING);
209   arm_conn (NULL, GNUNET_YES, GNUNET_NO);
210 #endif
211 }
212
213
214
215 static int
216 check ()
217 {
218   char *const argv[] = {
219     "test-arm-api",
220     "-c", "test_arm_api_data.conf",
221     NULL
222   };
223   struct GNUNET_GETOPT_CommandLineOption options[] = {
224     GNUNET_GETOPT_OPTION_END
225   };
226   GNUNET_assert (GNUNET_OK ==
227                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
228                                      argv, "test-arm-api", "nohelp", options,
229                                      &task, NULL));
230   return ok;
231 }
232
233 int
234 main (int argc, char *argv[])
235 {
236   int ret;
237
238
239   GNUNET_log_setup ("test-arm-api",
240                     "WARNING",
241                     NULL);
242   ret = check ();
243
244   return ret;
245 }
246
247 /* end of test_arm_api.c */