- add testcase for whole-tree iterator
[oweals/gnunet.git] / src / mesh / test_mesh_tree_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_tree_api.c
23  * @brief test mesh tree api: test of tree & 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.h"
32 #include "mesh.h"
33 #ifndef MESH_TUNNEL_TREE_C
34 #include "mesh_tunnel_tree.c"
35 #define MESH_TUNNEL_TREE_C
36 #endif
37
38 static int failed;
39 static int cb_call;
40 static struct GNUNET_PeerIdentity *pi[10];
41 static struct MeshTunnelTree *tree;
42
43
44 /**
45  * Whole tree iterator.
46  *
47  * @param cls Closure (unused).
48  * @param peer_id Short ID of the node.
49  * @param parent_id Short ID of the parent node.
50  */
51 static void
52 tree_cb (void *cls, GNUNET_PEER_Id peer_id, GNUNET_PEER_Id parent_id)
53 {
54   fprintf (stdout, "%u -> %u\n", peer_id, parent_id);;
55 }
56
57
58 /**
59  * Node children iterator.
60  *
61  * @param cls Closure (unused).
62  * @param peer_idShort ID of the child.
63  */
64 static void
65 cb (void *cls, GNUNET_PEER_Id peer_id)
66 {
67   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", peer_id);
68   if (0 == cb_call)
69   {
70     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:      and it shouldn't!\n");
71     failed++;
72   }
73   cb_call--;
74 }
75
76
77 /**
78  * Print debug information about the state of the tree.
79  *
80  * @param tree Tree to debug-print.
81  */
82 static void
83 test_debug (struct MeshTunnelTree *tree)
84 {
85   tree_debug (tree);
86   tree_iterate_all (tree, &tree_cb, NULL);
87 }
88
89 /**
90  * Iterator over a tunnel to build a message containing all peers the
91  * tunnel's tree.
92  *
93  * @param cls Closure (pointer to pointer of message being built).
94  * @param peer Short ID of a peer.
95  * @param parent Short ID of the @c peer 's parent.
96  *
97  * @return GNUNET_YES, to keep iterating.
98  */
99 static int
100 monitor_tunnel_iterator (void *cls,
101                          GNUNET_PEER_Id peer,
102                          GNUNET_PEER_Id parent)
103 {
104   struct GNUNET_MESH_LocalMonitor **msg = cls;
105   struct GNUNET_PeerIdentity *pid;
106   size_t size;
107   
108   size = ntohs (*msg->header.size);
109   size += sizeof (struct GNUNET_PeerIdentity) * 2;
110   *msg = GNUNET_realloc (*msg, size);
111   *pid = &((*msg)[1]);
112   
113   return GNUNET_YES;
114 }
115
116
117 /**
118  * Check if a node has all expected properties.
119  *
120  * @param peer_id Short ID of the peer to test.
121  * @param status Expected status of the peer.
122  * @param children Expected number of children of the peer.
123  * @param first_hop Short ID of the expected first hop towards the peer.
124  */
125 static void
126 test_assert (GNUNET_PEER_Id peer_id, enum MeshPeerState status,
127              unsigned int children, GNUNET_PEER_Id first_hop)
128 {
129   struct MeshTunnelTreeNode *n;
130   struct MeshTunnelTreeNode *c;
131   unsigned int i;
132   int pre_failed;
133
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Checking peer %u\n", peer_id);
135   pre_failed = failed;
136   n = tree_find_peer (tree, peer_id);
137   if (n->peer != peer_id)
138   {
139     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
140                 "Retrieved peer has wrong ID! (Got %u, expected %u)\n", n->peer,
141                 peer_id);
142     failed++;
143   }
144   if (n->status != status)
145   {
146     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147                 "Retrieved peer has wrong status! (Got %u, expected %u)\n",
148                 n->status, status);
149     failed++;
150   }
151   for (c = n->children_head, i = 0; NULL != c; c = c->next, i++) ;
152   if (i != children)
153   {
154     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
155                 "Retrieved peer wrong has number of children! (Got %u, expected %u)\n",
156                 i, children);
157     failed++;
158   }
159   if (0 != first_hop &&
160       GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)) != first_hop)
161   {
162     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
163                 "Wrong first hop! (Got %u, expected %u)\n",
164                 GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)),
165                 first_hop);
166     failed++;
167   }
168   if (pre_failed != failed)
169   {
170     struct GNUNET_PeerIdentity id;
171
172     GNUNET_PEER_resolve (peer_id, &id);
173     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
174                 "*** Peer %s (%u) has failed %d checks!\n", GNUNET_i2s (&id),
175                 peer_id, failed - pre_failed);
176   }
177 }
178
179
180 /**
181  * Clean up and free all memory.
182  */
183 static void
184 finish (void)
185 {
186   unsigned int i;
187
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
189   for (i = 0; i < 10; i++)
190   {
191     GNUNET_free (pi[i]);
192   }
193 }
194
195 /**
196  * Convert an integer int to a peer identity
197  */
198 static struct GNUNET_PeerIdentity *
199 get_pi (uint32_t id)
200 {
201   struct GNUNET_PeerIdentity *pi;
202
203   pi = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
204   pi->hashPubKey.bits[0] = id + 1;
205   return pi;
206 }
207
208
209 int
210 main (int argc, char *argv[])
211 {
212   struct MeshTunnelTreeNode *node;
213   struct MeshPeerPath *path;
214   struct MeshPeerPath *path1;
215   unsigned int i;
216
217   failed = 0;
218   cb_call = 0;
219   GNUNET_log_setup ("test_mesh_api_tree",
220                     "WARNING",
221                     NULL);
222   for (i = 0; i < 10; i++)
223   {
224     pi[i] = get_pi (i);
225     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
226     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i + 1,
227                 GNUNET_h2s (&pi[i]->hashPubKey));
228   }
229   tree = tree_new (1);
230   tree->me = tree->root;
231   path = path_new (5);
232   path->peers[0] = 1;
233   path->peers[1] = 2;
234   path->peers[2] = 3;
235   path->peers[3] = 4;
236   path->length = 4;
237
238   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
239   tree_add_path (tree, path, &cb, NULL);
240   test_debug (tree);
241   path1 = tree_get_path_to_peer (tree, 4);
242   if (NULL == path1 || path->length != path1->length ||
243       memcmp (path->peers, path1->peers, path->length) != 0)
244   {
245     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
246     failed++;
247   }
248   path_destroy (path1);
249   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
250   test_assert (3, MESH_PEER_RELAY, 1, 0);
251   test_assert (2, MESH_PEER_RELAY, 1, 0);
252   test_assert (1, MESH_PEER_ROOT, 1, 0);
253
254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
255   path->length--;
256   tree_add_path (tree, path, &cb, NULL);
257   test_debug (tree);
258
259   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
260   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
261   test_assert (2, MESH_PEER_RELAY, 1, 0);
262   test_assert (1, MESH_PEER_ROOT, 1, 0);
263
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path 1 2 3 5\n");
265   path->length++;
266   path->peers[3] = 5;
267   tree_add_path (tree, path, &cb, NULL);
268   test_debug (tree);
269
270   test_assert (5, MESH_PEER_SEARCHING, 0, 2);
271   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
272   test_assert (3, MESH_PEER_SEARCHING, 2, 2);
273   test_assert (2, MESH_PEER_RELAY, 1, 0);
274   test_assert (1, MESH_PEER_ROOT, 1, 0);
275
276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Calculating costs...\n");
277   for (i = 1; i < 5; i++)
278   {
279     path->length = i;
280     if (tree_get_path_cost (tree, path) != 0)
281     {
282       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n",
283                   i);
284       failed++;
285     }
286   }
287   path->length++;
288   path->peers[4] = 6;
289   if (tree_get_path_cost (tree, path) != 1)
290   {
291     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
292     failed++;
293   }
294   path->peers[3] = 7;
295   if (tree_get_path_cost (tree, path) != 2)
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
298     failed++;
299   }
300   path->length--;
301   if (tree_get_path_cost (tree, path) != 1)
302   {
303     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
304     failed++;
305   }
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path (5)\n");
307   tree_set_status (tree, 5, MESH_PEER_READY);
308   cb_call = 1;
309   node = tree_del_path (tree, 5, &cb, NULL);
310   test_debug (tree);
311   if (cb_call != 0)
312   {
313     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
314     failed++;
315   }
316   if (node->peer != 5)
317   {
318     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
319     failed++;
320   }
321
322   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
323   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
324   test_assert (2, MESH_PEER_RELAY, 1, 0);
325   test_assert (1, MESH_PEER_ROOT, 1, 0);
326
327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
328   GNUNET_free (node);
329
330   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331               "test: Adding new shorter first path...\n");
332   path->length = 2;
333   path->peers[1] = 4;
334   cb_call = 1;
335   tree_find_peer (tree, 4)->status = MESH_PEER_READY;
336   tree_add_path (tree, path, &cb, NULL);
337   test_debug (tree);
338   if (cb_call != 0)
339   {
340     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
341     failed++;
342   }
343
344   test_assert (4, MESH_PEER_SEARCHING, 0, 4);
345   test_assert (3, MESH_PEER_SEARCHING, 0, 2);
346   test_assert (2, MESH_PEER_RELAY, 1, 0);
347   test_assert (1, MESH_PEER_ROOT, 2, 0);
348
349   GNUNET_free (path->peers);
350   GNUNET_free (path);
351   tree_destroy (tree);
352
353   /****************************************************************************/
354
355   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:\n");
356   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Testing relay trees\n");
357   for (i = 0; i < 10; i++)
358   {
359     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
360   }
361   tree = tree_new (2);
362   path = path_new (8);
363   path->peers[0] = 2;
364   path->peers[1] = 1;
365   path->peers[2] = 3;
366   path->length = 3;
367
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
369   tree_add_path (tree, path, &cb, NULL);
370   test_debug (tree);
371
372   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
373   test_assert (1, MESH_PEER_RELAY, 1, 0);
374   test_assert (2, MESH_PEER_ROOT, 1, 0);
375
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding long path: 2 1 4 5 3\n");
377   path->peers[2] = 4;
378   path->peers[3] = 5;
379   path->peers[4] = 3;
380   path->length = 5;
381   tree_add_path (tree, path, &cb, NULL);
382   test_debug (tree);
383
384   test_assert (3, MESH_PEER_SEARCHING, 0, 4);
385   test_assert (5, MESH_PEER_RELAY, 1, 4);
386   test_assert (4, MESH_PEER_RELAY, 1, 4);
387   test_assert (1, MESH_PEER_RELAY, 1, 0);
388   test_assert (2, MESH_PEER_ROOT, 1, 0);
389
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391               "test: Even longer path: 2 6 1 7 8 4 5 3\n");
392   path->peers[0] = 2;
393   path->peers[1] = 6;
394   path->peers[2] = 1;
395   path->peers[3] = 7;
396   path->peers[4] = 8;
397   path->peers[5] = 4;
398   path->peers[6] = 5;
399   path->peers[7] = 3;
400   path->length = 8;
401   tree_add_path (tree, path, &cb, NULL);
402   test_debug (tree);
403
404   test_assert (3, MESH_PEER_SEARCHING, 0, 7);
405   test_assert (5, MESH_PEER_RELAY, 1, 7);
406   test_assert (4, MESH_PEER_RELAY, 1, 7);
407   test_assert (8, MESH_PEER_RELAY, 1, 7);
408   test_assert (7, MESH_PEER_RELAY, 1, 7);
409   test_assert (1, MESH_PEER_RELAY, 1, 0);
410   test_assert (6, MESH_PEER_RELAY, 1, 0);
411   test_assert (2, MESH_PEER_ROOT, 1, 0);
412
413   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
414   path->peers[1] = 1;
415   path->peers[2] = 3;
416   path->length = 3;
417   tree_add_path (tree, path, &cb, NULL);
418   test_debug (tree);
419
420   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
421   test_assert (1, MESH_PEER_RELAY, 1, 0);
422   test_assert (2, MESH_PEER_ROOT, 1, 0);
423
424   GNUNET_free (path->peers);
425   GNUNET_free (path);
426   tree_destroy (tree);
427   finish ();
428   if (failed > 0)
429   {
430     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
431     return 1;
432   }
433   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");
434
435   return 0;
436 }