GNUnet  0.11.x
gnunet-testing.c
Go to the documentation of this file.
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 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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 
30 
31 #define LOG(kind, ...) GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__)
32 
33 
37 static int ret;
38 
43 static char *create_hostkey;
44 
48 static int create_cfg;
49 
53 static unsigned int create_no;
54 
58 static char *create_cfg_template;
59 
63 static char *run_service_name;
64 
68 static struct GNUNET_DISK_FileHandle *fh;
69 
73 static char *tmpfilename;
74 
78 static struct GNUNET_SCHEDULER_Task *tid;
79 
84 
85 
86 static int
87 create_unique_cfgs (const char *template, const unsigned int no)
88 {
89  struct GNUNET_TESTING_System *system;
90  int fail;
91  unsigned int cur;
92  char *cur_file;
93  struct GNUNET_CONFIGURATION_Handle *cfg_new;
94  struct GNUNET_CONFIGURATION_Handle *cfg_tmpl;
95 
96  if (GNUNET_NO == GNUNET_DISK_file_test (template))
97  {
99  "Configuration template `%s': file not found\n",
101  return 1;
102  }
103  cfg_tmpl = GNUNET_CONFIGURATION_create ();
104 
105  /* load template */
106  if ((create_cfg_template != NULL) &&
108  {
110  "Could not load template `%s'\n",
112  GNUNET_CONFIGURATION_destroy (cfg_tmpl);
113 
114  return 1;
115  }
116  /* load defaults */
117  if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl, NULL))
118  {
120  "Could not load template `%s'\n",
122  GNUNET_CONFIGURATION_destroy (cfg_tmpl);
123  return 1;
124  }
125 
126  fail = GNUNET_NO;
127  system =
128  GNUNET_TESTING_system_create ("testing", NULL /* controller */, NULL, NULL);
129  for (cur = 0; cur < no; cur++)
130  {
132  "Creating configuration no. %u \n",
133  cur);
134  if (create_cfg_template != NULL)
135  GNUNET_asprintf (&cur_file, "%04u-%s", cur, create_cfg_template);
136  else
137  GNUNET_asprintf (&cur_file, "%04u%s", cur, ".conf");
138 
139  cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl);
140  if (GNUNET_OK != GNUNET_TESTING_configuration_create (system, cfg_new))
141  {
143  "Could not create another configuration\n");
145  fail = GNUNET_YES;
146  break;
147  }
149  "Writing configuration no. %u to file `%s' \n",
150  cur,
151  cur_file);
152  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg_new, cur_file))
153  {
155  "Failed to write configuration no. %u \n",
156  cur);
157  fail = GNUNET_YES;
158  }
160  GNUNET_free (cur_file);
161  if (GNUNET_YES == fail)
162  break;
163  }
164  GNUNET_CONFIGURATION_destroy (cfg_tmpl);
166  if (GNUNET_YES == fail)
167  return 1;
168  return 0;
169 }
170 
171 
172 static int
173 create_hostkeys (const unsigned int no)
174 {
175  struct GNUNET_TESTING_System *system;
176  struct GNUNET_PeerIdentity id;
177  struct GNUNET_DISK_FileHandle *fd;
179 
180  system = GNUNET_TESTING_system_create ("testing", NULL, NULL, NULL);
181  pk = GNUNET_TESTING_hostkey_get (system, create_no, &id);
182  if (NULL == pk)
183  {
184  fprintf (stderr,
185  _ ("Could not extract hostkey %u (offset too large?)\n"),
186  create_no);
188  return 1;
189  }
191  fd =
196  GNUNET_assert (fd != NULL);
198  pk,
199  sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
202  "transport-testing",
203  "Wrote hostkey to file: `%s'\n",
205  GNUNET_free (pk);
207  return 0;
208 }
209 
210 
217 static void
218 cleanup (void *cls)
219 {
220  if (NULL != tmpfilename)
221  {
222  if (0 != unlink (tmpfilename))
224  "unlink",
225  tmpfilename);
226  }
227  if (NULL != tid)
228  {
230  tid = NULL;
231  }
232  if (NULL != fh)
233  {
235  fh = NULL;
236  }
237 }
238 
239 
245 static void
246 stdin_cb (void *cls)
247 {
248  int c;
249 
250  tid = NULL;
251  c = getchar ();
252  switch (c)
253  {
254  case EOF:
255  case 'q':
257  return;
258 
259  case 'r':
260  if (GNUNET_OK != GNUNET_TESTING_peer_stop (my_peer))
261  LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to stop the peer\n");
262  if (GNUNET_OK != GNUNET_TESTING_peer_start (my_peer))
263  LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start the peer\n");
264  printf ("restarted\n");
265  fflush (stdout);
266  break;
267 
268  case '\n':
269  case '\r':
270  /* ignore whitespace */
271  break;
272 
273  default:
274  fprintf (stderr,
275  _ ("Unknown command, use 'q' to quit or 'r' to restart peer\n"));
276  break;
277  }
279  fh,
280  &stdin_cb,
281  NULL);
282 }
283 
284 
293 static void
294 testing_main (void *cls,
295  const struct GNUNET_CONFIGURATION_Handle *cfg,
296  struct GNUNET_TESTING_Peer *peer)
297 {
298  my_peer = peer;
299  if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-testing")))
300  {
301  GNUNET_break (0);
303  return;
304  }
305  if (GNUNET_SYSERR ==
307  tmpfilename))
308  {
309  GNUNET_break (0);
310  return;
311  }
312  printf ("ok\n%s\n", tmpfilename);
313  fflush (stdout);
317  fh,
318  &stdin_cb,
319  NULL);
320 }
321 
322 
331 static void
332 run_no_scheduler (void *cls,
333  char *const *args,
334  const char *cfgfile,
335  const struct GNUNET_CONFIGURATION_Handle *cfg)
336 {
337  if (NULL != run_service_name)
338  {
339  ret = GNUNET_TESTING_service_run ("gnunet_service_test",
341  cfgfile,
342  &testing_main,
343  NULL);
344  return;
345  }
346 
347  if (GNUNET_YES == create_cfg)
348  {
349  if (create_no > 0)
350  {
352  "Creating %u configuration files based on template `%s'\n",
353  create_no,
356  }
357  else
358  {
359  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
360  ret = 1;
361  }
362  }
363  if (NULL != create_hostkey)
364  {
365  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Extracting hostkey %u\n", create_no);
367  }
369 }
370 
371 
379 int
380 main (int argc, char *const *argv)
381 {
382  struct GNUNET_GETOPT_CommandLineOption options[] =
384  "cfg",
385  gettext_noop (
386  "create unique configuration files"),
387  &create_cfg),
389  'k',
390  "key",
391  "FILENAME",
392  gettext_noop ("extract hostkey file from pre-computed hostkey list"),
393  &create_hostkey),
394 
396  'n',
397  "number",
398  "NUMBER",
399  gettext_noop (
400  "number of unique configuration files to create, or number of the hostkey to extract"),
401  &create_no),
402 
403 
405  "template",
406  "FILENAME",
407  gettext_noop ("configuration template"),
409 
411  'r',
412  "run",
413  "SERVICE",
414  gettext_noop (
415  "run the given service, wait on stdin for 'r' (restart) or 'q' (quit)"),
418 
419  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
420  return 2;
421 
422  /* Run without scheduler, because we may want to call
423  * GNUNET_TESTING_service_run, which starts the scheduler on its own.
424  * Furthermore, the other functionality currently does not require the scheduler, too,
425  * but beware when extending gnunet-testing. */
426  ret =
427  (GNUNET_OK ==
428  GNUNET_PROGRAM_run2 (argc,
429  argv,
430  "gnunet-testing",
431  gettext_noop (
432  "Command line tool to access the testing library"),
433  options,
435  NULL,
436  GNUNET_YES))
437  ? ret
438  : 1;
439  GNUNET_free ((void *) argv);
440  return ret;
441 }
442 
443 
444 /* end of gnunet-testing.c */
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:544
int main(int argc, char *const *argv)
The main function.
Create file if it doesn&#39;t exist.
static struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1345
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
int GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
static void stdin_cb(void *cls)
Called whenever we can read stdin non-blocking.
int GNUNET_PROGRAM_run2(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls, int run_without_scheduler)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:141
static void testing_main(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Peer *peer)
Main function called by the testing library.
int GNUNET_TESTING_peer_start(struct GNUNET_TESTING_Peer *peer)
Start the peer.
Definition: testing.c:1359
Handle for a GNUnet peer controlled by testing.
Definition: testing.c:170
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1438
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_TESTING_hostkey_get(const struct GNUNET_TESTING_System *system, uint32_t key_number, struct GNUNET_PeerIdentity *id)
Testing includes a number of pre-created hostkeys for faster peer startup.
Definition: testing.c:710
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:684
#define GNUNET_NO
Definition: gnunet_common.h:78
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1636
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
static char * create_hostkey
Filename of the hostkey file we should write, null if we should not write a hostkey file...
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
int GNUNET_TESTING_peer_stop(struct GNUNET_TESTING_Peer *peer)
Stop the peer.
Definition: testing.c:1464
int GNUNET_TESTING_configuration_create(struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new configuration using the given configuration as a template; ports and paths will be modif...
Definition: testing.c:1145
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition: disk.c:468
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:820
int GNUNET_TESTING_service_run(const char *testdir, const char *service_name, const char *cfgfilename, GNUNET_TESTING_TestMain tm, void *tm_cls)
Start a single service (no ARM, except of course if the given service name is &#39;arm&#39;) and run a test u...
Definition: testing.c:1670
static struct GNUNET_SCHEDULER_Task * tid
Task identifier of the task that waits for stdin.
static char * create_cfg_template
Filename of the config template to be written.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define LOG(kind,...)
static unsigned int create_no
Number of config files to create.
struct GNUNET_TESTING_System * GNUNET_TESTING_system_create(const char *testdir, const char *trusted_ip, const char *hostname, const struct GNUNET_TESTING_SharedService *shared_services)
Create a system handle.
Definition: testing.c:453
void GNUNET_TESTING_system_destroy(struct GNUNET_TESTING_System *system, int remove_paths)
Free system resources.
Definition: testing.c:534
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static char * run_service_name
Service we are supposed to run.
static void run_no_scheduler(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be running without scheduler.
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:97
static struct GNUNET_TESTING_Peer * my_peer
Peer started for &#39;-r&#39;.
static int create_unique_cfgs(const char *template, const unsigned int no)
static int create_hostkeys(const unsigned int no)
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
Private ECC key encoded for transmission.
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:85
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
Open the file for both reading and writing.
static int ret
Final status code.
static char * tmpfilename
Temporary filename, used with &#39;-r&#39; to write the configuration to.
#define GNUNET_YES
Definition: gnunet_common.h:77
int fd
File handle on Unix-like systems.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_native(FILE *fd)
Get a handle from a native FD.
Definition: disk.c:1397
static void cleanup(void *cls)
Cleanup called by signal handlers and when stdin is closed.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
#define GNUNET_log_from(kind, comp,...)
static int create_cfg
Non-zero if we should create config files.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1268
Handle used to access files (and pipes).
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:966
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.