Added debug print of tree structure
[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: Disconnected %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   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test:  Finishing...\n");
61   for (i = 0; i < 10; i++)
62   {
63     GNUNET_free(pi[i]);
64   }
65   tree_destroy(tree);
66   exit(0);
67 }
68
69 /**
70  * Convert an integer int to a peer identity
71  */
72 static struct GNUNET_PeerIdentity *
73 get_pi (uint32_t id)
74 {
75   struct GNUNET_PeerIdentity *pi;
76
77   pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
78   pi->hashPubKey.bits[0] = id;
79   return pi;
80 }
81
82
83 int
84 main (int argc, char *argv[])
85 {
86   struct MeshTunnelTreeNode *node;
87   struct MeshTunnelTreeNode *node2;
88   struct MeshPeerPath *path;
89   struct MeshPeerPath *path1;
90   unsigned int i;
91
92   failed = 0;
93   cb_call = 0;
94   GNUNET_log_setup ("test_mesh_api_path",
95 #if VERBOSE
96                     "DEBUG",
97 #else
98                     "WARNING",
99 #endif
100                     NULL);
101   for (i = 0; i < 10; i++)
102   {
103       pi[i] = get_pi(i);
104       GNUNET_break (i != GNUNET_PEER_intern(pi[i]));
105       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i,
106                  GNUNET_h2s(&pi[i]->hashPubKey));
107   }
108   tree = GNUNET_malloc(sizeof(struct MeshTunnelTree));
109   tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
110   tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
111   tree->root->peer = 0;
112   tree->me = tree->root;
113   path = GNUNET_malloc(sizeof(struct MeshPeerPath));
114   path->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 4);
115   path->peers[0] = 0;
116   path->peers[1] = 1;
117   path->peers[2] = 2;
118   path->peers[3] = 3;
119   path->length = 4;
120
121   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 0 1 2 3\n");
122   tree_add_path(tree, path, &cb);
123   path1 = tree_get_path_to_peer(tree, 3);
124   if (path->length != path1->length ||
125       memcmp(path->peers, path1->peers, path->length) != 0)
126   {
127     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
128     failed++;
129   }
130   path_destroy(path1);
131   node = tree_find_peer(tree->root, 3);
132   if (node->peer != 3)
133   {
134     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
135     failed++;
136   }
137   if (node->status != MESH_PEER_SEARCHING)
138   {
139     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
140     failed++;
141   }
142   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
143   {
144     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
145     failed++;
146   }
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   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 0 1 2\n");
188   path->length--;
189   tree_add_path(tree, path, &cb);
190   
191   node = tree_find_peer(tree->root, 2);
192   if (node->peer != 2)
193   {
194     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
195     failed++;
196   }
197   if (node->status != MESH_PEER_SEARCHING)
198   {
199     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
200     failed++;
201   }
202   if (node->nchildren != 1)
203   {
204     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
205     failed++;
206   }
207   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
208   {
209     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
210     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "3 GOT: %u\n", GNUNET_PEER_search(path_get_first_hop(tree, 3)));
211     failed++;
212   }
213   if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
214   {
215     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
216     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "2 GOT: %u\n", GNUNET_PEER_search(path_get_first_hop(tree, 2)));
217     failed++;
218   }
219
220   node = tree_find_peer(tree->root, 1);
221   if (node->peer != 1)
222   {
223     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
224     failed++;
225   }
226   if (node->status != MESH_PEER_RELAY)
227   {
228     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
229     failed++;
230   }
231   if (node->nchildren != 1)
232   {
233     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
234     failed++;
235   }
236
237   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path...\n");
238   path->length++;
239   path->peers[3] = 4;
240   tree_add_path(tree, path, &cb);
241
242   path_destroy(path);
243   tree_debug(tree);
244   finish();
245
246   node = tree_find_peer(tree->root, 2);
247   if (node->peer != 2)
248   {
249     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
250     failed++;
251   }
252   if (node->status != MESH_PEER_SEARCHING)
253   {
254     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
255     failed++;
256   }
257   if (node->nchildren != 2)
258   {
259     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
260     failed++;
261   }
262   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
263   {
264     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
265     failed++;
266   }
267   if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 1)
268   {
269     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
270     failed++;
271   }
272
273   node = tree_find_peer(tree->root, 1);
274   if (node->peer != 1)
275   {
276     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
277     failed++;
278   }
279   if (node->status != MESH_PEER_RELAY)
280   {
281     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
282     failed++;
283   }
284   if (node->nchildren != 1)
285   {
286     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
287     failed++;
288   }
289
290   node = tree_find_peer(tree->root, 4);
291   if (node->peer != 4)
292   {
293     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
294     failed++;
295   }
296   
297   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path...\n");
298   node->status = MESH_PEER_READY;
299   cb_call = 1;
300   node2 = tree_del_path(tree, 4, &cb);
301   if (cb_call != 0)
302   {
303     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
304     failed++;
305   }
306   if (node2->peer != 4)
307   {
308     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
309     failed++;
310   }
311   
312   node = tree_find_peer(tree->root, 2);
313   if (node->peer != 2)
314   {
315     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
316     failed++;
317   }
318   if (node->status != MESH_PEER_SEARCHING)
319   {
320     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
321     failed++;
322   }
323   if (node->nchildren != 1)
324   {
325     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
326     failed++;
327   }
328
329   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
330   tree_node_destroy(node2);
331
332   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding new shorter first path...\n");
333   path->length = 2;
334   path->peers[1] = 3;
335   cb_call = 1;
336   tree_add_path(tree, path, cb);
337   if (cb_call != 0)
338   {
339     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
340     failed++;
341   }
342   node = tree_find_peer(tree->root, 2);
343   if (node->peer != 2)
344   {
345     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
346     failed++;
347   }
348   if (node->status != MESH_PEER_SEARCHING)
349   {
350     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
351     failed++;
352   }
353   if (node->nchildren != 0)
354   {
355     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
356     failed++;
357   }
358   node = tree_find_peer(tree->root, 3);
359   if (node->peer != 3)
360   {
361     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
362     failed++;
363   }
364   if (node->status != MESH_PEER_SEARCHING)
365   {
366     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
367     failed++;
368   }
369   if (node->nchildren != 0)
370   {
371     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
372     failed++;
373   }
374   if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
375   {
376     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
377     failed++;
378   }
379   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 3)
380   {
381     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
382     failed++;
383   }
384
385
386   if (failed > 0)
387   {
388     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
389     return 1;
390   }
391   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
392   return 0;
393 }