-bump name to 97
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_api_notify_friend_only.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2009 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 peerinfo/test_peerinfo_api_notify_friend_only.c
23  * @brief testcase friend only HELLO restrictions in for peerinfo
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  *
27  * TODO:
28  * - test merging of HELLOs (add same peer twice...)
29  */
30 #include "platform.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet_testing_lib.h"
35 #include "peerinfo.h"
36
37 #define TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
38
39 static struct GNUNET_PEERINFO_Handle *h;
40 static struct GNUNET_PEERINFO_NotifyContext *pnc_w_fo;
41 static struct GNUNET_PEERINFO_NotifyContext *pnc_wo_fo;
42
43 static const struct GNUNET_CONFIGURATION_Handle *mycfg;
44
45
46 static int global_ret;
47
48 /**
49  * Did we get a HELLO callback for notification handle with friend HELLOS
50  * (expected)
51  */
52 static int res_cb_w_fo;
53
54 /**
55  * Did we get a HELLO callback for notification handle without friend HELLOS
56  * (not expected)
57  */
58 static int res_cb_wo_fo;
59
60 struct GNUNET_PeerIdentity pid;
61
62 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
63
64
65 static void
66 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 {
68         timeout_task = GNUNET_SCHEDULER_NO_TASK;
69         GNUNET_break (0);
70         if (NULL != pnc_wo_fo)
71         {
72                 GNUNET_PEERINFO_notify_cancel (pnc_wo_fo);
73                 pnc_wo_fo = NULL;
74         }
75         if (NULL != pnc_w_fo)
76         {
77                 GNUNET_PEERINFO_notify_cancel (pnc_w_fo);
78                 pnc_w_fo = NULL;
79         }
80         if (NULL != h)
81         {
82                 GNUNET_PEERINFO_disconnect (h);
83                 h = NULL;
84         }
85         global_ret = 255;
86 }
87
88 static void
89 done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91                 if (NULL != pnc_w_fo)
92                 GNUNET_PEERINFO_notify_cancel (pnc_w_fo);
93                 pnc_w_fo = NULL;
94                 if (NULL != pnc_wo_fo)
95                         GNUNET_PEERINFO_notify_cancel (pnc_wo_fo);
96                 pnc_wo_fo = NULL;
97                 GNUNET_PEERINFO_disconnect (h);
98                 h = NULL;
99
100                 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
101                 {
102                                 GNUNET_SCHEDULER_cancel (timeout_task);
103                                 timeout_task = GNUNET_SCHEDULER_NO_TASK;
104                 }
105
106                 if ((GNUNET_YES == res_cb_w_fo) && (GNUNET_NO == res_cb_wo_fo))
107                         global_ret = 0;
108                 else
109                         GNUNET_break (0);
110 }
111
112 static size_t
113 address_generator (void *cls, size_t max, void *buf)
114 {
115   size_t *agc = cls;
116   size_t ret;
117   struct GNUNET_HELLO_Address address;
118
119   if (0 == *agc)
120     return 0;
121   memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
122   address.address = "Address";
123   address.transport_name = "peerinfotest";
124   address.address_length = *agc;
125   ret =
126       GNUNET_HELLO_add_address (&address,
127                                 GNUNET_TIME_relative_to_absolute
128                                 (GNUNET_TIME_UNIT_HOURS), buf, max);
129   (*agc)--;
130   return ret;
131 }
132
133 static void
134 process_w_fo (void *cls, const struct GNUNET_PeerIdentity *peer,
135          const struct GNUNET_HELLO_Message *hello, const char *err_msg)
136 {
137   if (err_msg != NULL)
138   {
139     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140                 _("Error in communication with PEERINFO service\n"));
141     GNUNET_SCHEDULER_add_now(&done, NULL);
142     return;
143   }
144
145   if (NULL != peer)
146   {
147                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received callback for peer `%s' %s HELLO\n",
148                                 GNUNET_i2s (peer),
149                                 (NULL != hello) ? "with" : "without");
150
151                 if (NULL == hello)
152                                 return;
153
154                 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello))
155                 {
156                                 GNUNET_break (0);
157                                 return;
158                 }
159
160                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received %s HELLO for peer `%s'\n",
161                                 (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ? "friend only" : "public",
162                                 GNUNET_i2s (peer));
163                 if (0 == memcmp (&pid, peer, sizeof (pid)))
164                 {
165                                 res_cb_w_fo = GNUNET_YES;
166                                 GNUNET_SCHEDULER_add_now(&done, NULL);
167                 }
168                         return;
169   }
170 }
171
172 static void
173 process_wo_fo (void *cls, const struct GNUNET_PeerIdentity *peer,
174          const struct GNUNET_HELLO_Message *hello, const char *err_msg)
175 {
176   if (err_msg != NULL)
177   {
178     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179                 _("Error in communication with PEERINFO service\n"));
180     GNUNET_SCHEDULER_add_now(&done, NULL);
181     return;
182   }
183
184   if (NULL != peer)
185   {
186                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received callback for peer `%s' %s HELLO\n",
187                                 GNUNET_i2s (peer),
188                                 (NULL != hello) ? "with" : "without");
189
190                 if (NULL == hello)
191                                 return;
192
193                 if (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello))
194                 {
195                                 GNUNET_break (0);
196                                 return;
197                 }
198
199                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received %s HELLO for peer `%s'\n",
200                                 (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ? "friend only" : "public",
201                                 GNUNET_i2s (peer));
202                 if (0 == memcmp (&pid, peer, sizeof (pid)))
203                 {
204                                 GNUNET_break (0);
205                                 res_cb_wo_fo = GNUNET_YES;
206                 }
207   }
208 }
209
210 static void
211 add_peer_done (void *cls, const char *emsg)
212 {
213         if (NULL == emsg)
214         {
215                         return;
216         }
217         else
218         {
219                         GNUNET_break (0);
220                         GNUNET_SCHEDULER_cancel (timeout_task);
221                         timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
222         }
223 }
224
225
226
227 static void
228 add_peer ()
229 {
230   struct GNUNET_HELLO_Message *h2;
231   size_t agc;
232
233   agc = 2;
234   memset (&pid, 32, sizeof (pid));
235   h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &agc, GNUNET_YES);
236   GNUNET_PEERINFO_add_peer (h, h2, &add_peer_done, NULL);
237   GNUNET_free (h2);
238
239 }
240
241
242
243 static void
244 run (void *cls,
245      const struct GNUNET_CONFIGURATION_Handle *cfg,
246      struct GNUNET_TESTING_Peer *peer)
247 {
248         timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
249         mycfg = cfg;
250         pnc_w_fo = GNUNET_PEERINFO_notify (mycfg, GNUNET_YES, &process_w_fo, NULL);
251         pnc_wo_fo = GNUNET_PEERINFO_notify (mycfg, GNUNET_NO, &process_wo_fo, NULL);
252   h = GNUNET_PEERINFO_connect (cfg);
253   GNUNET_assert (NULL != h);
254   add_peer ();
255 }
256
257
258 int
259 main (int argc, char *argv[])
260 {
261         res_cb_w_fo = GNUNET_NO;
262         res_cb_wo_fo = GNUNET_NO;
263   global_ret = 3;
264   if (0 != GNUNET_TESTING_service_run ("test-peerinfo-api-friend-only",
265                                        "peerinfo",
266                                        "test_peerinfo_api_data.conf",
267                                        &run, NULL))
268     return 1;
269   return global_ret;
270 }
271
272 /* end of test_peerinfo_api_notify_friend_only.c */