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