TNG testing: Add ability to open queue
[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 "transport.h"
32
33 #include <inttypes.h>
34
35 /**
36  * TODO
37  * - start two communicators
38  * - act like transport services
39  *   - get_server_addresses (service.c)
40  *   - open_listen_socket (service.c)
41  *   - GNUNET_MQ_queue_for_callbacks (service.c)
42  * - let them communicate
43  *
44  */
45
46
47
48 #define LOG(kind,...) GNUNET_log_from (kind, "test_transport_communicator_unix", __VA_ARGS__)
49
50 #define NUM_PEERS 2
51
52 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
53
54 static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
55
56 //static char *addresses[NUM_PEERS];
57
58 static void
59 communicator_available_cb (void *cls,
60                            struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
61                            enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
62                            char *address_prefix)
63 {
64   LOG (GNUNET_ERROR_TYPE_DEBUG,
65       "Communicator available. (cc: %u, prefix: %s)\n",
66       cc,
67       address_prefix);
68 }
69
70
71 static void
72 add_address_cb (void *cls,
73                 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
74                 const char *address,
75                 struct GNUNET_TIME_Relative expiration,
76                 uint32_t aid,
77                 enum GNUNET_NetworkType nt)
78 {
79   LOG (GNUNET_ERROR_TYPE_DEBUG,
80       "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
81       address,
82       expiration.rel_value_us,
83       aid,
84       nt);
85   //addresses[1] = GNUNET_strdup (address);
86   GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
87                                                               &peer_id[1],
88                                                               address);
89 }
90
91
92 static void
93 queue_create_reply_cb (void *cls,
94                        struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
95                        int success)
96 {
97   if (GNUNET_YES == success)
98     LOG (GNUNET_ERROR_TYPE_DEBUG,
99         "Got Queue!\n");
100   else
101     LOG (GNUNET_ERROR_TYPE_DEBUG,
102         "Failed getting queue!\n");
103 }
104
105
106 static void
107 add_queue_cb (void *cls,
108               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
109 {
110   LOG (GNUNET_ERROR_TYPE_DEBUG,
111       "Got Queue!\n");
112 }
113
114
115 static void
116 run (void *cls)
117 {
118   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
119
120   tc_hs[0] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
121       "transport",
122       "test_communicator_1.conf",
123       &communicator_available_cb,
124       NULL,
125       &queue_create_reply_cb,
126       &add_queue_cb,
127       NULL); /* cls */
128   tc_hs[1] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
129       "transport",
130       "test_communicator_2.conf",
131       &communicator_available_cb,
132       &add_address_cb,
133       NULL,
134       &add_queue_cb,
135       NULL); /* cls */
136 }
137
138 int
139 main (int argc,
140       char *const *argv)
141 {
142   char *cfg_filename;
143   char *opt_cfg_filename;
144   const char *xdg;
145   char *loglev;
146   char *logfile;
147   struct GNUNET_CONFIGURATION_Handle *cfg;
148
149   struct GNUNET_GETOPT_CommandLineOption service_options[] = {
150     GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
151     GNUNET_GETOPT_option_help (NULL),
152     GNUNET_GETOPT_option_loglevel (&loglev),
153     GNUNET_GETOPT_option_logfile (&logfile),
154     GNUNET_GETOPT_OPTION_END
155   };
156
157   if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix",
158                                      loglev,
159                                      logfile))
160   {
161     GNUNET_break (0);
162     return GNUNET_SYSERR;
163   }
164
165   xdg = getenv ("XDG_CONFIG_HOME");
166   if (NULL != xdg)
167     GNUNET_asprintf (&cfg_filename,
168                      "%s%s%s",
169                      xdg,
170                      DIR_SEPARATOR_STR,
171                      GNUNET_OS_project_data_get ()->config_file);
172   else
173     cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
174   cfg = GNUNET_CONFIGURATION_create ();
175   if (NULL != opt_cfg_filename)
176   {
177     if ( (GNUNET_YES !=
178           GNUNET_DISK_file_test (opt_cfg_filename)) ||
179        (GNUNET_SYSERR ==
180           GNUNET_CONFIGURATION_load (cfg,
181                                      opt_cfg_filename)) )
182     {
183       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184                   _("Malformed configuration file `%s', exit ...\n"),
185                     opt_cfg_filename);
186       return GNUNET_SYSERR;
187     }
188   }
189   else
190   {
191     if (GNUNET_YES ==
192         GNUNET_DISK_file_test (cfg_filename))
193     {
194       if (GNUNET_SYSERR ==
195           GNUNET_CONFIGURATION_load (cfg,
196                                      cfg_filename))
197       {
198         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199                     _("Malformed configuration file `%s', exit ...\n"),
200                     cfg_filename);
201         return GNUNET_SYSERR;
202       }
203     }
204     else
205     {
206       if (GNUNET_SYSERR ==
207           GNUNET_CONFIGURATION_load (cfg,
208                                      NULL))
209       {
210         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211                     _("Malformed configuration, exit ...\n"));
212         return GNUNET_SYSERR;
213       }
214     }
215   }
216   GNUNET_SCHEDULER_run (&run,
217                         cfg);
218 }
219