cmdline_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #include "test/core/util/cmdline.h"
  19. #include <string.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/gpr/useful.h"
  23. #include "test/core/util/test_config.h"
  24. #define LOG_TEST() gpr_log(GPR_INFO, "test at %s:%d", __FILE__, __LINE__)
  25. static void test_simple_int(void) {
  26. int x = 1;
  27. gpr_cmdline* cl;
  28. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo"),
  29. const_cast<char*>("3")};
  30. LOG_TEST();
  31. cl = gpr_cmdline_create(nullptr);
  32. gpr_cmdline_add_int(cl, "foo", nullptr, &x);
  33. GPR_ASSERT(x == 1);
  34. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  35. GPR_ASSERT(x == 3);
  36. gpr_cmdline_destroy(cl);
  37. }
  38. static void test_eq_int(void) {
  39. int x = 1;
  40. gpr_cmdline* cl;
  41. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo=3")};
  42. LOG_TEST();
  43. cl = gpr_cmdline_create(nullptr);
  44. gpr_cmdline_add_int(cl, "foo", nullptr, &x);
  45. GPR_ASSERT(x == 1);
  46. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  47. GPR_ASSERT(x == 3);
  48. gpr_cmdline_destroy(cl);
  49. }
  50. static void test_2dash_int(void) {
  51. int x = 1;
  52. gpr_cmdline* cl;
  53. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo"),
  54. const_cast<char*>("3")};
  55. LOG_TEST();
  56. cl = gpr_cmdline_create(nullptr);
  57. gpr_cmdline_add_int(cl, "foo", nullptr, &x);
  58. GPR_ASSERT(x == 1);
  59. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  60. GPR_ASSERT(x == 3);
  61. gpr_cmdline_destroy(cl);
  62. }
  63. static void test_2dash_eq_int(void) {
  64. int x = 1;
  65. gpr_cmdline* cl;
  66. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=3")};
  67. LOG_TEST();
  68. cl = gpr_cmdline_create(nullptr);
  69. gpr_cmdline_add_int(cl, "foo", nullptr, &x);
  70. GPR_ASSERT(x == 1);
  71. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  72. GPR_ASSERT(x == 3);
  73. gpr_cmdline_destroy(cl);
  74. }
  75. static void test_simple_string(void) {
  76. const char* x = nullptr;
  77. gpr_cmdline* cl;
  78. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo"),
  79. const_cast<char*>("3")};
  80. LOG_TEST();
  81. cl = gpr_cmdline_create(nullptr);
  82. gpr_cmdline_add_string(cl, "foo", nullptr, &x);
  83. GPR_ASSERT(x == nullptr);
  84. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  85. GPR_ASSERT(0 == strcmp(x, "3"));
  86. gpr_cmdline_destroy(cl);
  87. }
  88. static void test_eq_string(void) {
  89. const char* x = nullptr;
  90. gpr_cmdline* cl;
  91. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo=3")};
  92. LOG_TEST();
  93. cl = gpr_cmdline_create(nullptr);
  94. gpr_cmdline_add_string(cl, "foo", nullptr, &x);
  95. GPR_ASSERT(x == nullptr);
  96. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  97. GPR_ASSERT(0 == strcmp(x, "3"));
  98. gpr_cmdline_destroy(cl);
  99. }
  100. static void test_2dash_string(void) {
  101. const char* x = nullptr;
  102. gpr_cmdline* cl;
  103. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo"),
  104. const_cast<char*>("3")};
  105. LOG_TEST();
  106. cl = gpr_cmdline_create(nullptr);
  107. gpr_cmdline_add_string(cl, "foo", nullptr, &x);
  108. GPR_ASSERT(x == nullptr);
  109. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  110. GPR_ASSERT(0 == strcmp(x, "3"));
  111. gpr_cmdline_destroy(cl);
  112. }
  113. static void test_2dash_eq_string(void) {
  114. const char* x = nullptr;
  115. gpr_cmdline* cl;
  116. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=3")};
  117. LOG_TEST();
  118. cl = gpr_cmdline_create(nullptr);
  119. gpr_cmdline_add_string(cl, "foo", nullptr, &x);
  120. GPR_ASSERT(x == nullptr);
  121. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  122. GPR_ASSERT(0 == strcmp(x, "3"));
  123. gpr_cmdline_destroy(cl);
  124. }
  125. static void test_flag_on(void) {
  126. int x = 2;
  127. gpr_cmdline* cl;
  128. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo")};
  129. LOG_TEST();
  130. cl = gpr_cmdline_create(nullptr);
  131. gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
  132. GPR_ASSERT(x == 2);
  133. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  134. GPR_ASSERT(x == 1);
  135. gpr_cmdline_destroy(cl);
  136. }
  137. static void test_flag_no(void) {
  138. int x = 2;
  139. gpr_cmdline* cl;
  140. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--no-foo")};
  141. LOG_TEST();
  142. cl = gpr_cmdline_create(nullptr);
  143. gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
  144. GPR_ASSERT(x == 2);
  145. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  146. GPR_ASSERT(x == 0);
  147. gpr_cmdline_destroy(cl);
  148. }
  149. static void test_flag_val_1(void) {
  150. int x = 2;
  151. gpr_cmdline* cl;
  152. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=1")};
  153. LOG_TEST();
  154. cl = gpr_cmdline_create(nullptr);
  155. gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
  156. GPR_ASSERT(x == 2);
  157. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  158. GPR_ASSERT(x == 1);
  159. gpr_cmdline_destroy(cl);
  160. }
  161. static void test_flag_val_0(void) {
  162. int x = 2;
  163. gpr_cmdline* cl;
  164. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=0")};
  165. LOG_TEST();
  166. cl = gpr_cmdline_create(nullptr);
  167. gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
  168. GPR_ASSERT(x == 2);
  169. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  170. GPR_ASSERT(x == 0);
  171. gpr_cmdline_destroy(cl);
  172. }
  173. static void test_flag_val_true(void) {
  174. int x = 2;
  175. gpr_cmdline* cl;
  176. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=true")};
  177. LOG_TEST();
  178. cl = gpr_cmdline_create(nullptr);
  179. gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
  180. GPR_ASSERT(x == 2);
  181. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  182. GPR_ASSERT(x == 1);
  183. gpr_cmdline_destroy(cl);
  184. }
  185. static void test_flag_val_false(void) {
  186. int x = 2;
  187. gpr_cmdline* cl;
  188. char* args[] = {const_cast<char*>(__FILE__),
  189. const_cast<char*>("--foo=false")};
  190. LOG_TEST();
  191. cl = gpr_cmdline_create(nullptr);
  192. gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
  193. GPR_ASSERT(x == 2);
  194. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  195. GPR_ASSERT(x == 0);
  196. gpr_cmdline_destroy(cl);
  197. }
  198. static void test_many(void) {
  199. const char* str = nullptr;
  200. int x = 0;
  201. int flag = 2;
  202. gpr_cmdline* cl;
  203. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--str"),
  204. const_cast<char*>("hello"), const_cast<char*>("-x=4"),
  205. const_cast<char*>("-no-flag")};
  206. LOG_TEST();
  207. cl = gpr_cmdline_create(nullptr);
  208. gpr_cmdline_add_string(cl, "str", nullptr, &str);
  209. gpr_cmdline_add_int(cl, "x", nullptr, &x);
  210. gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
  211. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  212. GPR_ASSERT(x == 4);
  213. GPR_ASSERT(0 == strcmp(str, "hello"));
  214. GPR_ASSERT(flag == 0);
  215. gpr_cmdline_destroy(cl);
  216. }
  217. static void extra_arg_cb(void* user_data, const char* arg) {
  218. int* count = static_cast<int*>(user_data);
  219. GPR_ASSERT(arg != nullptr);
  220. GPR_ASSERT(strlen(arg) == 1);
  221. GPR_ASSERT(arg[0] == 'a' + *count);
  222. ++*count;
  223. }
  224. static void test_extra(void) {
  225. gpr_cmdline* cl;
  226. int count = 0;
  227. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("a"),
  228. const_cast<char*>("b"), const_cast<char*>("c")};
  229. LOG_TEST();
  230. cl = gpr_cmdline_create(nullptr);
  231. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  232. &count);
  233. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  234. GPR_ASSERT(count == 3);
  235. gpr_cmdline_destroy(cl);
  236. }
  237. static void test_extra_dashdash(void) {
  238. gpr_cmdline* cl;
  239. int count = 0;
  240. char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--"),
  241. const_cast<char*>("a"), const_cast<char*>("b"),
  242. const_cast<char*>("c")};
  243. LOG_TEST();
  244. cl = gpr_cmdline_create(nullptr);
  245. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  246. &count);
  247. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
  248. GPR_ASSERT(count == 3);
  249. gpr_cmdline_destroy(cl);
  250. }
  251. static void test_usage(void) {
  252. gpr_cmdline* cl;
  253. const char* str = nullptr;
  254. int x = 0;
  255. int flag = 2;
  256. LOG_TEST();
  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_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  262. nullptr);
  263. std::string usage = gpr_cmdline_usage_string(cl, "test");
  264. GPR_ASSERT(usage ==
  265. "Usage: test [--str=string] [--x=int] "
  266. "[--flag|--no-flag] [file...]\n");
  267. usage = gpr_cmdline_usage_string(cl, "/foo/test");
  268. GPR_ASSERT(usage ==
  269. "Usage: test [--str=string] [--x=int] "
  270. "[--flag|--no-flag] [file...]\n");
  271. gpr_cmdline_destroy(cl);
  272. }
  273. static void test_help(void) {
  274. gpr_cmdline* cl;
  275. const char* str = nullptr;
  276. int x = 0;
  277. int flag = 2;
  278. char* help[] = {const_cast<char*>(__FILE__), const_cast<char*>("-h")};
  279. LOG_TEST();
  280. cl = gpr_cmdline_create(nullptr);
  281. gpr_cmdline_set_survive_failure(cl);
  282. gpr_cmdline_add_string(cl, "str", nullptr, &str);
  283. gpr_cmdline_add_int(cl, "x", nullptr, &x);
  284. gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
  285. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  286. nullptr);
  287. GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(help), help));
  288. gpr_cmdline_destroy(cl);
  289. }
  290. static void test_badargs1(void) {
  291. gpr_cmdline* cl;
  292. const char* str = nullptr;
  293. int x = 0;
  294. int flag = 2;
  295. char* bad_arg_name[] = {const_cast<char*>(__FILE__),
  296. const_cast<char*>("--y")};
  297. LOG_TEST();
  298. cl = gpr_cmdline_create(nullptr);
  299. gpr_cmdline_set_survive_failure(cl);
  300. gpr_cmdline_add_string(cl, "str", nullptr, &str);
  301. gpr_cmdline_add_int(cl, "x", nullptr, &x);
  302. gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
  303. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  304. nullptr);
  305. GPR_ASSERT(0 ==
  306. gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_arg_name), bad_arg_name));
  307. gpr_cmdline_destroy(cl);
  308. }
  309. static void test_badargs2(void) {
  310. gpr_cmdline* cl;
  311. const char* str = nullptr;
  312. int x = 0;
  313. int flag = 2;
  314. char* bad_int_value[] = {const_cast<char*>(__FILE__),
  315. const_cast<char*>("--x"),
  316. const_cast<char*>("henry")};
  317. LOG_TEST();
  318. cl = gpr_cmdline_create(nullptr);
  319. gpr_cmdline_set_survive_failure(cl);
  320. gpr_cmdline_add_string(cl, "str", nullptr, &str);
  321. gpr_cmdline_add_int(cl, "x", nullptr, &x);
  322. gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
  323. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  324. nullptr);
  325. GPR_ASSERT(
  326. 0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_int_value), bad_int_value));
  327. gpr_cmdline_destroy(cl);
  328. }
  329. static void test_badargs3(void) {
  330. gpr_cmdline* cl;
  331. const char* str = nullptr;
  332. int x = 0;
  333. int flag = 2;
  334. char* bad_bool_value[] = {const_cast<char*>(__FILE__),
  335. const_cast<char*>("--flag=henry")};
  336. LOG_TEST();
  337. cl = gpr_cmdline_create(nullptr);
  338. gpr_cmdline_set_survive_failure(cl);
  339. gpr_cmdline_add_string(cl, "str", nullptr, &str);
  340. gpr_cmdline_add_int(cl, "x", nullptr, &x);
  341. gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
  342. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  343. nullptr);
  344. GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value),
  345. bad_bool_value));
  346. gpr_cmdline_destroy(cl);
  347. }
  348. static void test_badargs4(void) {
  349. gpr_cmdline* cl;
  350. const char* str = nullptr;
  351. int x = 0;
  352. int flag = 2;
  353. char* bad_bool_value[] = {const_cast<char*>(__FILE__),
  354. const_cast<char*>("--no-str")};
  355. LOG_TEST();
  356. cl = gpr_cmdline_create(nullptr);
  357. gpr_cmdline_set_survive_failure(cl);
  358. gpr_cmdline_add_string(cl, "str", nullptr, &str);
  359. gpr_cmdline_add_int(cl, "x", nullptr, &x);
  360. gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
  361. gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
  362. nullptr);
  363. GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value),
  364. bad_bool_value));
  365. gpr_cmdline_destroy(cl);
  366. }
  367. int main(int argc, char** argv) {
  368. grpc::testing::TestEnvironment env(argc, argv);
  369. test_simple_int();
  370. test_eq_int();
  371. test_2dash_int();
  372. test_2dash_eq_int();
  373. test_simple_string();
  374. test_eq_string();
  375. test_2dash_string();
  376. test_2dash_eq_string();
  377. test_flag_on();
  378. test_flag_no();
  379. test_flag_val_1();
  380. test_flag_val_0();
  381. test_flag_val_true();
  382. test_flag_val_false();
  383. test_many();
  384. test_extra();
  385. test_extra_dashdash();
  386. test_usage();
  387. test_help();
  388. test_badargs1();
  389. test_badargs2();
  390. test_badargs3();
  391. test_badargs4();
  392. return 0;
  393. }