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: Desconnected %u\n", n->peer);
48 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: and it shouldn't!\n");
60 for (i = 0; i < 10; i++)
64 if (tree->root->nchildren > 0)
65 GNUNET_free(tree->root->children);
66 GNUNET_free(tree->root);
72 * Convert an integer int to a peer identity
74 static struct GNUNET_PeerIdentity *
77 struct GNUNET_PeerIdentity *pi;
79 pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
80 pi->hashPubKey.bits[0] = id;
86 main (int argc, char *argv[])
88 struct MeshTunnelTreeNode *node;
89 struct MeshTunnelTreeNode *node2;
90 struct MeshPeerPath *path[10];
95 GNUNET_log_setup ("test_mesh_api_path",
102 for (i = 0; i < 10; i++)
105 GNUNET_break (i != GNUNET_PEER_intern(pi[i]));
106 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i,
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 = 0;
113 tree->me = tree->root;
114 path[0] = GNUNET_malloc(sizeof(struct MeshPeerPath));
115 path[0]->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 4);
116 path[0]->peers[0] = 0;
117 path[0]->peers[1] = 1;
118 path[0]->peers[2] = 2;
119 path[0]->peers[3] = 3;
123 tunnel_add_path(tree, path[0], &cb);
124 path[1] = tunnel_get_path_to_peer(tree, 3);
125 if (path[0]->length != path[1]->length ||
126 memcmp(path[0]->peers, path[1]->peers, path[0]->length) != 0)
128 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
131 path_destroy(path[1]);
132 node = tunnel_find_peer(tree->root, 3);
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 node->status = MESH_PEER_READY;
144 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
146 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
151 node = tunnel_find_peer(tree->root, 2);
154 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
157 if (node->status != MESH_PEER_RELAY)
159 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
162 if (node->nchildren != 1)
164 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
167 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
169 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
173 node = tunnel_find_peer(tree->root, 1);
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->nchildren != 1)
186 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
191 tunnel_add_path(tree, path[0], &cb);
193 node = tunnel_find_peer(tree->root, 2);
196 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
199 if (node->status != MESH_PEER_SEARCHING)
201 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
204 if (node->nchildren != 1)
206 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
209 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
211 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
214 if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
216 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
220 node = tunnel_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");
238 path[0]->peers[3] = 4;
239 tunnel_add_path(tree, path[0], &cb);
241 node = tunnel_find_peer(tree->root, 2);
244 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
247 if (node->status != MESH_PEER_SEARCHING)
249 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
252 if (node->nchildren != 2)
254 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
257 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
259 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
262 if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 1)
264 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
268 node = tunnel_find_peer(tree->root, 1);
271 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
274 if (node->status != MESH_PEER_RELAY)
276 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
279 if (node->nchildren != 1)
281 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
285 node = tunnel_find_peer(tree->root, 4);
288 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
291 node->status = MESH_PEER_READY;
293 node2 = tunnel_del_path(tree, 4, &cb);
296 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
299 if (node2->peer != 4)
301 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
304 // GNUNET_free(node2); FIXME destroy
305 node = tunnel_find_peer(tree->root, 2);
308 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
311 if (node->status != MESH_PEER_SEARCHING)
313 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
316 if (node->nchildren != 1)
318 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
323 path[0]->peers[1] = 3;
325 tunnel_add_path(tree, path[0], cb);
328 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
331 node = tunnel_find_peer(tree->root, 2);
334 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
337 if (node->status != MESH_PEER_SEARCHING)
339 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
342 if (node->nchildren != 0)
344 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
347 node = tunnel_find_peer(tree->root, 3);
350 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
353 if (node->status != MESH_PEER_SEARCHING)
355 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
358 if (node->nchildren != 0)
360 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
363 if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
365 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
368 if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 3)
370 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");