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