All-encompassing ARM update
[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 static void
46 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 {
48   GNUNET_ARM_disconnect (arm);
49   arm = NULL;
50 }
51
52 static void
53 arm_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
54 {
55   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
56   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPING);
57   if (result != GNUNET_ARM_RESULT_STOPPING)
58     ret = 4;
59   GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
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     ret = 3;
71   }
72   else
73   {
74     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75         "Resolved hostname, now stopping ARM\n");
76     ret = 0;
77   }
78   GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
79 }
80
81 static void
82 arm_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
83 {
84   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
85   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87       "Trying to resolve our own hostname!\n");
88   /* connect to the resolver service */
89   if (NULL == GNUNET_RESOLVER_hostname_resolve (
90       AF_UNSPEC, TIMEOUT, &hostNameResolveCB, NULL))
91   {
92     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
93         "Unable initiate connection to resolver service\n");
94     ret = 2;
95     GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
96   }
97 }
98
99 static void
100 run (void *cls, char *const *args, const char *cfgfile,
101      const struct GNUNET_CONFIGURATION_Handle *c)
102 {
103   char *armconfig;
104
105   if (NULL != cfgfile)
106   {
107     if (GNUNET_OK !=
108         GNUNET_CONFIGURATION_get_value_filename (c, "arm", "CONFIG",
109                                                &armconfig))
110     {
111       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
112                                               *) c, "arm", "CONFIG",
113                                              cfgfile);
114     }
115     else
116       GNUNET_free (armconfig);
117   }
118   arm = GNUNET_ARM_alloc (c);
119   GNUNET_ARM_connect (arm, NULL, NULL);
120   GNUNET_ARM_request_service_start (arm, "arm",
121       GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
122 }
123
124
125 int
126 main (int argc, char *av[])
127 {
128   static char *const argv[] = {
129     "test-gnunet-service-arm",
130     "-c", "test_arm_api_data.conf",
131     NULL
132   };
133   static struct GNUNET_GETOPT_CommandLineOption options[] = {
134     GNUNET_GETOPT_OPTION_END
135   };
136   char hostname[GNUNET_OS_get_hostname_max_length () + 1];
137
138   if (0 != gethostname (hostname, sizeof (hostname) - 1))
139     {
140       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
141                            "gethostname");
142       FPRINTF (stderr,
143                "%s", "Failed to determine my own hostname, testcase not run.\n");
144       return 0;
145     }
146   if (NULL == gethostbyname (hostname))
147     {
148       FPRINTF (stderr,
149                "Failed to resolve my hostname `%s', testcase not run.\n",
150                hostname);
151       return 0;
152     }
153   GNUNET_log_setup ("test-gnunet-service-arm",
154                     "WARNING",
155                     NULL);
156   GNUNET_assert (GNUNET_OK ==
157                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
158                                      argv, "test-gnunet-service-arm",
159                                      "nohelp", options, &run, NULL));
160   return ret;
161 }
162
163 /* end of test_gnunet_service_arm.c */