-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / test_getopt.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006, 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  * @file util/test_getopt.c
22  * @brief testcase for util/getopt.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_configuration_lib.h"
27 #include "gnunet_getopt_lib.h"
28
29
30 static int
31 testMinimal ()
32 {
33   char *const emptyargv[] = {
34     "test",
35     NULL
36   };
37   const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
38     GNUNET_GETOPT_OPTION_END
39   };
40
41   if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
42     return 1;
43
44   return 0;
45 }
46
47
48 static int
49 testVerbose ()
50 {
51   char *const myargv[] = {
52     "test",
53     "-V",
54     "-V",
55     "more",
56     NULL
57   };
58   unsigned int vflags = 0;
59
60   const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = {
61     GNUNET_GETOPT_OPTION_VERBOSE (&vflags),
62     GNUNET_GETOPT_OPTION_END
63   };
64
65   if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
66   {
67     GNUNET_break (0);
68     return 1;
69   }
70   if (vflags != 2)
71   {
72     GNUNET_break (0);
73     return 1;
74   }
75   return 0;
76 }
77
78
79 static int
80 testVersion ()
81 {
82   char *const myargv[] = {
83     "test_getopt",
84     "-v",
85     NULL
86   };
87   const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = {
88     GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
89     GNUNET_GETOPT_OPTION_END
90   };
91
92   if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
93   {
94     GNUNET_break (0);
95     return 1;
96   }
97   return 0;
98 }
99
100
101 static int
102 testAbout ()
103 {
104   char *const myargv[] = {
105     "test_getopt",
106     "-h",
107     NULL
108   };
109   const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = {
110     GNUNET_GETOPT_OPTION_HELP ("Testing"),
111     GNUNET_GETOPT_OPTION_END
112   };
113
114   if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
115   {
116     GNUNET_break (0);
117     return 1;
118   }
119   return 0;
120 }
121
122
123 static int
124 testLogOpts ()
125 {
126   char *const myargv[] = {
127     "test_getopt",
128     "-l", "filename",
129     "-L", "WARNING",
130     NULL
131   };
132   char *level = GNUNET_strdup ("stuff");
133   char *fn = NULL;
134
135   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
136     GNUNET_GETOPT_OPTION_LOGFILE (&fn),
137     GNUNET_GETOPT_OPTION_LOGLEVEL (&level),
138     GNUNET_GETOPT_OPTION_END
139   };
140
141   if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
142   {
143     GNUNET_break (0);
144     return 1;
145   }
146   GNUNET_assert (fn != NULL);
147   if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename")))
148   {
149     GNUNET_break (0);
150     GNUNET_free (level);
151     GNUNET_free (fn);
152     return 1;
153   }
154   GNUNET_free (level);
155   GNUNET_free (fn);
156   return 0;
157 }
158
159
160 static int
161 testFlagNum ()
162 {
163   char *const myargv[] = {
164     "test_getopt",
165     "-f",
166     "-n", "42",
167     "-N", "42",
168     NULL
169   };
170   int flag = 0;
171   unsigned int num = 0;
172   unsigned long long lnum = 0;
173
174   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
175     {'f', "--flag", NULL, "helptext", 0, &GNUNET_GETOPT_set_one,
176      (void *) &flag},
177     {'n', "--num", "ARG", "helptext", 1, &GNUNET_GETOPT_set_uint,
178      (void *) &num},
179     {'N', "--lnum", "ARG", "helptext", 1, &GNUNET_GETOPT_set_ulong,
180      (void *) &lnum},
181     GNUNET_GETOPT_OPTION_END
182   };
183
184   if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
185   {
186     GNUNET_break (0);
187     return 1;
188   }
189   if ((1 != flag) || (42 != num) || (42 != lnum))
190   {
191     GNUNET_break (0);
192     return 1;
193   }
194   return 0;
195 }
196
197
198 int
199 main (int argc, char *argv[])
200 {
201   int errCnt = 0;
202
203   GNUNET_log_setup ("test_getopt", "WARNING", NULL);
204   /* suppress output from -h, -v options */
205 #ifndef MINGW
206   GNUNET_break (0 == CLOSE (1));
207 #endif
208   if (0 != testMinimal ())
209     errCnt++;
210   if (0 != testVerbose ())
211     errCnt++;
212   if (0 != testVersion ())
213     errCnt++;
214   if (0 != testAbout ())
215     errCnt++;
216   if (0 != testLogOpts ())
217     errCnt++;
218   if (0 != testFlagNum ())
219     errCnt++;
220   return errCnt;
221 }