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