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