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