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: 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;
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 != GNUNET_PEER_intern(pi[i]));
105 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i,
106 GNUNET_h2s(&pi[i]->hashPubKey));
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);
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)
127 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
131 node = tree_find_peer(tree->root, 3);
134 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
137 if (node->status != MESH_PEER_SEARCHING)
139 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
142 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
144 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
148 node = tree_find_peer(tree->root, 2);
151 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
154 if (node->status != MESH_PEER_RELAY)
156 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
159 if (node->nchildren != 1)
161 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
164 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
166 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
170 node = tree_find_peer(tree->root, 1);
173 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
176 if (node->status != MESH_PEER_RELAY)
178 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
181 if (node->nchildren != 1)
183 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 0 1 2\n");
189 tree_add_path(tree, path, &cb);
191 node = tree_find_peer(tree->root, 2);
194 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
197 if (node->status != MESH_PEER_SEARCHING)
199 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
202 if (node->nchildren != 1)
204 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
207 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
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)));
213 if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
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)));
220 node = tree_find_peer(tree->root, 1);
223 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
226 if (node->status != MESH_PEER_RELAY)
228 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
231 if (node->nchildren != 1)
233 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path...\n");
243 tree_add_path(tree, path, &cb);
245 node = tree_find_peer(tree->root, 2);
248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
251 if (node->status != MESH_PEER_SEARCHING)
253 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
256 if (node->nchildren != 2)
258 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
261 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
263 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
266 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 1)
268 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
272 node = tree_find_peer(tree->root, 1);
275 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
278 if (node->status != MESH_PEER_RELAY)
280 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
283 if (node->nchildren != 1)
285 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
289 node = tree_find_peer(tree->root, 4);
292 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path...\n");
297 node->status = MESH_PEER_READY;
299 node2 = tree_del_path(tree, 4, &cb);
302 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
305 if (node2->peer != 4)
307 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
311 node = tree_find_peer(tree->root, 2);
314 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
317 if (node->status != MESH_PEER_SEARCHING)
319 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
322 if (node->nchildren != 1)
324 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
329 tree_node_destroy(node2);
331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding new shorter first path...\n");
335 tree_add_path(tree, path, cb);
338 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
341 node = tree_find_peer(tree->root, 2);
344 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
347 if (node->status != MESH_PEER_SEARCHING)
349 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
352 if (node->nchildren != 0)
354 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
357 node = tree_find_peer(tree->root, 3);
360 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
363 if (node->status != MESH_PEER_SEARCHING)
365 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
368 if (node->nchildren != 0)
370 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
373 if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
375 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
378 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 3)
380 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");