aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_communicator.c
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2019-12-22 12:41:26 +0900
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2019-12-22 12:41:26 +0900
commit6d8ba0321cde6cf5dd240b9cbcf36dc95bfdc798 (patch)
tree5c0964e7b3736667a27e735013f7d2d589c14845 /src/transport/test_communicator.c
parentdc090c05b18f416543266e9f47f6a2cff0125b92 (diff)
downloadgnunet-6d8ba0321cde6cf5dd240b9cbcf36dc95bfdc798.tar.gz
gnunet-6d8ba0321cde6cf5dd240b9cbcf36dc95bfdc798.zip
rename test as it is now communicator-independent
Diffstat (limited to 'src/transport/test_communicator.c')
-rw-r--r--src/transport/test_communicator.c281
1 files changed, 281 insertions, 0 deletions
diff --git a/src/transport/test_communicator.c b/src/transport/test_communicator.c
new file mode 100644
index 000000000..34be5d66a
--- /dev/null
+++ b/src/transport/test_communicator.c
@@ -0,0 +1,281 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2019 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 transport/test_communicator.c
23* @brief test the communicators
24* @author Julius Bünger
25* @author Martin Schanzenbach
26*/
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "transport-testing2.h"
30#include "gnunet_ats_transport_service.h"
31#include "gnunet_signatures.h"
32#include "gnunet_testing_lib.h"
33#include "transport.h"
34
35#include <inttypes.h>
36
37
38#define LOG(kind, ...) GNUNET_log_from (kind, \
39 "test_transport_communicator", \
40 __VA_ARGS__)
41
42#define NUM_PEERS 2
43
44static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
45
46static char *communicator_binary;
47
48static struct
49GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
50
51static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
52
53static char **cfg_peers_name;
54
55static int ret;
56
57// static char *addresses[NUM_PEERS];
58
59
60#define PAYLOAD_SIZE 256
61
62// static char payload[PAYLOAD_SIZE] = "TEST PAYLOAD";
63// static char payload[] = "TEST PAYLOAD";
64static uint32_t payload = 42;
65
66static void
67communicator_available_cb (void *cls,
68 struct
69 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
70 *tc_h,
71 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
72 char *address_prefix)
73{
74 LOG (GNUNET_ERROR_TYPE_INFO,
75 "Communicator available. (cc: %u, prefix: %s)\n",
76 cc,
77 address_prefix);
78}
79
80
81static void
82add_address_cb (void *cls,
83 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
84 tc_h,
85 const char *address,
86 struct GNUNET_TIME_Relative expiration,
87 uint32_t aid,
88 enum GNUNET_NetworkType nt)
89{
90 LOG (GNUNET_ERROR_TYPE_DEBUG,
91 "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
92 address,
93 expiration.rel_value_us,
94 aid,
95 nt);
96 // addresses[1] = GNUNET_strdup (address);
97 if (0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
98 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
99 &peer_id[
100 NUM_PEERS
101 - 1],
102 address);
103}
104
105
106/**
107 * @brief Callback that informs whether the requested queue will be
108 * established
109 *
110 * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
111 *
112 * @param cls Closure - unused
113 * @param tc_h Communicator handle - unused
114 * @param will_try #GNUNET_YES if queue will be established
115 * #GNUNET_NO if queue will not be established (bogous address)
116 */
117static void
118queue_create_reply_cb (void *cls,
119 struct
120 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
121 tc_h,
122 int will_try)
123{
124 if (GNUNET_YES == will_try)
125 LOG (GNUNET_ERROR_TYPE_DEBUG,
126 "Queue will be established!\n");
127 else
128 LOG (GNUNET_ERROR_TYPE_WARNING,
129 "Queue won't be established (bougus address?)!\n");
130}
131
132
133/**
134 * @brief Handle opening of queue
135 *
136 * Issues sending of test data
137 *
138 * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
139 *
140 * @param cls Closure
141 * @param tc_h Communicator handle
142 * @param tc_queue Handle to newly opened queue
143 */
144static void
145add_queue_cb (void *cls,
146 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
147 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *
148 tc_queue)
149{
150 LOG (GNUNET_ERROR_TYPE_DEBUG,
151 "Got Queue!\n");
152 GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_queue,
153 &payload,
154 sizeof(payload));
155}
156
157
158/**
159 * @brief Handle an incoming message
160 *
161 * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
162
163 * @param cls Closure
164 * @param tc_h Handle to the receiving communicator
165 * @param msg Received message
166 */
167void
168incoming_message_cb (void *cls,
169 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
170 *tc_h,
171 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
172{
173 char *payload_ptr;
174 if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
175 return; // TODO?
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "%s received data (%lu bytes payload)\n",
178 (char*) cls,
179 ntohs (msg->header.size) - sizeof (struct GNUNET_TRANSPORT_IncomingMessage));
180 payload_ptr = (char*)&msg[1] + sizeof (struct GNUNET_MessageHeader);
181 ret = memcmp (payload_ptr, &payload, sizeof (payload));
182 GNUNET_SCHEDULER_shutdown ();
183}
184
185/**
186 * @brief Main function called by the scheduler
187 *
188 * @param cls Closure - Handle to configuration
189 */
190static void
191run (void *cls)
192{
193 for (int i = 0; i < NUM_PEERS; i++)
194 {
195 tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
196 "transport",
197 communicator_binary,
198 cfg_peers_name[i],
199 &communicator_available_cb,
200 &add_address_cb,
201 &queue_create_reply_cb,
202 &add_queue_cb,
203 &incoming_message_cb,
204 cfg_peers_name[i]); /* cls */
205 }
206}
207
208
209int
210main (int argc,
211 char *const *argv)
212{
213 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
214 char *communicator_name;
215 char *cfg_peer;
216 ret = 1;
217
218 communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
219 GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s",
220 communicator_name);
221 cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS);
222 if (GNUNET_OK != GNUNET_log_setup ("test_communicator",
223 "DEBUG",
224 "test_communicator.log"))
225 {
226 fprintf (stderr, "Unable to setup log\n");
227 GNUNET_break (0);
228 return 2;
229 }
230 for (int i = 0; i < NUM_PEERS; i++)
231 {
232 GNUNET_asprintf ((&cfg_peer),
233 "test_communicator_%s_peer%u.conf",
234 communicator_name, i + 1);
235 cfg_peers_name[i] = cfg_peer;
236 cfg_peers[i] = GNUNET_CONFIGURATION_create ();
237 if (GNUNET_YES ==
238 GNUNET_DISK_file_test (cfg_peers_name[i]))
239 {
240 if (GNUNET_SYSERR ==
241 GNUNET_CONFIGURATION_load (cfg_peers[i],
242 cfg_peers_name[i]))
243 {
244 fprintf (stderr,
245 "Malformed configuration file `%s', exiting ...\n",
246 cfg_peers_name[i]);
247 return 1;
248 }
249 }
250 else
251 {
252 if (GNUNET_SYSERR ==
253 GNUNET_CONFIGURATION_load (cfg_peers[i],
254 NULL))
255 {
256 fprintf (stderr,
257 "Configuration file %s does not exist, exiting ...\n",
258 cfg_peers_name[i]);
259 return 1;
260 }
261 }
262 private_key =
263 GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]);
264 if (NULL == private_key)
265 {
266 LOG (GNUNET_ERROR_TYPE_ERROR,
267 "Unable to get peer ID\n");
268 return 1;
269 }
270 GNUNET_CRYPTO_eddsa_key_get_public (private_key,
271 &peer_id[i].public_key);
272 GNUNET_free (private_key);
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
274 "Identity of peer %u is %s\n",
275 i, GNUNET_i2s_full (&peer_id[i]));
276 }
277 fprintf (stderr, "Starting test...\n");
278 GNUNET_SCHEDULER_run (&run,
279 NULL);
280 return ret;
281}