-try with finished
[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 #define VERBOSE 0
30
31 static int
32 testMinimal ()
33 {
34   char *const emptyargv[] = {
35     "test",
36     NULL
37   };
38   const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
39     GNUNET_GETOPT_OPTION_END
40   };
41
42   if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
43     return 1;
44
45   return 0;
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 static int
79 testVersion ()
80 {
81   char *const myargv[] = {
82     "test_getopt",
83     "-v",
84     NULL
85   };
86   const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = {
87     GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
88     GNUNET_GETOPT_OPTION_END
89   };
90
91   if (-1 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
92   {
93     GNUNET_break (0);
94     return 1;
95   }
96   return 0;
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 (-1 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
113   {
114     GNUNET_break (0);
115     return 1;
116   }
117   return 0;
118 }
119
120 static int
121 testLogOpts ()
122 {
123   char *const myargv[] = {
124     "test_getopt",
125     "-l", "filename",
126     "-L", "WARNING",
127     NULL
128   };
129   char *level = GNUNET_strdup ("stuff");
130   char *fn = NULL;
131
132   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
133     GNUNET_GETOPT_OPTION_LOGFILE (&fn),
134     GNUNET_GETOPT_OPTION_LOGLEVEL (&level),
135     GNUNET_GETOPT_OPTION_END
136   };
137
138   if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
139   {
140     GNUNET_break (0);
141     return 1;
142   }
143   GNUNET_assert (fn != NULL);
144   if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename")))
145   {
146     GNUNET_break (0);
147     GNUNET_free (level);
148     GNUNET_free (fn);
149     return 1;
150   }
151   GNUNET_free (level);
152   GNUNET_free (fn);
153   return 0;
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     {'f', "--flag", NULL, "helptext", 0, &GNUNET_GETOPT_set_one,
172      (void *) &flag},
173     {'n', "--num", "ARG", "helptext", 1, &GNUNET_GETOPT_set_uint,
174      (void *) &num},
175     {'N', "--lnum", "ARG", "helptext", 1, &GNUNET_GETOPT_set_ulong,
176      (void *) &lnum},
177     GNUNET_GETOPT_OPTION_END
178   };
179
180   if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
181   {
182     GNUNET_break (0);
183     return 1;
184   }
185   if ((1 != flag) || (42 != num) || (42 != lnum))
186   {
187     GNUNET_break (0);
188     return 1;
189   }
190   return 0;
191 }
192
193 int
194 main (int argc, char *argv[])
195 {
196   int errCnt = 0;
197
198   GNUNET_log_setup ("test_getopt", "WARNING", NULL);
199   /* suppress output from -h, -v options */
200 #ifndef MINGW
201   GNUNET_break (0 == CLOSE (1));
202 #endif
203   if (0 != testMinimal ())
204     errCnt++;
205   if (0 != testVerbose ())
206     errCnt++;
207   if (0 != testVersion ())
208     errCnt++;
209   if (0 != testAbout ())
210     errCnt++;
211   if (0 != testLogOpts ())
212     errCnt++;
213   if (0 != testFlagNum ())
214     errCnt++;
215   return errCnt;
216 }