dht api, shell dht service, base of future test case.... not yet working
[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 #if START_ARM
55   pid_t arm_pid;
56 #endif
57 };
58
59 static struct PeerContext p1;
60
61 static struct GNUNET_SCHEDULER_Handle *sched;
62
63 static int ok;
64
65 GNUNET_SCHEDULER_TaskIdentifier die_task;
66
67 #if VERBOSE
68 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
69 #else
70 #define OKPP do { ok++; } while (0)
71 #endif
72
73
74 static void
75 end ()
76 {
77   /* do work here */
78   GNUNET_SCHEDULER_cancel (sched, die_task);
79
80   GNUNET_DHT_disconnect (p1.dht_handle);
81
82   die_task = GNUNET_SCHEDULER_NO_TASK;
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning success!\n");
84   ok = 0;
85 }
86
87 static void
88 stop_arm (struct PeerContext *p)
89 {
90 #if START_ARM
91   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
92     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
93   GNUNET_OS_process_wait (p->arm_pid);
94 #endif
95   GNUNET_CONFIGURATION_destroy (p->cfg);
96 }
97
98
99 static void
100 end_badly ()
101 {
102   /* do work here */
103 #if VERBOSE
104   fprintf(stderr, "Ending on an unhappy note.\n");
105 #endif
106
107   GNUNET_DHT_disconnect (p1.dht_handle);
108
109   ok = 1;
110   return;
111 }
112
113 /**
114  * Signature of the main function of a task.
115  *
116  * @param cls closure
117  * @param tc context information (why was this task triggered now)
118  */
119 void test_get (void *cls,
120                const struct GNUNET_SCHEDULER_TaskContext * tc)
121 {
122   struct PeerContext *peer = cls;
123   GNUNET_HashCode hash;
124   memset(&hash, 42, sizeof(GNUNET_HashCode));
125
126   peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg);
127   GNUNET_assert (peer->dht_handle != NULL);
128
129   GNUNET_DHT_get_start(peer->dht_handle, 42, &hash, NULL, NULL);
130
131   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10), &end, &p1);
132 }
133
134 static void
135 setup_peer (struct PeerContext *p, const char *cfgname)
136 {
137   p->cfg = GNUNET_CONFIGURATION_create ();
138 #if START_ARM
139   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
140                                         "gnunet-service-arm",
141 #if VERBOSE_ARM
142                                         "-L", "DEBUG",
143 #endif
144                                         "-c", cfgname, NULL);
145 #endif
146   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
147
148 }
149
150 static void
151 run (void *cls,
152      struct GNUNET_SCHEDULER_Handle *s,
153      char *const *args,
154      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
155 {
156   GNUNET_assert (ok == 1);
157   OKPP;
158   sched = s;
159
160   die_task = GNUNET_SCHEDULER_add_delayed (sched,
161       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
162
163   setup_peer (&p1, "test_dht_api_peer1.conf");
164
165   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &test_get, &p1);
166 }
167
168 static int
169 check ()
170 {
171
172   char *const argv[] = { "test-dht-api",
173     "-c",
174     "test_dht_api_data.conf",
175 #if VERBOSE
176     "-L", "DEBUG",
177 #endif
178     NULL
179   };
180
181   struct GNUNET_GETOPT_CommandLineOption options[] = {
182     GNUNET_GETOPT_OPTION_END
183   };
184
185   ok = 1;
186   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
187                       argv, "test-dht-api", "nohelp",
188                       options, &run, &ok);
189   stop_arm (&p1);
190   return ok;
191 }
192
193
194 int
195 main (int argc, char *argv[])
196 {
197   int ret;
198 #ifdef MINGW
199   return GNUNET_SYSERR;
200 #endif
201
202   GNUNET_log_setup ("test-dht-api",
203 #if VERBOSE
204                     "DEBUG",
205 #else
206                     "WARNING",
207 #endif
208                     NULL);
209   ret = check ();
210
211   //GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
212
213   return ret;
214 }
215
216 /* end of test_dht_api.c */