error handling
[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      SPDX-License-Identifier: AGPL3.0-or-later
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.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 ssize_t
64 address_generator (void *cls, size_t max, void *buf)
65 {
66   size_t *agc = cls;
67   ssize_t ret;
68   struct GNUNET_HELLO_Address address;
69
70   if (0 == *agc)
71     return GNUNET_SYSERR; /* Done */
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 struct GNUNET_PeerIdentity pid;
86
87 static void
88 add_peer ()
89 {
90   struct GNUNET_HELLO_Message *h2;
91   size_t agc;
92
93   agc = 2;
94   memset (&pid, 32, sizeof(pid));
95   h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &agc,
96                             GNUNET_NO);
97   GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
98   GNUNET_free (h2);
99 }
100
101
102 static void
103 process (void *cls, const struct GNUNET_PeerIdentity *peer,
104          const struct GNUNET_HELLO_Message *hello, const char *err_msg)
105 {
106   unsigned int agc;
107
108   if (err_msg != NULL)
109   {
110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                 _ ("Error in communication with PEERINFO service\n"));
112   }
113
114   if (peer == NULL)
115   {
116     ic = NULL;
117     if ((3 == global_ret) && (retries < 50))
118     {
119       /* try again */
120       retries++;
121       add_peer ();
122       ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, NULL,
123                                     &process,
124                                     cls);
125       return;
126     }
127     GNUNET_assert (peer == NULL);
128     GNUNET_assert (2 == global_ret);
129     GNUNET_PEERINFO_disconnect (h);
130     h = NULL;
131     global_ret = 0;
132     return;
133   }
134   if (hello != NULL)
135   {
136     GNUNET_assert (3 == global_ret);
137     agc = 3;
138     GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
139                                     &check_it, &agc);
140     GNUNET_assert (agc == 0);
141     global_ret = 2;
142   }
143 }
144
145
146 static void
147 run (void *cls,
148      const struct GNUNET_CONFIGURATION_Handle *cfg,
149      struct GNUNET_TESTING_Peer *peer)
150 {
151   h = GNUNET_PEERINFO_connect (cfg);
152   GNUNET_assert (NULL != h);
153   add_peer ();
154   ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, &pid,
155                                 &process, cls);
156 }
157
158
159 int
160 main (int argc, char *argv[])
161 {
162   global_ret = 3;
163   if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerinfo",
164                                        "peerinfo",
165                                        "test_peerinfo_api_data.conf",
166                                        &run, NULL))
167     return 1;
168   return global_ret;
169 }
170
171
172 /* end of test_peerinfo_api.c */