curly wars / auto-indentation
[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_path.c
23  * @brief test mesh path: test of 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   pre_failed = failed;
76   n = tree_find_peer (tree, peer_id);
77   if (n->peer != peer_id)
78   {
79     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
80                 "Retrieved peer has wrong ID! (%u, %u)\n", n->peer, peer_id);
81     failed++;
82   }
83   if (n->status != status)
84   {
85     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
86                 "Retrieved peer has wrong status! (%u, %u)\n", n->status,
87                 status);
88     failed++;
89   }
90   for (c = n->children_head, i = 0; NULL != c; c = c->next, i++) ;
91   if (i != children)
92   {
93     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
94                 "Retrieved peer wrong has number of children! (%u, %u)\n", i,
95                 children);
96     failed++;
97   }
98   if (0 != first_hop &&
99       GNUNET_PEER_search (path_get_first_hop (tree, peer_id)) != first_hop)
100   {
101     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Wrong first hop! (%u, %u)\n",
102                 GNUNET_PEER_search (path_get_first_hop (tree, peer_id)),
103                 first_hop);
104     failed++;
105   }
106   if (pre_failed != failed)
107   {
108     struct GNUNET_PeerIdentity id;
109
110     GNUNET_PEER_resolve (peer_id, &id);
111     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
112                 "*** Peer %s (%u) has failed %d checks! (real, expected)\n",
113                 GNUNET_i2s (&id), peer_id, failed - pre_failed);
114   }
115 }
116
117
118 static void
119 finish (void)
120 {
121   unsigned int i;
122
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
124   for (i = 0; i < 10; i++)
125   {
126     GNUNET_free (pi[i]);
127   }
128 }
129
130 /**
131  * Convert an integer int to a peer identity
132  */
133 static struct GNUNET_PeerIdentity *
134 get_pi (uint32_t id)
135 {
136   struct GNUNET_PeerIdentity *pi;
137
138   pi = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
139   pi->hashPubKey.bits[0] = id + 1;
140   return pi;
141 }
142
143
144 int
145 main (int argc, char *argv[])
146 {
147   struct MeshTunnelTreeNode *node;
148   struct MeshTunnelTreeNode *node2;
149   struct MeshPeerPath *path;
150   struct MeshPeerPath *path1;
151   unsigned int i;
152
153   failed = 0;
154   cb_call = 0;
155   GNUNET_log_setup ("test_mesh_api_path",
156 #if VERBOSE
157                     "DEBUG",
158 #else
159                     "WARNING",
160 #endif
161                     NULL);
162   for (i = 0; i < 10; i++)
163   {
164     pi[i] = get_pi (i);
165     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
166     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i + 1,
167                 GNUNET_h2s (&pi[i]->hashPubKey));
168   }
169   tree = tree_new (1);
170   tree->me = tree->root;
171   path = path_new (4);
172   path->peers[0] = 1;
173   path->peers[1] = 2;
174   path->peers[2] = 3;
175   path->peers[3] = 4;
176   path->length = 4;
177
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
179   tree_add_path (tree, path, &cb, NULL);
180   tree_debug (tree);
181   path1 = tree_get_path_to_peer (tree, 4);
182   if (path->length != path1->length ||
183       memcmp (path->peers, path1->peers, path->length) != 0)
184   {
185     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
186     failed++;
187   }
188   path_destroy (path1);
189   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
190   test_assert (3, MESH_PEER_RELAY, 1, 0);
191   test_assert (2, MESH_PEER_RELAY, 1, 0);
192   test_assert (1, MESH_PEER_ROOT, 1, 0);
193
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
195   path->length--;
196   tree_add_path (tree, path, &cb, NULL);
197   tree_debug (tree);
198
199   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
200   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
201   test_assert (2, MESH_PEER_RELAY, 1, 0);
202   test_assert (1, MESH_PEER_ROOT, 1, 0);
203
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path...\n");
205   path->length++;
206   path->peers[3] = 5;
207   tree_add_path (tree, path, &cb, NULL);
208   tree_debug (tree);
209
210   test_assert (5, MESH_PEER_SEARCHING, 0, 2);
211   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
212   test_assert (3, MESH_PEER_SEARCHING, 2, 2);
213   test_assert (2, MESH_PEER_RELAY, 1, 0);
214   test_assert (1, MESH_PEER_ROOT, 1, 0);
215
216   node = tree_find_peer (tree, 5);
217   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path...\n");
218   node->status = MESH_PEER_READY;
219   cb_call = 1;
220   node2 = tree_del_path (tree, 5, &cb, NULL);
221   tree_debug (tree);
222   if (cb_call != 0)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
225     failed++;
226   }
227   if (node2->peer != 5)
228   {
229     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
230     failed++;
231   }
232
233   test_assert (4, MESH_PEER_SEARCHING, 0, 2);
234   test_assert (3, MESH_PEER_SEARCHING, 1, 2);
235   test_assert (2, MESH_PEER_RELAY, 1, 0);
236   test_assert (1, MESH_PEER_ROOT, 1, 0);
237
238   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
239   GNUNET_free (node2);
240
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242               "test: Adding new shorter first path...\n");
243   path->length = 2;
244   path->peers[1] = 4;
245   cb_call = 1;
246   tree_find_peer (tree, 4)->status = MESH_PEER_READY;
247   tree_add_path (tree, path, &cb, NULL);
248   tree_debug (tree);
249   if (cb_call != 0)
250   {
251     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
252     failed++;
253   }
254
255   test_assert (4, MESH_PEER_SEARCHING, 0, 4);
256   test_assert (3, MESH_PEER_SEARCHING, 0, 2);
257   test_assert (2, MESH_PEER_RELAY, 1, 0);
258   test_assert (1, MESH_PEER_ROOT, 2, 0);
259
260   GNUNET_free (path->peers);
261   GNUNET_free (path);
262   tree_destroy (tree);
263
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:\n");
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Testing relay trees\n");
266   for (i = 0; i < 10; i++)
267   {
268     GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
269   }
270   tree = tree_new (1);
271   path = path_new (3);
272   path->peers[0] = 1;
273   path->peers[1] = 2;
274   path->peers[2] = 3;
275   path->length = 3;
276
277   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3\n");
278   tree_add_path (tree, path, &cb, NULL);
279   tree_debug (tree);
280   tree_set_me (tree, 2);
281
282   test_assert (3, MESH_PEER_SEARCHING, 0, 3);
283   test_assert (2, MESH_PEER_RELAY, 1, 0);
284   test_assert (1, MESH_PEER_ROOT, 1, 0);
285
286   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding same path: 1 2 3\n");
287   tree_add_path (tree, path, &cb, NULL);
288
289   GNUNET_free (path->peers);
290   GNUNET_free (path);
291   tree_destroy (tree);
292   finish ();
293   if (failed > 0)
294   {
295     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
296     return 1;
297   }
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");
299
300   return 0;
301 }