ec92a5eced8cc16c3bd581b31ab60f1b782dbf0d
[oweals/gnunet.git] / src / mesh / test_mesh_tree_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file mesh/test_mesh_tree_api.c
23  * @brief test mesh tree api: test of tree & path management api
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_mesh_service.h"
32 #include "mesh.h"
33 #ifndef MESH_TUNNEL_TREE_C
34 #include "mesh_tunnel_tree.c"
35 #define MESH_TUNNEL_TREE_C
36 #endif
37
38 #define VERBOSE 1
39
40 int failed;
41 int cb_call;
42 struct GNUNET_PeerIdentity *pi[10];
43 struct MeshTunnelTree *tree;
44
45 static void
46 cb (void *cls, GNUNET_PEER_Id peer_id)
47 {
48   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", peer_id);
49   if (0 == cb_call)
50   {
51     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:      and it shouldn't!\n");
52     failed++;
53   }
54   cb_call--;
55 }
56
57
58 /**
59  * Check if a node has all expected properties.
60  *
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.
65  */
66 static void
67 test_assert (GNUNET_PEER_Id peer_id, enum MeshPeerState status,
68              unsigned int children, GNUNET_PEER_Id first_hop)
69 {
70   struct MeshTunnelTreeNode *n;
71   struct MeshTunnelTreeNode *c;
72   unsigned int i;
73   int pre_failed;
74
75   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Checking peer %u\n", peer_id);
76   pre_failed = failed;
77   n = tree_find_peer (tree, peer_id);
78   if (n->peer != peer_id)
79   {
80     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
81                 "Retrieved peer has wrong ID! (Got %u, expected %u)\n",
82                 n->peer, peer_id);
83     failed++;
84   }
85   if (n->status != status)
86   {
87     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
88                 "Retrieved peer has wrong status! (Got %u, expected %u)\n",
89                 n->status,
90                 status);
91     failed++;
92   }
93   for (c = n->children_head, i = 0; NULL != c; c = c->next, i++) ;
94   if (i != children)
95   {
96     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
97                 "Retrieved peer wrong has number of children! (Got %u, expected %u)\n",
98                 i, children);
99     failed++;
100   }
101   if (0 != first_hop &&
102       GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)) != first_hop)
103   {
104     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Wrong first hop! (Got %u, expected %u)\n",
105                 GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)),
106                 first_hop);
107     failed++;
108   }
109   if (pre_failed != failed)
110   {
111     struct GNUNET_PeerIdentity id;
112
113     GNUNET_PEER_resolve (peer_id, &id);
114     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
115                 "*** Peer %s (%u) has failed %d checks!\n",
116                 GNUNET_i2s (&id), peer_id, failed - pre_failed);
117   }
118 }
119
120
121 static void
122 finish (void)
123 {
124   unsigned int i;
125
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
127   for (i = 0; i < 10; i++)
128   {
129     GNUNET_free (pi[i]);
130   }
131 }
132
133 /**
134  * Convert an integer int to a peer identity
135  */
136 static struct GNUNET_PeerIdentity *
137 get_pi (uint32_t id)
138 {
139   struct GNUNET_PeerIdentity *pi;
140
141   pi = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
142   pi->hashPubKey.bits[0] = id + 1;
143   return pi;
144 }
145
146
147 int
148 main (int argc, char *argv[])
149 {
150   struct MeshTunnelTreeNode *node;
151   struct MeshPeerPath *path;
152   struct MeshPeerPath *path1;
153   unsigned int i;
154
155   failed = 0;
156   cb_call = 0;
157   GNUNET_log_setup ("test_mesh_api_tree",
158 #if VERBOSE
159                     "DEBUG",
160 #else
161                     "WARNING",
162 #endif
163                     NULL);
164   for (i = 0; i < 10; i++)
165   {
166     pi[i] = get_pi (i);
167     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
168     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i + 1,
169                 GNUNET_h2s (&pi[i]->hashPubKey));
170   }
171   tree = tree_new (1);
172   tree->me = tree->root;
173   path = path_new (4);
174   path->peers[0] = 1;
175   path->peers[1] = 2;
176   path->peers[2] = 3;
177   path->peers[3] = 4;
178   path->length = 4;
179
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
181   tree_add_path (tree, path, &cb, NULL);
182   tree_debug (tree);
183   path1 = tree_get_path_to_peer (tree, 4);
184   if (NULL == path1 || path->length != path1->length ||
185       memcmp (path->peers, path1->peers, path->length) != 0)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
188     failed++;
189   }
190   path_destroy (path1);
191   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
192   test_assert (3, MESH_PEER_RELAY, 1, 0);
193   test_assert (2, MESH_PEER_RELAY, 1, 0);
194   test_assert (1, MESH_PEER_ROOT, 1, 0);
195
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
197   path->length--;
198   tree_add_path (tree, path, &cb, NULL);
199   tree_debug (tree);
200
201   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
202   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
203   test_assert (2, MESH_PEER_RELAY, 1, 0);
204   test_assert (1, MESH_PEER_ROOT, 1, 0);
205
206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path 1 2 3 5\n");
207   path->length++;
208   path->peers[3] = 5;
209   tree_add_path (tree, path, &cb, NULL);
210   tree_debug (tree);
211
212   test_assert (5, MESH_PEER_SEARCHING, 0, 2);
213   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
214   test_assert (3, MESH_PEER_SEARCHING, 2, 2);
215   test_assert (2, MESH_PEER_RELAY, 1, 0);
216   test_assert (1, MESH_PEER_ROOT, 1, 0);
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Calculating costs...\n");
219   for (i = 1; i < 5; i++)
220   {
221     path->length = i;
222     if (tree_get_path_cost(tree, path) != 0)
223     {
224       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
225                   "test: length %u cost failed!\n",
226                   i);
227       failed++;
228     }
229   }
230   path->length++;
231   path->peers[4] = 6;
232   if (tree_get_path_cost(tree, path) != 1)
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
235                 "test: length %u cost failed!\n",
236                 i);
237     failed++;
238   }
239   path->peers[3] = 7;
240   if (tree_get_path_cost(tree, path) != 2)
241   {
242     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243                 "test: length %u cost failed!\n",
244                 i);
245     failed++;
246   }
247   path->length--;
248   if (tree_get_path_cost(tree, path) != 1)
249   {
250     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
251                 "test: length %u cost failed!\n",
252                 i);
253     failed++;
254   }
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path (5)\n");
256   tree_set_status(tree, 5, MESH_PEER_READY);
257   cb_call = 1;
258   node = tree_del_path (tree, 5, &cb, NULL);
259   tree_debug (tree);
260   if (cb_call != 0)
261   {
262     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
263     failed++;
264   }
265   if (node->peer != 5)
266   {
267     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
268     failed++;
269   }
270
271   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
272   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
273   test_assert (2, MESH_PEER_RELAY, 1, 0);
274   test_assert (1, MESH_PEER_ROOT, 1, 0);
275
276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
277   GNUNET_free (node);
278
279   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280               "test: Adding new shorter first path...\n");
281   path->length = 2;
282   path->peers[1] = 4;
283   cb_call = 1;
284   tree_find_peer (tree, 4)->status = MESH_PEER_READY;
285   tree_add_path (tree, path, &cb, NULL);
286   tree_debug (tree);
287   if (cb_call != 0)
288   {
289     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
290     failed++;
291   }
292
293   test_assert (4, MESH_PEER_SEARCHING, 0, 4);
294   test_assert (3, MESH_PEER_SEARCHING, 0, 2);
295   test_assert (2, MESH_PEER_RELAY, 1, 0);
296   test_assert (1, MESH_PEER_ROOT, 2, 0);
297
298   GNUNET_free (path->peers);
299   GNUNET_free (path);
300   tree_destroy (tree);
301
302   /****************************************************************************/
303
304   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:\n");
305   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Testing relay trees\n");
306   for (i = 0; i < 10; i++)
307   {
308     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
309   }
310   tree = tree_new (2);
311   path = path_new (8);
312   path->peers[0] = 2;
313   path->peers[1] = 1;
314   path->peers[2] = 3;
315   path->length = 3;
316
317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
318   tree_add_path (tree, path, &cb, NULL);
319   tree_debug (tree);
320
321   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
322   test_assert (1, MESH_PEER_RELAY, 1, 0);
323   test_assert (2, MESH_PEER_ROOT, 1, 0);
324
325   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding long path: 2 1 4 5 3\n");
326   path->peers[2] = 4;
327   path->peers[3] = 5;
328   path->peers[4] = 3;
329   path->length = 5;
330   tree_add_path (tree, path, &cb, NULL);
331   tree_debug (tree);
332
333   test_assert (3, MESH_PEER_SEARCHING, 0, 4);
334   test_assert (5, MESH_PEER_RELAY, 1, 4);
335   test_assert (4, MESH_PEER_RELAY, 1, 4);
336   test_assert (1, MESH_PEER_RELAY, 1, 0);
337   test_assert (2, MESH_PEER_ROOT, 1, 0);
338
339   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340               "test: Even longer path: 2 6 1 7 8 4 5 3\n");
341   path->peers[0] = 2;
342   path->peers[1] = 6;
343   path->peers[2] = 1;
344   path->peers[3] = 7;
345   path->peers[4] = 8;
346   path->peers[5] = 4;
347   path->peers[6] = 5;
348   path->peers[7] = 3;
349   path->length = 8;
350   tree_add_path (tree, path, &cb, NULL);
351   tree_debug (tree);
352
353   test_assert (3, MESH_PEER_SEARCHING, 0, 7);
354   test_assert (5, MESH_PEER_RELAY, 1, 7);
355   test_assert (4, MESH_PEER_RELAY, 1, 7);
356   test_assert (8, MESH_PEER_RELAY, 1, 7);
357   test_assert (7, MESH_PEER_RELAY, 1, 7);
358   test_assert (1, MESH_PEER_RELAY, 1, 0);
359   test_assert (6, MESH_PEER_RELAY, 1, 0);
360   test_assert (2, MESH_PEER_ROOT, 1, 0);
361
362   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
363   path->peers[1] = 1;
364   path->peers[2] = 3;
365   path->length = 3;
366   tree_add_path (tree, path, &cb, NULL);
367   tree_debug (tree);
368
369   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
370   test_assert (1, MESH_PEER_RELAY, 1, 0);
371   test_assert (2, MESH_PEER_ROOT, 1, 0);
372
373   GNUNET_free (path->peers);
374   GNUNET_free (path);
375   tree_destroy (tree);
376   finish ();
377   if (failed > 0)
378   {
379     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
380     return 1;
381   }
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");
383
384   return 0;
385 }