use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / arm / test_arm_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
32
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35 static struct GNUNET_ARM_Handle *arm;
36
37 static struct GNUNET_ARM_Operation *op;
38
39 static int ok = 1;
40
41 static int phase = 0;
42
43
44 static void
45 arm_stop_cb (void *cls,
46              enum GNUNET_ARM_RequestStatus status,
47              enum GNUNET_ARM_Result result)
48 {
49   op = NULL;
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_STOPPED);
54   GNUNET_break (phase == 6);
55   phase++;
56   LOG ("Sent 'STOP' request for arm to ARM %s\n",
57        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" :
58        "unsuccessfully");
59   GNUNET_SCHEDULER_shutdown ();
60 }
61
62
63 static void
64 resolver_stop_cb (void *cls,
65                   enum GNUNET_ARM_RequestStatus status,
66                   enum GNUNET_ARM_Result result)
67 {
68   op = NULL;
69   /* (5), a stop request should be sent to ARM successfully.
70    * ARM should report that resolver is stopped.
71    */
72   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
73   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
74   GNUNET_break (phase == 5);
75   LOG ("Sent 'STOP' request for resolver to ARM %s\n",
76        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" :
77        "unsuccessfully");
78   phase++;
79   GNUNET_assert (NULL == op);
80   op = GNUNET_ARM_request_service_stop (arm,
81                                         "arm",
82                                         &arm_stop_cb,
83                                         NULL);
84 }
85
86
87 static void
88 dns_notify (void *cls,
89             const struct sockaddr *addr,
90             socklen_t addrlen)
91 {
92   if (addr == NULL)
93   {
94     /* (4), resolver should finish resolving localhost */
95     GNUNET_break (phase == 4);
96     phase++;
97     LOG ("Finished resolving localhost\n");
98     if (ok != 0)
99       ok = 2;
100     GNUNET_assert (NULL == op);
101     op = GNUNET_ARM_request_service_stop (arm,
102                                           "resolver",
103                                           &resolver_stop_cb,
104                                           NULL);
105     return;
106   }
107   /* (3), resolver should resolve localhost */
108   GNUNET_break (phase == 3);
109   LOG ("Resolved localhost\n");
110   phase++;
111   GNUNET_break (addr != NULL);
112   ok = 0;
113 }
114
115
116 static void
117 resolver_start_cb (void *cls,
118                    enum GNUNET_ARM_RequestStatus status,
119                    enum GNUNET_ARM_Result result)
120 {
121   op = NULL;
122   /* (2), the start request for resolver should be sent successfully
123    * ARM should report that resolver service is starting.
124    */
125   GNUNET_assert (status == GNUNET_ARM_REQUEST_SENT_OK);
126   GNUNET_break (phase == 2);
127   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
128   LOG ("Sent 'START' request for resolver to ARM %s\n",
129        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" :
130        "unsuccessfully");
131   phase++;
132   GNUNET_RESOLVER_ip_get ("localhost",
133                           AF_INET,
134                           TIMEOUT,
135                           &dns_notify, NULL);
136 }
137
138
139 static void
140 arm_conn (void *cls,
141           int connected)
142 {
143   if (GNUNET_SYSERR == connected)
144   {
145     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146                 _ ("Fatal error initializing ARM API.\n"));
147     GNUNET_SCHEDULER_shutdown ();
148     GNUNET_assert (0);
149     return;
150   }
151   if (GNUNET_YES == connected)
152   {
153     /* (1), arm connection should be established */
154     LOG ("Connected to ARM\n");
155     GNUNET_break (phase == 1);
156     phase++;
157     GNUNET_assert (NULL == op);
158     op = GNUNET_ARM_request_service_start (arm,
159                                            "resolver",
160                                            GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
161                                            &resolver_start_cb,
162                                            NULL);
163   }
164   else
165   {
166     /* (7), ARM should stop (we disconnect from it) */
167     LOG ("Disconnected from ARM\n");
168     GNUNET_break (phase == 7);
169     if (phase != 7)
170       ok = 3;
171     else if (ok == 1)
172       ok = 0;
173   }
174 }
175
176
177 static void
178 arm_start_cb (void *cls,
179               enum GNUNET_ARM_RequestStatus status,
180               enum GNUNET_ARM_Result result)
181 {
182   op = NULL;
183   /* (0) The request should be "sent" successfully
184    * ("sent", because it isn't going anywhere, ARM API starts ARM service
185    * by itself).
186    * ARM API should report that ARM service is starting.
187    */GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
188   GNUNET_break (phase == 0);
189   LOG ("Sent 'START' request for arm to ARM %s\n",
190        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" :
191        "unsuccessfully");
192   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
193   phase++;
194 }
195
196
197 static void
198 do_shutdown (void *cls)
199 {
200   if (NULL != op)
201   {
202     GNUNET_ARM_operation_cancel (op);
203     op = NULL;
204   }
205   if (NULL != arm)
206   {
207     GNUNET_ARM_disconnect (arm);
208     arm = NULL;
209   }
210 }
211
212
213 static void
214 task (void *cls,
215       char *const *args,
216       const char *cfgfile,
217       const struct GNUNET_CONFIGURATION_Handle *c)
218 {
219   cfg = c;
220   arm = GNUNET_ARM_connect (cfg,
221                             &arm_conn,
222                             NULL);
223   if (NULL == arm)
224     return;
225   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
226                                  NULL);
227   op = GNUNET_ARM_request_service_start (arm,
228                                          "arm",
229                                          GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
230                                          &arm_start_cb,
231                                          NULL);
232 }
233
234
235 int
236 main (int argc, char *argvx[])
237 {
238   char *const argv[] = {
239     "test-arm-api",
240     "-c", "test_arm_api_data.conf",
241     NULL
242   };
243   struct GNUNET_GETOPT_CommandLineOption options[] = {
244     GNUNET_GETOPT_OPTION_END
245   };
246
247   GNUNET_log_setup ("test-arm-api",
248                     "WARNING",
249                     NULL);
250   GNUNET_assert (GNUNET_OK ==
251                  GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
252                                      argv, "test-arm-api", "nohelp", options,
253                                      &task, NULL));
254   return ok;
255 }
256
257
258 /* end of test_arm_api.c */