Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / transport / test_transport_api_monitor_peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2016 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  * @file transport/test_transport_api_monitor_peers.c
22  * @brief base test case for transport peer monitor API
23  */
24 #include "platform.h"
25 #include "gnunet_transport_service.h"
26 #include "transport-testing.h"
27
28 /**
29  * How long until we give up on transmitting the message?
30  */
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply ( \
37     GNUNET_TIME_UNIT_SECONDS, 30)
38
39 #define TEST_MESSAGE_SIZE 2600
40
41 #define TEST_MESSAGE_TYPE 12345
42
43 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
44
45 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
46
47 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
48
49 static int p1_c;
50
51 static int p2_c;
52
53 static int p1_c_notify;
54
55 static int p2_c_notify;
56
57
58 static void
59 custom_shutdown (void *cls)
60 {
61   if (NULL != pmc_p1)
62   {
63     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
64     pmc_p1 = NULL;
65   }
66   if (NULL != pmc_p2)
67   {
68     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
69     pmc_p2 = NULL;
70   }
71 }
72
73
74 static void
75 notify_receive (void *cls,
76                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
77                 const struct GNUNET_PeerIdentity *sender,
78                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
79 {
80   char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
81
82   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83               "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
84               receiver->no,
85               ps,
86               ntohs (message->header.type),
87               ntohs (message->header.size),
88               GNUNET_i2s (sender));
89   GNUNET_free (ps);
90 }
91
92
93 static void
94 sendtask (void *cls)
95 {
96   /* intentionally empty */
97 }
98
99
100 static void
101 check_done ()
102 {
103   if ((GNUNET_YES == p1_c) &&
104       (GNUNET_YES == p2_c) &&
105       p1_c_notify &&
106       p2_c_notify)
107   {
108     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109                 "Both peers state to be connected\n");
110     ccc->global_ret = GNUNET_OK;
111     GNUNET_SCHEDULER_shutdown ();
112   }
113 }
114
115
116 static void
117 notify_connect (void *cls,
118                 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
119                 const struct GNUNET_PeerIdentity *other)
120 {
121   GNUNET_TRANSPORT_TESTING_log_connect (cls,
122                                         me,
123                                         other);
124   if (0 == memcmp (other, &ccc->p[0]->id, sizeof(struct GNUNET_PeerIdentity)))
125   {
126     p1_c_notify = GNUNET_YES;
127   }
128   if (0 == memcmp (other, &ccc->p[1]->id, sizeof(struct GNUNET_PeerIdentity)))
129   {
130     p2_c_notify = GNUNET_YES;
131   }
132   check_done ();
133 }
134
135
136 static void
137 monitor1_cb (void *cls,
138              const struct GNUNET_PeerIdentity *peer,
139              const struct GNUNET_HELLO_Address *address,
140              enum GNUNET_TRANSPORT_PeerState state,
141              struct GNUNET_TIME_Absolute state_timeout)
142 {
143   if ((NULL == address) || (NULL == ccc->p[0]))
144     return;
145
146   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
147               "Monitor 1: %s %s %s\n",
148               GNUNET_i2s (&address->peer),
149               GNUNET_TRANSPORT_ps2s (state),
150               GNUNET_STRINGS_absolute_time_to_string (state_timeout));
151   if ((0 == memcmp (&address->peer, &ccc->p[1]->id, sizeof(ccc->p[1]->id))) &&
152       (GNUNET_YES == GNUNET_TRANSPORT_is_connected (state)) &&
153       (GNUNET_NO == p1_c))
154   {
155     p1_c = GNUNET_YES;
156     check_done ();
157   }
158 }
159
160
161 static void
162 monitor2_cb (void *cls,
163              const struct GNUNET_PeerIdentity *peer,
164              const struct GNUNET_HELLO_Address *address,
165              enum GNUNET_TRANSPORT_PeerState state,
166              struct GNUNET_TIME_Absolute state_timeout)
167 {
168   if ((NULL == address) || (NULL == ccc->p[1]))
169     return;
170
171   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
172               "Monitor 2: %s %s %s\n",
173               GNUNET_i2s (&address->peer),
174               GNUNET_TRANSPORT_ps2s (state),
175               GNUNET_STRINGS_absolute_time_to_string (state_timeout));
176   if ((0 == memcmp (&address->peer, &ccc->p[0]->id, sizeof(ccc->p[0]->id))) &&
177       (GNUNET_YES == GNUNET_TRANSPORT_is_connected (state)) &&
178       (GNUNET_NO == p2_c))
179   {
180     p2_c = GNUNET_YES;
181     check_done ();
182   }
183 }
184
185
186 static void
187 start_monitors (void *cls)
188 {
189   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (ccc->p[0]->cfg,
190                                            NULL,
191                                            GNUNET_NO,
192                                            &monitor1_cb,
193                                            NULL);
194   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (ccc->p[1]->cfg,
195                                            NULL,
196                                            GNUNET_NO,
197                                            &monitor2_cb,
198                                            NULL);
199 }
200
201
202 int
203 main (int argc, char *argv[])
204 {
205   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
206     .pre_connect_task = &start_monitors,
207     .connect_continuation = &sendtask,
208     .config_file = "test_transport_api_data.conf",
209     .rec = &notify_receive,
210     .nc = &notify_connect,
211     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
212     .shutdown_task = &custom_shutdown,
213     .timeout = TIMEOUT
214   };
215
216   ccc = &my_ccc;
217   if (GNUNET_OK !=
218       GNUNET_TRANSPORT_TESTING_main (2,
219                                      &GNUNET_TRANSPORT_TESTING_connect_check,
220                                      ccc))
221     return 1;
222   return 0;
223 }
224
225
226 /* end of test_transport_api_monitor_peers.c */