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