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.h"
33 #ifndef MESH_TUNNEL_TREE_C
34 #include "mesh_tunnel_tree.c"
35 #define MESH_TUNNEL_TREE_C
42 struct GNUNET_PeerIdentity* pi[10];
43 struct MeshTunnelTree *tree;
46 cb (void *cls, GNUNET_PEER_Id peer_id)
48 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", peer_id);
51 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: and it shouldn't!\n");
59 * Check if a node has all expected properties.
61 * @param peer_id Short ID of the peer to test.
62 * @param status Expected status of the peer.
63 * @param children Expected number of children of the peer.
64 * @param first_hop Short ID of the expected first hop towards the peer.
67 test_assert (GNUNET_PEER_Id peer_id,
68 enum MeshPeerState status,
69 unsigned int children,
70 GNUNET_PEER_Id first_hop)
72 struct MeshTunnelTreeNode *n;
73 struct MeshTunnelTreeNode *c;
78 n = tree_find_peer(tree, peer_id);
79 if (n->peer != peer_id)
81 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
82 "Retrieved peer has wrong ID! (%u, %u)\n",
86 if (n->status != status)
88 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
89 "Retrieved peer has wrong status! (%u, %u)\n",
93 for (c = n->children_head, i = 0; NULL != c; c = c->next, i++);
96 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
97 "Retrieved peer wrong has number of children! (%u, %u)\n",
101 if (0 != first_hop &&
102 GNUNET_PEER_search(path_get_first_hop(tree, peer_id)) != first_hop)
104 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
105 "Wrong first hop! (%u, %u)\n",
106 GNUNET_PEER_search(path_get_first_hop(tree, peer_id)),
110 if (pre_failed != failed)
112 struct GNUNET_PeerIdentity id;
113 GNUNET_PEER_resolve (peer_id, &id);
114 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
115 "*** Peer %s (%u) has failed %d checks! (real, expected)\n",
116 GNUNET_i2s (&id), peer_id, failed - pre_failed);
126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
127 for (i = 0; i < 10; i++)
134 * Convert an integer int to a peer identity
136 static struct GNUNET_PeerIdentity *
139 struct GNUNET_PeerIdentity *pi;
141 pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
142 pi->hashPubKey.bits[0] = id + 1;
148 main (int argc, char *argv[])
150 struct MeshTunnelTreeNode *node;
151 struct MeshTunnelTreeNode *node2;
152 struct MeshPeerPath *path;
153 struct MeshPeerPath *path1;
158 GNUNET_log_setup ("test_mesh_api_path",
165 for (i = 0; i < 10; i++)
168 GNUNET_break (i + 1 == GNUNET_PEER_intern(pi[i]));
169 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n",
171 GNUNET_h2s(&pi[i]->hashPubKey));
174 tree->me = tree->root;
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
183 tree_add_path(tree, path, &cb, NULL);
185 path1 = tree_get_path_to_peer(tree, 4);
186 if (path->length != path1->length ||
187 memcmp(path->peers, path1->peers, path->length) != 0)
189 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
193 test_assert (4, MESH_PEER_SEARCHING, 0, 2);
194 test_assert (3, MESH_PEER_RELAY, 1, 0);
195 test_assert (2, MESH_PEER_RELAY, 1, 0);
196 test_assert (1, MESH_PEER_ROOT, 1, 0);
198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
200 tree_add_path(tree, path, &cb, NULL);
203 test_assert (4, MESH_PEER_SEARCHING, 0, 2);
204 test_assert (3, MESH_PEER_SEARCHING, 1, 2);
205 test_assert (2, MESH_PEER_RELAY, 1, 0);
206 test_assert (1, MESH_PEER_ROOT, 1, 0);
208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path...\n");
211 tree_add_path(tree, path, &cb, NULL);
214 test_assert (5, MESH_PEER_SEARCHING, 0, 2);
215 test_assert (4, MESH_PEER_SEARCHING, 0, 2);
216 test_assert (3, MESH_PEER_SEARCHING, 2, 2);
217 test_assert (2, MESH_PEER_RELAY, 1, 0);
218 test_assert (1, MESH_PEER_ROOT, 1, 0);
220 node = tree_find_peer(tree, 5);
221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path...\n");
222 node->status = MESH_PEER_READY;
224 node2 = tree_del_path(tree, 5, &cb, NULL);
228 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
231 if (node2->peer != 5)
233 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
237 test_assert (4, MESH_PEER_SEARCHING, 0, 2);
238 test_assert (3, MESH_PEER_SEARCHING, 1, 2);
239 test_assert (2, MESH_PEER_RELAY, 1, 0);
240 test_assert (1, MESH_PEER_ROOT, 1, 0);
242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
246 "test: Adding new shorter first path...\n");
250 tree_find_peer(tree, 4)->status = MESH_PEER_READY;
251 tree_add_path(tree, path, &cb, NULL);
255 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
259 test_assert (4, MESH_PEER_SEARCHING, 0, 4);
260 test_assert (3, MESH_PEER_SEARCHING, 0, 2);
261 test_assert (2, MESH_PEER_RELAY, 1, 0);
262 test_assert (1, MESH_PEER_ROOT, 2, 0);
264 GNUNET_free (path->peers);
268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test:\n");
269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Testing relay trees\n");
270 for (i = 0; i < 10; i++)
272 GNUNET_break (i + 1 == GNUNET_PEER_intern(pi[i]));
281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3\n");
282 tree_add_path(tree, path, &cb, NULL);
284 tree_set_me (tree, 2);
286 test_assert (3, MESH_PEER_SEARCHING, 0, 3);
287 test_assert (2, MESH_PEER_RELAY, 1, 0);
288 test_assert (1, MESH_PEER_ROOT, 1, 0);
290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding same path: 1 2 3\n");
291 tree_add_path(tree, path, &cb, NULL);
293 GNUNET_free (path->peers);
299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");