-svn ignore
[oweals/gnunet.git] / src / arm / test_arm_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2011 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_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, 1500)
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
40
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 static struct GNUNET_ARM_Handle *arm;
44
45 static int ok = 1;
46
47 static void
48 arm_stopped (void *cls,  enum GNUNET_ARM_ProcessStatus success)
49 {
50   GNUNET_break (success == GNUNET_ARM_PROCESS_DOWN);
51   if (success != GNUNET_ARM_PROCESS_DOWN)
52     ok = 3;
53   else if (ok == 1)
54     ok = 0;
55 }
56
57
58 static void
59 arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus success)
60 {
61   GNUNET_break (success == GNUNET_ARM_PROCESS_DOWN);
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,
79                                NULL);
80       return;
81     }
82   GNUNET_break (addr != NULL);
83   ok = 0;
84 }
85
86
87 static void
88 resolver_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
89 {
90   if (success != GNUNET_ARM_PROCESS_STARTING)
91     {
92       GNUNET_break (0);
93       ok = 2;
94 #if START_ARM
95       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
96 #endif
97       return;
98     }
99   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
100 }
101
102
103 static void
104 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
105 {
106   if (success != GNUNET_ARM_PROCESS_STARTING)
107     {
108       GNUNET_break (0);
109       ok = 2;
110 #if START_ARM
111       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
112 #endif
113     }
114   GNUNET_ARM_start_service (arm, "resolver", START_TIMEOUT, &resolver_notify,
115                             NULL);
116 }
117
118
119 static void
120 task (void *cls, char *const *args, const char *cfgfile,
121       const struct GNUNET_CONFIGURATION_Handle *c)
122 {
123   cfg = c;
124   arm = GNUNET_ARM_connect (cfg, NULL);
125 #if START_ARM
126   GNUNET_ARM_start_service (arm, "arm", START_TIMEOUT, &arm_notify, NULL);
127 #else
128   arm_notify (NULL, GNUNET_YES);
129 #endif
130 }
131
132
133
134 static int
135 check ()
136 {
137   char *const argv[] = {
138     "test-arm-api",
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-arm-api", "nohelp", options,
151                                      &task, NULL));
152   return ok;
153 }
154
155 int
156 main (int argc, char *argv[])
157 {
158   int ret;
159
160
161   GNUNET_log_setup ("test-arm-api",
162 #if VERBOSE
163                     "DEBUG",
164 #else
165                     "WARNING",
166 #endif
167                     NULL);
168   ret = check ();
169
170   return ret;
171 }
172
173 /* end of test_arm_api.c */