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