Added testcase code, fixed minor bugs
[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
40 void
41 cb (const struct MeshTunnelTreeNode *n)
42 {
43   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Desconnected %u\n", n->peer);
44   if(0 == cb_call)
45   {
46     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test:    and it shouldn't!\n");
47     failed++;
48   }
49   cb_call--;
50 }
51
52 /**
53  * Convert an integer int to a peer identity
54  */
55 static struct GNUNET_PeerIdentity *
56 get_pi (uint32_t id)
57 {
58   struct GNUNET_PeerIdentity *pi;
59
60   pi = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
61   pi->hashPubKey.bits[0] = id;
62   return pi;
63 }
64
65
66 int
67 main (int argc, char *argv[])
68 {
69   struct GNUNET_PeerIdentity* pi[10];
70   struct MeshTunnelTreeNode *node;
71   struct MeshTunnelTreeNode *node2;
72   struct MeshTunnelTree *tree;
73   struct MeshPeerPath *path[10];
74   unsigned int i;
75
76   failed = 0;
77   cb_call = 0;
78   GNUNET_log_setup ("test_mesh_api_path",
79 #if VERBOSE
80                     "DEBUG",
81 #else
82                     "WARNING",
83 #endif
84                     NULL);
85   for (i = 0; i < 10; i++)
86   {
87       pi[i] = get_pi(i);
88       GNUNET_break (i != GNUNET_PEER_intern(pi[i]));
89       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i,
90                  GNUNET_h2s(&pi[i]->hashPubKey));
91   }
92   tree = GNUNET_malloc(sizeof(struct MeshTunnelTree));
93   tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
94   tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
95   tree->root->peer = 0;
96   tree->me = tree->root;
97   path[0] = GNUNET_malloc(sizeof(struct MeshPeerPath));
98   path[0]->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 4);
99   path[0]->peers[0] = 0;
100   path[0]->peers[1] = 1;
101   path[0]->peers[2] = 2;
102   path[0]->peers[3] = 3;
103   path[0]->length = 4;
104
105   tunnel_add_path(tree, path[0], &cb);
106   path[1] = tunnel_get_path_to_peer(tree, 3);
107   if (path[0]->length != path[1]->length ||
108       memcmp(path[0]->peers, path[1]->peers, path[0]->length) != 0)
109   {
110     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
111     failed++;
112   }
113   path_destroy(path[1]);
114   node = tunnel_find_peer(tree->root, 3);
115   if (node->peer != 3)
116   {
117     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
118     failed++;
119   }
120   if (node->status != MESH_PEER_SEARCHING)
121   {
122     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
123     failed++;
124   }
125
126   node = tunnel_find_peer(tree->root, 2);
127   if (node->peer != 2)
128   {
129     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
130     failed++;
131   }
132   if (node->status != MESH_PEER_RELAY)
133   {
134     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
135     failed++;
136   }
137   if (node->nchildren != 1)
138   {
139     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
140     failed++;
141   }
142
143   path[0]->length--;
144   tunnel_add_path(tree, path[0], &cb);
145
146   node = tunnel_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_SEARCHING)
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
163   path[0]->length = 4;
164   path[0]->peers[3] = 4;
165   tunnel_add_path(tree, path[0], &cb);
166
167   node = tunnel_find_peer(tree->root, 2);
168   if (node->peer != 2)
169   {
170     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
171     failed++;
172   }
173   if (node->status != MESH_PEER_SEARCHING)
174   {
175     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
176     failed++;
177   }
178   if (node->nchildren != 2)
179   {
180     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
181     failed++;
182   }
183
184   node = tunnel_find_peer(tree->root, 4);
185   if (node->peer != 4)
186   {
187     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
188     failed++;
189   }
190   node->status = MESH_PEER_READY;
191   cb_call = 1;
192   node2 = tunnel_del_path(tree, 4, &cb);
193   if (cb_call != 0)
194   {
195     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
196     failed++;
197   }
198   if (node2->peer != 4)
199   {
200     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
201     failed++;
202   }
203 //   GNUNET_free(node2); FIXME destroy
204   node = tunnel_find_peer(tree->root, 2);
205   if (node->peer != 2)
206   {
207     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
208     failed++;
209   }
210   if (node->status != MESH_PEER_SEARCHING)
211   {
212     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong status!\n");
213     failed++;
214   }
215   if (node->nchildren != 1)
216   {
217     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Retrieved peer wrong nchildren!\n");
218     failed++;
219   }
220
221   if (failed > 0)
222   {
223     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
224     return 1;
225   }
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
227   return 0;
228 }