- assert hard
[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 START_ARM GNUNET_YES
34
35 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1500)
36
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
38
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 static struct GNUNET_ARM_Handle *arm;
42
43 static int ok = 1;
44
45 static void
46 arm_stopped (void *cls,  enum GNUNET_ARM_ProcessStatus success)
47 {
48   GNUNET_break (success == GNUNET_ARM_PROCESS_DOWN);
49   if (success != GNUNET_ARM_PROCESS_DOWN)
50     ok = 3;
51   else if (ok == 1)
52     ok = 0;
53 }
54
55
56 static void
57 arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus success)
58 {
59   GNUNET_break (success == GNUNET_ARM_PROCESS_DOWN);
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,
77                                NULL);
78       return;
79     }
80   GNUNET_break (addr != NULL);
81   ok = 0;
82 }
83
84
85 static void
86 resolver_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
87 {
88   if (success != GNUNET_ARM_PROCESS_STARTING)
89     {
90       GNUNET_break (0);
91       ok = 2;
92 #if START_ARM
93       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
94 #endif
95       return;
96     }
97   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
98 }
99
100
101 static void
102 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
103 {
104   if (success != GNUNET_ARM_PROCESS_STARTING)
105     {
106       GNUNET_break (0);
107       ok = 2;
108 #if START_ARM
109       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
110 #endif
111     }
112   GNUNET_ARM_start_service (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, &resolver_notify,
113                             NULL);
114 }
115
116
117 static void
118 task (void *cls, char *const *args, const char *cfgfile,
119       const struct GNUNET_CONFIGURATION_Handle *c)
120 {
121   cfg = c;
122   arm = GNUNET_ARM_connect (cfg, NULL);
123 #if START_ARM
124   GNUNET_ARM_start_service (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, &arm_notify, NULL);
125 #else
126   arm_notify (NULL, GNUNET_YES);
127 #endif
128 }
129
130
131
132 static int
133 check ()
134 {
135   char *const argv[] = {
136     "test-arm-api",
137     "-c", "test_arm_api_data.conf",
138     NULL
139   };
140   struct GNUNET_GETOPT_CommandLineOption options[] = {
141     GNUNET_GETOPT_OPTION_END
142   };
143   GNUNET_assert (GNUNET_OK ==
144                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
145                                      argv, "test-arm-api", "nohelp", options,
146                                      &task, NULL));
147   return ok;
148 }
149
150 int
151 main (int argc, char *argv[])
152 {
153   int ret;
154
155
156   GNUNET_log_setup ("test-arm-api",
157                     "WARNING",
158                     NULL);
159   ret = check ();
160
161   return ret;
162 }
163
164 /* end of test_arm_api.c */