bugfixes
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist.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 2, 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 hostlist/test_gnunet_daemon_hostlist.c
22  * @brief test for gnunet_daemon_hostslist.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_transport_service.h"
29
30 #define VERBOSE GNUNET_YES
31
32 #define START_ARM GNUNET_YES
33
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
39
40 static int ok;
41
42 static struct GNUNET_SCHEDULER_Handle *sched;
43     
44 struct PeerContext
45 {
46   struct GNUNET_CONFIGURATION_Handle *cfg;
47   struct GNUNET_TRANSPORT_Handle *th;
48   struct GNUNET_MessageHeader *hello;
49 #if START_ARM
50   pid_t arm_pid;
51 #endif
52 };
53
54 static struct PeerContext p1;
55
56 static struct PeerContext p2;
57
58
59
60 static void
61 process_hello (void *cls,
62                struct GNUNET_TIME_Relative latency,
63                const struct GNUNET_PeerIdentity *peer,
64                const struct GNUNET_MessageHeader *message)
65 {
66   struct PeerContext *p = cls;
67
68   GNUNET_assert (peer != NULL);
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70               "Received (my) `%s' from transport service of `%4s'\n",
71               "HELLO", GNUNET_i2s (peer));
72   GNUNET_assert (message != NULL);
73   p->hello = GNUNET_malloc (ntohs (message->size));
74   memcpy (p->hello, message, ntohs (message->size));
75   if ((p == &p1) && (p2.th != NULL))
76     GNUNET_TRANSPORT_offer_hello (p2.th, message);
77   if ((p == &p2) && (p1.th != NULL))
78     GNUNET_TRANSPORT_offer_hello (p1.th, message);
79
80   if ((p == &p1) && (p2.hello != NULL))
81     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
82   if ((p == &p2) && (p1.hello != NULL))
83     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
84 }
85
86
87 static void
88 setup_peer (struct PeerContext *p, const char *cfgname)
89 {
90   p->cfg = GNUNET_CONFIGURATION_create ();
91 #if START_ARM
92   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
93                                         "gnunet-service-arm",
94 #if VERBOSE
95                                         "-L", "DEBUG",
96 #endif
97                                         "-c", cfgname, NULL);
98   sleep (1);                    /* allow ARM to start */
99 #endif
100   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
101   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
102   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
103   GNUNET_assert (p->th != NULL);
104   GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
105 }
106
107
108 static void
109 run (void *cls,
110      struct GNUNET_SCHEDULER_Handle *s,
111      char *const *args,
112      const char *cfgfile, 
113      const struct GNUNET_CONFIGURATION_Handle *cfg)
114 {
115   GNUNET_assert (ok == 1);
116   ok++;
117   sched = s;
118   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
119   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
120 }
121
122
123 static void
124 stop_arm (struct PeerContext *p)
125 {
126   GNUNET_ARM_stop_services (p->cfg, sched, "core", NULL);
127 #if START_ARM
128   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
129     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
130   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
131     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133               "ARM process %u stopped\n", p->arm_pid);
134 #endif
135   GNUNET_CONFIGURATION_destroy (p->cfg);
136 }
137
138
139 static int
140 check ()
141 {
142   char *const argv[] = { "test-gnunet-daemon-hostlist",
143     "-c", "test_gnunet_daemon_hostlist_data.conf",
144 #if VERBOSE
145     "-L", "DEBUG",
146 #endif
147     NULL
148   };
149   struct GNUNET_GETOPT_CommandLineOption options[] = {
150     GNUNET_GETOPT_OPTION_END
151   };
152   ok = 1;
153   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
154                       argv, "test-gnunet-daemon-hostlist",
155                       "nohelp", options, &run, &ok);
156   stop_arm (&p1);
157   stop_arm (&p2);
158   return ok;
159 }
160
161
162 int
163 main (int argc, char *argv[])
164 {
165   
166   int ret;
167
168   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
169 #if VERBOSE
170                     "DEBUG",
171 #else
172                     "WARNING",
173 #endif
174                     NULL);
175   ret = check ();
176   return ret; 
177 }
178
179 /* end of test_gnunet_daemon_hostlist.c */