klocwork fixes, dht routing changes (hopefully fixes)
[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_YES
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
47   GNUNET_HashCode k1;
48   GNUNET_HashCode k2;
49
50   int ret;
51   unsigned int i = 42;
52   unsigned long long trialuid;
53   unsigned long long sqlqueryuid;
54   unsigned long long sqlrouteuid = 0;
55   unsigned long long nodeuid = 0;
56   unsigned long long internaluid = 1010223344LL;
57   unsigned long long dhtkeyuid = 0;
58   memset (&p1.hashPubKey, 3, sizeof (GNUNET_HashCode));
59   memset (&p2.hashPubKey, 4, sizeof (GNUNET_HashCode));
60   memset (&p3.hashPubKey, 5, sizeof (GNUNET_HashCode));
61   memset (&p4.hashPubKey, 6, sizeof (GNUNET_HashCode));
62
63   memset (&k1, 0, sizeof (GNUNET_HashCode));
64   memset (&k2, 1, sizeof (GNUNET_HashCode));
65
66   ret =
67     api->insert_trial (&trialuid, i, 5, 4, 3, 2,
68                        .75, .25, .5, 42, 14,
69                        5, 1, 12, 0, 0, 0, 1, 0, 1,
70                        0, 1, 0, "TEST INSERT TRIAL");
71   CHECK(ret);
72   ret = api->insert_topology(500);
73   CHECK(ret);
74   ret = api->insert_node (&nodeuid, &p1);
75   CHECK(ret);
76   ret = api->insert_node (&nodeuid, &p2);
77   CHECK(ret);
78   ret = api->insert_node (&nodeuid, &p3);
79   CHECK(ret);
80   ret = api->insert_node (&nodeuid, &p4);
81   CHECK(ret);
82   ret = api->set_malicious(&p1);
83   CHECK(ret);
84   ret = api->insert_topology(0);
85   CHECK(ret);
86   ret = api->insert_extended_topology(&p1, &p2);
87   CHECK(ret);
88   ret = api->insert_extended_topology(&p3, &p4);
89   CHECK(ret);
90   ret = api->update_topology(101);
91   CHECK(ret);
92   ret = api->insert_dhtkey (&dhtkeyuid, &k1);
93   CHECK(ret);
94   ret = api->insert_dhtkey (&dhtkeyuid, &k2);
95   CHECK(ret);
96   ret = api->insert_query (&sqlqueryuid, internaluid, 2, 4, 0, &p2, &k1);
97   CHECK(ret);
98   ret =
99     api->insert_route (&sqlrouteuid, sqlqueryuid, 1, 1, DHTLOG_GET, &p1, &k2,
100                        &p4, &p3);
101   CHECK(ret);
102   ret =
103     api->insert_route (&sqlrouteuid, sqlqueryuid, 2, 0, DHTLOG_PUT, &p3, &k1,
104                        &p4, &p2);
105   CHECK(ret);
106   ret =
107     api->insert_route (&sqlrouteuid, sqlqueryuid, 3, 1, DHTLOG_ROUTE, &p3, &k2,
108                        &p2, NULL);
109   CHECK(ret);
110   ret =
111     api->insert_route (&sqlrouteuid, sqlqueryuid, 4, 7, DHTLOG_ROUTE, &p3, &k2,
112                        NULL, NULL);
113   CHECK(ret);
114   sleep (1);
115   ret = api->insert_stat(&p1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
116   CHECK(ret);
117   ret = api->insert_stat(&p2, 12, 23, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
118   CHECK(ret);
119   ret = api->update_trial (trialuid, 787);
120   CHECK(ret);
121   ret = api->add_generic_stat (&p2, "nonsense", "section", 77765);
122   CHECK(ret);
123   return 0;
124 }
125
126
127
128 static void
129 run (void *cls,
130      struct GNUNET_SCHEDULER_Handle *sched,
131      char *const *args,
132      const char *cfgfile,
133      const struct GNUNET_CONFIGURATION_Handle *cfg)
134 {
135   struct GNUNET_DHTLOG_Handle *api;
136   ok = 0;
137   api = GNUNET_DHTLOG_connect (cfg);
138
139   if (api == NULL)
140     {
141       ok = 1;
142       return;
143     }
144   ok = test(api);
145
146   GNUNET_DHTLOG_disconnect(api);
147 }
148
149
150 static int
151 check ()
152 {
153   char *const argv[] = { "test-dhtlog-api",
154     "-c",
155     "test_dhtlog_data.conf",
156 #if VERBOSE
157     "-L", "DEBUG",
158 #endif
159     NULL
160   };
161   struct GNUNET_GETOPT_CommandLineOption options[] = {
162     GNUNET_GETOPT_OPTION_END
163   };
164   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
165                       argv, "test-dhtlog-api", "nohelp",
166                       options, &run, NULL);
167   if (ok != 0)
168     fprintf (stderr, "Test failed with error code: %d\n", ok);
169   return ok;
170 }
171
172
173 int
174 main (int argc, char *argv[])
175 {
176   int ret;
177
178   GNUNET_log_setup ("test-datacache-api",
179 #if VERBOSE
180                     "DEBUG",
181 #else
182                     "WARNING",
183 #endif
184                     NULL);
185   ret = check ();
186
187   return ret;
188 }
189
190 /* end of test_dhtlog.c */