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 static int ret = 1;
42
43 static struct GNUNET_SCHEDULER_Handle *sched;
44
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 static struct GNUNET_ARM_Handle *arm;
48
49 static void
50 arm_stopped (void *cls, int success)
51 {
52   if (success != GNUNET_OK)        
53     ret = 4;
54 }
55
56 static void 
57 hostNameResolveCB(void *cls, 
58                   const struct sockaddr *addr, 
59                   socklen_t addrlen)
60 {
61   if ( (ret == 0) || (ret == 4) )
62     return;
63   if (NULL == addr)
64     {
65       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 
66                  "Name not resolved!\n");
67 #if START_ARM
68       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
69 #endif
70       ret = 3;
71       return;
72     }  
73   ret = 0;
74 #if START_ARM
75   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
76 #endif
77 }
78
79
80
81 static void
82 arm_notify (void *cls, int success)
83 {
84   if (success != GNUNET_YES)
85     {
86       GNUNET_break (0);
87       ret = 1;
88       return;
89     }
90   /* connect to the resolver service */
91   if (NULL == GNUNET_RESOLVER_hostname_resolve (sched,
92                                                 cfg, AF_UNSPEC,
93                                                 TIMEOUT,
94                                                 &hostNameResolveCB,
95                                                 NULL))
96     {
97       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
98                   "Unable to resolve our own hostname!\n");
99       ret = 2;
100 #if START_ARM
101       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
102 #endif
103     }
104 }
105
106
107 static void
108 run(void *cls, 
109     struct GNUNET_SCHEDULER_Handle *s, 
110     char * const *args,
111     const char *cfgfile, 
112     const struct GNUNET_CONFIGURATION_Handle *c)
113 {
114   cfg = c;
115   sched = s;    
116   arm = GNUNET_ARM_connect (cfg, sched, NULL);
117 #if START_ARM
118   GNUNET_ARM_start_service (arm, "arm", START_TIMEOUT, &arm_notify, NULL);
119 #else
120   arm_notify (NULL, GNUNET_YES);
121 #endif
122 }
123
124
125 static void
126 check()
127 {
128   char *const argv[] = {
129     "test-gnunet-service-manager",
130     "-c", "test_arm_api_data.conf",
131 #if VERBOSE
132     "-L", "DEBUG",
133 #endif
134     NULL
135   };
136   struct GNUNET_GETOPT_CommandLineOption options[] = {
137     GNUNET_GETOPT_OPTION_END
138   };
139   GNUNET_assert (GNUNET_OK ==
140                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
141                                      argv,
142                                      "test-gnunet-service-manager",
143                                      "nohelp", options, &run, NULL));
144 }
145
146
147 int
148 main (int argc, char *argv[])
149 {
150   GNUNET_log_setup("test-gnunet-service-manager",
151 #if VERBOSE
152                    "DEBUG",
153 #else
154                    "WARNING",
155 #endif
156                    NULL);
157   check();
158   return ret;
159 }