fixes
[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_YES
35
36 #define START_ARM GNUNET_YES
37
38 #define MTYPE 12345
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43   struct GNUNET_CORE_Handle *ch;
44   struct GNUNET_PeerIdentity id;
45 #if START_ARM
46   struct GNUNET_OS_Process *arm_proc;
47 #endif
48 };
49
50 static struct PeerContext p1;
51
52 static struct PeerContext p2;
53
54
55 static int ok;
56
57 #if VERBOSE
58 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
59 #else
60 #define OKPP do { ok++; } while (0)
61 #endif
62
63
64
65 static void
66 connect_notify (void *cls,
67                 const struct GNUNET_PeerIdentity *peer,
68                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
69 {
70 }
71
72
73 static void
74 disconnect_notify (void *cls,
75                    const struct GNUNET_PeerIdentity *peer)
76 {
77 }
78
79
80 static int
81 inbound_notify (void *cls,
82                 const struct GNUNET_PeerIdentity *other,
83                 const struct GNUNET_MessageHeader *message,
84                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
85 {
86   return GNUNET_OK;
87 }
88
89
90 static int
91 outbound_notify (void *cls,
92                  const struct GNUNET_PeerIdentity *other,
93                  const struct GNUNET_MessageHeader *message,
94                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
95 {
96   return GNUNET_OK;
97 }
98
99
100 static struct GNUNET_CORE_MessageHandler handlers[] = {
101   {NULL, 0, 0}
102 };
103
104
105
106 static void
107 init_notify (void *cls,
108              struct GNUNET_CORE_Handle *server,
109              const struct GNUNET_PeerIdentity *my_identity,
110              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
111 {
112   struct PeerContext *p = cls;
113
114   GNUNET_assert (server != NULL);
115   p->ch = server;
116   if (cls == &p1)
117     {
118       /* connect p2 */
119       GNUNET_CORE_connect (p2.cfg, 1,
120                            &p2,
121                            &init_notify,                         
122                            &connect_notify,
123                            &disconnect_notify,
124                            NULL,
125                            &inbound_notify,
126                            GNUNET_YES,
127                            &outbound_notify, GNUNET_YES, handlers);
128     }
129   else
130     {
131       GNUNET_assert (cls == &p2);
132       GNUNET_CORE_disconnect (p1.ch);
133       GNUNET_CORE_disconnect (p2.ch);
134       ok = 0;
135     }
136 }
137
138
139 static void
140 setup_peer (struct PeerContext *p, const char *cfgname)
141 {
142   p->cfg = GNUNET_CONFIGURATION_create ();
143 #if START_ARM
144   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
145                                         "gnunet-service-arm",
146 #if VERBOSE
147                                         "-L", "DEBUG",
148 #endif
149                                         "-c", cfgname, NULL);
150 #endif
151   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
152 }
153
154
155 static void
156 run (void *cls,
157      char *const *args,
158      const char *cfgfile, 
159      const struct GNUNET_CONFIGURATION_Handle *cfg)
160 {
161   GNUNET_assert (ok == 1);
162   OKPP;
163   setup_peer (&p1, "test_core_api_peer1.conf");
164   setup_peer (&p2, "test_core_api_peer2.conf");
165   GNUNET_CORE_connect (p1.cfg, 1,
166                        &p1,
167                        &init_notify,
168                        &connect_notify,
169                        &disconnect_notify,
170                        NULL,
171                        &inbound_notify,
172                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
173 }
174
175
176 static void
177 stop_arm (struct PeerContext *p)
178 {
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180               "Stopping peer\n");
181 #if START_ARM
182   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
183     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
184   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
185     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
188   GNUNET_OS_process_close (p->arm_proc);
189   p->arm_proc = NULL;
190 #endif
191   GNUNET_CONFIGURATION_destroy (p->cfg);
192 }
193
194
195 static int
196 check ()
197 {
198   char *const argv[] = { "test-core-api-start-only",
199     "-c",
200     "test_core_api_data.conf",
201 #if VERBOSE
202     "-L", "DEBUG",
203 #endif
204     NULL
205   };
206   struct GNUNET_GETOPT_CommandLineOption options[] = {
207     GNUNET_GETOPT_OPTION_END
208   };
209
210   ok = 1;
211   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
212                       argv, "test-core-api-start-only", "nohelp", options, &run, &ok);
213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214               "Test finished\n");
215   stop_arm (&p1);
216   stop_arm (&p2);
217   return ok;
218 }
219
220 int
221 main (int argc, char *argv[])
222 {
223   int ret;
224
225   GNUNET_log_setup ("test-core-api-start-only",
226 #if VERBOSE
227                     "DEBUG",
228 #else
229                     "WARNING",
230 #endif
231                     NULL);
232   ret = check ();
233
234   return ret;
235 }
236
237 /* end of test_core_api_start_only.c */