DHT api switch adjustments
[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
57   memset (&p1.hashPubKey, 3, sizeof (GNUNET_HashCode));
58   memset (&p2.hashPubKey, 4, sizeof (GNUNET_HashCode));
59   memset (&p3.hashPubKey, 5, sizeof (GNUNET_HashCode));
60   memset (&p4.hashPubKey, 6, sizeof (GNUNET_HashCode));
61
62   memset (&k1, 0, sizeof (GNUNET_HashCode));
63   memset (&k2, 1, sizeof (GNUNET_HashCode));
64   memset (&trial_info, 0, sizeof (struct GNUNET_DHTLOG_TrialInfo));
65   trial_info.other_identifier = 777;
66   trial_info.num_nodes = i;
67   trial_info.topology = 5;
68   trial_info.blacklist_topology = 4;
69   trial_info.connect_topology = 3;
70   trial_info.connect_topology_option = 2;
71   trial_info.connect_topology_option_modifier = .75;
72   trial_info.topology_percentage = .25;
73   trial_info.topology_probability = .5;
74   trial_info.puts = 42;
75   trial_info.gets = 14;
76   trial_info.concurrent = 5;
77   trial_info.settle_time = 1;
78   trial_info.num_rounds = 12;
79   trial_info.malicious_getters = 0;
80   trial_info.malicious_putters = 0;
81   trial_info.malicious_droppers = 0;
82   trial_info.malicious_get_frequency = 1;
83   trial_info.malicious_put_frequency = 0;
84   trial_info.stop_closest = 1;
85   trial_info.stop_found = 0;
86   trial_info.strict_kademlia = 1;
87   trial_info.message = GNUNET_strdup ("TEST INSERT_TRIAL");
88   ret = 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,
154                          &k2, &p2, NULL);
155   CHECK (ret);
156   ret =
157       api->insert_route (&sqlrouteuid, sqlqueryuid, 4, 7, DHTLOG_ROUTE, &p3,
158                          &k2, NULL, NULL);
159   CHECK (ret);
160 #if VERBOSE
161   fprintf (stderr, "Insert route succeeded!\n");
162 #endif
163   sleep (1);
164   ret =
165       api->insert_stat (&p1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
166                         17);
167   CHECK (ret);
168   ret =
169       api->insert_stat (&p2, 12, 23, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
170                         25, 26, 27);
171   CHECK (ret);
172 #if VERBOSE
173   fprintf (stderr, "Insert stat succeeded!\n");
174 #endif
175   ret = api->update_trial (787);
176   CHECK (ret);
177 #if VERBOSE
178   fprintf (stderr, "Update trial succeeded!\n");
179 #endif
180   ret = api->add_generic_stat (&p2, "nonsense", "section", 77765);
181   CHECK (ret);
182 #if VERBOSE
183   fprintf (stderr, "Insert generic stat succeeded!\n");
184 #endif
185   ret = api->insert_round (401, 507);
186   CHECK (ret);
187   ret = api->insert_round_details (402, 507, 1123, 985);
188   CHECK (ret);
189 #if VERBOSE
190   fprintf (stderr, "Insert round succeeded!\n");
191 #endif
192   return 0;
193 }
194
195
196
197 static void
198 run (void *cls, char *const *args, const char *cfgfile,
199      const struct GNUNET_CONFIGURATION_Handle *cfg)
200 {
201   struct GNUNET_DHTLOG_Handle *api;
202
203   ok = 0;
204   api = GNUNET_DHTLOG_connect (cfg);
205
206   if (api == NULL)
207   {
208     ok = 1;
209     return;
210   }
211   ok = test (api);
212
213   GNUNET_DHTLOG_disconnect (api);
214 }
215
216
217 static int
218 check ()
219 {
220   char *const argv[] = { "test-dhtlog-api",
221     "-c",
222     "test_dhtlog_data.conf",
223 #if VERBOSE
224     "-L", "DEBUG",
225 #endif
226     NULL
227   };
228   struct GNUNET_GETOPT_CommandLineOption options[] = {
229     GNUNET_GETOPT_OPTION_END
230   };
231   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
232                       "test-dhtlog-api", "nohelp", options, &run, NULL);
233   if (ok != 0)
234     fprintf (stderr, "Test failed with error code: %d\n", ok);
235   return ok;
236 }
237
238
239 int
240 main (int argc, char *argv[])
241 {
242   int ret;
243
244   GNUNET_log_setup ("test-datacache-api",
245 #if VERBOSE
246                     "DEBUG",
247 #else
248                     "WARNING",
249 #endif
250                     NULL);
251   ret = check ();
252
253   return ret;
254 }
255
256 /* end of test_dhtlog.c */