TNG testing: Cleanup and Structuring
[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 /**
34  * TODO
35  * - start two communicators
36  * - act like transport services
37  *   - get_server_addresses (service.c)
38  *   - open_listen_socket (service.c)
39  *   - GNUNET_MQ_queue_for_callbacks (service.c)
40  * - let them communicate
41  *
42  */
43
44
45
46 #define LOG(kind,...) GNUNET_log_from (kind, "test_transport_communicator_unix", __VA_ARGS__)
47
48 static void
49 communicator_available (void *cls,
50     const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
51 {
52   LOG (GNUNET_ERROR_TYPE_DEBUG,
53       "communicator_available()\n");
54 }
55
56 static void
57 run (void *cls)
58 {
59   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
60
61   GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
62       "transport",
63       "test_communicator_1.conf",
64       &communicator_available,
65       NULL); /* cls */
66   GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
67       "transport",
68       "test_communicator_2.conf",
69       &communicator_available,
70       NULL); /* cls */
71 }
72
73 int
74 main (int argc,
75       char *const *argv)
76 {
77   char *cfg_filename;
78   char *opt_cfg_filename;
79   const char *xdg;
80   char *loglev;
81   char *logfile;
82   struct GNUNET_CONFIGURATION_Handle *cfg;
83
84   struct GNUNET_GETOPT_CommandLineOption service_options[] = {
85     GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
86     GNUNET_GETOPT_option_help (NULL),
87     GNUNET_GETOPT_option_loglevel (&loglev),
88     GNUNET_GETOPT_option_logfile (&logfile),
89     GNUNET_GETOPT_OPTION_END
90   };
91
92   if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix",
93                                      loglev,
94                                      logfile))
95   {
96     GNUNET_break (0);
97     return GNUNET_SYSERR;
98   }
99
100   xdg = getenv ("XDG_CONFIG_HOME");
101   if (NULL != xdg)
102     GNUNET_asprintf (&cfg_filename,
103                      "%s%s%s",
104                      xdg,
105                      DIR_SEPARATOR_STR,
106                      GNUNET_OS_project_data_get ()->config_file);
107   else
108     cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
109   cfg = GNUNET_CONFIGURATION_create ();
110   if (NULL != opt_cfg_filename)
111   {
112     if ( (GNUNET_YES !=
113           GNUNET_DISK_file_test (opt_cfg_filename)) ||
114        (GNUNET_SYSERR ==
115           GNUNET_CONFIGURATION_load (cfg,
116                                      opt_cfg_filename)) )
117     {
118       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119                   _("Malformed configuration file `%s', exit ...\n"),
120                     opt_cfg_filename);
121       return GNUNET_SYSERR;
122     }
123   }
124   else
125   {
126     if (GNUNET_YES ==
127         GNUNET_DISK_file_test (cfg_filename))
128     {
129       if (GNUNET_SYSERR ==
130           GNUNET_CONFIGURATION_load (cfg,
131                                      cfg_filename))
132       {
133         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
134                     _("Malformed configuration file `%s', exit ...\n"),
135                     cfg_filename);
136         return GNUNET_SYSERR;
137       }
138     }
139     else
140     {
141       if (GNUNET_SYSERR ==
142           GNUNET_CONFIGURATION_load (cfg,
143                                      NULL))
144       {
145         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146                     _("Malformed configuration, exit ...\n"));
147         return GNUNET_SYSERR;
148       }
149     }
150   }
151   GNUNET_SCHEDULER_run (&run,
152                         cfg);
153 }
154