Updated testcase
[oweals/gnunet.git] / src / mesh / test_mesh_path_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_new.h"
32 #include "mesh.h"
33 #include "mesh_tunnel_tree.h"
34
35 #define VERBOSE 1
36
37 int failed;
38 int cb_call;
39 struct GNUNET_PeerIdentity* pi[10];
40 struct MeshTunnelTree *tree;
41
42 void
43 cb (const struct MeshTunnelTreeNode *n)
44 {
45   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Desconnected %u\n", n->peer);
46   if(0 == cb_call)
47   {
48     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test:    and it shouldn't!\n");
49     failed++;
50   }
51   cb_call--;
52 }
53
54
55 void
56 finish(void)
57 {
58   unsigned int i;
59
60   for (i = 0; i < 10; i++)
61   {
62     GNUNET_free(pi[i]);
63   }
64   if (tree->root->nchildren > 0)
65     GNUNET_free(tree->root->children);
66   GNUNET_free(tree->root);
67   GNUNET_free(tree);
68   exit(0);
69 }
70
71 /**
72  * Convert an integer int to a peer identity
73  */
74 static struct GNUNET_PeerIdentity *
75 get_pi (uint32_t id)
76 {
77   struct GNUNET_PeerIdentity *pi;
78
79   pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
80   pi->hashPubKey.bits[0] = id;
81   return pi;
82 }
83
84
85 int
86 main (int argc, char *argv[])
87 {
88   struct MeshTunnelTreeNode *node;
89   struct MeshTunnelTreeNode *node2;
90   struct MeshPeerPath *path[10];
91   unsigned int i;
92
93   failed = 0;
94   cb_call = 0;
95   GNUNET_log_setup ("test_mesh_api_path",
96 #if VERBOSE
97                     "DEBUG",
98 #else
99                     "WARNING",
100 #endif
101                     NULL);
102   for (i = 0; i < 10; i++)
103   {
104       pi[i] = get_pi(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));
108   }
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;
120   path[0]->length = 4;
121
122   finish();
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)
127   {
128     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
129     failed++;
130   }
131   path_destroy(path[1]);
132   node = tunnel_find_peer(tree->root, 3);
133   if (node->peer != 3)
134   {
135     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
136     failed++;
137   }
138   if (node->status != MESH_PEER_SEARCHING)
139   {
140     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
141     failed++;
142   }
143   node->status = MESH_PEER_READY;
144   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
145   {
146     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
147     failed++;
148   }
149   return 0;
150
151   node = tunnel_find_peer(tree->root, 2);
152   if (node->peer != 2)
153   {
154     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
155     failed++;
156   }
157   if (node->status != MESH_PEER_RELAY)
158   {
159     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
160     failed++;
161   }
162   if (node->nchildren != 1)
163   {
164     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
165     failed++;
166   }
167   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
168   {
169     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
170     failed++;
171   }
172
173   node = tunnel_find_peer(tree->root, 1);
174   if (node->peer != 1)
175   {
176     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
177     failed++;
178   }
179   if (node->status != MESH_PEER_RELAY)
180   {
181     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
182     failed++;
183   }
184   if (node->nchildren != 1)
185   {
186     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
187     failed++;
188   }
189
190   path[0]->length--;
191   tunnel_add_path(tree, path[0], &cb);
192
193   node = tunnel_find_peer(tree->root, 2);
194   if (node->peer != 2)
195   {
196     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
197     failed++;
198   }
199   if (node->status != MESH_PEER_SEARCHING)
200   {
201     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
202     failed++;
203   }
204   if (node->nchildren != 1)
205   {
206     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
207     failed++;
208   }
209   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
210   {
211     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
212     failed++;
213   }
214   if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
215   {
216     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
217     failed++;
218   }
219
220   node = tunnel_find_peer(tree->root, 1);
221   if (node->peer != 1)
222   {
223     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
224     failed++;
225   }
226   if (node->status != MESH_PEER_RELAY)
227   {
228     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
229     failed++;
230   }
231   if (node->nchildren != 1)
232   {
233     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
234     failed++;
235   }
236
237   path[0]->length++;
238   path[0]->peers[3] = 4;
239   tunnel_add_path(tree, path[0], &cb);
240
241   node = tunnel_find_peer(tree->root, 2);
242   if (node->peer != 2)
243   {
244     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
245     failed++;
246   }
247   if (node->status != MESH_PEER_SEARCHING)
248   {
249     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
250     failed++;
251   }
252   if (node->nchildren != 2)
253   {
254     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
255     failed++;
256   }
257   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
258   {
259     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
260     failed++;
261   }
262   if (GNUNET_PEER_search(path_get_first_hop(tree, 4)) != 1)
263   {
264     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
265     failed++;
266   }
267
268   node = tunnel_find_peer(tree->root, 1);
269   if (node->peer != 1)
270   {
271     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
272     failed++;
273   }
274   if (node->status != MESH_PEER_RELAY)
275   {
276     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
277     failed++;
278   }
279   if (node->nchildren != 1)
280   {
281     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
282     failed++;
283   }
284
285   node = tunnel_find_peer(tree->root, 4);
286   if (node->peer != 4)
287   {
288     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
289     failed++;
290   }
291   node->status = MESH_PEER_READY;
292   cb_call = 1;
293   node2 = tunnel_del_path(tree, 4, &cb);
294   if (cb_call != 0)
295   {
296     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
297     failed++;
298   }
299   if (node2->peer != 4)
300   {
301     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
302     failed++;
303   }
304 //   GNUNET_free(node2); FIXME destroy
305   node = tunnel_find_peer(tree->root, 2);
306   if (node->peer != 2)
307   {
308     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
309     failed++;
310   }
311   if (node->status != MESH_PEER_SEARCHING)
312   {
313     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
314     failed++;
315   }
316   if (node->nchildren != 1)
317   {
318     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
319     failed++;
320   }
321
322   path[0]->length = 2;
323   path[0]->peers[1] = 3;
324   cb_call = 1;
325   tunnel_add_path(tree, path[0], cb);
326   if (cb_call != 0)
327   {
328     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
329     failed++;
330   }
331   node = tunnel_find_peer(tree->root, 2);
332   if (node->peer != 2)
333   {
334     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
335     failed++;
336   }
337   if (node->status != MESH_PEER_SEARCHING)
338   {
339     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
340     failed++;
341   }
342   if (node->nchildren != 0)
343   {
344     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
345     failed++;
346   }
347   node = tunnel_find_peer(tree->root, 3);
348   if (node->peer != 3)
349   {
350     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
351     failed++;
352   }
353   if (node->status != MESH_PEER_SEARCHING)
354   {
355     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
356     failed++;
357   }
358   if (node->nchildren != 0)
359   {
360     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
361     failed++;
362   }
363   if (GNUNET_PEER_search(path_get_first_hop(tree, 2)) != 1)
364   {
365     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
366     failed++;
367   }
368   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 3)
369   {
370     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
371     failed++;
372   }
373
374
375   if (failed > 0)
376   {
377     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
378     return 1;
379   }
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
381   return 0;
382 }