WiP
[oweals/gnunet.git] / src / dht / test_dhtlog.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 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 src/dht/test_dhtlog.c
22  * @brief Test of the dhtlog service
23  * @author Nathan Evans
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "dhtlog.h"
30
31 #define VERBOSE GNUNET_NO
32
33 static int ok;
34
35 #define CHECK(a) if (a != GNUNET_OK) return a
36 /**
37  * Actual test of the service operations
38  */
39 static int
40 test (struct GNUNET_DHTLOG_Handle * api)
41 {
42   struct GNUNET_PeerIdentity p1;
43   struct GNUNET_PeerIdentity p2;
44   struct GNUNET_PeerIdentity p3;
45   struct GNUNET_PeerIdentity p4;
46   struct GNUNET_DHTLOG_TrialInfo trial_info;
47   GNUNET_HashCode k1;
48   GNUNET_HashCode k2;
49   int ret;
50   unsigned int i = 42;
51   unsigned long long sqlqueryuid;
52   unsigned long long sqlrouteuid = 0;
53   unsigned long long nodeuid = 0;
54   unsigned long long internaluid = 1010223344LL;
55   unsigned long long dhtkeyuid = 0;
56   memset (&p1.hashPubKey, 3, sizeof (GNUNET_HashCode));
57   memset (&p2.hashPubKey, 4, sizeof (GNUNET_HashCode));
58   memset (&p3.hashPubKey, 5, sizeof (GNUNET_HashCode));
59   memset (&p4.hashPubKey, 6, sizeof (GNUNET_HashCode));
60
61   memset (&k1, 0, sizeof (GNUNET_HashCode));
62   memset (&k2, 1, sizeof (GNUNET_HashCode));
63   memset(&trial_info, 0, sizeof(struct GNUNET_DHTLOG_TrialInfo));
64   trial_info.other_identifier = 777;
65   trial_info.num_nodes = i;
66   trial_info.topology = 5;
67   trial_info.blacklist_topology = 4;
68   trial_info.connect_topology = 3;
69   trial_info.connect_topology_option = 2;
70   trial_info.connect_topology_option_modifier = .75;
71   trial_info.topology_percentage = .25;
72   trial_info.topology_probability = .5;
73   trial_info.puts = 42;
74   trial_info.gets = 14;
75   trial_info.concurrent = 5;
76   trial_info.settle_time = 1;
77   trial_info.num_rounds = 12;
78   trial_info.malicious_getters = 0;
79   trial_info.malicious_putters = 0;
80   trial_info.malicious_droppers = 0;
81   trial_info.malicious_get_frequency = 1;
82   trial_info.malicious_put_frequency = 0;
83   trial_info.stop_closest = 1;
84   trial_info.stop_found = 0;
85   trial_info.strict_kademlia = 1;
86   trial_info.message = GNUNET_strdup("TEST INSERT_TRIAL");
87   ret =
88     api->insert_trial (&trial_info);
89   GNUNET_free(trial_info.message);
90   CHECK(ret);
91 #if VERBOSE
92   fprintf(stderr, "Insert trial succeeded!\n");
93 #endif
94   ret = api->insert_topology(500);
95   CHECK(ret);
96 #if VERBOSE
97   fprintf(stderr, "Insert topology succeeded!\n");
98 #endif
99   ret = api->insert_node (&nodeuid, &p1);
100   CHECK(ret);
101   ret = api->insert_node (&nodeuid, &p2);
102   CHECK(ret);
103   ret = api->insert_node (&nodeuid, &p3);
104   CHECK(ret);
105   ret = api->insert_node (&nodeuid, &p4);
106   CHECK(ret);
107 #if VERBOSE
108   fprintf(stderr, "Insert node succeeded!\n");
109 #endif
110   ret = api->set_malicious(&p1);
111   CHECK(ret);
112 #if VERBOSE
113   fprintf(stderr, "Set malicious succeeded!\n");
114 #endif
115   ret = api->insert_topology(0);
116   CHECK(ret);
117 #if VERBOSE
118   fprintf(stderr, "Insert topology succeeded!\n");
119 #endif
120   ret = api->insert_extended_topology(&p1, &p2);
121   CHECK(ret);
122   ret = api->insert_extended_topology(&p3, &p4);
123   CHECK(ret);
124 #if VERBOSE
125   fprintf(stderr, "Insert extended topology succeeded!\n");
126 #endif
127   ret = api->update_topology(101);
128   CHECK(ret);
129 #if VERBOSE
130   fprintf(stderr, "Update topology succeeded!\n");
131 #endif
132   ret = api->insert_dhtkey (&dhtkeyuid, &k1);
133   CHECK(ret);
134   ret = api->insert_dhtkey (&dhtkeyuid, &k2);
135   CHECK(ret);
136 #if VERBOSE
137   fprintf(stderr, "Insert dhtkey succeeded!\n");
138 #endif
139   ret = api->insert_query (&sqlqueryuid, internaluid, 2, 4, 0, &p2, &k1);
140   CHECK(ret);
141 #if VERBOSE
142   fprintf(stderr, "Insert query succeeded!\n");
143 #endif
144   ret =
145     api->insert_route (&sqlrouteuid, sqlqueryuid, 1, 1, DHTLOG_GET, &p1, &k2,
146                        &p4, &p3);
147   CHECK(ret);
148   ret =
149     api->insert_route (&sqlrouteuid, sqlqueryuid, 2, 0, DHTLOG_PUT, &p3, &k1,
150                        &p4, &p2);
151   CHECK(ret);
152   ret =
153     api->insert_route (&sqlrouteuid, sqlqueryuid, 3, 1, DHTLOG_ROUTE, &p3, &k2,
154                        &p2, NULL);
155   CHECK(ret);
156   ret =
157     api->insert_route (&sqlrouteuid, sqlqueryuid, 4, 7, DHTLOG_ROUTE, &p3, &k2,
158                        NULL, NULL);
159   CHECK(ret);
160 #if VERBOSE
161   fprintf(stderr, "Insert route succeeded!\n");
162 #endif
163   sleep (1);
164   ret = api->insert_stat(&p1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
165   CHECK(ret);
166   ret = api->insert_stat(&p2, 12, 23, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
167   CHECK(ret);
168 #if VERBOSE
169   fprintf(stderr, "Insert stat succeeded!\n");
170 #endif
171   ret = api->update_trial (787);
172   CHECK(ret);
173 #if VERBOSE
174   fprintf(stderr, "Update trial succeeded!\n");
175 #endif
176   ret = api->add_generic_stat (&p2, "nonsense", "section", 77765);
177   CHECK(ret);
178 #if VERBOSE
179   fprintf(stderr, "Insert generic stat succeeded!\n");
180 #endif
181   ret = api->insert_round(401, 507);
182   CHECK(ret);
183   ret = api->insert_round_details(402, 507, 1123, 985);
184   CHECK(ret);
185 #if VERBOSE
186   fprintf(stderr, "Insert round succeeded!\n");
187 #endif
188   return 0;
189 }
190
191
192
193 static void
194 run (void *cls,
195      char *const *args,
196      const char *cfgfile,
197      const struct GNUNET_CONFIGURATION_Handle *cfg)
198 {
199   struct GNUNET_DHTLOG_Handle *api;
200   ok = 0;
201   api = GNUNET_DHTLOG_connect (cfg);
202
203   if (api == NULL)
204     {
205       ok = 1;
206       return;
207     }
208   ok = test(api);
209
210   GNUNET_DHTLOG_disconnect(api);
211 }
212
213
214 static int
215 check ()
216 {
217   char *const argv[] = { "test-dhtlog-api",
218     "-c",
219     "test_dhtlog_data.conf",
220 #if VERBOSE
221     "-L", "DEBUG",
222 #endif
223     NULL
224   };
225   struct GNUNET_GETOPT_CommandLineOption options[] = {
226     GNUNET_GETOPT_OPTION_END
227   };
228   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
229                       argv, "test-dhtlog-api", "nohelp",
230                       options, &run, NULL);
231   if (ok != 0)
232     fprintf (stderr, "Test failed with error code: %d\n", ok);
233   return ok;
234 }
235
236
237 int
238 main (int argc, char *argv[])
239 {
240   int ret;
241
242   GNUNET_log_setup ("test-datacache-api",
243 #if VERBOSE
244                     "DEBUG",
245 #else
246                     "WARNING",
247 #endif
248                     NULL);
249   ret = check ();
250
251   return ret;
252 }
253
254 /* end of test_dhtlog.c */