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