Renamed functions, make valgrind stop complaining about memory leaks by explicitly...
[oweals/gnunet.git] / src / mesh / test_mesh_path_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 3, 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 /**
22  * @file mesh/test_mesh_path.c
23  * @brief test mesh path: test of path management api
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_mesh_service_new.h"
32 #include "mesh.h"
33 #include "mesh_tunnel_tree.h"
34
35 #define VERBOSE 1
36
37 int failed;
38 int cb_call;
39 struct GNUNET_PeerIdentity* pi[10];
40 struct MeshTunnelTree *tree;
41
42 void
43 cb (const struct MeshTunnelTreeNode *n)
44 {
45   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Desconnected %u\n", n->peer);
46   if(0 == cb_call)
47   {
48     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test:    and it shouldn't!\n");
49     failed++;
50   }
51   cb_call--;
52 }
53
54
55 void
56 finish(void)
57 {
58   unsigned int i;
59
60   for (i = 0; i < 10; i++)
61   {
62     GNUNET_free(pi[i]);
63   }
64   tree_destroy(tree);
65   exit(0);
66 }
67
68 /**
69  * Convert an integer int to a peer identity
70  */
71 static struct GNUNET_PeerIdentity *
72 get_pi (uint32_t id)
73 {
74   struct GNUNET_PeerIdentity *pi;
75
76   pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
77   pi->hashPubKey.bits[0] = id;
78   return pi;
79 }
80
81
82 int
83 main (int argc, char *argv[])
84 {
85   struct MeshTunnelTreeNode *node;
86   struct MeshTunnelTreeNode *node2;
87   struct MeshPeerPath *path[10];
88   unsigned int i;
89
90   failed = 0;
91   cb_call = 0;
92   GNUNET_log_setup ("test_mesh_api_path",
93 #if VERBOSE
94                     "DEBUG",
95 #else
96                     "WARNING",
97 #endif
98                     NULL);
99   for (i = 0; i < 10; i++)
100   {
101       pi[i] = get_pi(i);
102       GNUNET_break (i != GNUNET_PEER_intern(pi[i]));
103       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i,
104                  GNUNET_h2s(&pi[i]->hashPubKey));
105   }
106   tree = GNUNET_malloc(sizeof(struct MeshTunnelTree));
107   tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
108   tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
109   tree->root->peer = 0;
110   tree->me = tree->root;
111   path[0] = GNUNET_malloc(sizeof(struct MeshPeerPath));
112   path[0]->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 4);
113   path[0]->peers[0] = 0;
114   path[0]->peers[1] = 1;
115   path[0]->peers[2] = 2;
116   path[0]->peers[3] = 3;
117   path[0]->length = 4;
118
119   finish();
120   tree_add_path(tree, path[0], &cb);
121   path[1] = tree_get_path_to_peer(tree, 3);
122   if (path[0]->length != path[1]->length ||
123       memcmp(path[0]->peers, path[1]->peers, path[0]->length) != 0)
124   {
125     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
126     failed++;
127   }
128   path_destroy(path[1]);
129   node = tree_find_peer(tree->root, 3);
130   if (node->peer != 3)
131   {
132     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
133     failed++;
134   }
135   if (node->status != MESH_PEER_SEARCHING)
136   {
137     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
138     failed++;
139   }
140   node->status = MESH_PEER_READY;
141   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
142   {
143     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
144     failed++;
145   }
146   return 0;
147
148   node = tree_find_peer(tree->root, 2);
149   if (node->peer != 2)
150   {
151     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
152     failed++;
153   }
154   if (node->status != MESH_PEER_RELAY)
155   {
156     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
157     failed++;
158   }
159   if (node->nchildren != 1)
160   {
161     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
162     failed++;
163   }
164   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
165   {
166     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
167     failed++;
168   }
169
170   node = tree_find_peer(tree->root, 1);
171   if (node->peer != 1)
172   {
173     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
174     failed++;
175   }
176   if (node->status != MESH_PEER_RELAY)
177   {
178     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
179     failed++;
180   }
181   if (node->nchildren != 1)
182   {
183     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
184     failed++;
185   }
186
187   path[0]->length--;
188   tree_add_path(tree, path[0], &cb);
189
190   node = tree_find_peer(tree->root, 2);
191   if (node->peer != 2)
192   {
193     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
194     failed++;
195   }
196   if (node->status != MESH_PEER_SEARCHING)
197   {
198     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
199     failed++;
200   }
201   if (node->nchildren != 1)
202   {
203     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
204     failed++;
205   }
206   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
207   {
208     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
209     failed++;
210   }
211   if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
212   {
213     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
214     failed++;
215   }
216
217   node = tree_find_peer(tree->root, 1);
218   if (node->peer != 1)
219   {
220     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
221     failed++;
222   }
223   if (node->status != MESH_PEER_RELAY)
224   {
225     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
226     failed++;
227   }
228   if (node->nchildren != 1)
229   {
230     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
231     failed++;
232   }
233
234   path[0]->length++;
235   path[0]->peers[3] = 4;
236   tree_add_path(tree, path[0], &cb);
237
238   node = tree_find_peer(tree->root, 2);
239   if (node->peer != 2)
240   {
241     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
242     failed++;
243   }
244   if (node->status != MESH_PEER_SEARCHING)
245   {
246     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
247     failed++;
248   }
249   if (node->nchildren != 2)
250   {
251     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
252     failed++;
253   }
254   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
255   {
256     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
257     failed++;
258   }
259   if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 1)
260   {
261     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
262     failed++;
263   }
264
265   node = tree_find_peer(tree->root, 1);
266   if (node->peer != 1)
267   {
268     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
269     failed++;
270   }
271   if (node->status != MESH_PEER_RELAY)
272   {
273     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
274     failed++;
275   }
276   if (node->nchildren != 1)
277   {
278     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
279     failed++;
280   }
281
282   node = tree_find_peer(tree->root, 4);
283   if (node->peer != 4)
284   {
285     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
286     failed++;
287   }
288   node->status = MESH_PEER_READY;
289   cb_call = 1;
290   node2 = tree_del_path(tree, 4, &cb);
291   if (cb_call != 0)
292   {
293     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
294     failed++;
295   }
296   if (node2->peer != 4)
297   {
298     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
299     failed++;
300   }
301 //   GNUNET_free(node2); FIXME destroy
302   node = tree_find_peer(tree->root, 2);
303   if (node->peer != 2)
304   {
305     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
306     failed++;
307   }
308   if (node->status != MESH_PEER_SEARCHING)
309   {
310     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
311     failed++;
312   }
313   if (node->nchildren != 1)
314   {
315     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
316     failed++;
317   }
318
319   path[0]->length = 2;
320   path[0]->peers[1] = 3;
321   cb_call = 1;
322   tree_add_path(tree, path[0], cb);
323   if (cb_call != 0)
324   {
325     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
326     failed++;
327   }
328   node = tree_find_peer(tree->root, 2);
329   if (node->peer != 2)
330   {
331     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
332     failed++;
333   }
334   if (node->status != MESH_PEER_SEARCHING)
335   {
336     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
337     failed++;
338   }
339   if (node->nchildren != 0)
340   {
341     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
342     failed++;
343   }
344   node = tree_find_peer(tree->root, 3);
345   if (node->peer != 3)
346   {
347     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
348     failed++;
349   }
350   if (node->status != MESH_PEER_SEARCHING)
351   {
352     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
353     failed++;
354   }
355   if (node->nchildren != 0)
356   {
357     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
358     failed++;
359   }
360   if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
361   {
362     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
363     failed++;
364   }
365   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 3)
366   {
367     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
368     failed++;
369   }
370
371
372   if (failed > 0)
373   {
374     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
375     return 1;
376   }
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
378   return 0;
379 }