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