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