- assert hard
[oweals/gnunet.git] / src / arm / test_gnunet_service_arm.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_gnunet_service_arm.c 
22  * @brief testcase for gnunet-service-arm.c; tests ARM by making it start the resolver
23  * @author Safey
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_resolver_service.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
31
32 /**
33  * Timeout for starting services, very short because of the strange way start works
34  * (by checking if running before starting, so really this time is always waited on
35  * startup (annoying)).
36  */
37 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40
41 static int ret = 1;
42
43 static struct GNUNET_ARM_Handle *arm;
44
45
46 static void
47 arm_stopped (void *cls, enum GNUNET_ARM_ProcessStatus success)
48 {
49   if (success != GNUNET_ARM_PROCESS_DOWN)
50     {
51       GNUNET_break (0);
52       ret = 4;
53     }
54   else
55     {
56       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM stopped\n");
57     }
58   GNUNET_ARM_disconnect (arm);
59   arm = NULL;
60 }
61
62
63 static void
64 hostNameResolveCB (void *cls, const struct sockaddr *addr, socklen_t addrlen)
65 {
66   if ((ret == 0) || (ret == 4))
67     return;
68   if (NULL == addr)
69     {
70       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Name not resolved!\n");
71       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
72       ret = 3;
73       return;
74     }
75   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76               "Resolved hostname, now stopping ARM\n");
77   ret = 0;
78   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
79 }
80
81
82 static void
83 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus success)
84 {
85   if (success != GNUNET_ARM_PROCESS_STARTING)
86     {
87       GNUNET_break (0);
88       ret = 1;
89       return;
90     }
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92               "Trying to resolve our own hostname!\n");
93   /* connect to the resolver service */
94   if (NULL ==
95       GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, TIMEOUT,
96                                         &hostNameResolveCB, NULL))
97     {
98       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99                   "Unable initiate connection to resolver service\n");
100       ret = 2;
101       GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
102     }
103 }
104
105
106 static void
107 run (void *cls, char *const *args, const char *cfgfile,
108      const struct GNUNET_CONFIGURATION_Handle *c)
109 {
110   arm = GNUNET_ARM_connect (c, NULL);
111   GNUNET_ARM_start_service (arm, "arm",
112                             GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, 
113                             &arm_notify, NULL);
114 }
115
116
117 int
118 main (int argc, char *av[])
119 {
120   static char *const argv[] = {
121     "test-gnunet-service-arm",
122     "-c", "test_arm_api_data.conf",
123     NULL
124   };
125   static struct GNUNET_GETOPT_CommandLineOption options[] = {
126     GNUNET_GETOPT_OPTION_END
127   };
128   char hostname[GNUNET_OS_get_hostname_max_length () + 1];
129
130   if (0 != gethostname (hostname, sizeof (hostname) - 1))
131     {
132       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
133                            "gethostname");
134       FPRINTF (stderr,
135                "%s", "Failed to determine my own hostname, testcase not run.\n");
136       return 0;
137     }
138   if (NULL == gethostbyname (hostname))
139     {
140       FPRINTF (stderr,
141                "Failed to resolve my hostname `%s', testcase not run.\n",
142                hostname);
143       return 0;
144     }
145   GNUNET_log_setup ("test-gnunet-service-arm",
146                     "WARNING",
147                     NULL);
148   GNUNET_assert (GNUNET_OK ==
149                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
150                                      argv, "test-gnunet-service-arm",
151                                      "nohelp", options, &run, NULL));
152   return ret;
153 }
154
155 /* end of test_gnunet_service_arm.c */