Fixed use of uninitialized memory from realloc leading to segfaults and bus errors
[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   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   finish();
122   path1 = tree_get_path_to_peer(tree, 3);
123   if (path->length != path1->length ||
124       memcmp(path->peers, path1->peers, path->length) != 0)
125   {
126     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
127     failed++;
128   }
129   path_destroy(path1);
130   node = tree_find_peer(tree->root, 3);
131   if (node->peer != 3)
132   {
133     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
134     failed++;
135   }
136   if (node->status != MESH_PEER_SEARCHING)
137   {
138     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
139     failed++;
140   }
141   node->status = MESH_PEER_READY;
142   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
143   {
144     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
145     failed++;
146   }
147   return 0;
148
149   node = tree_find_peer(tree->root, 2);
150   if (node->peer != 2)
151   {
152     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
153     failed++;
154   }
155   if (node->status != MESH_PEER_RELAY)
156   {
157     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
158     failed++;
159   }
160   if (node->nchildren != 1)
161   {
162     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
163     failed++;
164   }
165   if (GNUNET_PEER_search(path_get_first_hop(tree, 3)) != 1)
166   {
167     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Wrong first hop!\n");
168     failed++;
169   }
170
171   node = tree_find_peer(tree->root, 1);
172   if (node->peer != 1)
173   {
174     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
175     failed++;
176   }
177   if (node->status != MESH_PEER_RELAY)
178   {
179     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
180     failed++;
181   }
182   if (node->nchildren != 1)
183   {
184     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
185     failed++;
186   }
187
188   path->length--;
189   tree_add_path(tree, path, &cb);
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 }