(no commit message)
[oweals/gnunet.git] / src / testing / gnunet-testing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file template/gnunet-testing.c
23  * @brief tool to use testing functionality from cmd line
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_testing_lib.h"
30
31 /**
32  * Final status code.
33  */
34 static int ret;
35
36 unsigned int create_cfg;
37
38  int create_cfg_no;
39
40 static char * create_cfg_template;
41
42
43 static int
44 create_unique_cfgs (const char * template, const unsigned int no)
45 {
46   if (GNUNET_NO == GNUNET_DISK_file_test(template))
47   {
48     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
49     return 1;
50   }
51   return 0;
52 }
53
54 /**
55  * Main function that will be run by the scheduler.
56  *
57  * @param cls closure
58  * @param args remaining command-line arguments
59  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
60  * @param cfg configuration
61  */
62 static void
63 run (void *cls, char *const *args, const char *cfgfile,
64      const struct GNUNET_CONFIGURATION_Handle *cfg)
65 {
66   /* main code here */
67   if ((create_cfg == GNUNET_YES) &&
68       (create_cfg_no > 0) &&
69       (create_cfg_template != NULL))
70   {
71     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u configuration files based on template `%s'\n", create_cfg_no, create_cfg_template);
72     ret = create_unique_cfgs (create_cfg_template, create_cfg_no);
73   }
74   else
75   {
76     ret = 1;
77   }
78   GNUNET_free_non_null (create_cfg_template);
79 }
80
81
82 /**
83  * The main function.
84  *
85  * @param argc number of arguments from the command line
86  * @param argv command line arguments
87  * @return 0 ok, 1 on error
88  */
89 int
90 main (int argc, char *const *argv)
91 {
92   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
93     {'C', "create", NULL, gettext_noop ("create unique configuration files"),
94      GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
95     {'n', "number", NULL, gettext_noop ("number of unique configuration files to create"),
96      GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_cfg_no},
97     {'t', "template", NULL, gettext_noop ("configuration template"),
98      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
99     GNUNET_GETOPT_OPTION_END
100   };
101   return (GNUNET_OK ==
102           GNUNET_PROGRAM_run (argc, argv, "gnunet-testing",
103                               gettext_noop ("Command line tool to access the testing library"), options, &run,
104                               NULL)) ? ret : 1;
105 }
106
107 /* end of gnunet-testing.c */