7c5162102d4c41c23073dc7fc0f9e2e8e8023afb
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_api_notify_friend_only.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2004, 2009 GNUnet e.V.
4
5    GNUnet is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License,
8    or (at your 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    Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 struct GNUNET_SCHEDULER_Task *timeout_task;
62
63 static void
64 end_badly (void *cls)
65 {
66   timeout_task = NULL;
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
87 static void
88 done (void *cls)
89 {
90   if (NULL != pnc_w_fo)
91     GNUNET_PEERINFO_notify_cancel (pnc_w_fo);
92   pnc_w_fo = NULL;
93   if (NULL != pnc_wo_fo)
94     GNUNET_PEERINFO_notify_cancel (pnc_wo_fo);
95   pnc_wo_fo = NULL;
96   GNUNET_PEERINFO_disconnect (h);
97   h = NULL;
98
99   if (NULL != timeout_task)
100   {
101     GNUNET_SCHEDULER_cancel (timeout_task);
102     timeout_task = NULL;
103   }
104
105   if ((GNUNET_YES == res_cb_w_fo) && (GNUNET_NO == res_cb_wo_fo))
106     global_ret = 0;
107   else
108     GNUNET_break (0);
109 }
110
111 static ssize_t
112 address_generator (void *cls, size_t max, void *buf)
113 {
114   size_t *agc = cls;
115   ssize_t ret;
116   struct GNUNET_HELLO_Address address;
117
118   if (0 == *agc)
119     return GNUNET_SYSERR; /* Done */
120   memset (&address.peer, 0, sizeof(struct GNUNET_PeerIdentity));
121   address.address = "Address";
122   address.transport_name = "peerinfotest";
123   address.address_length = *agc;
124   ret = GNUNET_HELLO_add_address (&address,
125                                   GNUNET_TIME_relative_to_absolute (
126                                     GNUNET_TIME_UNIT_HOURS), buf, max);
127   (*agc)--;
128   return ret;
129 }
130
131
132 static void
133 process_w_fo (void *cls,
134               const struct GNUNET_PeerIdentity *peer,
135               const struct GNUNET_HELLO_Message *hello,
136               const char *err_msg)
137 {
138   if (err_msg != NULL)
139   {
140     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141                 _ ("Error in communication with PEERINFO service\n"));
142     GNUNET_SCHEDULER_add_now (&done, NULL);
143     return;
144   }
145
146   if (NULL != peer)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149                 "Received callback for peer `%s' %s HELLO\n", GNUNET_i2s (peer),
150                 (NULL != hello) ? "with" : "without");
151
152     if (NULL == hello)
153       return;
154
155     if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello))
156     {
157       GNUNET_break (0);
158       return;
159     }
160
161     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received %s HELLO for peer `%s'\n",
162                 (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ?
163                 "friend only" : "public",
164                 GNUNET_i2s (peer));
165     if (0 == GNUNET_memcmp (&pid, peer))
166     {
167       res_cb_w_fo = GNUNET_YES;
168       GNUNET_SCHEDULER_add_now (&done, NULL);
169     }
170     return;
171   }
172 }
173
174 static void
175 process_wo_fo (void *cls, const struct GNUNET_PeerIdentity *peer,
176                const struct GNUNET_HELLO_Message *hello, const char *err_msg)
177 {
178   if (err_msg != NULL)
179   {
180     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
181                 _ ("Error in communication with PEERINFO service\n"));
182     GNUNET_SCHEDULER_add_now (&done, NULL);
183     return;
184   }
185
186   if (NULL != peer)
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
189                 "Received callback for peer `%s' %s HELLO\n", GNUNET_i2s (peer),
190                 (NULL != hello) ? "with" : "without");
191
192     if (NULL == hello)
193       return;
194
195     if (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello))
196     {
197       GNUNET_break (0);
198       return;
199     }
200
201     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received %s HELLO for peer `%s'\n",
202                 (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ?
203                 "friend only" : "public",
204                 GNUNET_i2s (peer));
205     if (0 == GNUNET_memcmp (&pid, peer))
206     {
207       GNUNET_break (0);
208       res_cb_wo_fo = GNUNET_YES;
209     }
210   }
211 }
212
213
214 static void
215 add_peer ()
216 {
217   struct GNUNET_HELLO_Message *h2;
218   size_t agc;
219
220   agc = 2;
221   memset (&pid, 32, sizeof(pid));
222   h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &agc,
223                             GNUNET_YES);
224   GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
225   GNUNET_free (h2);
226 }
227
228
229 static void
230 run (void *cls,
231      const struct GNUNET_CONFIGURATION_Handle *cfg,
232      struct GNUNET_TESTING_Peer *peer)
233 {
234   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
235   mycfg = cfg;
236   pnc_w_fo = GNUNET_PEERINFO_notify (mycfg, GNUNET_YES, &process_w_fo, NULL);
237   pnc_wo_fo = GNUNET_PEERINFO_notify (mycfg, GNUNET_NO, &process_wo_fo, NULL);
238   h = GNUNET_PEERINFO_connect (cfg);
239   GNUNET_assert (NULL != h);
240   add_peer ();
241 }
242
243
244 int
245 main (int argc, char *argv[])
246 {
247   res_cb_w_fo = GNUNET_NO;
248   res_cb_wo_fo = GNUNET_NO;
249   global_ret = 3;
250   if (0 != GNUNET_TESTING_service_run ("test-peerinfo-api-friend-only",
251                                        "peerinfo",
252                                        "test_peerinfo_api_data.conf",
253                                        &run, NULL))
254     return 1;
255   return global_ret;
256 }
257
258 /* end of test_peerinfo_api_notify_friend_only.c */