uncrustify as demanded.
[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(GNUNET_TIME_UNIT_HOURS),
67                                  buf,
68                                  max);
69   (*agc)--;
70   return ret;
71 }
72
73
74 static void
75 add_peer()
76 {
77   struct GNUNET_HELLO_Message *h2;
78   size_t agc;
79
80   agc = 2;
81   memset(&pid, 32, sizeof(pid));
82   h2 = GNUNET_HELLO_create(&pid.public_key,
83                            &address_generator,
84                            &agc,
85                            GNUNET_YES);
86   GNUNET_PEERINFO_add_peer(h,
87                            h2,
88                            NULL,
89                            NULL);
90   GNUNET_free(h2);
91 }
92
93
94 static void
95 process(void *cls,
96         const struct GNUNET_PeerIdentity *peer,
97         const struct GNUNET_HELLO_Message *hello,
98         const char *err_msg)
99 {
100   if (NULL != err_msg)
101     {
102       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
103                  "Error in communication with PEERINFO service: %s\n",
104                  err_msg);
105     }
106   if (NULL == peer)
107     {
108       ic = NULL;
109       if ((3 == global_ret) && (retries < 50))
110         {
111           /* try again */
112           retries++;
113           add_peer();
114           ic = GNUNET_PEERINFO_iterate(h,
115                                        GNUNET_NO,
116                                        NULL,
117                                        &process,
118                                        cls);
119           return;
120         }
121       GNUNET_assert(peer == NULL);
122       GNUNET_PEERINFO_disconnect(h);
123       h = NULL;
124       global_ret = 0;
125       return;
126     }
127
128   if ((NULL != hello) &&
129       (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello)))
130     {
131       fprintf(stderr,
132               "Received friend-only HELLO\n");
133       global_ret = 1;
134       GNUNET_PEERINFO_disconnect(h);
135       h = NULL;
136       return;
137     }
138 }
139
140
141 static void
142 run(void *cls,
143     const struct GNUNET_CONFIGURATION_Handle *cfg,
144     struct GNUNET_TESTING_Peer *peer)
145 {
146   h = GNUNET_PEERINFO_connect(cfg);
147   GNUNET_assert(NULL != h);
148   add_peer();
149   ic = GNUNET_PEERINFO_iterate(h,
150                                GNUNET_NO,
151                                &pid,
152                                &process,
153                                NULL);
154 }
155
156
157 int
158 main(int argc,
159      char *argv[])
160 {
161   global_ret = 3;
162   if (0 != GNUNET_TESTING_service_run("test-peerinfo-api-friend-only",
163                                       "peerinfo",
164                                       "test_peerinfo_api_data.conf",
165                                       &run, NULL))
166     return 1;
167   return global_ret;
168 }
169
170 /* end of test_peerinfo_api_friend_only */