-remove trailing whitespace
[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_util_lib.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_resolver_service.h"
28
29 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
30
31 #define START_ARM GNUNET_YES
32
33 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1500)
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
36
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 static struct GNUNET_ARM_Handle *arm;
40
41 static int ok = 1;
42
43 static int phase = 0;
44
45 static void
46 arm_stop_cb (void *cls,
47              enum GNUNET_ARM_RequestStatus status,
48              const char *servicename,
49              enum GNUNET_ARM_Result result)
50 {
51   /* (6), a stop request should be sent to ARM successfully */
52   /* ARM should report that it is stopping */
53   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
54   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
55   GNUNET_break (phase == 6);
56   phase++;
57   LOG ("Sent 'STOP' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
58 }
59
60
61 static void
62 resolver_stop_cb (void *cls,
63                   enum GNUNET_ARM_RequestStatus status,
64                   const char *servicename, enum GNUNET_ARM_Result result)
65 {
66   /* (5), a stop request should be sent to ARM successfully.
67    * ARM should report that resolver is stopped.
68    */
69   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
70   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
71   GNUNET_break (phase == 5);
72   LOG ("Sent 'STOP' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
73   phase++;
74 #if START_ARM
75   GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
76 #else
77   arm_stop_cb (NULL, GNUNET_ARM_STATUS_SENT_OK, "arm", GNUNET_ARM_SERVICE_STOPPING);
78   arm_conn (NULL, GNUNET_NO);
79 #endif
80 }
81
82
83 static void
84 dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
85 {
86   if (addr == NULL)
87     {
88       /* (4), resolver should finish resolving localhost */
89       GNUNET_break (phase == 4);
90       phase++;
91       LOG ("Finished resolving localhost\n");
92       if (ok != 0)
93         ok = 2;
94       GNUNET_ARM_request_service_stop (arm, "resolver", TIMEOUT, resolver_stop_cb, NULL);
95       return;
96     }
97   /* (3), resolver should resolve localhost */
98   GNUNET_break (phase == 3);
99   LOG ("Resolved localhost\n");
100   phase++;
101   GNUNET_break (addr != NULL);
102   ok = 0;
103 }
104
105
106 static void
107 resolver_start_cb (void *cls,
108                    enum GNUNET_ARM_RequestStatus status,
109                    const char *servicename,
110                    enum GNUNET_ARM_Result result)
111 {
112   /* (2), the start request for resolver should be sent successfully
113    * ARM should report that resolver service is starting.
114    */
115   GNUNET_assert (status == GNUNET_ARM_REQUEST_SENT_OK);
116   GNUNET_break (phase == 2);
117   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
118   LOG ("Sent 'START' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
119   phase++;
120   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
121 }
122
123
124 static void
125 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   GNUNET_ARM_disconnect_and_free ((struct GNUNET_ARM_Handle *) cls);
128 }
129
130
131 static void
132 arm_conn (void *cls,
133           int connected)
134 {
135   if (GNUNET_SYSERR == connected)
136   {
137     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138                 _("Fatal error initializing ARM API.\n"));
139     GNUNET_SCHEDULER_shutdown ();
140     GNUNET_assert (0);
141     return;
142   }
143   if (GNUNET_YES == connected)
144   {
145     /* (1), arm connection should be established */
146     LOG ("Connected to ARM\n");
147     GNUNET_break (phase == 1);
148     phase++;
149     GNUNET_ARM_request_service_start (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, resolver_start_cb, NULL);
150   }
151   else
152   {
153     /* (7), ARM should stop (we disconnect from it) */
154     LOG ("Disconnected from ARM\n");
155     GNUNET_break (phase == 7);
156     if (phase != 7)
157       ok = 3;
158     else if (ok == 1)
159       ok = 0;
160     GNUNET_SCHEDULER_add_now (trigger_disconnect, arm);
161   }
162 }
163
164
165 static void
166 arm_start_cb (void *cls,
167               enum GNUNET_ARM_RequestStatus status,
168               const char *servicename,
169               enum GNUNET_ARM_Result result)
170 {
171   /* (0) The request should be "sent" successfully
172    * ("sent", because it isn't going anywhere, ARM API starts ARM service
173    * by itself).
174    * ARM API should report that ARM service is starting.
175    */
176   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
177   GNUNET_break (phase == 0);
178   LOG ("Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
179   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
180   phase++;
181 }
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_connect (cfg, &arm_conn, NULL);
204   if (NULL == arm)
205     return;
206 #if START_ARM
207   GNUNET_ARM_request_service_start (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
208 #else
209   arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_STARTING);
210   arm_conn (NULL, arm, GNUNET_YES);
211 #endif
212 }
213
214
215 int
216 main (int argc, char *argvx[])
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_log_setup ("test-arm-api",
227                     "WARNING",
228                     NULL);
229   GNUNET_assert (GNUNET_OK ==
230                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
231                                      argv, "test-arm-api", "nohelp", options,
232                                      &task, NULL));
233   return ok;
234 }
235
236 /* end of test_arm_api.c */