83ab1ea269c792b67a8825616b83f22e7190455b
[oweals/gnunet.git] / src / transport / test_communicator_unix.c
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_unix.c
23 * @brief test the unix communicator
24 * @author Julius Bünger
25 */
26 #include "platform.h"
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"
33
34 #include <inttypes.h>
35
36
37 #define LOG(kind, ...) GNUNET_log_from (kind, \
38                                         "test_transport_communicator_unix", \
39                                         __VA_ARGS__)
40
41 #define NUM_PEERS 2
42
43 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
44
45 static char *communicator_binary;
46
47 static struct
48 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
49
50 static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
51
52 static char **cfg_peers_name;
53
54 static int ret;
55
56 // static char *addresses[NUM_PEERS];
57
58
59 #define PAYLOAD_SIZE 256
60
61 // static char payload[PAYLOAD_SIZE] = "TEST PAYLOAD";
62 // static char payload[] = "TEST PAYLOAD";
63 static uint32_t payload = 42;
64
65 static void
66 communicator_available_cb (void *cls,
67                            struct
68                            GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
69                            *tc_h,
70                            enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
71                            char *address_prefix)
72 {
73   LOG (GNUNET_ERROR_TYPE_INFO,
74        "Communicator available. (cc: %u, prefix: %s)\n",
75        cc,
76        address_prefix);
77 }
78
79
80 static void
81 add_address_cb (void *cls,
82                 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
83                 tc_h,
84                 const char *address,
85                 struct GNUNET_TIME_Relative expiration,
86                 uint32_t aid,
87                 enum GNUNET_NetworkType nt)
88 {
89   LOG (GNUNET_ERROR_TYPE_DEBUG,
90        "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
91        address,
92        expiration.rel_value_us,
93        aid,
94        nt);
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],
98                                                                 &peer_id[
99                                                                   NUM_PEERS
100                                                                   - 1],
101                                                                 address);
102 }
103
104
105 /**
106  * @brief Callback that informs whether the requested queue will be
107  * established
108  *
109  * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
110  *
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)
115  */
116 static void
117 queue_create_reply_cb (void *cls,
118                        struct
119                        GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
120                        tc_h,
121                        int will_try)
122 {
123   if (GNUNET_YES == will_try)
124     LOG (GNUNET_ERROR_TYPE_DEBUG,
125          "Queue will be established!\n");
126   else
127     LOG (GNUNET_ERROR_TYPE_WARNING,
128          "Queue won't be established (bougus address?)!\n");
129 }
130
131
132 /**
133  * @brief Handle opening of queue
134  *
135  * Issues sending of test data
136  *
137  * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
138  *
139  * @param cls Closure
140  * @param tc_h Communicator handle
141  * @param tc_queue Handle to newly opened queue
142  */
143 static void
144 add_queue_cb (void *cls,
145               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
146               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *
147               tc_queue)
148 {
149   LOG (GNUNET_ERROR_TYPE_DEBUG,
150        "Got Queue!\n");
151   GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_queue,
152                                                         &payload,
153                                                         sizeof(payload));
154 }
155
156
157 /**
158  * @brief Handle an incoming message
159  *
160  * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
161
162  * @param cls Closure
163  * @param tc_h Handle to the receiving communicator
164  * @param msg Received message
165  */
166 void
167 incoming_message_cb (void *cls,
168                      struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
169                      *tc_h,
170                      const struct GNUNET_TRANSPORT_IncomingMessage *msg)
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 ();
182 }
183
184 /**
185  * @brief Main function called by the scheduler
186  *
187  * @param cls Closure - Handle to configuration
188  */
189 static void
190 run (void *cls)
191 {
192   for (int i = 0; i < NUM_PEERS; i++)
193   {
194     tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
195       "transport",
196       communicator_binary,
197       cfg_peers_name[i],
198       &communicator_available_cb,
199       &add_address_cb,
200       &queue_create_reply_cb,
201       &add_queue_cb,
202       &incoming_message_cb,
203       cfg_peers_name[i]); /* cls */
204   }
205 }
206
207
208 int
209 main (int argc,
210       char *const *argv)
211 {
212   struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
213   char *communicator_name;
214   char *cfg_peer;
215   ret = 1;
216
217   communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
218   GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s",
219                    communicator_name);
220   cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS);
221   if (GNUNET_OK != GNUNET_log_setup ("test_communicator",
222                                      "DEBUG",
223                                      "test_communicator.log"))
224   {
225     fprintf (stderr, "Unable to setup log\n");
226     GNUNET_break (0);
227     return 2;
228   }
229   for (int i = 0; i < NUM_PEERS; i++)
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 ();
236     if (GNUNET_YES ==
237         GNUNET_DISK_file_test (cfg_peers_name[i]))
238     {
239       if (GNUNET_SYSERR ==
240           GNUNET_CONFIGURATION_load (cfg_peers[i],
241                                      cfg_peers_name[i]))
242       {
243         fprintf (stderr,
244                  "Malformed configuration file `%s', exiting ...\n",
245                  cfg_peers_name[i]);
246         return 1;
247       }
248     }
249     else
250     {
251       if (GNUNET_SYSERR ==
252           GNUNET_CONFIGURATION_load (cfg_peers[i],
253                                      NULL))
254       {
255         fprintf (stderr,
256                  "Configuration file %s does not exist, exiting ...\n",
257                  cfg_peers_name[i]);
258         return 1;
259       }
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   }
276   fprintf (stderr, "Starting test...\n");
277   GNUNET_SCHEDULER_run (&run,
278                         NULL);
279   return ret;
280 }