diff options
author | Bart Polot <bart@net.in.tum.de> | 2012-11-20 12:02:01 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2012-11-20 12:02:01 +0000 |
commit | 984eaa29119a4b1ae01490db9b16cf1020e01d55 (patch) | |
tree | b98a834fee36c46a433dd0638ebd2d4487a8185c /src/mesh/test_mesh_tree_api.c | |
parent | afd56af75f0a16ee5b8ab7777871294931ca265c (diff) | |
download | gnunet-984eaa29119a4b1ae01490db9b16cf1020e01d55.tar.gz gnunet-984eaa29119a4b1ae01490db9b16cf1020e01d55.zip |
- add testcase for whole-tree iterator
Diffstat (limited to 'src/mesh/test_mesh_tree_api.c')
-rw-r--r-- | src/mesh/test_mesh_tree_api.c | 82 |
1 files changed, 73 insertions, 9 deletions
diff --git a/src/mesh/test_mesh_tree_api.c b/src/mesh/test_mesh_tree_api.c index 60b88239c..bc824d61a 100644 --- a/src/mesh/test_mesh_tree_api.c +++ b/src/mesh/test_mesh_tree_api.c | |||
@@ -40,6 +40,27 @@ static int cb_call; | |||
40 | static struct GNUNET_PeerIdentity *pi[10]; | 40 | static struct GNUNET_PeerIdentity *pi[10]; |
41 | static struct MeshTunnelTree *tree; | 41 | static struct MeshTunnelTree *tree; |
42 | 42 | ||
43 | |||
44 | /** | ||
45 | * Whole tree iterator. | ||
46 | * | ||
47 | * @param cls Closure (unused). | ||
48 | * @param peer_id Short ID of the node. | ||
49 | * @param parent_id Short ID of the parent node. | ||
50 | */ | ||
51 | static void | ||
52 | tree_cb (void *cls, GNUNET_PEER_Id peer_id, GNUNET_PEER_Id parent_id) | ||
53 | { | ||
54 | fprintf (stdout, "%u -> %u\n", peer_id, parent_id);; | ||
55 | } | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Node children iterator. | ||
60 | * | ||
61 | * @param cls Closure (unused). | ||
62 | * @param peer_idShort ID of the child. | ||
63 | */ | ||
43 | static void | 64 | static void |
44 | cb (void *cls, GNUNET_PEER_Id peer_id) | 65 | cb (void *cls, GNUNET_PEER_Id peer_id) |
45 | { | 66 | { |
@@ -54,6 +75,46 @@ cb (void *cls, GNUNET_PEER_Id peer_id) | |||
54 | 75 | ||
55 | 76 | ||
56 | /** | 77 | /** |
78 | * Print debug information about the state of the tree. | ||
79 | * | ||
80 | * @param tree Tree to debug-print. | ||
81 | */ | ||
82 | static void | ||
83 | test_debug (struct MeshTunnelTree *tree) | ||
84 | { | ||
85 | tree_debug (tree); | ||
86 | tree_iterate_all (tree, &tree_cb, NULL); | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Iterator over a tunnel to build a message containing all peers the | ||
91 | * tunnel's tree. | ||
92 | * | ||
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. | ||
96 | * | ||
97 | * @return GNUNET_YES, to keep iterating. | ||
98 | */ | ||
99 | static int | ||
100 | monitor_tunnel_iterator (void *cls, | ||
101 | GNUNET_PEER_Id peer, | ||
102 | GNUNET_PEER_Id parent) | ||
103 | { | ||
104 | struct GNUNET_MESH_LocalMonitor **msg = cls; | ||
105 | struct GNUNET_PeerIdentity *pid; | ||
106 | size_t size; | ||
107 | |||
108 | size = ntohs (*msg->header.size); | ||
109 | size += sizeof (struct GNUNET_PeerIdentity) * 2; | ||
110 | *msg = GNUNET_realloc (*msg, size); | ||
111 | *pid = &((*msg)[1]); | ||
112 | |||
113 | return GNUNET_YES; | ||
114 | } | ||
115 | |||
116 | |||
117 | /** | ||
57 | * Check if a node has all expected properties. | 118 | * Check if a node has all expected properties. |
58 | * | 119 | * |
59 | * @param peer_id Short ID of the peer to test. | 120 | * @param peer_id Short ID of the peer to test. |
@@ -116,6 +177,9 @@ test_assert (GNUNET_PEER_Id peer_id, enum MeshPeerState status, | |||
116 | } | 177 | } |
117 | 178 | ||
118 | 179 | ||
180 | /** | ||
181 | * Clean up and free all memory. | ||
182 | */ | ||
119 | static void | 183 | static void |
120 | finish (void) | 184 | finish (void) |
121 | { | 185 | { |
@@ -173,7 +237,7 @@ main (int argc, char *argv[]) | |||
173 | 237 | ||
174 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n"); | 238 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n"); |
175 | tree_add_path (tree, path, &cb, NULL); | 239 | tree_add_path (tree, path, &cb, NULL); |
176 | tree_debug (tree); | 240 | test_debug (tree); |
177 | path1 = tree_get_path_to_peer (tree, 4); | 241 | path1 = tree_get_path_to_peer (tree, 4); |
178 | if (NULL == path1 || path->length != path1->length || | 242 | if (NULL == path1 || path->length != path1->length || |
179 | memcmp (path->peers, path1->peers, path->length) != 0) | 243 | memcmp (path->peers, path1->peers, path->length) != 0) |
@@ -190,7 +254,7 @@ main (int argc, char *argv[]) | |||
190 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n"); | 254 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n"); |
191 | path->length--; | 255 | path->length--; |
192 | tree_add_path (tree, path, &cb, NULL); | 256 | tree_add_path (tree, path, &cb, NULL); |
193 | tree_debug (tree); | 257 | test_debug (tree); |
194 | 258 | ||
195 | test_assert (4, MESH_PEER_SEARCHING, 0, 2); | 259 | test_assert (4, MESH_PEER_SEARCHING, 0, 2); |
196 | test_assert (3, MESH_PEER_SEARCHING, 1, 2); | 260 | test_assert (3, MESH_PEER_SEARCHING, 1, 2); |
@@ -201,7 +265,7 @@ main (int argc, char *argv[]) | |||
201 | path->length++; | 265 | path->length++; |
202 | path->peers[3] = 5; | 266 | path->peers[3] = 5; |
203 | tree_add_path (tree, path, &cb, NULL); | 267 | tree_add_path (tree, path, &cb, NULL); |
204 | tree_debug (tree); | 268 | test_debug (tree); |
205 | 269 | ||
206 | test_assert (5, MESH_PEER_SEARCHING, 0, 2); | 270 | test_assert (5, MESH_PEER_SEARCHING, 0, 2); |
207 | test_assert (4, MESH_PEER_SEARCHING, 0, 2); | 271 | test_assert (4, MESH_PEER_SEARCHING, 0, 2); |
@@ -243,7 +307,7 @@ main (int argc, char *argv[]) | |||
243 | tree_set_status (tree, 5, MESH_PEER_READY); | 307 | tree_set_status (tree, 5, MESH_PEER_READY); |
244 | cb_call = 1; | 308 | cb_call = 1; |
245 | node = tree_del_path (tree, 5, &cb, NULL); | 309 | node = tree_del_path (tree, 5, &cb, NULL); |
246 | tree_debug (tree); | 310 | test_debug (tree); |
247 | if (cb_call != 0) | 311 | if (cb_call != 0) |
248 | { | 312 | { |
249 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call); | 313 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call); |
@@ -270,7 +334,7 @@ main (int argc, char *argv[]) | |||
270 | cb_call = 1; | 334 | cb_call = 1; |
271 | tree_find_peer (tree, 4)->status = MESH_PEER_READY; | 335 | tree_find_peer (tree, 4)->status = MESH_PEER_READY; |
272 | tree_add_path (tree, path, &cb, NULL); | 336 | tree_add_path (tree, path, &cb, NULL); |
273 | tree_debug (tree); | 337 | test_debug (tree); |
274 | if (cb_call != 0) | 338 | if (cb_call != 0) |
275 | { | 339 | { |
276 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call); | 340 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call); |
@@ -303,7 +367,7 @@ main (int argc, char *argv[]) | |||
303 | 367 | ||
304 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n"); | 368 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n"); |
305 | tree_add_path (tree, path, &cb, NULL); | 369 | tree_add_path (tree, path, &cb, NULL); |
306 | tree_debug (tree); | 370 | test_debug (tree); |
307 | 371 | ||
308 | test_assert (3, MESH_PEER_SEARCHING, 0, 3); | 372 | test_assert (3, MESH_PEER_SEARCHING, 0, 3); |
309 | test_assert (1, MESH_PEER_RELAY, 1, 0); | 373 | test_assert (1, MESH_PEER_RELAY, 1, 0); |
@@ -315,7 +379,7 @@ main (int argc, char *argv[]) | |||
315 | path->peers[4] = 3; | 379 | path->peers[4] = 3; |
316 | path->length = 5; | 380 | path->length = 5; |
317 | tree_add_path (tree, path, &cb, NULL); | 381 | tree_add_path (tree, path, &cb, NULL); |
318 | tree_debug (tree); | 382 | test_debug (tree); |
319 | 383 | ||
320 | test_assert (3, MESH_PEER_SEARCHING, 0, 4); | 384 | test_assert (3, MESH_PEER_SEARCHING, 0, 4); |
321 | test_assert (5, MESH_PEER_RELAY, 1, 4); | 385 | test_assert (5, MESH_PEER_RELAY, 1, 4); |
@@ -335,7 +399,7 @@ main (int argc, char *argv[]) | |||
335 | path->peers[7] = 3; | 399 | path->peers[7] = 3; |
336 | path->length = 8; | 400 | path->length = 8; |
337 | tree_add_path (tree, path, &cb, NULL); | 401 | tree_add_path (tree, path, &cb, NULL); |
338 | tree_debug (tree); | 402 | test_debug (tree); |
339 | 403 | ||
340 | test_assert (3, MESH_PEER_SEARCHING, 0, 7); | 404 | test_assert (3, MESH_PEER_SEARCHING, 0, 7); |
341 | test_assert (5, MESH_PEER_RELAY, 1, 7); | 405 | test_assert (5, MESH_PEER_RELAY, 1, 7); |
@@ -351,7 +415,7 @@ main (int argc, char *argv[]) | |||
351 | path->peers[2] = 3; | 415 | path->peers[2] = 3; |
352 | path->length = 3; | 416 | path->length = 3; |
353 | tree_add_path (tree, path, &cb, NULL); | 417 | tree_add_path (tree, path, &cb, NULL); |
354 | tree_debug (tree); | 418 | test_debug (tree); |
355 | 419 | ||
356 | test_assert (3, MESH_PEER_SEARCHING, 0, 3); | 420 | test_assert (3, MESH_PEER_SEARCHING, 0, 3); |
357 | test_assert (1, MESH_PEER_RELAY, 1, 0); | 421 | test_assert (1, MESH_PEER_RELAY, 1, 0); |