diff options
Diffstat (limited to 'src/transport/test_communicator_unix.c')
-rw-r--r-- | src/transport/test_communicator_unix.c | 212 |
1 files changed, 107 insertions, 105 deletions
diff --git a/src/transport/test_communicator_unix.c b/src/transport/test_communicator_unix.c index db355c93e..83ab1ea26 100644 --- a/src/transport/test_communicator_unix.c +++ b/src/transport/test_communicator_unix.c | |||
@@ -1,33 +1,34 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2019 GNUnet e.V. | 3 | Copyright (C) 2019 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 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, | 7 | by the Free Software Foundation, either version 3 of the License, |
8 | or (at your option) any later version. | 8 | or (at your option) any later version. |
9 | 9 | ||
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | GNUnet is distributed in the hope that it will be useful, but |
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file transport/test_communicator_unix.c | 22 | * @file transport/test_communicator_unix.c |
23 | * @brief test the unix communicator | 23 | * @brief test the unix communicator |
24 | * @author Julius Bünger | 24 | * @author Julius Bünger |
25 | */ | 25 | */ |
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | #include "transport-testing2.h" | 28 | #include "transport-testing2.h" |
29 | #include "gnunet_ats_transport_service.h" | 29 | #include "gnunet_ats_transport_service.h" |
30 | #include "gnunet_signatures.h" | 30 | #include "gnunet_signatures.h" |
31 | #include "gnunet_testing_lib.h" | ||
31 | #include "transport.h" | 32 | #include "transport.h" |
32 | 33 | ||
33 | #include <inttypes.h> | 34 | #include <inttypes.h> |
@@ -41,9 +42,17 @@ | |||
41 | 42 | ||
42 | static struct GNUNET_PeerIdentity peer_id[NUM_PEERS]; | 43 | static struct GNUNET_PeerIdentity peer_id[NUM_PEERS]; |
43 | 44 | ||
45 | static char *communicator_binary; | ||
46 | |||
44 | static struct | 47 | static struct |
45 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS]; | 48 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS]; |
46 | 49 | ||
50 | static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS]; | ||
51 | |||
52 | static char **cfg_peers_name; | ||
53 | |||
54 | static int ret; | ||
55 | |||
47 | // static char *addresses[NUM_PEERS]; | 56 | // static char *addresses[NUM_PEERS]; |
48 | 57 | ||
49 | 58 | ||
@@ -53,7 +62,6 @@ GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS]; | |||
53 | // static char payload[] = "TEST PAYLOAD"; | 62 | // static char payload[] = "TEST PAYLOAD"; |
54 | static uint32_t payload = 42; | 63 | static uint32_t payload = 42; |
55 | 64 | ||
56 | |||
57 | static void | 65 | static void |
58 | communicator_available_cb (void *cls, | 66 | communicator_available_cb (void *cls, |
59 | struct | 67 | struct |
@@ -62,7 +70,7 @@ communicator_available_cb (void *cls, | |||
62 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, | 70 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, |
63 | char *address_prefix) | 71 | char *address_prefix) |
64 | { | 72 | { |
65 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 73 | LOG (GNUNET_ERROR_TYPE_INFO, |
66 | "Communicator available. (cc: %u, prefix: %s)\n", | 74 | "Communicator available. (cc: %u, prefix: %s)\n", |
67 | cc, | 75 | cc, |
68 | address_prefix); | 76 | address_prefix); |
@@ -85,9 +93,12 @@ add_address_cb (void *cls, | |||
85 | aid, | 93 | aid, |
86 | nt); | 94 | nt); |
87 | // addresses[1] = GNUNET_strdup (address); | 95 | // addresses[1] = GNUNET_strdup (address); |
88 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0], | 96 | if (0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1])) |
89 | &peer_id[1], | 97 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0], |
90 | address); | 98 | &peer_id[ |
99 | NUM_PEERS | ||
100 | - 1], | ||
101 | address); | ||
91 | } | 102 | } |
92 | 103 | ||
93 | 104 | ||
@@ -156,11 +167,20 @@ void | |||
156 | incoming_message_cb (void *cls, | 167 | incoming_message_cb (void *cls, |
157 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 168 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle |
158 | *tc_h, | 169 | *tc_h, |
159 | const struct GNUNET_MessageHeader *msg) | 170 | const struct GNUNET_TRANSPORT_IncomingMessage *msg) |
160 | { | 171 | { |
172 | char *payload_ptr; | ||
173 | if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1])) | ||
174 | return; // TODO? | ||
175 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
176 | "%s received data (%lu bytes payload)\n", | ||
177 | (char*) cls, | ||
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 (); | ||
161 | } | 182 | } |
162 | 183 | ||
163 | |||
164 | /** | 184 | /** |
165 | * @brief Main function called by the scheduler | 185 | * @brief Main function called by the scheduler |
166 | * | 186 | * |
@@ -169,28 +189,19 @@ incoming_message_cb (void *cls, | |||
169 | static void | 189 | static void |
170 | run (void *cls) | 190 | run (void *cls) |
171 | { | 191 | { |
172 | struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 192 | for (int i = 0; i < NUM_PEERS; i++) |
173 | 193 | { | |
174 | tc_hs[0] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | 194 | tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( |
175 | "transport", | 195 | "transport", |
176 | "gnunet-communicator-unix", | 196 | communicator_binary, |
177 | "test_communicator_1.conf", | 197 | cfg_peers_name[i], |
178 | &communicator_available_cb, | 198 | &communicator_available_cb, |
179 | NULL, | 199 | &add_address_cb, |
180 | &queue_create_reply_cb, | 200 | &queue_create_reply_cb, |
181 | &add_queue_cb, | 201 | &add_queue_cb, |
182 | NULL, | 202 | &incoming_message_cb, |
183 | NULL); /* cls */ | 203 | cfg_peers_name[i]); /* cls */ |
184 | tc_hs[1] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | 204 | } |
185 | "transport", | ||
186 | "gnunet-communicator-unix", | ||
187 | "test_communicator_2.conf", | ||
188 | &communicator_available_cb, | ||
189 | &add_address_cb, | ||
190 | NULL, | ||
191 | &add_queue_cb, | ||
192 | NULL, | ||
193 | NULL); /* cls */ | ||
194 | } | 205 | } |
195 | 206 | ||
196 | 207 | ||
@@ -198,81 +209,72 @@ int | |||
198 | main (int argc, | 209 | main (int argc, |
199 | char *const *argv) | 210 | char *const *argv) |
200 | { | 211 | { |
201 | char *cfg_filename; | 212 | struct GNUNET_CRYPTO_EddsaPrivateKey *private_key; |
202 | char *opt_cfg_filename; | 213 | char *communicator_name; |
203 | const char *xdg; | 214 | char *cfg_peer; |
204 | char *loglev; | 215 | ret = 1; |
205 | char *logfile; | 216 | |
206 | struct GNUNET_CONFIGURATION_Handle *cfg; | 217 | communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]); |
207 | 218 | GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s", | |
208 | struct GNUNET_GETOPT_CommandLineOption service_options[] = { | 219 | communicator_name); |
209 | GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename), | 220 | cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS); |
210 | GNUNET_GETOPT_option_help (NULL), | 221 | if (GNUNET_OK != GNUNET_log_setup ("test_communicator", |
211 | GNUNET_GETOPT_option_loglevel (&loglev), | 222 | "DEBUG", |
212 | GNUNET_GETOPT_option_logfile (&logfile), | 223 | "test_communicator.log")) |
213 | GNUNET_GETOPT_OPTION_END | ||
214 | }; | ||
215 | |||
216 | if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix", | ||
217 | loglev, | ||
218 | logfile)) | ||
219 | { | 224 | { |
225 | fprintf (stderr, "Unable to setup log\n"); | ||
220 | GNUNET_break (0); | 226 | GNUNET_break (0); |
221 | return GNUNET_SYSERR; | 227 | return 2; |
222 | } | ||
223 | |||
224 | xdg = getenv ("XDG_CONFIG_HOME"); | ||
225 | if (NULL != xdg) | ||
226 | GNUNET_asprintf (&cfg_filename, | ||
227 | "%s%s%s", | ||
228 | xdg, | ||
229 | DIR_SEPARATOR_STR, | ||
230 | GNUNET_OS_project_data_get ()->config_file); | ||
231 | else | ||
232 | cfg_filename = GNUNET_strdup ( | ||
233 | GNUNET_OS_project_data_get ()->user_config_file); | ||
234 | cfg = GNUNET_CONFIGURATION_create (); | ||
235 | if (NULL != opt_cfg_filename) | ||
236 | { | ||
237 | if ((GNUNET_YES != | ||
238 | GNUNET_DISK_file_test (opt_cfg_filename)) || | ||
239 | (GNUNET_SYSERR == | ||
240 | GNUNET_CONFIGURATION_load (cfg, | ||
241 | opt_cfg_filename))) | ||
242 | { | ||
243 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
244 | _ ("Malformed configuration file `%s', exit ...\n"), | ||
245 | opt_cfg_filename); | ||
246 | return GNUNET_SYSERR; | ||
247 | } | ||
248 | } | 228 | } |
249 | else | 229 | for (int i = 0; i < NUM_PEERS; i++) |
250 | { | 230 | { |
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 (); | ||
251 | if (GNUNET_YES == | 236 | if (GNUNET_YES == |
252 | GNUNET_DISK_file_test (cfg_filename)) | 237 | GNUNET_DISK_file_test (cfg_peers_name[i])) |
253 | { | 238 | { |
254 | if (GNUNET_SYSERR == | 239 | if (GNUNET_SYSERR == |
255 | GNUNET_CONFIGURATION_load (cfg, | 240 | GNUNET_CONFIGURATION_load (cfg_peers[i], |
256 | cfg_filename)) | 241 | cfg_peers_name[i])) |
257 | { | 242 | { |
258 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 243 | fprintf (stderr, |
259 | _ ("Malformed configuration file `%s', exit ...\n"), | 244 | "Malformed configuration file `%s', exiting ...\n", |
260 | cfg_filename); | 245 | cfg_peers_name[i]); |
261 | return GNUNET_SYSERR; | 246 | return 1; |
262 | } | 247 | } |
263 | } | 248 | } |
264 | else | 249 | else |
265 | { | 250 | { |
266 | if (GNUNET_SYSERR == | 251 | if (GNUNET_SYSERR == |
267 | GNUNET_CONFIGURATION_load (cfg, | 252 | GNUNET_CONFIGURATION_load (cfg_peers[i], |
268 | NULL)) | 253 | NULL)) |
269 | { | 254 | { |
270 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 255 | fprintf (stderr, |
271 | _ ("Malformed configuration, exit ...\n")); | 256 | "Configuration file %s does not exist, exiting ...\n", |
272 | return GNUNET_SYSERR; | 257 | cfg_peers_name[i]); |
258 | return 1; | ||
273 | } | 259 | } |
274 | } | 260 | } |
261 | private_key = | ||
262 | GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]); | ||
263 | if (NULL == private_key) | ||
264 | { | ||
265 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
266 | "Unable to get peer ID\n"); | ||
267 | return 1; | ||
268 | } | ||
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])); | ||
275 | } | 275 | } |
276 | fprintf (stderr, "Starting test...\n"); | ||
276 | GNUNET_SCHEDULER_run (&run, | 277 | GNUNET_SCHEDULER_run (&run, |
277 | cfg); | 278 | NULL); |
279 | return ret; | ||
278 | } | 280 | } |