command_options_unittest.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // Copyright (C) 2012-2013 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 <cstddef>
  15. #include <stdint.h>
  16. #include <string>
  17. #include <gtest/gtest.h>
  18. #include <boost/date_time/posix_time/posix_time.hpp>
  19. #include <dhcp/iface_mgr.h>
  20. #include <exceptions/exceptions.h>
  21. #include "command_options_helper.h"
  22. using namespace std;
  23. using namespace isc;
  24. using namespace isc::perfdhcp;
  25. using namespace boost::posix_time;
  26. // Verify that default constructor sets lease type to the expected value.
  27. TEST(LeaseTypeTest, defaultConstructor) {
  28. CommandOptions::LeaseType lease_type;
  29. EXPECT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS));
  30. }
  31. // Verify that the constructor sets the lease type to the specified value.
  32. TEST(LeaseTypeTest, constructor) {
  33. CommandOptions::LeaseType
  34. lease_type1(CommandOptions::LeaseType::ADDRESS);
  35. EXPECT_TRUE(lease_type1.is(CommandOptions::LeaseType::ADDRESS));
  36. CommandOptions::LeaseType
  37. lease_type2(CommandOptions::LeaseType::PREFIX);
  38. EXPECT_TRUE(lease_type2.is(CommandOptions::LeaseType::PREFIX));
  39. }
  40. // Verify that the lease type can be modified using set() function.
  41. TEST(LeaseTypeTest, set) {
  42. CommandOptions::LeaseType
  43. lease_type(CommandOptions::LeaseType::ADDRESS);
  44. EXPECT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS));
  45. lease_type.set(CommandOptions::LeaseType::PREFIX);
  46. EXPECT_TRUE(lease_type.is(CommandOptions::LeaseType::PREFIX));
  47. }
  48. // Verify that the includes() function returns true when the lease type
  49. // specified with the function argument is the same as the lease type
  50. // encapsulated by the LeaseType object on which include function is called
  51. // or when the lease type value encapsulated by this object is
  52. // ADDRESS_AND_PREFIX.
  53. TEST(LeaseTypeTest, includes) {
  54. // Lease type: ADDRESS
  55. CommandOptions::LeaseType lease_type(CommandOptions::LeaseType::ADDRESS);
  56. // Lease type IS ADDRESS.
  57. ASSERT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS));
  58. // Lease type includes the ADDRESS.
  59. EXPECT_TRUE(lease_type.includes(CommandOptions::LeaseType::ADDRESS));
  60. // Lease type does not include PREFIX.
  61. EXPECT_FALSE(lease_type.includes(CommandOptions::LeaseType::PREFIX));
  62. // Lease type does not include ADDRESS_AND_PREFIX.
  63. EXPECT_FALSE(
  64. lease_type.includes(CommandOptions::LeaseType::ADDRESS_AND_PREFIX)
  65. );
  66. // Do the same check for PREFIX.
  67. lease_type.set(CommandOptions::LeaseType::PREFIX);
  68. EXPECT_FALSE(lease_type.includes(CommandOptions::LeaseType::ADDRESS));
  69. EXPECT_TRUE(lease_type.includes(CommandOptions::LeaseType::PREFIX));
  70. EXPECT_FALSE(
  71. lease_type.includes(CommandOptions::LeaseType::ADDRESS_AND_PREFIX)
  72. );
  73. // When lease type is set to 'address-and-prefix' it means that client
  74. // requests both address and prefix (IA_NA and IA_PD). Therefore, the
  75. // LeaseType::includes() function should return true for both ADDRESS
  76. // and PREFIX.
  77. lease_type.set(CommandOptions::LeaseType::ADDRESS_AND_PREFIX);
  78. EXPECT_TRUE(lease_type.includes(CommandOptions::LeaseType::ADDRESS));
  79. EXPECT_TRUE(lease_type.includes(CommandOptions::LeaseType::PREFIX));
  80. EXPECT_TRUE(
  81. lease_type.includes(CommandOptions::LeaseType::ADDRESS_AND_PREFIX)
  82. );
  83. }
  84. // Verify that the LeaseType::fromCommandLine() function parses the lease-type
  85. // argument specified as -e<lease-type>.
  86. TEST(LeaseTypeTest, fromCommandLine) {
  87. CommandOptions::LeaseType
  88. lease_type(CommandOptions::LeaseType::ADDRESS);
  89. ASSERT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS));
  90. lease_type.fromCommandLine("prefix-only");
  91. ASSERT_TRUE(lease_type.is(CommandOptions::LeaseType::PREFIX));
  92. lease_type.fromCommandLine("address-only");
  93. EXPECT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS));
  94. lease_type.fromCommandLine("address-and-prefix");
  95. EXPECT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS_AND_PREFIX));
  96. EXPECT_THROW(lease_type.fromCommandLine("bogus-parameter"),
  97. isc::InvalidParameter);
  98. }
  99. // Verify that the LeaseType::toText() function returns the textual
  100. // representation of the lease type specified.
  101. TEST(LeaseTypeTest, toText) {
  102. CommandOptions::LeaseType lease_type;
  103. ASSERT_TRUE(lease_type.is(CommandOptions::LeaseType::ADDRESS));
  104. EXPECT_EQ("address-only (IA_NA option added to the client's request)",
  105. lease_type.toText());
  106. lease_type.set(CommandOptions::LeaseType::PREFIX);
  107. EXPECT_EQ("prefix-only (IA_PD option added to the client's request)",
  108. lease_type.toText());
  109. lease_type.set(CommandOptions::LeaseType::ADDRESS_AND_PREFIX);
  110. EXPECT_EQ("address-and-prefix (Both IA_NA and IA_PD options added to the"
  111. " client's request)", lease_type.toText());
  112. }
  113. /// \brief Test Fixture Class
  114. ///
  115. /// This test fixture class is used to perform
  116. /// unit tests on perfdhcp CommandOptions class.
  117. class CommandOptionsTest : public virtual ::testing::Test
  118. {
  119. public:
  120. /// \brief Default Constructor
  121. CommandOptionsTest() { }
  122. protected:
  123. /// \brief Parse command line and cleanup
  124. ///
  125. /// The method tokenizes command line to array of C-strings,
  126. /// parses arguments using CommandOptions class to set
  127. /// its data members and de-allocates array of C-strings.
  128. ///
  129. /// \param cmdline Command line to parse.
  130. /// \throws std::bad allocation if tokenization failed.
  131. /// \return true if program has been run in help or version mode ('h' or 'v' flag).
  132. bool process(const std::string& cmdline) {
  133. return (CommandOptionsHelper::process(cmdline));
  134. }
  135. /// \brief Check default initialized values
  136. ///
  137. /// Check if initialized values are correct
  138. void checkDefaults() {
  139. CommandOptions& opt = CommandOptions::instance();
  140. EXPECT_NO_THROW(process("perfdhcp 192.168.0.1"));
  141. EXPECT_EQ(4, opt.getIpVersion());
  142. EXPECT_EQ(CommandOptions::DORA_SARR, opt.getExchangeMode());
  143. EXPECT_TRUE(opt.getLeaseType().is(CommandOptions::LeaseType::ADDRESS));
  144. EXPECT_EQ(0, opt.getRate());
  145. EXPECT_EQ(0, opt.getRenewRate());
  146. EXPECT_EQ(0, opt.getReleaseRate());
  147. EXPECT_EQ(0, opt.getReportDelay());
  148. EXPECT_EQ(0, opt.getClientsNum());
  149. // default mac
  150. const uint8_t mac[6] = { 0x00, 0x0C, 0x01, 0x02, 0x03, 0x04 };
  151. std::vector<uint8_t> v1 = opt.getMacTemplate();
  152. ASSERT_EQ(6, v1.size());
  153. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  154. // Check if DUID is initialized. The DUID-LLT is expected
  155. // to start with DUID_LLT value of 1 and hardware ethernet
  156. // type equal to 1 (HWETHER_TYPE).
  157. const uint8_t duid_llt_and_hw[4] = { 0x0, 0x1, 0x0, 0x1 };
  158. // We assume DUID-LLT length 14. This includes 4 octets of
  159. // DUID_LLT value, two octets of hardware type, 4 octets
  160. // of time value and 6 octets of variable link layer (MAC)
  161. // address.
  162. const int duid_llt_size = 14;
  163. // DUID is not given from the command line but it is supposed
  164. // to be initialized by the CommandOptions private method
  165. // generateDuidTemplate().
  166. std::vector<uint8_t> v2 = opt.getDuidTemplate();
  167. ASSERT_EQ(duid_llt_size, opt.getDuidTemplate().size());
  168. EXPECT_TRUE(std::equal(v2.begin(), v2.begin() + 4,
  169. duid_llt_and_hw));
  170. // Check time field contents.
  171. ptime now = microsec_clock::universal_time();
  172. ptime duid_epoch(from_iso_string("20000101T000000"));
  173. time_period period(duid_epoch, now);
  174. uint32_t duration_sec = period.length().total_seconds();
  175. // Read time from the template generated.
  176. uint32_t duration_from_template = 0;
  177. memcpy(&duration_from_template, &v2[4], 4);
  178. duration_from_template = htonl(duration_from_template);
  179. // In special cases, we may have overflow in time field
  180. // so we give ourselves the margin of 10 seconds here.
  181. // If time value has been set more then 10 seconds back
  182. // it is safe to compare it with the time value generated
  183. // from now.
  184. if (duration_from_template > 10) {
  185. EXPECT_GE(duration_sec, duration_from_template);
  186. }
  187. EXPECT_EQ(0, opt.getBase().size());
  188. EXPECT_EQ(0, opt.getNumRequests().size());
  189. EXPECT_EQ(0, opt.getPeriod());
  190. for (int i = 0; i < opt.getDropTime().size(); ++i) {
  191. EXPECT_DOUBLE_EQ(1, opt.getDropTime()[i]);
  192. }
  193. ASSERT_EQ(opt.getMaxDrop().size(), opt.getMaxDropPercentage().size());
  194. for (int i = 0; i < opt.getMaxDrop().size(); ++i) {
  195. EXPECT_EQ(0, opt.getMaxDrop()[i]);
  196. EXPECT_EQ(0, opt.getMaxDropPercentage()[i]);
  197. }
  198. EXPECT_EQ("", opt.getLocalName());
  199. EXPECT_FALSE(opt.isInterface());
  200. EXPECT_EQ(0, opt.getPreload());
  201. EXPECT_EQ(1, opt.getAggressivity());
  202. EXPECT_EQ(0, opt.getLocalPort());
  203. EXPECT_FALSE(opt.isSeeded());
  204. EXPECT_EQ(0, opt.getSeed());
  205. EXPECT_FALSE(opt.isBroadcast());
  206. EXPECT_FALSE(opt.isRapidCommit());
  207. EXPECT_FALSE(opt.isUseFirst());
  208. EXPECT_EQ(0, opt.getTemplateFiles().size());
  209. EXPECT_EQ(0, opt.getTransactionIdOffset().size());
  210. EXPECT_EQ(0, opt.getRandomOffset().size());
  211. EXPECT_GT(0, opt.getElapsedTimeOffset());
  212. EXPECT_GT(0, opt.getServerIdOffset());
  213. EXPECT_GT(0, opt.getRequestedIpOffset());
  214. EXPECT_EQ("", opt.getDiags());
  215. EXPECT_EQ("", opt.getWrapped());
  216. EXPECT_EQ("192.168.0.1", opt.getServerName());
  217. }
  218. };
  219. TEST_F(CommandOptionsTest, Defaults) {
  220. EXPECT_NO_THROW(process("perfdhcp all"));
  221. checkDefaults();
  222. }
  223. TEST_F(CommandOptionsTest, HelpVersion) {
  224. // The parser is supposed to return true if 'h' or 'v' options
  225. // are specified.
  226. EXPECT_TRUE(process("perfdhcp -h"));
  227. EXPECT_TRUE(process("perfdhcp -v"));
  228. EXPECT_TRUE(process("perfdhcp -h -v"));
  229. EXPECT_TRUE(process("perfdhcp -6 -l ethx -h all"));
  230. EXPECT_TRUE(process("perfdhcp -l ethx -v all"));
  231. // No 'h' or 'v' option specified. The false value
  232. // should be returned.
  233. EXPECT_FALSE(process("perfdhcp -l ethx all"));
  234. }
  235. TEST_F(CommandOptionsTest, UseFirst) {
  236. CommandOptions& opt = CommandOptions::instance();
  237. EXPECT_NO_THROW(process("perfdhcp -1 -B -l ethx all"));
  238. EXPECT_TRUE(opt.isUseFirst());
  239. }
  240. TEST_F(CommandOptionsTest, IpVersion) {
  241. CommandOptions& opt = CommandOptions::instance();
  242. EXPECT_NO_THROW(process("perfdhcp -6 -l ethx -c -i all"));
  243. EXPECT_EQ(6, opt.getIpVersion());
  244. EXPECT_EQ("ethx", opt.getLocalName());
  245. EXPECT_TRUE(opt.isRapidCommit());
  246. EXPECT_FALSE(opt.isBroadcast());
  247. process("perfdhcp -4 -B -l ethx all");
  248. EXPECT_EQ(4, opt.getIpVersion());
  249. EXPECT_TRUE(opt.isBroadcast());
  250. EXPECT_FALSE(opt.isRapidCommit());
  251. // Negative test cases
  252. // -4 and -6 must not coexist
  253. EXPECT_THROW(process("perfdhcp -4 -6 -l ethx all"), isc::InvalidParameter);
  254. // -6 and -B must not coexist
  255. EXPECT_THROW(process("perfdhcp -6 -B -l ethx all"), isc::InvalidParameter);
  256. // -c and -4 (default) must not coexist
  257. EXPECT_THROW(process("perfdhcp -c -l ethx all"), isc::InvalidParameter);
  258. }
  259. TEST_F(CommandOptionsTest, LeaseType) {
  260. CommandOptions& opt = CommandOptions::instance();
  261. // Check that the -e address-only works for IPv6.
  262. ASSERT_NO_THROW(process("perfdhcp -6 -l etx -e address-only all"));
  263. EXPECT_EQ(6, opt.getIpVersion());
  264. EXPECT_EQ("etx", opt.getLocalName());
  265. EXPECT_TRUE(opt.getLeaseType().is(CommandOptions::LeaseType::ADDRESS));
  266. // Check that the -e address-only works for IPv4.
  267. ASSERT_NO_THROW(process("perfdhcp -4 -l etx -e address-only all"));
  268. EXPECT_EQ(4, opt.getIpVersion());
  269. EXPECT_EQ("etx", opt.getLocalName());
  270. EXPECT_TRUE(opt.getLeaseType().is(CommandOptions::LeaseType::ADDRESS));
  271. // Check that the -e prefix-only works.
  272. ASSERT_NO_THROW(process("perfdhcp -6 -l etx -e prefix-only all"));
  273. EXPECT_EQ(6, opt.getIpVersion());
  274. EXPECT_EQ("etx", opt.getLocalName());
  275. EXPECT_TRUE(opt.getLeaseType().is(CommandOptions::LeaseType::PREFIX));
  276. // Check that -e prefix-only must not coexist with -4 option.
  277. EXPECT_THROW(process("perfdhcp -4 -l ethx -e prefix-only all"),
  278. InvalidParameter);
  279. // Check that -e prefix-only must not coexist with -T options.
  280. EXPECT_THROW(process("perfdhcp -6 -l ethx -e prefix-only -T file1.hex"
  281. " -T file2.hex -E 4 all"), InvalidParameter);
  282. }
  283. TEST_F(CommandOptionsTest, Rate) {
  284. CommandOptions& opt = CommandOptions::instance();
  285. EXPECT_NO_THROW(process("perfdhcp -4 -r 10 -l ethx all"));
  286. EXPECT_EQ(10, opt.getRate());
  287. // Negative test cases
  288. // Rate must not be 0
  289. EXPECT_THROW(process("perfdhcp -4 -r 0 -l ethx all"),
  290. isc::InvalidParameter);
  291. // -r must be specified to use -n, -p and -D
  292. EXPECT_THROW(process("perfdhcp -6 -t 5 -l ethx all"),
  293. isc::InvalidParameter);
  294. EXPECT_THROW(process("perfdhcp -4 -n 150 -l ethx all"),
  295. isc::InvalidParameter);
  296. EXPECT_THROW(process("perfdhcp -6 -p 120 -l ethx all"),
  297. isc::InvalidParameter);
  298. EXPECT_THROW(process("perfdhcp -4 -D 1400 -l ethx all"),
  299. isc::InvalidParameter);
  300. }
  301. TEST_F(CommandOptionsTest, RenewRate) {
  302. CommandOptions& opt = CommandOptions::instance();
  303. // If -f is specified together with -r the command line should
  304. // be accepted and the renew rate should be set.
  305. EXPECT_NO_THROW(process("perfdhcp -6 -r 10 -f 10 -l ethx all"));
  306. EXPECT_EQ(10, opt.getRenewRate());
  307. // Check that the release rate can be set to different value than
  308. // rate specified as -r<rate>. Also, swap -f and -r to make sure
  309. // that order doesn't matter.
  310. EXPECT_NO_THROW(process("perfdhcp -6 -f 5 -r 10 -l ethx all"));
  311. EXPECT_EQ(5, opt.getRenewRate());
  312. // The renew rate should not be greater than the rate.
  313. EXPECT_THROW(process("perfdhcp -6 -r 10 -f 11 -l ethx all"),
  314. isc::InvalidParameter);
  315. // The renew-rate of 0 is invalid.
  316. EXPECT_THROW(process("perfdhcp -6 -r 10 -f 0 -l ethx all"),
  317. isc::InvalidParameter);
  318. // The negative renew-rate is invalid.
  319. EXPECT_THROW(process("perfdhcp -6 -r 10 -f -5 -l ethx all"),
  320. isc::InvalidParameter);
  321. // If -r<rate> is not specified the -f<renew-rate> should not
  322. // be accepted.
  323. EXPECT_THROW(process("perfdhcp -6 -f 10 -l ethx all"),
  324. isc::InvalidParameter);
  325. // Currently the -f<renew-rate> can be specified for IPv6 mode
  326. // only.
  327. EXPECT_THROW(process("perfdhcp -4 -r 10 -f 10 -l ethx all"),
  328. isc::InvalidParameter);
  329. // Renew rate should be specified.
  330. EXPECT_THROW(process("perfdhcp -6 -r 10 -f -l ethx all"),
  331. isc::InvalidParameter);
  332. // -f and -i are mutually exclusive
  333. EXPECT_THROW(process("perfdhcp -6 -r 10 -f 10 -l ethx -i all"),
  334. isc::InvalidParameter);
  335. }
  336. TEST_F(CommandOptionsTest, ReleaseRate) {
  337. CommandOptions& opt = CommandOptions::instance();
  338. // If -F is specified together with -r the command line should
  339. // be accepted and the release rate should be set.
  340. EXPECT_NO_THROW(process("perfdhcp -6 -r 10 -F 10 -l ethx all"));
  341. EXPECT_EQ(10, opt.getReleaseRate());
  342. // Check that the release rate can be set to different value than
  343. // rate specified as -r<rate>. Also, swap -F and -r to make sure
  344. // that order doesn't matter.
  345. EXPECT_NO_THROW(process("perfdhcp -6 -F 5 -r 10 -l ethx all"));
  346. EXPECT_EQ(5, opt.getReleaseRate());
  347. // The release rate should not be greater than the rate.
  348. EXPECT_THROW(process("perfdhcp -6 -r 10 -F 11 -l ethx all"),
  349. isc::InvalidParameter);
  350. // The release-rate of 0 is invalid.
  351. EXPECT_THROW(process("perfdhcp -6 -r 10 -F 0 -l ethx all"),
  352. isc::InvalidParameter);
  353. // The negative rlease-rate is invalid.
  354. EXPECT_THROW(process("perfdhcp -6 -r 10 -F -5 -l ethx all"),
  355. isc::InvalidParameter);
  356. // If -r<rate> is not specified the -F<release-rate> should not
  357. // be accepted.
  358. EXPECT_THROW(process("perfdhcp -6 -F 10 -l ethx all"),
  359. isc::InvalidParameter);
  360. // Currently the -F<release-rate> can be specified for IPv6 mode
  361. // only.
  362. EXPECT_THROW(process("perfdhcp -4 -r 10 -F 10 -l ethx all"),
  363. isc::InvalidParameter);
  364. // Release rate should be specified.
  365. EXPECT_THROW(process("perfdhcp -6 -r 10 -F -l ethx all"),
  366. isc::InvalidParameter);
  367. // -F and -i are mutually exclusive
  368. EXPECT_THROW(process("perfdhcp -6 -r 10 -F 10 -l ethx -i all"),
  369. isc::InvalidParameter);
  370. }
  371. TEST_F(CommandOptionsTest, ReleaseRenew) {
  372. CommandOptions& opt = CommandOptions::instance();
  373. // It should be possible to specify the -F, -f and -r options.
  374. EXPECT_NO_THROW(process("perfdhcp -6 -r 10 -F 3 -f 5 -l ethx all"));
  375. EXPECT_EQ(10, opt.getRate());
  376. EXPECT_EQ(3, opt.getReleaseRate());
  377. EXPECT_EQ(5, opt.getRenewRate());
  378. // It should be possible to specify the -F and -f with the values which
  379. // sum is equal to the rate specified as -r<rate>.
  380. EXPECT_NO_THROW(process("perfdhcp -6 -r 8 -F 3 -f 5 -l ethx all"));
  381. EXPECT_EQ(8, opt.getRate());
  382. EXPECT_EQ(3, opt.getReleaseRate());
  383. EXPECT_EQ(5, opt.getRenewRate());
  384. // Check that the sum of the release and renew rate is not greater
  385. // than the rate specified as -r<rate>.
  386. EXPECT_THROW(process("perfdhcp -6 -F 6 -f 5 -r 10 -l ethx all"),
  387. isc::InvalidParameter);
  388. }
  389. TEST_F(CommandOptionsTest, ReportDelay) {
  390. CommandOptions& opt = CommandOptions::instance();
  391. EXPECT_NO_THROW(process("perfdhcp -r 100 -t 17 -l ethx all"));
  392. EXPECT_EQ(17, opt.getReportDelay());
  393. // Negative test cases
  394. // -t must be positive integer
  395. EXPECT_THROW(process("perfdhcp -r 10 -t -8 -l ethx all"),
  396. isc::InvalidParameter);
  397. EXPECT_THROW(process("perfdhcp -r 10 -t 0 -l ethx all"),
  398. isc::InvalidParameter);
  399. EXPECT_THROW(process("perfdhcp -r 10 -t s -l ethx all"),
  400. isc::InvalidParameter);
  401. }
  402. TEST_F(CommandOptionsTest, ClientsNum) {
  403. CommandOptions& opt = CommandOptions::instance();
  404. EXPECT_NO_THROW(process("perfdhcp -R 200 -l ethx all"));
  405. EXPECT_EQ(200, opt.getClientsNum());
  406. process("perfdhcp -R 0 -l ethx all");
  407. EXPECT_EQ(0, opt.getClientsNum());
  408. // Negative test cases
  409. // Number of clients must be non-negative integer
  410. EXPECT_THROW(process("perfdhcp -R -5 -l ethx all"),
  411. isc::InvalidParameter);
  412. EXPECT_THROW(process("perfdhcp -R gs -l ethx all"),
  413. isc::InvalidParameter);
  414. }
  415. TEST_F(CommandOptionsTest, Base) {
  416. CommandOptions& opt = CommandOptions::instance();
  417. uint8_t mac[6] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60 };
  418. uint8_t duid[14] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  419. 0x01, 0x01, 0x01, 0x10, 0x11, 0x1F, 0x14 };
  420. // Test DUID and MAC together.
  421. EXPECT_NO_THROW(process("perfdhcp -b DUID=0101010101010101010110111F14"
  422. " -b MAC=10::20::30::40::50::60"
  423. " -l 127.0.0.1 all"));
  424. std::vector<uint8_t> v1 = opt.getMacTemplate();
  425. std::vector<uint8_t> v2 = opt.getDuidTemplate();
  426. v2 = opt.getDuidTemplate();
  427. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  428. EXPECT_TRUE(std::equal(v2.begin(), v2.end(), duid));
  429. // Test valid DUID.
  430. EXPECT_NO_THROW(
  431. process("perfdhcp -b duid=0101010101010101010110111F14 -l 127.0.0.1 all")
  432. );
  433. ASSERT_EQ(sizeof(duid) / sizeof(uint8_t), v2.size());
  434. EXPECT_TRUE(std::equal(v2.begin(), v2.end(), duid));
  435. // Test mix of upper/lower case letters.
  436. EXPECT_NO_THROW(process("perfdhcp -b DuiD=0101010101010101010110111F14"
  437. " -b Mac=10::20::30::40::50::60"
  438. " -l 127.0.0.1 all"));
  439. v1 = opt.getMacTemplate();
  440. v2 = opt.getDuidTemplate();
  441. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  442. EXPECT_TRUE(std::equal(v2.begin(), v2.end(), duid));
  443. // Use "ether" instead of "mac".
  444. EXPECT_NO_THROW(process("perfdhcp -b ether=10::20::30::40::50::60"
  445. " -l 127.0.0.1 all"));
  446. v1 = opt.getMacTemplate();
  447. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  448. // Use "ETHER" in upper case.
  449. EXPECT_NO_THROW(process("perfdhcp -b ETHER=10::20::30::40::50::60"
  450. " -l 127.0.0.1 all"));
  451. v1 = opt.getMacTemplate();
  452. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  453. // "t" is invalid character in DUID
  454. EXPECT_THROW(process("perfdhcp -6 -l ethx -b "
  455. "duid=010101010101010101t110111F14 all"),
  456. isc::InvalidParameter);
  457. // "3x" is invalid value in MAC address
  458. EXPECT_THROW(process("perfdhcp -b mac=10::2::3x::4::5::6 -l ethx all"),
  459. isc::InvalidParameter);
  460. // Base is not specified
  461. EXPECT_THROW(process("perfdhcp -b -l ethx all"),
  462. isc::InvalidParameter);
  463. // Typo: should be mac= instead of mc=
  464. EXPECT_THROW(process("perfdhcp -l ethx -b mc=00:01:02:03::04:05 all"),
  465. isc::InvalidParameter);
  466. // Too short DUID (< 6).
  467. EXPECT_THROW(process("perfdhcp -l ethx -b duid=00010203 all"),
  468. isc::InvalidParameter);
  469. // Odd number of digits.
  470. EXPECT_THROW(process("perfdhcp -l ethx -b duid=000102030405060 all"),
  471. isc::InvalidParameter);
  472. // Too short MAC (!= 6).
  473. EXPECT_THROW(process("perfdhcp -l ethx -b mac=00:01:02:04 all"),
  474. isc::InvalidParameter);
  475. }
  476. TEST_F(CommandOptionsTest, DropTime) {
  477. CommandOptions& opt = CommandOptions::instance();
  478. EXPECT_NO_THROW(process("perfdhcp -l ethx -d 12 all"));
  479. ASSERT_EQ(2, opt.getDropTime().size());
  480. EXPECT_DOUBLE_EQ(12, opt.getDropTime()[0]);
  481. EXPECT_DOUBLE_EQ(1, opt.getDropTime()[1]);
  482. EXPECT_NO_THROW(process("perfdhcp -l ethx -d 2 -d 4.7 all"));
  483. ASSERT_EQ(2, opt.getDropTime().size());
  484. EXPECT_DOUBLE_EQ(2, opt.getDropTime()[0]);
  485. EXPECT_DOUBLE_EQ(4.7, opt.getDropTime()[1]);
  486. // Negative test cases
  487. // Drop time must not be negative
  488. EXPECT_THROW(process("perfdhcp -l ethx -d -2 -d 4.7 all"),
  489. isc::InvalidParameter);
  490. EXPECT_THROW(process("perfdhcp -l ethx -d -9.1 -d 0 all"),
  491. isc::InvalidParameter);
  492. }
  493. TEST_F(CommandOptionsTest, TimeOffset) {
  494. CommandOptions& opt = CommandOptions::instance();
  495. EXPECT_NO_THROW(process("perfdhcp -l ethx -T file1.x -T file2.x -E 4 all"));
  496. EXPECT_EQ(4, opt.getElapsedTimeOffset());
  497. // Negative test cases
  498. // Argument -E must be used with -T
  499. EXPECT_THROW(process("perfdhcp -l ethx -E 3 -i all"),
  500. isc::InvalidParameter);
  501. // Value in -E not specified
  502. EXPECT_THROW(process("perfdhcp -l ethx -T file.x -E -i all"),
  503. isc::InvalidParameter);
  504. // Value for -E must not be negative
  505. EXPECT_THROW(process("perfdhcp -l ethx -E -3 -T file.x all"),
  506. isc::InvalidParameter);
  507. }
  508. TEST_F(CommandOptionsTest, ExchangeMode) {
  509. CommandOptions& opt = CommandOptions::instance();
  510. process("perfdhcp -l ethx -i all");
  511. EXPECT_EQ(CommandOptions::DO_SA, opt.getExchangeMode());
  512. // Negative test cases
  513. // No template file specified
  514. EXPECT_THROW(process("perfdhcp -i -l ethx -X 3 all"),
  515. isc::InvalidParameter);
  516. // Offsets can't be used in simple exchanges (-i)
  517. EXPECT_THROW(process("perfdhcp -i -l ethx -O 2 -T file.x all"),
  518. isc::InvalidParameter);
  519. EXPECT_THROW(process("perfdhcp -i -l ethx -E 3 -T file.x all"),
  520. isc::InvalidParameter);
  521. EXPECT_THROW(process("perfdhcp -i -l ethx -S 1 -T file.x all"),
  522. isc::InvalidParameter);
  523. EXPECT_THROW(process("perfdhcp -i -l ethx -I 2 -T file.x all"),
  524. isc::InvalidParameter);
  525. }
  526. TEST_F(CommandOptionsTest, Offsets) {
  527. CommandOptions& opt = CommandOptions::instance();
  528. EXPECT_NO_THROW(process("perfdhcp -E5 -4 -I 2 -S3 -O 30 -X7 -l ethx "
  529. "-X3 -T file1.x -T file2.x all"));
  530. EXPECT_EQ(2, opt.getRequestedIpOffset());
  531. EXPECT_EQ(5, opt.getElapsedTimeOffset());
  532. EXPECT_EQ(3, opt.getServerIdOffset());
  533. ASSERT_EQ(2, opt.getRandomOffset().size());
  534. EXPECT_EQ(30, opt.getRandomOffset()[0]);
  535. EXPECT_EQ(30, opt.getRandomOffset()[1]);
  536. ASSERT_EQ(2, opt.getTransactionIdOffset().size());
  537. EXPECT_EQ(7, opt.getTransactionIdOffset()[0]);
  538. EXPECT_EQ(3, opt.getTransactionIdOffset()[1]);
  539. // Negative test cases
  540. // IP offset/IA_NA offset must be positive
  541. EXPECT_THROW(process("perfdhcp -6 -I 0 -l ethx all"),
  542. isc::InvalidParameter);
  543. EXPECT_THROW(process("perfdhcp -6 -I -4 -l ethx all"),
  544. isc::InvalidParameter);
  545. // TODO - other negative cases
  546. }
  547. TEST_F(CommandOptionsTest, LocalPort) {
  548. CommandOptions& opt = CommandOptions::instance();
  549. EXPECT_NO_THROW(process("perfdhcp -l ethx -L 2000 all"));
  550. EXPECT_EQ(2000, opt.getLocalPort());
  551. // Negative test cases
  552. // Local port must be between 0..65535
  553. EXPECT_THROW(process("perfdhcp -l ethx -L -2 all"),
  554. isc::InvalidParameter);
  555. EXPECT_THROW(process("perfdhcp -l ethx -L all"),
  556. isc::InvalidParameter);
  557. EXPECT_THROW(process("perfdhcp -l ethx -L 65540 all"),
  558. isc::InvalidParameter);
  559. }
  560. TEST_F(CommandOptionsTest, Preload) {
  561. CommandOptions& opt = CommandOptions::instance();
  562. EXPECT_NO_THROW(process("perfdhcp -1 -P 3 -l ethx all"));
  563. EXPECT_EQ(3, opt.getPreload());
  564. // Negative test cases
  565. // Number of preload packages must not be negative integer
  566. EXPECT_THROW(process("perfdhcp -P -1 -l ethx all"),
  567. isc::InvalidParameter);
  568. EXPECT_THROW(process("perfdhcp -P -3 -l ethx all"),
  569. isc::InvalidParameter);
  570. }
  571. TEST_F(CommandOptionsTest, Seed) {
  572. CommandOptions& opt = CommandOptions::instance();
  573. EXPECT_NO_THROW(process("perfdhcp -6 -P 2 -s 23 -l ethx all"));
  574. EXPECT_EQ(23, opt.getSeed());
  575. EXPECT_TRUE(opt.isSeeded());
  576. EXPECT_NO_THROW(process("perfdhcp -6 -P 2 -s 0 -l ethx all"));
  577. EXPECT_EQ(0, opt.getSeed());
  578. EXPECT_FALSE(opt.isSeeded());
  579. // Negtaive test cases
  580. // Seed must be non-negative integer
  581. EXPECT_THROW(process("perfdhcp -6 -P 2 -s -5 -l ethx all"),
  582. isc::InvalidParameter);
  583. EXPECT_THROW(process("perfdhcp -6 -P 2 -s -l ethx all"),
  584. isc::InvalidParameter);
  585. }
  586. TEST_F(CommandOptionsTest, TemplateFiles) {
  587. CommandOptions& opt = CommandOptions::instance();
  588. EXPECT_NO_THROW(process("perfdhcp -T file1.x -l ethx all"));
  589. ASSERT_EQ(1, opt.getTemplateFiles().size());
  590. EXPECT_EQ("file1.x", opt.getTemplateFiles()[0]);
  591. EXPECT_NO_THROW(process("perfdhcp -T file1.x -s 12 -w start -T file2.x -4 -l ethx all"));
  592. ASSERT_EQ(2, opt.getTemplateFiles().size());
  593. EXPECT_EQ("file1.x", opt.getTemplateFiles()[0]);
  594. EXPECT_EQ("file2.x", opt.getTemplateFiles()[1]);
  595. // Negative test cases
  596. // No template file specified
  597. EXPECT_THROW(process("perfdhcp -s 12 -T -l ethx all"),
  598. isc::InvalidParameter);
  599. // Too many template files specified
  600. EXPECT_THROW(process("perfdhcp -s 12 -l ethx -T file.x "
  601. "-T file.x -T file.x all"),
  602. isc::InvalidParameter);
  603. }
  604. TEST_F(CommandOptionsTest, Wrapped) {
  605. CommandOptions& opt = CommandOptions::instance();
  606. EXPECT_NO_THROW(process("perfdhcp -B -w start -i -l ethx all"));
  607. EXPECT_EQ("start", opt.getWrapped());
  608. // Negative test cases
  609. // Missing command after -w, expected start/stop
  610. EXPECT_THROW(process("perfdhcp -B -i -l ethx -w all"),
  611. isc::InvalidParameter);
  612. }
  613. TEST_F(CommandOptionsTest, Diagnostics) {
  614. CommandOptions& opt = CommandOptions::instance();
  615. EXPECT_NO_THROW(process("perfdhcp -l ethx -i -x asTe all"));
  616. EXPECT_EQ("asTe", opt.getDiags());
  617. // Negative test cases
  618. // No diagnostics string specified
  619. EXPECT_THROW(process("perfdhcp -l ethx -i -x all"),
  620. isc::InvalidParameter);
  621. }
  622. TEST_F(CommandOptionsTest, Aggressivity) {
  623. CommandOptions& opt = CommandOptions::instance();
  624. process("perfdhcp -a 10 -l 192.168.0.1 all");
  625. EXPECT_EQ(10, opt.getAggressivity());
  626. // Negative test cases
  627. // Aggressivity must be non negative integer
  628. EXPECT_THROW(process("perfdhcp -l ethx -a 0 all"),
  629. isc::InvalidParameter);
  630. EXPECT_THROW(process("perfdhcp -l ethx -a all"),
  631. isc::InvalidParameter);
  632. EXPECT_THROW(process("perfdhcp -a -2 -l ethx -a 3 all"),
  633. isc::InvalidParameter);
  634. }
  635. TEST_F(CommandOptionsTest, MaxDrop) {
  636. CommandOptions& opt = CommandOptions::instance();
  637. EXPECT_NO_THROW(process("perfdhcp -D 25 -l ethx -r 10 all"));
  638. EXPECT_EQ(25, opt.getMaxDrop()[0]);
  639. EXPECT_NO_THROW(process("perfdhcp -D 25 -l ethx -D 15 -r 10 all"));
  640. EXPECT_EQ(25, opt.getMaxDrop()[0]);
  641. EXPECT_EQ(15, opt.getMaxDrop()[1]);
  642. EXPECT_NO_THROW(process("perfdhcp -D 15% -l ethx -r 10 all"));
  643. EXPECT_EQ(15, opt.getMaxDropPercentage()[0]);
  644. EXPECT_NO_THROW(process("perfdhcp -D 15% -D25% -l ethx -r 10 all"));
  645. EXPECT_EQ(15, opt.getMaxDropPercentage()[0]);
  646. EXPECT_EQ(25, opt.getMaxDropPercentage()[1]);
  647. EXPECT_NO_THROW(process("perfdhcp -D 1% -D 99% -l ethx -r 10 all"));
  648. EXPECT_EQ(1, opt.getMaxDropPercentage()[0]);
  649. EXPECT_EQ(99, opt.getMaxDropPercentage()[1]);
  650. // Negative test cases
  651. // Too many -D<value> options
  652. EXPECT_THROW(process("perfdhcp -D 0% -D 1 -l ethx -r20 -D 3 all"),
  653. isc::InvalidParameter);
  654. // Too many -D<value%> options
  655. EXPECT_THROW(process("perfdhcp -D 99% -D 13% -l ethx -r20 -D 10% all"),
  656. isc::InvalidParameter);
  657. // Percentage is out of bounds
  658. EXPECT_THROW(process("perfdhcp -D101% -D 13% -l ethx -r20 all"),
  659. isc::InvalidParameter);
  660. EXPECT_THROW(process("perfdhcp -D0% -D 13% -l ethx -r20 all"),
  661. isc::InvalidParameter);
  662. }
  663. TEST_F(CommandOptionsTest, NumRequest) {
  664. CommandOptions& opt = CommandOptions::instance();
  665. EXPECT_NO_THROW(process("perfdhcp -n 1000 -r 10 -l ethx all"));
  666. EXPECT_EQ(1000, opt.getNumRequests()[0]);
  667. EXPECT_NO_THROW(process("perfdhcp -n 5 -r 10 -n 500 -l ethx all"));
  668. EXPECT_EQ(5, opt.getNumRequests()[0]);
  669. EXPECT_EQ(500, opt.getNumRequests()[1]);
  670. // Negative test cases
  671. // Too many -n<value> parameters, expected maximum 2
  672. EXPECT_THROW(process("perfdhcp -n 1 -n 2 -l ethx -n3 -r 20 all"),
  673. isc::InvalidParameter);
  674. // Num request must be positive integer
  675. EXPECT_THROW(process("perfdhcp -n 1 -n -22 -l ethx -r 10 all"),
  676. isc::InvalidParameter);
  677. EXPECT_THROW(process("perfdhcp -n 0 -l ethx -r 10 all"),
  678. isc::InvalidParameter);
  679. }
  680. TEST_F(CommandOptionsTest, Period) {
  681. CommandOptions& opt = CommandOptions::instance();
  682. EXPECT_NO_THROW(process("perfdhcp -p 120 -l ethx -r 100 all"));
  683. EXPECT_EQ(120, opt.getPeriod());
  684. // Negative test cases
  685. // Test period must be positive integer
  686. EXPECT_THROW(process("perfdhcp -p 0 -l ethx -r 50 all"),
  687. isc::InvalidParameter);
  688. EXPECT_THROW(process("perfdhcp -p -3 -l ethx -r 50 all"),
  689. isc::InvalidParameter);
  690. }
  691. TEST_F(CommandOptionsTest, Interface) {
  692. // In order to make this test portable we need to know
  693. // at least one interface name on OS where test is run.
  694. // Interface Manager has ability to detect interfaces.
  695. // Although we don't call initIsInterface explicitly
  696. // here it is called by CommandOptions object interally
  697. // so this function is covered by the test.
  698. dhcp::IfaceMgr& iface_mgr = dhcp::IfaceMgr::instance();
  699. const dhcp::IfaceMgr::IfaceCollection& ifaces = iface_mgr.getIfaces();
  700. std::string iface_name;
  701. CommandOptions& opt = CommandOptions::instance();
  702. // The local loopback interface should be available.
  703. // If no interface have been found for any reason we should
  704. // not fail this test.
  705. if (ifaces.size() > 0) {
  706. // Get the name of the interface we detected.
  707. iface_name = ifaces.begin()->getName();
  708. // Use the name in the command parser.
  709. ASSERT_NO_THROW(process("perfdhcp -4 -l " + iface_name + " abc"));
  710. // We expect that command parser will detect that argument
  711. // specified along with '-l' is the interface name.
  712. EXPECT_TRUE(opt.isInterface());
  713. // If neither interface nor server is specified then
  714. // exception is expected to be thrown.
  715. EXPECT_THROW(process("perfdhcp -4"), isc::InvalidParameter);
  716. }
  717. }
  718. TEST_F(CommandOptionsTest, Server) {
  719. CommandOptions& opt = CommandOptions::instance();
  720. // There is at least server parameter needed. If server is not
  721. // specified the local interface must be specified.
  722. // The server value equal to 'all' means use broadcast.
  723. ASSERT_NO_THROW(process("perfdhcp all"));
  724. // Once command line is parsed we expect that server name is
  725. // set to broadcast address because 'all' was specified.
  726. EXPECT_TRUE(opt.isBroadcast());
  727. // The broadcast address is 255.255.255.255.
  728. EXPECT_EQ(DHCP_IPV4_BROADCAST_ADDRESS, opt.getServerName());
  729. // When all is specified for DHCPv6 mode we expect
  730. // FF02::1:2 as a server name which means All DHCP
  731. // servers and relay agents in local network segment
  732. ASSERT_NO_THROW(process("perfdhcp -6 all"));
  733. EXPECT_EQ(ALL_DHCP_RELAY_AGENTS_AND_SERVERS, opt.getServerName());
  734. // When server='servers' in DHCPv6 mode we expect
  735. // FF05::1:3 as server name which means All DHCP
  736. // servers in local network.
  737. ASSERT_NO_THROW(process("perfdhcp -6 servers"));
  738. EXPECT_EQ(ALL_DHCP_SERVERS, opt.getServerName());
  739. // If server name is neither 'all' nor 'servers'
  740. // the given argument value is expected to be
  741. // returned.
  742. ASSERT_NO_THROW(process("perfdhcp -6 abc"));
  743. EXPECT_EQ("abc", opt.getServerName());
  744. }