cloptions_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <stdint.h>
  15. #include <gtest/gtest.h>
  16. #include <exceptions/exceptions.h>
  17. #include "../cloptions.h"
  18. ///// \brief Check Non-Limit Options
  19. /////
  20. ///// Checks that the options that are NOT related to the message are set to
  21. ///// their default values.
  22. //void checkDefaultOtherValues() {
  23. // EXPECT_STREQ("127.0.0.1", getAddress());
  24. // EXPECT_EQ(53, getPort());
  25. // EXPECT_EQ(500, getTimeout());
  26. // EXPECT_STREQ("www.example.com", getQname());
  27. //}
  28. //
  29. ///// \brief Checks the minimum and maximum value specified for an option
  30. /////
  31. ///// Checks the values for one of the options whose values are stored in the
  32. ///// class's options_ array.
  33. /////
  34. ///// \param index Index of the option in the limits_ array
  35. ///// \param minval Expected minimum value
  36. ///// \param maxval Expected maximum value
  37. //void checkValuePair(int, uint32_t, uint32_t) {
  38. ////void checkValuePair(int index, uint32_t minval = 0, uint32_t maxval = 0) {
  39. // // Argument names commented out as they are not used, so this avoid a
  40. // // compiler warning (which by default in BIND 10 is promoted to an
  41. // // error).
  42. // // maximum and minimum are not yet defined so following lines commented
  43. // // out.
  44. // // EXPECT_EQ(minimum(index), minval);
  45. // // EXPECT_EQ(maximum(index), maxval);
  46. //}
  47. //
  48. ///// \brief Checks that all options are at default values
  49. /////
  50. ///// Checks that all options have both their maximum and minimum set to the
  51. ///// default values.
  52. /////
  53. ///// \param except Index not to check. (This allows options not being tested
  54. ///// to be checked to see that they are at the default value.) As all
  55. ///// index values are positive, a negative value means check
  56. ///// everything.
  57. //void checkDefaultLimitsValues(int except = -1) {
  58. // // Dummy use of except to avoid an error
  59. // EXPECT_EQ(-1, except);
  60. // // OptionInfo not yet defined!
  61. // /*
  62. // for (int i = 0; i < OptionInfo::SIZE; ++i) {
  63. // if (i != except) {
  64. // checkValuePair(i, OptionInfo::defval(i), OptionInfo::defval(i));
  65. // }
  66. // }
  67. // */
  68. //}
  69. //
  70. ///// \brief Check valid command option
  71. /////
  72. ///// Checks that the command line specification of one of the options taking
  73. ///// a value correctly processes the option.
  74. /////
  75. ///// \param index Option index
  76. ///// \param optflag Option flag (in the form '--option')
  77. ///// \param optval Value to be passed to the option.
  78. ///// \param minval Expected minimum value
  79. ///// \param maxval Expected maximum value
  80. //void checkCommandValid(int index, const char* optflag, const char* optval,
  81. // uint32_t minval, uint32_t maxval) {
  82. //
  83. // // Set up the command line and parse it.
  84. // const char* argv[] = {"badpacket", NULL, NULL};
  85. // argv[1] = optflag;
  86. // argv[2] = optval;
  87. // int argc = 3;
  88. // parse(argc, const_cast<char**>(argv));
  89. //
  90. // // Check the results. Everything should be at the defaults except for
  91. // // the specified option, where the minimum and maximum should be as
  92. // // specified.
  93. // checkDefaultOtherValues();
  94. // checkDefaultLimitsValues(index);
  95. // checkValuePair(index, minval, maxval);
  96. //}
  97. //
  98. /// \brief Check invalid command option
  99. ///
  100. /// Passed a command with an invalid value, checks that the parsing indicates
  101. /// error.
  102. ///
  103. /// \param optflag Option flag (in the form '-c')
  104. /// \param optval Value to be passed to the option.
  105. void checkOptionInvalid(const char* optflag, const char* optval) {
  106. const char* argv[] = { "perfdhcp", NULL, NULL, "foo" };
  107. int argc = sizeof(argv) / sizeof(const char* );
  108. confdata_t confdata;
  109. const char *server;
  110. argv[1] = optflag;
  111. argv[2] = optval;
  112. EXPECT_EQ(procArgs(argc, argv, &confdata, &server), 2);
  113. }
  114. void checkOptionValid(const char* optflag, const char* optval,
  115. confdata_t *confdata) {
  116. const char* argv[] = { "perfdhcp", NULL, NULL, "foo" };
  117. int argc = sizeof(argv) / sizeof(const char* );
  118. const char *server;
  119. argv[1] = optflag;
  120. argv[2] = optval;
  121. EXPECT_EQ(procArgs(argc, argv, confdata, &server), 1);
  122. }
  123. void checkPosIntOpt(const char *optflag) {
  124. confdata_t confdata;
  125. checkOptionValid(optflag, "100", &confdata);
  126. EXPECT_EQ(100, confdata.map[(unsigned)optflag[1]]->values[0]->value.nnint);
  127. checkOptionInvalid(optflag, "0");
  128. checkOptionInvalid(optflag, "-1");
  129. checkOptionInvalid(optflag, "1.0");
  130. checkOptionInvalid(optflag, "x");
  131. }
  132. TEST(CommandOptionsTest, numreq) {
  133. checkPosIntOpt("-n");
  134. }
  135. TEST(CommandOptionsTest, rate) {
  136. checkPosIntOpt("-r");
  137. }
  138. ///// \brief Check one-bit field
  139. /////
  140. ///// Explicitly for those fields in the flags word that are one bit wide,
  141. ///// perform a series of tests to check that they accept valid values and
  142. ///// reject invalid ones.
  143. /////
  144. ///// \param index Option index
  145. ///// \param optflag Option flag (in the form '--option')
  146. //void checkOneBitField(int index, const char* optflag) {
  147. // checkCommandValid(index, optflag, "0", 0, 0);
  148. // checkCommandValid(index, optflag, "1", 1, 1);
  149. // checkCommandValid(index, optflag, "0-1", 0, 1);
  150. // checkCommandValid(index, optflag, "1-0", 0, 1);
  151. // checkCommandInvalid(optflag, "0-3");
  152. // checkCommandInvalid(optflag, "4");
  153. // checkCommandInvalid(optflag, "xyz");
  154. //}
  155. //
  156. ///// \brief Check four-bit field
  157. /////
  158. ///// Explicitly for those fields in the flags word that are four bits wide,
  159. ///// perform a series of tests to check that they accept valid values and
  160. ///// reject invalid ones.
  161. /////
  162. ///// \param index Option index
  163. ///// \param optflag Option flag (in the form '--option')
  164. //void checkFourBitField(int index, const char* optflag) {
  165. // checkCommandValid(index, optflag, "0", 0, 0);
  166. // checkCommandValid(index, optflag, "15", 15, 15);
  167. // checkCommandValid(index, optflag, "0-15", 0, 15);
  168. // checkCommandValid(index, optflag, "15-0", 0, 15);
  169. // checkCommandInvalid(optflag, "0-17");
  170. // checkCommandInvalid(optflag, "24");
  171. // checkCommandInvalid(optflag, "xyz");
  172. //}
  173. //
  174. ///// \brief Check sixteen-bit field
  175. /////
  176. ///// Explicitly test the parsing of the fields that can take a 16-bit
  177. ///// value ranging from 0 to 65535.
  178. /////
  179. ///// \param index Option index
  180. ///// \param optflag Option flag (in the form '--option')
  181. //void checkSixteenBitField(int index, const char* optflag) {
  182. // checkCommandValid(index, optflag, "0", 0, 0);
  183. // checkCommandValid(index, optflag, "65535", 65535, 65535);
  184. // checkCommandValid(index, optflag, "0-65535", 0, 65535);
  185. // checkCommandValid(index, optflag, "65535-0", 0, 65535);
  186. // checkCommandInvalid(optflag, "0-65536");
  187. // checkCommandInvalid(optflag, "65537");
  188. // checkCommandInvalid(optflag, "xyz");
  189. //}
  190. //
  191. //// Check that each of the non-message options will be recognised
  192. //
  193. //TEST(CommandOptionsTest, address) {
  194. //
  195. // const char* argv[] = {"badpacket", "--address", "192.0.2.1"};
  196. // int argc = sizeof(argv) / sizeof(const char*);
  197. //
  198. // // The conversion is ugly but it simplifies the process of entering the
  199. // // string constant. The cast throws away the "const"ness of the pointed-to
  200. // // strings in order to conform to the function signature; however, the
  201. // // called functions all treat the strings as const.
  202. // parse(argc, const_cast<char**>(argv));
  203. // EXPECT_STREQ("192.0.2.1", getAddress());
  204. // EXPECT_EQ(53, getPort());
  205. // EXPECT_EQ(500, getTimeout());
  206. // EXPECT_STREQ("www.example.com", getQname());
  207. // checkDefaultLimitsValues();
  208. //}
  209. //TEST(CommandOptionsTest, timeout) {
  210. // const char* argv[] = {"badpacket", "--timeout", "250"};
  211. // int argc = sizeof(argv) / sizeof(const char*);
  212. //
  213. // parse(argc, const_cast<char**>(argv));
  214. // EXPECT_STREQ("127.0.0.1", getAddress());
  215. // EXPECT_EQ(53, getPort());
  216. // EXPECT_EQ(250, getTimeout());
  217. // EXPECT_STREQ("www.example.com", getQname());
  218. // checkDefaultLimitsValues();
  219. //}
  220. //
  221. //TEST(CommandOptionsTest, parameter) {
  222. // const char* argv[] = {"badpacket", "ftp.example.net"};
  223. // int argc = sizeof(argv) / sizeof(const char*);
  224. //
  225. // parse(argc, const_cast<char**>(argv));
  226. // EXPECT_STREQ("127.0.0.1", getAddress());
  227. // EXPECT_EQ(53, getPort());
  228. // EXPECT_EQ(500, getTimeout());
  229. // EXPECT_STREQ("ftp.example.net", getQname());
  230. // checkDefaultLimitsValues();
  231. //}
  232. //
  233. //// Test options representing the flags fields.
  234. //
  235. //TEST(CommandOptionsTest, qr) {
  236. // checkOneBitField(OptionInfo::QR, "--qr");
  237. //}
  238. //
  239. //TEST(CommandOptionsTest, op) {
  240. // checkFourBitField(OptionInfo::OP, "--op");
  241. //}
  242. //
  243. //TEST(CommandOptionsTest, aa) {
  244. // checkOneBitField(OptionInfo::AA, "--aa");
  245. //}
  246. //
  247. //TEST(CommandOptionsTest, tc) {
  248. // checkOneBitField(OptionInfo::TC, "--tc");
  249. //}
  250. //
  251. //TEST(CommandOptionsTest, z) {
  252. // checkOneBitField(OptionInfo::Z, "--z");
  253. //}
  254. //
  255. //TEST(CommandOptionsTest, ad) {
  256. // checkOneBitField(OptionInfo::AD, "--ad");
  257. //}
  258. //
  259. //TEST(CommandOptionsTest, cd) {
  260. // checkOneBitField(OptionInfo::CD, "--cd");
  261. //}
  262. //
  263. //TEST(CommandOptionsTest, rc) {
  264. // checkFourBitField(OptionInfo::RC, "--rc");
  265. //}
  266. //
  267. //// Section count options
  268. //
  269. //TEST(CommandOptionsTest, qc) {
  270. // checkSixteenBitField(OptionInfo::QC, "--qc");
  271. //}
  272. //
  273. //TEST(CommandOptionsTest, ac) {
  274. // checkSixteenBitField(OptionInfo::AC, "--ac");
  275. //}
  276. //
  277. //TEST(CommandOptionsTest, uc) {
  278. // checkSixteenBitField(OptionInfo::UC, "--uc");
  279. //}
  280. //
  281. //TEST(CommandOptionsTest, dc) {
  282. // checkSixteenBitField(OptionInfo::DC, "--dc");
  283. //}
  284. //
  285. //// ... and the message size option
  286. //
  287. //TEST(CommandOptionsTest, ms) {
  288. // int index = OptionInfo::MS;
  289. // const char* optflag = "--ms";
  290. //
  291. // checkCommandValid(index, optflag, "1", 1, 1);
  292. // checkCommandValid(index, optflag, "65536", 65536, 65536);
  293. // checkCommandValid(index, optflag, "1-65536", 1, 65536);
  294. // checkCommandValid(index, optflag, "65536-1", 1, 65536);
  295. // checkCommandInvalid(optflag, "0");
  296. // checkCommandInvalid(optflag, "1-65537");
  297. // checkCommandInvalid(optflag, "65538");
  298. // checkCommandInvalid(optflag, "xyz");
  299. //}