LRN: Fix automake deps to allow -j* builds again
[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   const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = {
60     GNUNET_GETOPT_OPTION_VERBOSE (&vflags),
61     GNUNET_GETOPT_OPTION_END
62   };
63
64   if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
65     {
66       GNUNET_break (0);
67       return 1;
68     }
69   if (vflags != 2)
70     {
71       GNUNET_break (0);
72       return 1;
73     }
74   return 0;
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),
87     GNUNET_GETOPT_OPTION_END
88   };
89
90   if (-1 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
91     {
92       GNUNET_break (0);
93       return 1;
94     }
95   return 0;
96 }
97
98 static int
99 testAbout ()
100 {
101   char *const myargv[] = {
102     "test_getopt",
103     "-h",
104     NULL
105   };
106   const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = {
107     GNUNET_GETOPT_OPTION_HELP ("Testing"),
108     GNUNET_GETOPT_OPTION_END
109   };
110
111   if (-1 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
112     {
113       GNUNET_break (0);
114       return 1;
115     }
116   return 0;
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   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
131     GNUNET_GETOPT_OPTION_LOGFILE (&fn),
132     GNUNET_GETOPT_OPTION_LOGLEVEL (&level),
133     GNUNET_GETOPT_OPTION_END
134   };
135
136   if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
137     {
138       GNUNET_break (0);
139       return 1;
140     }
141   GNUNET_assert (fn != NULL);
142   if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename")))
143     {
144       GNUNET_break (0);
145       GNUNET_free (level);
146       GNUNET_free (fn);
147       return 1;
148     }
149   GNUNET_free (level);
150   GNUNET_free (fn);
151   return 0;
152 }
153
154 static int
155 testFlagNum ()
156 {
157   char *const myargv[] = {
158     "test_getopt",
159     "-f",
160     "-n", "42",
161     "-N", "42",
162     NULL
163   };
164   int flag = 0;
165   unsigned int num = 0;
166   unsigned long long lnum = 0;
167   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
168     {'f', "--flag", NULL, "helptext", 0, &GNUNET_GETOPT_set_one,
169      (void *) &flag},
170     {'n', "--num", "ARG", "helptext", 1, &GNUNET_GETOPT_set_uint,
171      (void *) &num},
172     {'N', "--lnum", "ARG", "helptext", 1, &GNUNET_GETOPT_set_ulong,
173      (void *) &lnum},
174     GNUNET_GETOPT_OPTION_END
175   };
176
177   if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
178     {
179       GNUNET_break (0);
180       return 1;
181     }
182   if ((1 != flag) || (42 != num) || (42 != lnum))
183     {
184       GNUNET_break (0);
185       return 1;
186     }
187   return 0;
188 }
189
190 int
191 main (int argc, char *argv[])
192 {
193   int errCnt = 0;
194
195   GNUNET_log_setup ("test_getopt", "WARNING", NULL);
196   /* suppress output from -h, -v options */
197 #ifndef MINGW
198   GNUNET_break (0 == CLOSE (1));
199 #endif
200   if (0 != testMinimal ())
201     errCnt++;
202   if (0 != testVerbose ())
203     errCnt++;
204   if (0 != testVersion ())
205     errCnt++;
206   if (0 != testAbout ())
207     errCnt++;
208   if (0 != testLogOpts ())
209     errCnt++;
210   if (0 != testFlagNum ())
211     errCnt++;
212   return errCnt;
213 }