tighten formatting rules
[oweals/gnunet.git] / src / testbed / test_testbed_underlay.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 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 testbed/test_testbed_underlay.c
23  * @brief testcase binary for testing testbed underlay restrictions
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30
31
32 /**
33  * Number of peers we start in this test case
34  */
35 #define NUM_PEERS 3
36
37 /**
38  * Result of this test case
39  */
40 static int result;
41
42 static struct GNUNET_TESTBED_Operation *op;
43
44
45 /**
46  * Shutdown testcase
47  *
48  * @param cls NULL
49  * @param tc scheduler task context
50  */
51 static void
52 do_shutdown (void *cls)
53 {
54   if (NULL != op)
55     GNUNET_TESTBED_operation_done (op);
56   op = NULL;
57 }
58
59
60 /**
61  * Callback to be called when an operation is completed
62  *
63  * @param cls the callback closure from functions generating an operation
64  * @param op the operation that has been finished
65  * @param emsg error message in case the operation has failed; will be NULL if
66  *          operation has executed successfully.
67  */
68 static void
69 overlay_connect_status (void *cls,
70                         struct GNUNET_TESTBED_Operation *op_,
71                         const char *emsg)
72 {
73   GNUNET_assert (op_ == op);
74   GNUNET_TESTBED_operation_done (op);
75   op = NULL;
76   if (NULL == emsg)
77     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
78                 "Peers 0 and 2 should not get connected\n");
79   else
80   {
81     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82                 "Peers 0 and 2 not connected: %s.  Success!\n", emsg);
83     result = GNUNET_OK;
84   }
85   GNUNET_SCHEDULER_shutdown ();
86 }
87
88
89 /**
90  * Signature of a main function for a testcase.
91  *
92  * @param cls closure
93  * @param h the run handle
94  * @param num_peers number of peers in 'peers'
95  * @param peers_ handle to peers run in the testbed
96  * @param links_succeeded the number of overlay link connection attempts that
97  *          succeeded
98  * @param links_failed the number of overlay link connection attempts that
99  *          failed
100  */
101 static void
102 test_master (void *cls,
103              struct GNUNET_TESTBED_RunHandle *h,
104              unsigned int num_peers,
105              struct GNUNET_TESTBED_Peer **peers_,
106              unsigned int links_succeeded,
107              unsigned int links_failed)
108 {
109   GNUNET_assert (NULL == cls);
110   if (NULL == peers_)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
113     GNUNET_SCHEDULER_shutdown ();
114     return;
115   }
116   GNUNET_assert (NUM_PEERS == num_peers);
117   op = GNUNET_TESTBED_overlay_connect (NULL,
118                                        &overlay_connect_status,
119                                        NULL,
120                                        peers_[0],
121                                        peers_[2]);
122   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
123                                   GNUNET_TIME_UNIT_SECONDS,
124                                   60),
125                                 &do_shutdown, NULL);
126 }
127
128
129 #ifndef PATH_MAX
130 /**
131  * Assumed maximum path length (for the log file name).
132  */
133 #define PATH_MAX 4096
134 #endif
135
136
137 /**
138  * Main function
139  */
140 int
141 main (int argc, char **argv)
142 {
143   struct GNUNET_CONFIGURATION_Handle *cfg;
144   char pwd[PATH_MAX];
145   char *dbfile;
146   uint64_t event_mask;
147
148   result = GNUNET_SYSERR;
149   event_mask = 0;
150   cfg = GNUNET_CONFIGURATION_create ();
151   GNUNET_assert (GNUNET_YES ==
152                  GNUNET_CONFIGURATION_parse (cfg,
153                                              "test_testbed_underlay.conf.in"));
154   if (NULL == getcwd (pwd, PATH_MAX))
155     return 1;
156   GNUNET_assert (0 < GNUNET_asprintf (&dbfile, "%s/%s", pwd,
157                                       "test-underlay.sqlite"));
158   GNUNET_CONFIGURATION_set_value_string (cfg, "TESTBED-UNDERLAY", "DBFILE",
159                                          dbfile);
160   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
161                    (cfg, "test_testbed_underlay.conf"));
162   GNUNET_CONFIGURATION_destroy (cfg);
163   cfg = NULL;
164   GNUNET_free (dbfile);
165   dbfile = NULL;
166   (void) GNUNET_TESTBED_test_run ("test_testbed_underlay",
167                                   "test_testbed_underlay.conf", NUM_PEERS,
168                                   event_mask, NULL, NULL,
169                                   &test_master, NULL);
170   (void) unlink ("test_testbed_underlay.conf");
171   if (GNUNET_OK != result)
172     return 1;
173   return 0;
174 }