gpl3
[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 static struct GNUNET_SCHEDULER_Handle *sched;
47
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
49
50 #if START_ARM
51 static struct GNUNET_ARM_Handle *arm;
52 #endif
53
54 static void
55 arm_stopped (void *cls, int success)
56 {
57   if (success != GNUNET_NO)       
58     {
59       GNUNET_break (0);
60       ret = 4;
61     }
62   else
63     {
64       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
65                   "ARM stopped\n");
66     }
67 #if START_ARM
68   GNUNET_ARM_disconnect (arm);
69   arm = NULL;
70 #endif
71 }
72
73 static void 
74 hostNameResolveCB(void *cls, 
75                   const struct sockaddr *addr, 
76                   socklen_t addrlen)
77 {
78   if ( (ret == 0) || (ret == 4) )
79     return;
80   if (NULL == addr)
81     {
82       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
83                   "Name not resolved!\n");
84 #if START_ARM
85       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
86 #endif
87       ret = 3;
88       return;
89     }  
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
91               "Resolved hostname, now stopping ARM\n");
92   ret = 0;
93 #if START_ARM
94   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
95 #endif
96 }
97
98
99 static void
100 arm_notify (void *cls, int success)
101 {
102   if (success != GNUNET_YES)
103     {
104       GNUNET_break (0);
105       ret = 1;
106       return;
107     }
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
109               "Trying to resolve our own hostname!\n");
110   /* connect to the resolver service */
111   if (NULL == GNUNET_RESOLVER_hostname_resolve (sched,
112                                                 cfg, AF_UNSPEC,
113                                                 TIMEOUT,
114                                                 &hostNameResolveCB,
115                                                 NULL))
116     {
117       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
118                   "Unable initiate connection to resolver service\n");
119       ret = 2;
120 #if START_ARM
121       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
122 #endif
123     }
124 }
125
126
127 static void
128 run(void *cls, 
129     struct GNUNET_SCHEDULER_Handle *s, 
130     char * const *args,
131     const char *cfgfile, 
132     const struct GNUNET_CONFIGURATION_Handle *c)
133 {
134   cfg = c;
135   sched = s;    
136 #if START_ARM
137   arm = GNUNET_ARM_connect (cfg, sched, NULL);
138   GNUNET_ARM_start_service (arm, "arm", START_TIMEOUT, &arm_notify, NULL);
139 #else
140   arm_notify (NULL, GNUNET_YES);
141 #endif
142 }
143
144
145 static void
146 check()
147 {
148   char *const argv[] = {
149     "test-gnunet-service-manager",
150     "-c", "test_arm_api_data.conf",
151 #if VERBOSE
152     "-L", "DEBUG",
153 #endif
154     NULL
155   };
156   struct GNUNET_GETOPT_CommandLineOption options[] = {
157     GNUNET_GETOPT_OPTION_END
158   };
159   GNUNET_assert (GNUNET_OK ==
160                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
161                                      argv,
162                                      "test-gnunet-service-manager",
163                                      "nohelp", options, &run, NULL));
164 }
165
166
167 int
168 main (int argc, char *argv[])
169 {
170   char hostname[GNUNET_OS_get_hostname_max_length() + 1];
171
172   if (0 != gethostname (hostname, sizeof (hostname) - 1))
173     {
174       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
175                            GNUNET_ERROR_TYPE_BULK, "gethostname");
176       fprintf (stderr, "Failed to determine my own hostname, testcase not run.\n");
177       return 0;
178     }
179   if (NULL == gethostbyname (hostname))
180     {
181       fprintf (stderr, "Failed to resolve my hostname `%s', testcase not run.\n",
182                hostname);
183       return 0;
184     }
185
186   GNUNET_log_setup("test-gnunet-service-manager",
187 #if VERBOSE
188                    "DEBUG",
189 #else
190                    "WARNING",
191 #endif
192                    NULL);
193   check();
194   return ret;
195 }