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