fix
[oweals/gnunet.git] / src / core / test_core_api_start_only.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 transport/test_core_api_start_only.c
22  * @brief testcase for core_api.c that only starts two peers,
23  *        connects to the core service and shuts down again
24  */
25 #include "platform.h"
26 #include "gnunet_common.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33
34 #define VERBOSE GNUNET_NO
35
36 #define START_ARM GNUNET_YES
37
38
39 /**
40  * How long until we give up on transmitting the message?
41  */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
43
44 #define MTYPE 12345
45
46 struct PeerContext
47 {
48   struct GNUNET_CONFIGURATION_Handle *cfg;
49   struct GNUNET_CORE_Handle *ch;
50   struct GNUNET_PeerIdentity id;
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 static struct GNUNET_SCHEDULER_Handle *sched;
61
62 static int ok;
63
64 #if VERBOSE
65 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
66 #else
67 #define OKPP do { ok++; } while (0)
68 #endif
69
70
71
72 static void
73 connect_notify (void *cls,
74                 const struct GNUNET_PeerIdentity *peer,
75                 struct GNUNET_TIME_Relative latency,
76                 uint32_t distance)
77 {
78 }
79
80
81 static void
82 disconnect_notify (void *cls,
83                    const struct GNUNET_PeerIdentity *peer)
84 {
85 }
86
87
88 static int
89 inbound_notify (void *cls,
90                 const struct GNUNET_PeerIdentity *other,
91                 const struct GNUNET_MessageHeader *message,
92                 struct GNUNET_TIME_Relative latency,
93                 uint32_t distance)
94 {
95   return GNUNET_OK;
96 }
97
98
99 static int
100 outbound_notify (void *cls,
101                  const struct GNUNET_PeerIdentity *other,
102                  const struct GNUNET_MessageHeader *message,
103                  struct GNUNET_TIME_Relative latency,
104                  uint32_t distance)
105 {
106   return GNUNET_OK;
107 }
108
109
110 static struct GNUNET_CORE_MessageHandler handlers[] = {
111   {NULL, 0, 0}
112 };
113
114
115
116 static void
117 init_notify (void *cls,
118              struct GNUNET_CORE_Handle *server,
119              const struct GNUNET_PeerIdentity *my_identity,
120              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
121 {
122   struct PeerContext *p = cls;
123
124   GNUNET_assert (server != NULL);
125   p->ch = server;
126   if (cls == &p1)
127     {
128       /* connect p2 */
129       GNUNET_CORE_connect (sched,
130                            p2.cfg,
131                            TIMEOUT,
132                            &p2,
133                            &init_notify,                         
134                            &connect_notify,
135                            &disconnect_notify,
136                            NULL,
137                            &inbound_notify,
138                            GNUNET_YES,
139                            &outbound_notify, GNUNET_YES, handlers);
140     }
141   else
142     {
143       GNUNET_assert (cls == &p2);
144       GNUNET_CORE_disconnect (p1.ch);
145       GNUNET_CORE_disconnect (p2.ch);
146       ok = 0;
147     }
148 }
149
150
151 static void
152 setup_peer (struct PeerContext *p, const char *cfgname)
153 {
154   p->cfg = GNUNET_CONFIGURATION_create ();
155 #if START_ARM
156   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
157                                         "gnunet-service-arm",
158 #if VERBOSE
159                                         "-L", "DEBUG",
160 #endif
161                                         "-c", cfgname, NULL);
162 #endif
163   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
164 }
165
166
167 static void
168 run (void *cls,
169      struct GNUNET_SCHEDULER_Handle *s,
170      char *const *args,
171      const char *cfgfile, 
172      const struct GNUNET_CONFIGURATION_Handle *cfg)
173 {
174   GNUNET_assert (ok == 1);
175   OKPP;
176   sched = s;
177   setup_peer (&p1, "test_core_api_peer1.conf");
178   setup_peer (&p2, "test_core_api_peer2.conf");
179   GNUNET_CORE_connect (sched,
180                        p1.cfg,
181                        TIMEOUT,
182                        &p1,
183                        &init_notify,
184                        &connect_notify,
185                        &disconnect_notify,
186                        NULL,
187                        &inbound_notify,
188                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
189 }
190
191
192 static void
193 stop_arm (struct PeerContext *p)
194 {
195 #if START_ARM
196   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
197     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
198   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
199     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201               "ARM process %u stopped\n", p->arm_pid);
202 #endif
203   GNUNET_CONFIGURATION_destroy (p->cfg);
204 }
205
206
207 static int
208 check ()
209 {
210   char *const argv[] = { "test-core-api-start-only",
211     "-c",
212     "test_core_api_data.conf",
213 #if VERBOSE
214     "-L", "DEBUG",
215 #endif
216     NULL
217   };
218   struct GNUNET_GETOPT_CommandLineOption options[] = {
219     GNUNET_GETOPT_OPTION_END
220   };
221
222   ok = 1;
223   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
224                       argv, "test-core-api-start-only", "nohelp", options, &run, &ok);
225   stop_arm (&p1);
226   stop_arm (&p2);
227   return ok;
228 }
229
230 int
231 main (int argc, char *argv[])
232 {
233   int ret;
234
235   GNUNET_log_setup ("test-core-api-start-only",
236 #if VERBOSE
237                     "DEBUG",
238 #else
239                     "WARNING",
240 #endif
241                     NULL);
242   ret = check ();
243
244   return ret;
245 }
246
247 /* end of test_core_api_start_only.c */