- fixes
[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 /**
91  * Check if a node has all expected properties.
92  *
93  * @param peer_id Short ID of the peer to test.
94  * @param status Expected status of the peer.
95  * @param children Expected number of children of the peer.
96  * @param first_hop Short ID of the expected first hop towards the peer.
97  */
98 static void
99 test_assert (GNUNET_PEER_Id peer_id, enum MeshPeerState status,
100              unsigned int children, GNUNET_PEER_Id first_hop)
101 {
102   struct MeshTunnelTreeNode *n;
103   struct MeshTunnelTreeNode *c;
104   unsigned int i;
105   int pre_failed;
106
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Checking peer %u\n", peer_id);
108   pre_failed = failed;
109   n = tree_find_peer (tree, peer_id);
110   if (n->peer != peer_id)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
113                 "Retrieved peer has wrong ID! (Got %u, expected %u)\n", n->peer,
114                 peer_id);
115     failed++;
116   }
117   if (n->status != status)
118   {
119     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
120                 "Retrieved peer has wrong status! (Got %u, expected %u)\n",
121                 n->status, status);
122     failed++;
123   }
124   for (c = n->children_head, i = 0; NULL != c; c = c->next, i++) ;
125   if (i != children)
126   {
127     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
128                 "Retrieved peer wrong has number of children! (Got %u, expected %u)\n",
129                 i, children);
130     failed++;
131   }
132   if (0 != first_hop &&
133       GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)) != first_hop)
134   {
135     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
136                 "Wrong first hop! (Got %u, expected %u)\n",
137                 GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)),
138                 first_hop);
139     failed++;
140   }
141   if (pre_failed != failed)
142   {
143     struct GNUNET_PeerIdentity id;
144
145     GNUNET_PEER_resolve (peer_id, &id);
146     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147                 "*** Peer %s (%u) has failed %d checks!\n", GNUNET_i2s (&id),
148                 peer_id, failed - pre_failed);
149   }
150 }
151
152
153 /**
154  * Clean up and free all memory.
155  */
156 static void
157 finish (void)
158 {
159   unsigned int i;
160
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
162   for (i = 0; i < 10; i++)
163   {
164     GNUNET_free (pi[i]);
165   }
166 }
167
168 /**
169  * Convert an integer int to a peer identity
170  */
171 static struct GNUNET_PeerIdentity *
172 get_pi (uint32_t id)
173 {
174   struct GNUNET_PeerIdentity *pi;
175
176   pi = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
177   pi->hashPubKey.bits[0] = id + 1;
178   return pi;
179 }
180
181
182 int
183 main (int argc, char *argv[])
184 {
185   struct MeshTunnelTreeNode *node;
186   struct MeshPeerPath *path;
187   struct MeshPeerPath *path1;
188   unsigned int i;
189
190   failed = 0;
191   cb_call = 0;
192   GNUNET_log_setup ("test_mesh_api_tree",
193                     "WARNING",
194                     NULL);
195   for (i = 0; i < 10; i++)
196   {
197     pi[i] = get_pi (i);
198     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
199     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i + 1,
200                 GNUNET_h2s (&pi[i]->hashPubKey));
201   }
202   tree = tree_new (1);
203   tree->me = tree->root;
204   path = path_new (5);
205   path->peers[0] = 1;
206   path->peers[1] = 2;
207   path->peers[2] = 3;
208   path->peers[3] = 4;
209   path->length = 4;
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
212   tree_add_path (tree, path, &cb, NULL);
213   test_debug (tree);
214   path1 = tree_get_path_to_peer (tree, 4);
215   if (NULL == path1 || path->length != path1->length ||
216       memcmp (path->peers, path1->peers, path->length) != 0)
217   {
218     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
219     failed++;
220   }
221   path_destroy (path1);
222   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
223   test_assert (3, MESH_PEER_RELAY, 1, 0);
224   test_assert (2, MESH_PEER_RELAY, 1, 0);
225   test_assert (1, MESH_PEER_ROOT, 1, 0);
226
227   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
228   path->length--;
229   tree_add_path (tree, path, &cb, NULL);
230   test_debug (tree);
231
232   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
233   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
234   test_assert (2, MESH_PEER_RELAY, 1, 0);
235   test_assert (1, MESH_PEER_ROOT, 1, 0);
236
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path 1 2 3 5\n");
238   path->length++;
239   path->peers[3] = 5;
240   tree_add_path (tree, path, &cb, NULL);
241   test_debug (tree);
242
243   test_assert (5, MESH_PEER_SEARCHING, 0, 2);
244   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
245   test_assert (3, MESH_PEER_SEARCHING, 2, 2);
246   test_assert (2, MESH_PEER_RELAY, 1, 0);
247   test_assert (1, MESH_PEER_ROOT, 1, 0);
248
249   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Calculating costs...\n");
250   for (i = 1; i < 5; i++)
251   {
252     path->length = i;
253     if (tree_get_path_cost (tree, path) != 0)
254     {
255       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n",
256                   i);
257       failed++;
258     }
259   }
260   path->length++;
261   path->peers[4] = 6;
262   if (tree_get_path_cost (tree, path) != 1)
263   {
264     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
265     failed++;
266   }
267   path->peers[3] = 7;
268   if (tree_get_path_cost (tree, path) != 2)
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
271     failed++;
272   }
273   path->length--;
274   if (tree_get_path_cost (tree, path) != 1)
275   {
276     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
277     failed++;
278   }
279   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path (5)\n");
280   tree_set_status (tree, 5, MESH_PEER_READY);
281   cb_call = 1;
282   node = tree_del_path (tree, 5, &cb, NULL);
283   test_debug (tree);
284   if (cb_call != 0)
285   {
286     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
287     failed++;
288   }
289   if (node->peer != 5)
290   {
291     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
292     failed++;
293   }
294
295   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
296   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
297   test_assert (2, MESH_PEER_RELAY, 1, 0);
298   test_assert (1, MESH_PEER_ROOT, 1, 0);
299
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
301   GNUNET_free (node);
302
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304               "test: Adding new shorter first path...\n");
305   path->length = 2;
306   path->peers[1] = 4;
307   cb_call = 1;
308   tree_find_peer (tree, 4)->status = MESH_PEER_READY;
309   tree_add_path (tree, path, &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
317   test_assert (4, MESH_PEER_SEARCHING, 0, 4);
318   test_assert (3, MESH_PEER_SEARCHING, 0, 2);
319   test_assert (2, MESH_PEER_RELAY, 1, 0);
320   test_assert (1, MESH_PEER_ROOT, 2, 0);
321
322   GNUNET_free (path->peers);
323   GNUNET_free (path);
324   tree_destroy (tree);
325
326   /****************************************************************************/
327
328   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:\n");
329   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Testing relay trees\n");
330   for (i = 0; i < 10; i++)
331   {
332     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
333   }
334   tree = tree_new (2);
335   path = path_new (8);
336   path->peers[0] = 2;
337   path->peers[1] = 1;
338   path->peers[2] = 3;
339   path->length = 3;
340
341   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
342   tree_add_path (tree, path, &cb, NULL);
343   test_debug (tree);
344
345   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
346   test_assert (1, MESH_PEER_RELAY, 1, 0);
347   test_assert (2, MESH_PEER_ROOT, 1, 0);
348
349   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding long path: 2 1 4 5 3\n");
350   path->peers[2] = 4;
351   path->peers[3] = 5;
352   path->peers[4] = 3;
353   path->length = 5;
354   tree_add_path (tree, path, &cb, NULL);
355   test_debug (tree);
356
357   test_assert (3, MESH_PEER_SEARCHING, 0, 4);
358   test_assert (5, MESH_PEER_RELAY, 1, 4);
359   test_assert (4, MESH_PEER_RELAY, 1, 4);
360   test_assert (1, MESH_PEER_RELAY, 1, 0);
361   test_assert (2, MESH_PEER_ROOT, 1, 0);
362
363   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364               "test: Even longer path: 2 6 1 7 8 4 5 3\n");
365   path->peers[0] = 2;
366   path->peers[1] = 6;
367   path->peers[2] = 1;
368   path->peers[3] = 7;
369   path->peers[4] = 8;
370   path->peers[5] = 4;
371   path->peers[6] = 5;
372   path->peers[7] = 3;
373   path->length = 8;
374   tree_add_path (tree, path, &cb, NULL);
375   test_debug (tree);
376
377   test_assert (3, MESH_PEER_SEARCHING, 0, 7);
378   test_assert (5, MESH_PEER_RELAY, 1, 7);
379   test_assert (4, MESH_PEER_RELAY, 1, 7);
380   test_assert (8, MESH_PEER_RELAY, 1, 7);
381   test_assert (7, MESH_PEER_RELAY, 1, 7);
382   test_assert (1, MESH_PEER_RELAY, 1, 0);
383   test_assert (6, MESH_PEER_RELAY, 1, 0);
384   test_assert (2, MESH_PEER_ROOT, 1, 0);
385
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
387   path->peers[1] = 1;
388   path->peers[2] = 3;
389   path->length = 3;
390   tree_add_path (tree, path, &cb, NULL);
391   test_debug (tree);
392
393   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
394   test_assert (1, MESH_PEER_RELAY, 1, 0);
395   test_assert (2, MESH_PEER_ROOT, 1, 0);
396
397   GNUNET_free (path->peers);
398   GNUNET_free (path);
399   tree_destroy (tree);
400   finish ();
401   if (failed > 0)
402   {
403     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
404     return 1;
405   }
406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");
407
408   return 0;
409 }