basic api to service communication has been hammered out, as well as service confirma...
[oweals/gnunet.git] / src / dht / test_dht_api.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 dht/test_dht_api.c
22  * @brief base test case for dht api
23  *
24  * This test case tests DHT api to DUMMY DHT service communication.
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_dht_service.h"
35
36 #define VERBOSE GNUNET_YES
37
38 #define VERBOSE_ARM GNUNET_YES
39
40 #define START_ARM GNUNET_YES
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
46
47 #define MTYPE 12345
48
49 struct PeerContext
50 {
51   struct GNUNET_CONFIGURATION_Handle *cfg;
52   struct GNUNET_DHT_Handle *dht_handle;
53   struct GNUNET_PeerIdentity id;
54   struct GNUNET_DHT_RouteHandle *get_handle;
55   struct GNUNET_DHT_RouteHandle *find_peer_handle;
56
57 #if START_ARM
58   pid_t arm_pid;
59 #endif
60 };
61
62 static struct PeerContext p1;
63
64 static struct GNUNET_SCHEDULER_Handle *sched;
65
66 static int ok;
67
68 GNUNET_SCHEDULER_TaskIdentifier die_task;
69
70 #if VERBOSE
71 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
72 #else
73 #define OKPP do { ok++; } while (0)
74 #endif
75
76
77 static void
78 end ()
79 {
80   /* do work here */
81   GNUNET_SCHEDULER_cancel (sched, die_task);
82
83   GNUNET_DHT_disconnect (p1.dht_handle);
84
85   die_task = GNUNET_SCHEDULER_NO_TASK;
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning success!\n");
87   ok = 0;
88 }
89
90 static void
91 stop_arm (struct PeerContext *p)
92 {
93 #if START_ARM
94   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
95     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
96   GNUNET_OS_process_wait (p->arm_pid);
97 #endif
98   GNUNET_CONFIGURATION_destroy (p->cfg);
99 }
100
101
102 static void
103 end_badly ()
104 {
105   /* do work here */
106 #if VERBOSE
107   fprintf(stderr, "Ending on an unhappy note.\n");
108 #endif
109
110   GNUNET_DHT_disconnect (p1.dht_handle);
111
112   ok = 1;
113   return;
114 }
115
116
117 /**
118  * Signature of the main function of a task.
119  *
120  * @param cls closure
121  * @param tc context information (why was this task triggered now)
122  */
123 void test_put (void *cls,
124                const struct GNUNET_SCHEDULER_TaskContext * tc)
125 {
126   struct PeerContext *peer = cls;
127   GNUNET_HashCode hash;
128   char *data;
129   size_t data_size = 42;
130   memset(&hash, 42, sizeof(GNUNET_HashCode));
131   data = GNUNET_malloc(data_size);
132   memset(data, 43, data_size);
133
134   GNUNET_assert (peer->dht_handle != NULL);
135
136   GNUNET_DHT_put(peer->dht_handle, &hash, 0, data_size, data, GNUNET_TIME_relative_to_absolute(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 360)) ,GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 360), NULL, NULL);
137
138   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
139
140   GNUNET_SCHEDULER_add_now(sched, &end, NULL);
141 }
142
143 /**
144  * Signature of the main function of a task.
145  *
146  * @param cls closure
147  * @param tc context information (why was this task triggered now)
148  */
149 void test_get_stop (void *cls,
150                const struct GNUNET_SCHEDULER_TaskContext * tc)
151 {
152   struct PeerContext *peer = cls;
153   GNUNET_HashCode hash;
154   memset(&hash, 42, sizeof(GNUNET_HashCode));
155
156   GNUNET_assert (peer->dht_handle != NULL);
157
158   GNUNET_DHT_get_stop(peer->get_handle);
159
160   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
161   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
162
163 }
164
165 /**
166  * Signature of the main function of a task.
167  *
168  * @param cls closure
169  * @param tc context information (why was this task triggered now)
170  */
171 void test_get (void *cls,
172                const struct GNUNET_SCHEDULER_TaskContext * tc)
173 {
174   struct PeerContext *peer = cls;
175   GNUNET_HashCode hash;
176   memset(&hash, 42, sizeof(GNUNET_HashCode));
177
178   peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
179   GNUNET_assert (peer->dht_handle != NULL);
180
181   peer->get_handle = GNUNET_DHT_get_start(peer->dht_handle, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 100), 42, &hash, NULL, NULL);
182
183   if (peer->get_handle == NULL)
184     GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
185
186   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get_stop, &p1);
187 }
188
189 static void
190 setup_peer (struct PeerContext *p, const char *cfgname)
191 {
192   p->cfg = GNUNET_CONFIGURATION_create ();
193 #if START_ARM
194   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
195                                         "gnunet-service-arm",
196 #if VERBOSE_ARM
197                                         "-L", "DEBUG",
198 #endif
199                                         "-c", cfgname, NULL);
200 #endif
201   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
202
203 }
204
205 static void
206 run (void *cls,
207      struct GNUNET_SCHEDULER_Handle *s,
208      char *const *args,
209      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
210 {
211   GNUNET_assert (ok == 1);
212   OKPP;
213   sched = s;
214
215   die_task = GNUNET_SCHEDULER_add_delayed (sched,
216       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
217
218   setup_peer (&p1, "test_dht_api_peer1.conf");
219
220   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get, &p1);
221 }
222
223 static int
224 check ()
225 {
226
227   char *const argv[] = { "test-dht-api",
228     "-c",
229     "test_dht_api_data.conf",
230 #if VERBOSE
231     "-L", "DEBUG",
232 #endif
233     NULL
234   };
235
236   struct GNUNET_GETOPT_CommandLineOption options[] = {
237     GNUNET_GETOPT_OPTION_END
238   };
239
240   ok = 1;
241   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
242                       argv, "test-dht-api", "nohelp",
243                       options, &run, &ok);
244   stop_arm (&p1);
245   return ok;
246 }
247
248
249 int
250 main (int argc, char *argv[])
251 {
252   int ret;
253 #ifdef MINGW
254   return GNUNET_SYSERR;
255 #endif
256
257   GNUNET_log_setup ("test-dht-api",
258 #if VERBOSE
259                     "DEBUG",
260 #else
261                     "WARNING",
262 #endif
263                     NULL);
264   ret = check ();
265
266   //GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
267
268   return ret;
269 }
270
271 /* end of test_dht_api.c */