doxy
[oweals/gnunet.git] / src / arm / test_gnunet_service_manager.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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_gnunet_service_manager.c (A mockup testcase, not functionally complete)
22  * @brief testcase for gnunet-service-manager.c
23  */
24
25 #include "platform.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_resolver_service.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30
31 /**
32  * Timeout for starting services, very short because of the strange way start works
33  * (by checking if running before starting, so really this time is always waited on
34  * startup (annoying)).
35  */
36 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
37
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39
40 #define START_ARM GNUNET_YES
41
42 #define VERBOSE GNUNET_NO
43
44 static int ret = 1;
45
46
47 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49 #if START_ARM
50 static struct GNUNET_ARM_Handle *arm;
51 #endif
52
53 static void
54 arm_stopped (void *cls, enum GNUNET_ARM_ProcessStatus success)
55 {
56   if (success != GNUNET_ARM_PROCESS_DOWN)
57     {
58       GNUNET_break (0);
59       ret = 4;
60     }
61   else
62     {
63       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM stopped\n");
64     }
65 #if START_ARM
66   GNUNET_ARM_disconnect (arm);
67   arm = NULL;
68 #endif
69 }
70
71 static void
72 hostNameResolveCB (void *cls, const struct sockaddr *addr, socklen_t addrlen)
73 {
74   if ((ret == 0) || (ret == 4))
75     return;
76   if (NULL == addr)
77     {
78       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Name not resolved!\n");
79 #if START_ARM
80       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
81 #endif
82       ret = 3;
83       return;
84     }
85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86               "Resolved hostname, now stopping ARM\n");
87   ret = 0;
88 #if START_ARM
89   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
90 #endif
91 }
92
93
94 static void
95 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
96 {
97   if (success != GNUNET_ARM_PROCESS_STARTING)
98     {
99       GNUNET_break (0);
100       ret = 1;
101       return;
102     }
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104               "Trying to resolve our own hostname!\n");
105   /* connect to the resolver service */
106   if (NULL ==
107       GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, TIMEOUT,
108                                         &hostNameResolveCB, NULL))
109     {
110       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                   "Unable initiate connection to resolver service\n");
112       ret = 2;
113 #if START_ARM
114       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
115 #endif
116     }
117 }
118
119
120 static void
121 run (void *cls, char *const *args, const char *cfgfile,
122      const struct GNUNET_CONFIGURATION_Handle *c)
123 {
124   cfg = c;
125 #if START_ARM
126   arm = GNUNET_ARM_connect (cfg, NULL);
127   GNUNET_ARM_start_service (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, &arm_notify, NULL);
128 #else
129   arm_notify (NULL, GNUNET_YES);
130 #endif
131 }
132
133
134 static void
135 check ()
136 {
137   char *const argv[] = {
138     "test-gnunet-service-manager",
139     "-c", "test_arm_api_data.conf",
140 #if VERBOSE
141     "-L", "DEBUG",
142 #endif
143     NULL
144   };
145   struct GNUNET_GETOPT_CommandLineOption options[] = {
146     GNUNET_GETOPT_OPTION_END
147   };
148   GNUNET_assert (GNUNET_OK ==
149                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
150                                      argv, "test-gnunet-service-manager",
151                                      "nohelp", options, &run, NULL));
152 }
153
154
155 int
156 main (int argc, char *argv[])
157 {
158   char hostname[GNUNET_OS_get_hostname_max_length () + 1];
159
160   if (0 != gethostname (hostname, sizeof (hostname) - 1))
161     {
162       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
163                            "gethostname");
164       FPRINTF (stderr,
165                "%s", "Failed to determine my own hostname, testcase not run.\n");
166       return 0;
167     }
168   if (NULL == gethostbyname (hostname))
169     {
170       FPRINTF (stderr,
171                "Failed to resolve my hostname `%s', testcase not run.\n",
172                hostname);
173       return 0;
174     }
175
176   GNUNET_log_setup ("test-gnunet-service-manager",
177 #if VERBOSE
178                     "DEBUG",
179 #else
180                     "WARNING",
181 #endif
182                     NULL);
183   check ();
184   return ret;
185 }