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_path.c
23 * @brief test mesh path: test of 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_new.h"
33 #include "mesh_tunnel_tree.h"
39 struct GNUNET_PeerIdentity* pi[10];
40 struct MeshTunnelTree *tree;
43 cb (const struct MeshTunnelTreeNode *n)
45 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", n->peer);
48 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: and it shouldn't!\n");
60 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
61 for (i = 0; i < 10; i++)
70 * Convert an integer int to a peer identity
72 static struct GNUNET_PeerIdentity *
75 struct GNUNET_PeerIdentity *pi;
77 pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
78 pi->hashPubKey.bits[0] = id + 1;
84 main (int argc, char *argv[])
86 struct MeshTunnelTreeNode *node;
87 struct MeshTunnelTreeNode *node2;
88 struct MeshPeerPath *path;
89 struct MeshPeerPath *path1;
94 GNUNET_log_setup ("test_mesh_api_path",
101 for (i = 0; i < 10; i++)
104 GNUNET_break (i + 1 == GNUNET_PEER_intern(pi[i]));
105 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n",
107 GNUNET_h2s(&pi[i]->hashPubKey));
109 tree = GNUNET_malloc(sizeof(struct MeshTunnelTree));
110 tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
111 tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
112 tree->root->peer = 1;
113 tree->me = tree->root;
121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
122 tree_add_path(tree, path, &cb);
124 path1 = tree_get_path_to_peer(tree, 4);
125 if (path->length != path1->length ||
126 memcmp(path->peers, path1->peers, path->length) != 0)
128 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
132 node = tree_find_peer(tree->root, 4);
135 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
138 if (node->status != MESH_PEER_SEARCHING)
140 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
143 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 2)
145 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
146 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "4 GOT: %u\n", GNUNET_PEER_search(path_get_first_hop(tree, 4)));
150 node = tree_find_peer(tree->root, 3);
153 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
156 if (node->status != MESH_PEER_RELAY)
158 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
161 if (node->children_head != node->children_tail)
163 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
166 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 2)
168 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
169 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "4 GOT: %u\n", GNUNET_PEER_search(path_get_first_hop(tree, 4)));
173 node = tree_find_peer(tree->root, 2);
176 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
179 if (node->status != MESH_PEER_RELAY)
181 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
184 if (node->children_head != node->children_tail)
186 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
192 tree_add_path(tree, path, &cb);
195 node = tree_find_peer(tree->root, 4);
198 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
201 if (node->status != MESH_PEER_SEARCHING)
203 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
204 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, " expected SEARCHING, got %u\n", node->status);
207 if (node->children_head != node->children_tail)
209 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
212 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 2)
214 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
215 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "4 GOT: %u\n", GNUNET_PEER_search(path_get_first_hop(tree, 4)));
218 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 2)
220 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
221 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "3 GOT: %u\n", GNUNET_PEER_search(path_get_first_hop(tree, 3)));
225 node = tree_find_peer(tree->root, 2);
228 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
231 if (node->status != MESH_PEER_RELAY)
233 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
234 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, " expected RELAY\n");
237 if (node->children_head != node->children_tail)
239 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path...\n");
246 tree_add_path(tree, path, &cb);
249 node = tree_find_peer(tree->root, 3);
252 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
255 if (node->status != MESH_PEER_SEARCHING)
257 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
260 if (node->children_head->next != node->children_tail)
262 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
265 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 2)
267 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
270 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 2)
272 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
276 node = tree_find_peer(tree->root, 2);
279 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
282 if (node->status != MESH_PEER_RELAY)
284 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
287 if (node->children_head != node->children_tail)
289 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
293 node = tree_find_peer(tree->root, 5);
296 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path...\n");
301 node->status = MESH_PEER_READY;
303 node2 = tree_del_path(tree, 5, &cb);
307 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
310 if (node2->peer != 5)
312 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
316 node = tree_find_peer(tree->root, 3);
319 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
322 if (node->status != MESH_PEER_SEARCHING)
324 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
327 if (node->children_head != node->children_tail)
329 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
333 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding new shorter first path...\n");
340 tree_find_peer(tree->root, 4)->status = MESH_PEER_READY;
341 tree_add_path(tree, path, cb);
345 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
348 node = tree_find_peer(tree->root, 3);
351 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
354 if (node->status != MESH_PEER_SEARCHING)
356 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
359 if (node->children_head != NULL)
361 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
364 node = tree_find_peer(tree->root, 4);
367 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
370 if (node->status != MESH_PEER_SEARCHING)
372 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
375 if (node->children_head != NULL)
377 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
380 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 2)
382 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
385 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 4)
387 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");