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