diff options
Diffstat (limited to 'src/service/testbed/gnunet-testbed.c')
-rw-r--r-- | src/service/testbed/gnunet-testbed.c | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/src/service/testbed/gnunet-testbed.c b/src/service/testbed/gnunet-testbed.c new file mode 100644 index 000000000..59864085d --- /dev/null +++ b/src/service/testbed/gnunet-testbed.c | |||
@@ -0,0 +1,230 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 FORp 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/gnunet-testbed.c | ||
23 | * @brief tool to use testing functionality from cmd line | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testbed_lib.h" | ||
29 | |||
30 | |||
31 | #define LOG(kind, ...) GNUNET_log_from (kind, "gnunet-testbed", __VA_ARGS__) | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Final status code. | ||
36 | */ | ||
37 | static int ret; | ||
38 | |||
39 | /** | ||
40 | * Number of config files to create. | ||
41 | */ | ||
42 | static unsigned int create_no; | ||
43 | |||
44 | /** | ||
45 | * Filename of the config template to be written. | ||
46 | */ | ||
47 | static char *create_cfg_template; | ||
48 | |||
49 | |||
50 | static int | ||
51 | create_unique_cfgs (const char *template, | ||
52 | const unsigned int no) | ||
53 | { | ||
54 | struct GNUNET_TESTBED_System *system; | ||
55 | int fail; | ||
56 | char *cur_file; | ||
57 | struct GNUNET_CONFIGURATION_Handle *cfg_new; | ||
58 | struct GNUNET_CONFIGURATION_Handle *cfg_tmpl; | ||
59 | |||
60 | if (GNUNET_NO == GNUNET_DISK_file_test (template)) | ||
61 | { | ||
62 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
63 | "Configuration template `%s': file not found\n", | ||
64 | create_cfg_template); | ||
65 | return 1; | ||
66 | } | ||
67 | cfg_tmpl = GNUNET_CONFIGURATION_create (); | ||
68 | |||
69 | /* load template */ | ||
70 | if ((create_cfg_template != NULL) && | ||
71 | (GNUNET_OK != | ||
72 | GNUNET_CONFIGURATION_load (cfg_tmpl, | ||
73 | create_cfg_template))) | ||
74 | { | ||
75 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
76 | "Could not load template `%s'\n", | ||
77 | create_cfg_template); | ||
78 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); | ||
79 | |||
80 | return 1; | ||
81 | } | ||
82 | /* load defaults */ | ||
83 | if (GNUNET_OK != | ||
84 | GNUNET_CONFIGURATION_load (cfg_tmpl, | ||
85 | NULL)) | ||
86 | { | ||
87 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
88 | "Could not load template `%s'\n", | ||
89 | create_cfg_template); | ||
90 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); | ||
91 | return 1; | ||
92 | } | ||
93 | |||
94 | fail = GNUNET_NO; | ||
95 | system = | ||
96 | GNUNET_TESTBED_system_create ("testing", | ||
97 | NULL /* controller */, | ||
98 | NULL); | ||
99 | for (unsigned int cur = 0; cur < no; cur++) | ||
100 | { | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
102 | "Creating configuration no. %u \n", | ||
103 | cur); | ||
104 | if (create_cfg_template != NULL) | ||
105 | GNUNET_asprintf (&cur_file, "%04u-%s", cur, create_cfg_template); | ||
106 | else | ||
107 | GNUNET_asprintf (&cur_file, "%04u%s", cur, ".conf"); | ||
108 | |||
109 | cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl); | ||
110 | if (GNUNET_OK != | ||
111 | GNUNET_TESTBED_configuration_create (system, | ||
112 | cfg_new, | ||
113 | NULL, | ||
114 | NULL)) | ||
115 | { | ||
116 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
117 | "Could not create another configuration\n"); | ||
118 | GNUNET_CONFIGURATION_destroy (cfg_new); | ||
119 | fail = GNUNET_YES; | ||
120 | break; | ||
121 | } | ||
122 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
123 | "Writing configuration no. %u to file `%s' \n", | ||
124 | cur, | ||
125 | cur_file); | ||
126 | if (GNUNET_OK != | ||
127 | GNUNET_CONFIGURATION_write (cfg_new, | ||
128 | cur_file)) | ||
129 | { | ||
130 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
131 | "Failed to write configuration no. %u \n", | ||
132 | cur); | ||
133 | fail = GNUNET_YES; | ||
134 | } | ||
135 | GNUNET_CONFIGURATION_destroy (cfg_new); | ||
136 | GNUNET_free (cur_file); | ||
137 | if (GNUNET_YES == fail) | ||
138 | break; | ||
139 | } | ||
140 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); | ||
141 | GNUNET_TESTBED_system_destroy (system, | ||
142 | false); | ||
143 | if (GNUNET_YES == fail) | ||
144 | return 1; | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | |||
149 | /** | ||
150 | * Main function that will be running without scheduler. | ||
151 | * | ||
152 | * @param cls closure | ||
153 | * @param args remaining command-line arguments | ||
154 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
155 | * @param cfg configuration | ||
156 | */ | ||
157 | static void | ||
158 | run_no_scheduler (void *cls, | ||
159 | char *const *args, | ||
160 | const char *cfgfile, | ||
161 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
162 | { | ||
163 | if (create_no > 0) | ||
164 | { | ||
165 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
166 | "Creating %u configuration files based on template `%s'\n", | ||
167 | create_no, | ||
168 | create_cfg_template); | ||
169 | ret = create_unique_cfgs (create_cfg_template, | ||
170 | create_no); | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
175 | "Missing arguments!\n"); | ||
176 | ret = 1; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | |||
181 | /** | ||
182 | * The main function. | ||
183 | * | ||
184 | * @param argc number of arguments from the command line | ||
185 | * @param argv command line arguments | ||
186 | * @return 0 ok, 1 on error | ||
187 | */ | ||
188 | int | ||
189 | main (int argc, char *const *argv) | ||
190 | { | ||
191 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
192 | GNUNET_GETOPT_option_uint ( | ||
193 | 'n', | ||
194 | "number", | ||
195 | "NUMBER", | ||
196 | gettext_noop ("number of unique configuration files to create"), | ||
197 | &create_no), | ||
198 | GNUNET_GETOPT_option_string ( | ||
199 | 't', | ||
200 | "template", | ||
201 | "FILENAME", | ||
202 | gettext_noop ("configuration template"), | ||
203 | &create_cfg_template), | ||
204 | GNUNET_GETOPT_OPTION_END | ||
205 | }; | ||
206 | |||
207 | if (GNUNET_OK != | ||
208 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
209 | &argc, &argv)) | ||
210 | return 2; | ||
211 | |||
212 | ret = | ||
213 | (GNUNET_OK == | ||
214 | GNUNET_PROGRAM_run2 (argc, | ||
215 | argv, | ||
216 | "gnunet-testing", | ||
217 | gettext_noop ( | ||
218 | "Command line tool to access the testing library"), | ||
219 | options, | ||
220 | &run_no_scheduler, | ||
221 | NULL, | ||
222 | GNUNET_YES)) | ||
223 | ? ret | ||
224 | : 1; | ||
225 | GNUNET_free_nz ((void *) argv); | ||
226 | return ret; | ||
227 | } | ||
228 | |||
229 | |||
230 | /* end of gnunet-testbed.c */ | ||