65e8b779751379c19a83c1a3ad9cf355d83361a8
[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);
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_and_free ((struct GNUNET_ARM_Handle *) cls);
118 }
119
120
121 void
122 arm_conn (void *cls, struct GNUNET_ARM_Handle *arm, char connected)
123 {
124   if (GNUNET_SYSERR == connected)
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 (GNUNET_YES == 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", status,
161              service);
162     GNUNET_break (0);
163     ok = 2;
164 #if START_ARM
165     GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, NULL, NULL);
166 #endif
167   }
168 }
169
170 static void
171 arm_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
172 {
173   /* (0) The request should be "sent" successfully
174    * ("sent", because it isn't going anywhere, ARM API starts ARM service
175    * by itself).
176    * ARM API should report that ARM service is starting.
177    */
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);
182   phase++;
183 }
184
185 static void
186 task (void *cls, char *const *args, const char *cfgfile,
187       const struct GNUNET_CONFIGURATION_Handle *c)
188 {
189   char *armconfig;
190   cfg = c;
191   if (NULL != cfgfile)
192   {
193     if (GNUNET_OK !=
194         GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
195                                                &armconfig))
196     {
197       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
198                                               *) cfg, "arm", "CONFIG",
199                                              cfgfile);
200     }
201     else
202       GNUNET_free (armconfig);
203   }
204   arm = GNUNET_ARM_connect (cfg, arm_conn, NULL);
205   if (NULL == arm)
206     return;
207 #if START_ARM
208   GNUNET_ARM_request_service_start (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
209 #else
210   arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_STARTING);
211   arm_conn (NULL, arm, GNUNET_YES);
212 #endif
213 }
214
215
216
217 static int
218 check ()
219 {
220   char *const argv[] = {
221     "test-arm-api",
222     "-c", "test_arm_api_data.conf",
223     NULL
224   };
225   struct GNUNET_GETOPT_CommandLineOption options[] = {
226     GNUNET_GETOPT_OPTION_END
227   };
228   GNUNET_assert (GNUNET_OK ==
229                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
230                                      argv, "test-arm-api", "nohelp", options,
231                                      &task, NULL));
232   return ok;
233 }
234
235 int
236 main (int argc, char *argv[])
237 {
238   int ret;
239
240
241   GNUNET_log_setup ("test-arm-api",
242                     "WARNING",
243                     NULL);
244   ret = check ();
245
246   return ret;
247 }
248
249 /* end of test_arm_api.c */