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