-remove debug message
[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[] = { "test", NULL };
32   const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
33     GNUNET_GETOPT_OPTION_END
34   };
35
36   if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
37     return 1;
38
39   return 0;
40 }
41
42
43 static int
44 testVerbose ()
45 {
46   char *const myargv[] = { "test", "-V", "-V", "more", NULL };
47   unsigned int vflags = 0;
48
49   const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] =
50   { GNUNET_GETOPT_option_verbose (&vflags), GNUNET_GETOPT_OPTION_END };
51
52   if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
53   {
54     GNUNET_break (0);
55     return 1;
56   }
57   if (vflags != 2)
58   {
59     GNUNET_break (0);
60     return 1;
61   }
62   return 0;
63 }
64
65
66 static int
67 testVersion ()
68 {
69   char *const myargv[] = { "test_getopt", "-v", NULL };
70   const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] =
71   { GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
72     GNUNET_GETOPT_OPTION_END };
73
74   if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
75   {
76     GNUNET_break (0);
77     return 1;
78   }
79   return 0;
80 }
81
82
83 static int
84 testAbout ()
85 {
86   char *const myargv[] = { "test_getopt", "-h", NULL };
87   const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] =
88   { GNUNET_GETOPT_option_help ("Testing"), GNUNET_GETOPT_OPTION_END };
89
90   if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
91   {
92     GNUNET_break (0);
93     return 1;
94   }
95   return 0;
96 }
97
98
99 static int
100 testLogOpts ()
101 {
102   char *const myargv[] =
103   { "test_getopt", "-l", "filename", "-L", "WARNING", NULL };
104   char *level = GNUNET_strdup ("stuff");
105   char *fn = NULL;
106
107   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
108   { GNUNET_GETOPT_option_logfile (&fn),
109     GNUNET_GETOPT_option_loglevel (&level),
110     GNUNET_GETOPT_OPTION_END };
111
112   if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
113   {
114     GNUNET_break (0);
115     return 1;
116   }
117   GNUNET_assert (NULL != fn);
118   if ((0 != strcmp (level, "WARNING")) || (NULL == strstr (fn, "/filename")))
119   {
120     GNUNET_break (0);
121     GNUNET_free (level);
122     GNUNET_free (fn);
123     return 1;
124   }
125   GNUNET_free (level);
126   GNUNET_free (fn);
127   return 0;
128 }
129
130
131 static int
132 testFlagNum ()
133 {
134   char *const myargv[] = { "test_getopt", "-f", "-n", "42", "-N", "42", NULL };
135   int flag = 0;
136   unsigned int num = 0;
137   unsigned long long lnum = 0;
138
139   const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
140   { GNUNET_GETOPT_option_flag ('f', "--flag", "helptext", &flag),
141     GNUNET_GETOPT_option_uint ('n', "--num", "ARG", "helptext", &num),
142     GNUNET_GETOPT_option_ulong ('N', "--lnum", "ARG", "helptext", &lnum),
143     GNUNET_GETOPT_OPTION_END };
144
145   if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
146   {
147     GNUNET_break (0);
148     return 1;
149   }
150   if ((1 != flag) || (42 != num) || (42 != lnum))
151   {
152     GNUNET_break (0);
153     return 1;
154   }
155   return 0;
156 }
157
158
159 int
160 main (int argc, char *argv[])
161 {
162   int errCnt = 0;
163
164   GNUNET_log_setup ("test_getopt", "WARNING", NULL);
165   /* suppress output from -h, -v options */
166
167   GNUNET_break (0 == close (1));
168
169   if (0 != testMinimal ())
170     errCnt++;
171   if (0 != testVerbose ())
172     errCnt++;
173   if (0 != testVersion ())
174     errCnt++;
175   if (0 != testAbout ())
176     errCnt++;
177   if (0 != testLogOpts ())
178     errCnt++;
179   if (0 != testFlagNum ())
180     errCnt++;
181   return errCnt;
182 }