1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "test/core/util/cmdline.h"
20
21 #include <string.h>
22
23 #include "gtest/gtest.h"
24
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/gpr/useful.h"
28 #include "test/core/util/test_config.h"
29
30 #define LOG_TEST() gpr_log(GPR_INFO, "test at %s:%d", __FILE__, __LINE__)
31
TEST(CmdlineTest,SimpleInt)32 TEST(CmdlineTest, SimpleInt) {
33 int x = 1;
34 gpr_cmdline* cl;
35 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo"),
36 const_cast<char*>("3")};
37
38 LOG_TEST();
39
40 cl = gpr_cmdline_create(nullptr);
41 gpr_cmdline_add_int(cl, "foo", nullptr, &x);
42 ASSERT_EQ(x, 1);
43 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
44 ASSERT_EQ(x, 3);
45 gpr_cmdline_destroy(cl);
46 }
47
TEST(CmdlineTest,EqInt)48 TEST(CmdlineTest, EqInt) {
49 int x = 1;
50 gpr_cmdline* cl;
51 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo=3")};
52
53 LOG_TEST();
54
55 cl = gpr_cmdline_create(nullptr);
56 gpr_cmdline_add_int(cl, "foo", nullptr, &x);
57 ASSERT_EQ(x, 1);
58 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
59 ASSERT_EQ(x, 3);
60 gpr_cmdline_destroy(cl);
61 }
62
63 TEST(CmdlineTest, 2DashInt) {
64 int x = 1;
65 gpr_cmdline* cl;
66 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo"),
67 const_cast<char*>("3")};
68
69 LOG_TEST();
70
71 cl = gpr_cmdline_create(nullptr);
72 gpr_cmdline_add_int(cl, "foo", nullptr, &x);
73 ASSERT_EQ(x, 1);
74 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
75 ASSERT_EQ(x, 3);
76 gpr_cmdline_destroy(cl);
77 }
78
79 TEST(CmdlineTest, 2DashEqInt) {
80 int x = 1;
81 gpr_cmdline* cl;
82 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=3")};
83
84 LOG_TEST();
85
86 cl = gpr_cmdline_create(nullptr);
87 gpr_cmdline_add_int(cl, "foo", nullptr, &x);
88 ASSERT_EQ(x, 1);
89 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
90 ASSERT_EQ(x, 3);
91 gpr_cmdline_destroy(cl);
92 }
93
TEST(CmdlineTest,SimpleString)94 TEST(CmdlineTest, SimpleString) {
95 const char* x = nullptr;
96 gpr_cmdline* cl;
97 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo"),
98 const_cast<char*>("3")};
99
100 LOG_TEST();
101
102 cl = gpr_cmdline_create(nullptr);
103 gpr_cmdline_add_string(cl, "foo", nullptr, &x);
104 ASSERT_EQ(x, nullptr);
105 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
106 ASSERT_STREQ(x, "3");
107 gpr_cmdline_destroy(cl);
108 }
109
TEST(CmdlineTest,EqString)110 TEST(CmdlineTest, EqString) {
111 const char* x = nullptr;
112 gpr_cmdline* cl;
113 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo=3")};
114
115 LOG_TEST();
116
117 cl = gpr_cmdline_create(nullptr);
118 gpr_cmdline_add_string(cl, "foo", nullptr, &x);
119 ASSERT_EQ(x, nullptr);
120 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
121 ASSERT_STREQ(x, "3");
122 gpr_cmdline_destroy(cl);
123 }
124
125 TEST(CmdlineTest, 2DashString) {
126 const char* x = nullptr;
127 gpr_cmdline* cl;
128 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo"),
129 const_cast<char*>("3")};
130
131 LOG_TEST();
132
133 cl = gpr_cmdline_create(nullptr);
134 gpr_cmdline_add_string(cl, "foo", nullptr, &x);
135 ASSERT_EQ(x, nullptr);
136 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
137 ASSERT_STREQ(x, "3");
138 gpr_cmdline_destroy(cl);
139 }
140
141 TEST(CmdlineTest, 2DashEqString) {
142 const char* x = nullptr;
143 gpr_cmdline* cl;
144 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=3")};
145
146 LOG_TEST();
147
148 cl = gpr_cmdline_create(nullptr);
149 gpr_cmdline_add_string(cl, "foo", nullptr, &x);
150 ASSERT_EQ(x, nullptr);
151 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
152 ASSERT_STREQ(x, "3");
153 gpr_cmdline_destroy(cl);
154 }
155
TEST(CmdlineTest,FlagOn)156 TEST(CmdlineTest, FlagOn) {
157 int x = 2;
158 gpr_cmdline* cl;
159 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo")};
160
161 LOG_TEST();
162
163 cl = gpr_cmdline_create(nullptr);
164 gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
165 ASSERT_EQ(x, 2);
166 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
167 ASSERT_EQ(x, 1);
168 gpr_cmdline_destroy(cl);
169 }
170
TEST(CmdlineTest,FlagNo)171 TEST(CmdlineTest, FlagNo) {
172 int x = 2;
173 gpr_cmdline* cl;
174 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--no-foo")};
175
176 LOG_TEST();
177
178 cl = gpr_cmdline_create(nullptr);
179 gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
180 ASSERT_EQ(x, 2);
181 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
182 ASSERT_EQ(x, 0);
183 gpr_cmdline_destroy(cl);
184 }
185
TEST(CmdlineTest,FlagVal1)186 TEST(CmdlineTest, FlagVal1) {
187 int x = 2;
188 gpr_cmdline* cl;
189 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=1")};
190
191 LOG_TEST();
192
193 cl = gpr_cmdline_create(nullptr);
194 gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
195 ASSERT_EQ(x, 2);
196 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
197 ASSERT_EQ(x, 1);
198 gpr_cmdline_destroy(cl);
199 }
200
TEST(CmdlineTest,FlagVal0)201 TEST(CmdlineTest, FlagVal0) {
202 int x = 2;
203 gpr_cmdline* cl;
204 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=0")};
205
206 LOG_TEST();
207
208 cl = gpr_cmdline_create(nullptr);
209 gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
210 ASSERT_EQ(x, 2);
211 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
212 ASSERT_EQ(x, 0);
213 gpr_cmdline_destroy(cl);
214 }
215
TEST(CmdlineTest,FlagValTrue)216 TEST(CmdlineTest, FlagValTrue) {
217 int x = 2;
218 gpr_cmdline* cl;
219 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=true")};
220
221 LOG_TEST();
222
223 cl = gpr_cmdline_create(nullptr);
224 gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
225 ASSERT_EQ(x, 2);
226 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
227 ASSERT_EQ(x, 1);
228 gpr_cmdline_destroy(cl);
229 }
230
TEST(CmdlineTest,FlagValFalse)231 TEST(CmdlineTest, FlagValFalse) {
232 int x = 2;
233 gpr_cmdline* cl;
234 char* args[] = {const_cast<char*>(__FILE__),
235 const_cast<char*>("--foo=false")};
236
237 LOG_TEST();
238
239 cl = gpr_cmdline_create(nullptr);
240 gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
241 ASSERT_EQ(x, 2);
242 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
243 ASSERT_EQ(x, 0);
244 gpr_cmdline_destroy(cl);
245 }
246
TEST(CmdlineTest,Many)247 TEST(CmdlineTest, Many) {
248 const char* str = nullptr;
249 int x = 0;
250 int flag = 2;
251 gpr_cmdline* cl;
252
253 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--str"),
254 const_cast<char*>("hello"), const_cast<char*>("-x=4"),
255 const_cast<char*>("-no-flag")};
256
257 LOG_TEST();
258
259 cl = gpr_cmdline_create(nullptr);
260 gpr_cmdline_add_string(cl, "str", nullptr, &str);
261 gpr_cmdline_add_int(cl, "x", nullptr, &x);
262 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
263 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
264 ASSERT_EQ(x, 4);
265 ASSERT_STREQ(str, "hello");
266 ASSERT_EQ(flag, 0);
267 gpr_cmdline_destroy(cl);
268 }
269
extra_arg_cb(void * user_data,const char * arg)270 static void extra_arg_cb(void* user_data, const char* arg) {
271 int* count = static_cast<int*>(user_data);
272 ASSERT_NE(arg, nullptr);
273 ASSERT_EQ(strlen(arg), 1);
274 ASSERT_EQ(arg[0], 'a' + *count);
275 ++*count;
276 }
277
TEST(CmdlineTest,Extra)278 TEST(CmdlineTest, Extra) {
279 gpr_cmdline* cl;
280 int count = 0;
281 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("a"),
282 const_cast<char*>("b"), const_cast<char*>("c")};
283
284 LOG_TEST();
285
286 cl = gpr_cmdline_create(nullptr);
287 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
288 &count);
289 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
290 ASSERT_EQ(count, 3);
291 gpr_cmdline_destroy(cl);
292 }
293
TEST(CmdlineTest,ExtraDashdash)294 TEST(CmdlineTest, ExtraDashdash) {
295 gpr_cmdline* cl;
296 int count = 0;
297 char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--"),
298 const_cast<char*>("a"), const_cast<char*>("b"),
299 const_cast<char*>("c")};
300
301 LOG_TEST();
302
303 cl = gpr_cmdline_create(nullptr);
304 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
305 &count);
306 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
307 ASSERT_EQ(count, 3);
308 gpr_cmdline_destroy(cl);
309 }
310
TEST(CmdlineTest,Usage)311 TEST(CmdlineTest, Usage) {
312 gpr_cmdline* cl;
313
314 const char* str = nullptr;
315 int x = 0;
316 int flag = 2;
317
318 LOG_TEST();
319
320 cl = gpr_cmdline_create(nullptr);
321 gpr_cmdline_add_string(cl, "str", nullptr, &str);
322 gpr_cmdline_add_int(cl, "x", nullptr, &x);
323 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
324 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
325 nullptr);
326
327 std::string usage = gpr_cmdline_usage_string(cl, "test");
328 ASSERT_EQ(usage,
329 "Usage: test [--str=string] [--x=int] "
330 "[--flag|--no-flag] [file...]\n");
331
332 usage = gpr_cmdline_usage_string(cl, "/foo/test");
333 ASSERT_EQ(usage,
334 "Usage: test [--str=string] [--x=int] "
335 "[--flag|--no-flag] [file...]\n");
336
337 gpr_cmdline_destroy(cl);
338 }
339
TEST(CmdlineTest,Help)340 TEST(CmdlineTest, Help) {
341 gpr_cmdline* cl;
342
343 const char* str = nullptr;
344 int x = 0;
345 int flag = 2;
346
347 char* help[] = {const_cast<char*>(__FILE__), const_cast<char*>("-h")};
348
349 LOG_TEST();
350
351 cl = gpr_cmdline_create(nullptr);
352 gpr_cmdline_set_survive_failure(cl);
353 gpr_cmdline_add_string(cl, "str", nullptr, &str);
354 gpr_cmdline_add_int(cl, "x", nullptr, &x);
355 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
356 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
357 nullptr);
358
359 ASSERT_EQ(0, gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(help), help));
360
361 gpr_cmdline_destroy(cl);
362 }
363
TEST(CmdlineTest,Badargs1)364 TEST(CmdlineTest, Badargs1) {
365 gpr_cmdline* cl;
366
367 const char* str = nullptr;
368 int x = 0;
369 int flag = 2;
370
371 char* bad_arg_name[] = {const_cast<char*>(__FILE__),
372 const_cast<char*>("--y")};
373
374 LOG_TEST();
375
376 cl = gpr_cmdline_create(nullptr);
377 gpr_cmdline_set_survive_failure(cl);
378 gpr_cmdline_add_string(cl, "str", nullptr, &str);
379 gpr_cmdline_add_int(cl, "x", nullptr, &x);
380 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
381 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
382 nullptr);
383
384 ASSERT_EQ(0,
385 gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_arg_name), bad_arg_name));
386
387 gpr_cmdline_destroy(cl);
388 }
389
TEST(CmdlineTest,Badargs2)390 TEST(CmdlineTest, Badargs2) {
391 gpr_cmdline* cl;
392
393 const char* str = nullptr;
394 int x = 0;
395 int flag = 2;
396
397 char* bad_int_value[] = {const_cast<char*>(__FILE__),
398 const_cast<char*>("--x"),
399 const_cast<char*>("henry")};
400
401 LOG_TEST();
402
403 cl = gpr_cmdline_create(nullptr);
404 gpr_cmdline_set_survive_failure(cl);
405 gpr_cmdline_add_string(cl, "str", nullptr, &str);
406 gpr_cmdline_add_int(cl, "x", nullptr, &x);
407 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
408 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
409 nullptr);
410
411 ASSERT_EQ(
412 0, gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_int_value), bad_int_value));
413
414 gpr_cmdline_destroy(cl);
415 }
416
TEST(CmdlineTest,Badargs3)417 TEST(CmdlineTest, Badargs3) {
418 gpr_cmdline* cl;
419
420 const char* str = nullptr;
421 int x = 0;
422 int flag = 2;
423
424 char* bad_bool_value[] = {const_cast<char*>(__FILE__),
425 const_cast<char*>("--flag=henry")};
426
427 LOG_TEST();
428
429 cl = gpr_cmdline_create(nullptr);
430 gpr_cmdline_set_survive_failure(cl);
431 gpr_cmdline_add_string(cl, "str", nullptr, &str);
432 gpr_cmdline_add_int(cl, "x", nullptr, &x);
433 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
434 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
435 nullptr);
436
437 ASSERT_EQ(
438 0, gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value), bad_bool_value));
439
440 gpr_cmdline_destroy(cl);
441 }
442
TEST(CmdlineTest,Badargs4)443 TEST(CmdlineTest, Badargs4) {
444 gpr_cmdline* cl;
445
446 const char* str = nullptr;
447 int x = 0;
448 int flag = 2;
449
450 char* bad_bool_value[] = {const_cast<char*>(__FILE__),
451 const_cast<char*>("--no-str")};
452
453 LOG_TEST();
454
455 cl = gpr_cmdline_create(nullptr);
456 gpr_cmdline_set_survive_failure(cl);
457 gpr_cmdline_add_string(cl, "str", nullptr, &str);
458 gpr_cmdline_add_int(cl, "x", nullptr, &x);
459 gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
460 gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
461 nullptr);
462
463 ASSERT_EQ(
464 0, gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value), bad_bool_value));
465
466 gpr_cmdline_destroy(cl);
467 }
468
main(int argc,char ** argv)469 int main(int argc, char** argv) {
470 grpc::testing::TestEnvironment env(&argc, argv);
471 ::testing::InitGoogleTest(&argc, argv);
472 return RUN_ALL_TESTS();
473 }
474