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