2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file mesh/test_mesh_tree_api.c
23 * @brief test mesh tree api: test of tree & path management api
24 * @author Bartlomiej Polot
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_mesh_service.h"
33 #ifndef MESH_TUNNEL_TREE_C
34 #include "mesh_tunnel_tree.c"
35 #define MESH_TUNNEL_TREE_C
40 static struct GNUNET_PeerIdentity *pi[10];
41 static struct MeshTunnelTree *tree;
45 * Whole tree iterator.
47 * @param cls Closure (unused).
48 * @param peer_id Short ID of the node.
49 * @param parent_id Short ID of the parent node.
52 tree_cb (void *cls, GNUNET_PEER_Id peer_id, GNUNET_PEER_Id parent_id)
54 fprintf (stdout, "%u -> %u\n", peer_id, parent_id);;
59 * Node children iterator.
61 * @param cls Closure (unused).
62 * @param peer_idShort ID of the child.
65 cb (void *cls, GNUNET_PEER_Id peer_id)
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", peer_id);
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: and it shouldn't!\n");
78 * Print debug information about the state of the tree.
80 * @param tree Tree to debug-print.
83 test_debug (struct MeshTunnelTree *tree)
86 tree_iterate_all (tree, &tree_cb, NULL);
90 * Iterator over a tunnel to build a message containing all peers the
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.
97 * @return GNUNET_YES, to keep iterating.
100 monitor_tunnel_iterator (void *cls,
102 GNUNET_PEER_Id parent)
104 struct GNUNET_MESH_LocalMonitor **msg = cls;
105 struct GNUNET_PeerIdentity *pid;
108 size = ntohs (*msg->header.size);
109 size += sizeof (struct GNUNET_PeerIdentity) * 2;
110 *msg = GNUNET_realloc (*msg, size);
118 * Check if a node has all expected properties.
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.
126 test_assert (GNUNET_PEER_Id peer_id, enum MeshPeerState status,
127 unsigned int children, GNUNET_PEER_Id first_hop)
129 struct MeshTunnelTreeNode *n;
130 struct MeshTunnelTreeNode *c;
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Checking peer %u\n", peer_id);
136 n = tree_find_peer (tree, peer_id);
137 if (n->peer != peer_id)
139 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
140 "Retrieved peer has wrong ID! (Got %u, expected %u)\n", n->peer,
144 if (n->status != status)
146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147 "Retrieved peer has wrong status! (Got %u, expected %u)\n",
151 for (c = n->children_head, i = 0; NULL != c; c = c->next, i++) ;
154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
155 "Retrieved peer wrong has number of children! (Got %u, expected %u)\n",
159 if (0 != first_hop &&
160 GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)) != first_hop)
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)),
168 if (pre_failed != failed)
170 struct GNUNET_PeerIdentity id;
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);
181 * Clean up and free all memory.
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
189 for (i = 0; i < 10; i++)
196 * Convert an integer int to a peer identity
198 static struct GNUNET_PeerIdentity *
201 struct GNUNET_PeerIdentity *pi;
203 pi = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
204 pi->hashPubKey.bits[0] = id + 1;
210 main (int argc, char *argv[])
212 struct MeshTunnelTreeNode *node;
213 struct MeshPeerPath *path;
214 struct MeshPeerPath *path1;
219 GNUNET_log_setup ("test_mesh_api_tree",
222 for (i = 0; i < 10; 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));
230 tree->me = tree->root;
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
239 tree_add_path (tree, path, &cb, NULL);
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)
245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
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);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
256 tree_add_path (tree, path, &cb, NULL);
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);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path 1 2 3 5\n");
267 tree_add_path (tree, path, &cb, NULL);
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);
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Calculating costs...\n");
277 for (i = 1; i < 5; i++)
280 if (tree_get_path_cost (tree, path) != 0)
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n",
289 if (tree_get_path_cost (tree, path) != 1)
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
295 if (tree_get_path_cost (tree, path) != 2)
297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
301 if (tree_get_path_cost (tree, path) != 1)
303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path (5)\n");
307 tree_set_status (tree, 5, MESH_PEER_READY);
309 node = tree_del_path (tree, 5, &cb, NULL);
313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
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);
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "test: Adding new shorter first path...\n");
335 tree_find_peer (tree, 4)->status = MESH_PEER_READY;
336 tree_add_path (tree, path, &cb, NULL);
340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
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);
349 GNUNET_free (path->peers);
353 /****************************************************************************/
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++)
359 GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
369 tree_add_path (tree, path, &cb, NULL);
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);
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding long path: 2 1 4 5 3\n");
381 tree_add_path (tree, path, &cb, NULL);
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);
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "test: Even longer path: 2 6 1 7 8 4 5 3\n");
401 tree_add_path (tree, path, &cb, NULL);
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);
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
417 tree_add_path (tree, path, &cb, NULL);
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);
424 GNUNET_free (path->peers);
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");