use testbed methods to alter underlaylinkmodel
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_api_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_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
36
37 static struct GNUNET_PEERINFO_IteratorContext *ic;
38
39 static struct GNUNET_PEERINFO_Handle *h;
40
41 static struct GNUNET_PeerIdentity pid;
42
43 static unsigned int retries;
44
45 static int global_ret;
46
47
48 static ssize_t
49 address_generator (void *cls,
50                    size_t max,
51                    void *buf)
52 {
53   size_t *agc = cls;
54   ssize_t ret;
55   struct GNUNET_HELLO_Address address;
56
57   if (0 == *agc)
58     return GNUNET_SYSERR; /* Done */
59   memset (&address.peer,
60           0,
61           sizeof(struct GNUNET_PeerIdentity));
62   address.address = "Address";
63   address.transport_name = "peerinfotest";
64   address.address_length = *agc;
65   ret = GNUNET_HELLO_add_address (&address,
66                                   GNUNET_TIME_relative_to_absolute (
67                                     GNUNET_TIME_UNIT_HOURS),
68                                   buf,
69                                   max);
70   (*agc)--;
71   return ret;
72 }
73
74
75 static void
76 add_peer ()
77 {
78   struct GNUNET_HELLO_Message *h2;
79   size_t agc;
80
81   agc = 2;
82   memset (&pid, 32, sizeof(pid));
83   h2 = GNUNET_HELLO_create (&pid.public_key,
84                             &address_generator,
85                             &agc,
86                             GNUNET_YES);
87   GNUNET_PEERINFO_add_peer (h,
88                             h2,
89                             NULL,
90                             NULL);
91   GNUNET_free (h2);
92 }
93
94
95 static void
96 process (void *cls,
97          const struct GNUNET_PeerIdentity *peer,
98          const struct GNUNET_HELLO_Message *hello,
99          const char *err_msg)
100 {
101   if (NULL != err_msg)
102   {
103     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
104                 "Error in communication with PEERINFO service: %s\n",
105                 err_msg);
106   }
107   if (NULL == peer)
108   {
109     ic = NULL;
110     if ((3 == global_ret) && (retries < 50))
111     {
112       /* try again */
113       retries++;
114       add_peer ();
115       ic = GNUNET_PEERINFO_iterate (h,
116                                     GNUNET_NO,
117                                     NULL,
118                                     &process,
119                                     cls);
120       return;
121     }
122     GNUNET_assert (peer == NULL);
123     GNUNET_PEERINFO_disconnect (h);
124     h = NULL;
125     global_ret = 0;
126     return;
127   }
128
129   if ((NULL != hello) &&
130       (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)))
131   {
132     fprintf (stderr,
133              "Received friend-only HELLO\n");
134     global_ret = 1;
135     GNUNET_PEERINFO_disconnect (h);
136     h = NULL;
137     return;
138   }
139 }
140
141
142 static void
143 run (void *cls,
144      const struct GNUNET_CONFIGURATION_Handle *cfg,
145      struct GNUNET_TESTING_Peer *peer)
146 {
147   h = GNUNET_PEERINFO_connect (cfg);
148   GNUNET_assert (NULL != h);
149   add_peer ();
150   ic = GNUNET_PEERINFO_iterate (h,
151                                 GNUNET_NO,
152                                 &pid,
153                                 &process,
154                                 NULL);
155 }
156
157
158 int
159 main (int argc,
160       char *argv[])
161 {
162   global_ret = 3;
163   if (0 != GNUNET_TESTING_service_run ("test-peerinfo-api-friend-only",
164                                        "peerinfo",
165                                        "test_peerinfo_api_data.conf",
166                                        &run, NULL))
167     return 1;
168   return global_ret;
169 }
170
171
172 /* end of test_peerinfo_api_friend_only */