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