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