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