- fix 2699
[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 void
46 arm_stopped (void *cls,  enum GNUNET_ARM_ProcessStatus success)
47 {
48   GNUNET_break (success == GNUNET_ARM_PROCESS_DOWN);
49   if (success != GNUNET_ARM_PROCESS_DOWN)
50     ok = 3;
51   else if (ok == 1)
52     ok = 0;
53 }
54
55
56 static void
57 arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus success)
58 {
59   GNUNET_break (success == GNUNET_ARM_PROCESS_DOWN);
60 #if START_ARM
61   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
62 #endif
63 }
64
65
66 static void
67 dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
68 {
69   if (addr == NULL)
70     {
71       if (ok != 0)
72         {
73           GNUNET_break (0);
74           ok = 2;
75         }
76       GNUNET_ARM_stop_service (arm, "resolver", TIMEOUT, &arm_notify_stop,
77                                NULL);
78       return;
79     }
80   GNUNET_break (addr != NULL);
81   ok = 0;
82 }
83
84
85 static void
86 resolver_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
87 {
88   if (success != GNUNET_ARM_PROCESS_STARTING)
89     {
90       GNUNET_break (0);
91       ok = 2;
92 #if START_ARM
93       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
94 #endif
95       return;
96     }
97   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
98 }
99
100
101 static void
102 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
103 {
104   if (success != GNUNET_ARM_PROCESS_STARTING)
105     {
106       GNUNET_break (0);
107       ok = 2;
108 #if START_ARM
109       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
110 #endif
111     }
112   GNUNET_ARM_start_service (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, &resolver_notify,
113                             NULL);
114 }
115
116
117 static void
118 task (void *cls, char *const *args, const char *cfgfile,
119       const struct GNUNET_CONFIGURATION_Handle *c)
120 {
121   char *armconfig;
122   cfg = c;
123   if (NULL != cfgfile)
124   {
125     if (GNUNET_OK !=
126         GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
127                                                &armconfig))
128     {
129       GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG",
130                                              cfgfile);
131     }
132     else
133       GNUNET_free (armconfig);
134   }
135   arm = GNUNET_ARM_connect (cfg, NULL);
136 #if START_ARM
137   GNUNET_ARM_start_service (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, &arm_notify, NULL);
138 #else
139   arm_notify (NULL, GNUNET_YES);
140 #endif
141 }
142
143
144
145 static int
146 check ()
147 {
148   char *const argv[] = {
149     "test-arm-api",
150     "-c", "test_arm_api_data.conf",
151     NULL
152   };
153   struct GNUNET_GETOPT_CommandLineOption options[] = {
154     GNUNET_GETOPT_OPTION_END
155   };
156   GNUNET_assert (GNUNET_OK ==
157                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
158                                      argv, "test-arm-api", "nohelp", options,
159                                      &task, NULL));
160   return ok;
161 }
162
163 int
164 main (int argc, char *argv[])
165 {
166   int ret;
167
168
169   GNUNET_log_setup ("test-arm-api",
170                     "WARNING",
171                     NULL);
172   ret = check ();
173
174   return ret;
175 }
176
177 /* end of test_arm_api.c */