2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/test_communicator_unix.c
23 * @brief test the unix communicator
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "transport-testing2.h"
29 #include "gnunet_ats_transport_service.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_testing_lib.h"
32 #include "transport.h"
37 #define LOG(kind, ...) GNUNET_log_from (kind, \
38 "test_transport_communicator_unix", \
43 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
45 static char *communicator_binary;
48 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
50 static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
52 static char **cfg_peers_name;
56 // static char *addresses[NUM_PEERS];
59 #define PAYLOAD_SIZE 256
61 // static char payload[PAYLOAD_SIZE] = "TEST PAYLOAD";
62 // static char payload[] = "TEST PAYLOAD";
63 static uint32_t payload = 42;
66 communicator_available_cb (void *cls,
68 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
70 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
73 LOG (GNUNET_ERROR_TYPE_INFO,
74 "Communicator available. (cc: %u, prefix: %s)\n",
81 add_address_cb (void *cls,
82 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
85 struct GNUNET_TIME_Relative expiration,
87 enum GNUNET_NetworkType nt)
89 LOG (GNUNET_ERROR_TYPE_DEBUG,
90 "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
92 expiration.rel_value_us,
95 // addresses[1] = GNUNET_strdup (address);
96 if (0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
97 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
106 * @brief Callback that informs whether the requested queue will be
109 * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
111 * @param cls Closure - unused
112 * @param tc_h Communicator handle - unused
113 * @param will_try #GNUNET_YES if queue will be established
114 * #GNUNET_NO if queue will not be established (bogous address)
117 queue_create_reply_cb (void *cls,
119 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
123 if (GNUNET_YES == will_try)
124 LOG (GNUNET_ERROR_TYPE_DEBUG,
125 "Queue will be established!\n");
127 LOG (GNUNET_ERROR_TYPE_WARNING,
128 "Queue won't be established (bougus address?)!\n");
133 * @brief Handle opening of queue
135 * Issues sending of test data
137 * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
140 * @param tc_h Communicator handle
141 * @param tc_queue Handle to newly opened queue
144 add_queue_cb (void *cls,
145 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
146 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *
149 LOG (GNUNET_ERROR_TYPE_DEBUG,
151 GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_queue,
158 * @brief Handle an incoming message
160 * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
163 * @param tc_h Handle to the receiving communicator
164 * @param msg Received message
167 incoming_message_cb (void *cls,
168 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
170 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
173 if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "%s received data (%lu bytes payload)\n",
178 ntohs (msg->header.size) - sizeof (struct GNUNET_TRANSPORT_IncomingMessage));
179 payload_ptr = (char*)&msg[1] + sizeof (struct GNUNET_MessageHeader);
180 ret = memcmp (payload_ptr, &payload, sizeof (payload));
181 GNUNET_SCHEDULER_shutdown ();
185 * @brief Main function called by the scheduler
187 * @param cls Closure - Handle to configuration
192 for (int i = 0; i < NUM_PEERS; i++)
194 tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
198 &communicator_available_cb,
200 &queue_create_reply_cb,
202 &incoming_message_cb,
203 cfg_peers_name[i]); /* cls */
212 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
213 char *communicator_name;
217 communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
218 GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s",
220 cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS);
221 if (GNUNET_OK != GNUNET_log_setup ("test_communicator",
223 "test_communicator.log"))
225 fprintf (stderr, "Unable to setup log\n");
229 for (int i = 0; i < NUM_PEERS; i++)
231 GNUNET_asprintf ((&cfg_peer),
232 "test_communicator_%s_peer%u.conf",
233 communicator_name, i + 1);
234 cfg_peers_name[i] = cfg_peer;
235 cfg_peers[i] = GNUNET_CONFIGURATION_create ();
237 GNUNET_DISK_file_test (cfg_peers_name[i]))
240 GNUNET_CONFIGURATION_load (cfg_peers[i],
244 "Malformed configuration file `%s', exiting ...\n",
252 GNUNET_CONFIGURATION_load (cfg_peers[i],
256 "Configuration file %s does not exist, exiting ...\n",
262 GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]);
263 if (NULL == private_key)
265 LOG (GNUNET_ERROR_TYPE_ERROR,
266 "Unable to get peer ID\n");
269 GNUNET_CRYPTO_eddsa_key_get_public (private_key,
270 &peer_id[i].public_key);
271 GNUNET_free (private_key);
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273 "Identity of peer %u is %s\n",
274 i, GNUNET_i2s_full (&peer_id[i]));
276 fprintf (stderr, "Starting test...\n");
277 GNUNET_SCHEDULER_run (&run,