add function conv param string
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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
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 /* end of test_peerinfo_api_friend_only */