fix for starting problems of SUID binaries
[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      SPDX-License-Identifier: AGPL3.0-or-later
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",
140                               logoptionlist,
141                               5, myargv))
142   {
143     GNUNET_break (0);
144     return 1;
145   }
146   GNUNET_assert (NULL != fn);
147   if ( (0 != strcmp (level, "WARNING")) ||
148        (NULL == strstr (fn, "/filename")) )
149   {
150     GNUNET_break (0);
151     GNUNET_free (level);
152     GNUNET_free (fn);
153     return 1;
154   }
155   GNUNET_free (level);
156   GNUNET_free (fn);
157   return 0;
158 }
159
160
161 static int
162 testFlagNum ()
163 {
164   char *const myargv[] = {
165     "test_getopt",
166     "-f",
167     "-n", "42",
168     "-N", "42",
169     NULL
170   };
171   int flag = 0;
172   unsigned int num = 0;
173   unsigned long long lnum = 0;
174
175   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
176     GNUNET_GETOPT_option_flag ('f',
177                                   "--flag",
178                                   "helptext",
179                                   &flag),
180     GNUNET_GETOPT_option_uint ('n',
181                                    "--num",
182                                    "ARG",
183                                    "helptext",
184                                    &num),
185     GNUNET_GETOPT_option_ulong ('N',
186                                     "--lnum",
187                                     "ARG",
188                                     "helptext",
189                                     &lnum),
190     GNUNET_GETOPT_OPTION_END
191   };
192
193   if (6 !=
194       GNUNET_GETOPT_run ("test_getopt",
195                          logoptionlist,
196                          6,
197                          myargv))
198   {
199     GNUNET_break (0);
200     return 1;
201   }
202   if ( (1 != flag) ||
203        (42 != num) ||
204        (42 != lnum))
205   {
206     GNUNET_break (0);
207     return 1;
208   }
209   return 0;
210 }
211
212
213 int
214 main (int argc, char *argv[])
215 {
216   int errCnt = 0;
217
218   GNUNET_log_setup ("test_getopt",
219                     "WARNING",
220                     NULL);
221   /* suppress output from -h, -v options */
222 #ifndef MINGW
223   GNUNET_break (0 == CLOSE (1));
224 #endif
225   if (0 != testMinimal ())
226     errCnt++;
227   if (0 != testVerbose ())
228     errCnt++;
229   if (0 != testVersion ())
230     errCnt++;
231   if (0 != testAbout ())
232     errCnt++;
233   if (0 != testLogOpts ())
234     errCnt++;
235   if (0 != testFlagNum ())
236     errCnt++;
237   return errCnt;
238 }