command_options_unittest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // Copyright (C) 2012 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. /// \brief Test Fixture Class
  27. ///
  28. /// This test fixture class is used to perform
  29. /// unit tests on perfdhcp CommandOptions class.
  30. class CommandOptionsTest : public virtual ::testing::Test
  31. {
  32. public:
  33. /// \brief Default Constructor
  34. CommandOptionsTest() { }
  35. protected:
  36. /// \brief Parse command line and cleanup
  37. ///
  38. /// The method tokenizes command line to array of C-strings,
  39. /// parses arguments using CommandOptions class to set
  40. /// its data members and de-allocates array of C-strings.
  41. ///
  42. /// \param cmdline Command line to parse
  43. /// \throws std::bad allocation if tokenization failed
  44. void process(const std::string& cmdline) {
  45. CommandOptionsHelper::process(cmdline);
  46. }
  47. /// \brief Check default initialized values
  48. ///
  49. /// Check if initialized values are correct
  50. void checkDefaults() {
  51. CommandOptions& opt = CommandOptions::instance();
  52. process("perfdhcp 192.168.0.1");
  53. EXPECT_EQ(4, opt.getIpVersion());
  54. EXPECT_EQ(CommandOptions::DORA_SARR, opt.getExchangeMode());
  55. EXPECT_EQ(0, opt.getRate());
  56. EXPECT_EQ(0, opt.getReportDelay());
  57. EXPECT_EQ(0, opt.getClientsNum());
  58. // default mac
  59. const uint8_t mac[6] = { 0x00, 0x0C, 0x01, 0x02, 0x03, 0x04 };
  60. std::vector<uint8_t> v1 = opt.getMacTemplate();
  61. ASSERT_EQ(6, v1.size());
  62. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  63. // Check if DUID is initialized. The DUID-LLT is expected
  64. // to start with DUID_LLT value of 1 and hardware ethernet
  65. // type equal to 1 (HWETHER_TYPE).
  66. const uint8_t duid_llt_and_hw[4] = { 0x0, 0x1, 0x0, 0x1 };
  67. // We assume DUID-LLT length 14. This includes 4 octets of
  68. // DUID_LLT value, two octets of hardware type, 4 octets
  69. // of time value and 6 octets of variable link layer (MAC)
  70. // address.
  71. const int duid_llt_size = 14;
  72. // DUID is not given from the command line but it is supposed
  73. // to be initialized by the CommandOptions private method
  74. // generateDuidTemplate().
  75. std::vector<uint8_t> v2 = opt.getDuidTemplate();
  76. ASSERT_EQ(duid_llt_size, opt.getDuidTemplate().size());
  77. EXPECT_TRUE(std::equal(v2.begin(), v2.begin() + 4,
  78. duid_llt_and_hw));
  79. // Check time field contents.
  80. ptime now = microsec_clock::universal_time();
  81. ptime duid_epoch(from_iso_string("20000101T000000"));
  82. time_period period(duid_epoch, now);
  83. uint32_t duration_sec = period.length().total_seconds();
  84. // Read time from the template generated.
  85. uint32_t duration_from_template = 0;
  86. memcpy(&duration_from_template, &v2[4], 4);
  87. duration_from_template = htonl(duration_from_template);
  88. // In special cases, we may have overflow in time field
  89. // so we give ourselves the margin of 10 seconds here.
  90. // If time value has been set more then 10 seconds back
  91. // it is safe to compare it with the time value generated
  92. // from now.
  93. if (duration_from_template > 10) {
  94. EXPECT_GE(duration_sec, duration_from_template);
  95. }
  96. EXPECT_EQ(0, opt.getBase().size());
  97. EXPECT_EQ(0, opt.getNumRequests().size());
  98. EXPECT_EQ(0, opt.getPeriod());
  99. for (int i = 0; i < opt.getDropTime().size(); ++i) {
  100. EXPECT_DOUBLE_EQ(1, opt.getDropTime()[i]);
  101. }
  102. ASSERT_EQ(opt.getMaxDrop().size(), opt.getMaxDropPercentage().size());
  103. for (int i = 0; i < opt.getMaxDrop().size(); ++i) {
  104. EXPECT_EQ(0, opt.getMaxDrop()[i]);
  105. EXPECT_EQ(0, opt.getMaxDropPercentage()[i]);
  106. }
  107. EXPECT_EQ("", opt.getLocalName());
  108. EXPECT_FALSE(opt.isInterface());
  109. EXPECT_EQ(0, opt.getPreload());
  110. EXPECT_EQ(1, opt.getAggressivity());
  111. EXPECT_EQ(0, opt.getLocalPort());
  112. EXPECT_FALSE(opt.isSeeded());
  113. EXPECT_EQ(0, opt.getSeed());
  114. EXPECT_FALSE(opt.isBroadcast());
  115. EXPECT_FALSE(opt.isRapidCommit());
  116. EXPECT_FALSE(opt.isUseFirst());
  117. EXPECT_EQ(0, opt.getTemplateFiles().size());
  118. EXPECT_EQ(0, opt.getTransactionIdOffset().size());
  119. EXPECT_EQ(0, opt.getRandomOffset().size());
  120. EXPECT_GT(0, opt.getElapsedTimeOffset());
  121. EXPECT_GT(0, opt.getServerIdOffset());
  122. EXPECT_GT(0, opt.getRequestedIpOffset());
  123. EXPECT_EQ("", opt.getDiags());
  124. EXPECT_EQ("", opt.getWrapped());
  125. EXPECT_EQ("192.168.0.1", opt.getServerName());
  126. }
  127. };
  128. TEST_F(CommandOptionsTest, Defaults) {
  129. process("perfdhcp all");
  130. checkDefaults();
  131. }
  132. TEST_F(CommandOptionsTest, UseFirst) {
  133. CommandOptions& opt = CommandOptions::instance();
  134. process("perfdhcp -1 -B -l ethx all");
  135. EXPECT_TRUE(opt.isUseFirst());
  136. }
  137. TEST_F(CommandOptionsTest, IpVersion) {
  138. CommandOptions& opt = CommandOptions::instance();
  139. process("perfdhcp -6 -l ethx -c -i all");
  140. EXPECT_EQ(6, opt.getIpVersion());
  141. EXPECT_EQ("ethx", opt.getLocalName());
  142. EXPECT_TRUE(opt.isRapidCommit());
  143. EXPECT_FALSE(opt.isBroadcast());
  144. process("perfdhcp -4 -B -l ethx all");
  145. EXPECT_EQ(4, opt.getIpVersion());
  146. EXPECT_TRUE(opt.isBroadcast());
  147. EXPECT_FALSE(opt.isRapidCommit());
  148. // Negative test cases
  149. // -4 and -6 must not coexist
  150. EXPECT_THROW(process("perfdhcp -4 -6 -l ethx all"), isc::InvalidParameter);
  151. // -6 and -B must not coexist
  152. EXPECT_THROW(process("perfdhcp -6 -B -l ethx all"), isc::InvalidParameter);
  153. // -c and -4 (default) must not coexist
  154. EXPECT_THROW(process("perfdhcp -c -l ethx all"), isc::InvalidParameter);
  155. }
  156. TEST_F(CommandOptionsTest, Rate) {
  157. CommandOptions& opt = CommandOptions::instance();
  158. process("perfdhcp -4 -r 10 -l ethx all");
  159. EXPECT_EQ(10, opt.getRate());
  160. // Negative test cases
  161. // Rate must not be 0
  162. EXPECT_THROW(process("perfdhcp -4 -r 0 -l ethx all"),
  163. isc::InvalidParameter);
  164. // -r must be specified to use -n, -p and -D
  165. EXPECT_THROW(process("perfdhcp -6 -t 5 -l ethx all"),
  166. isc::InvalidParameter);
  167. EXPECT_THROW(process("perfdhcp -4 -n 150 -l ethx all"),
  168. isc::InvalidParameter);
  169. EXPECT_THROW(process("perfdhcp -6 -p 120 -l ethx all"),
  170. isc::InvalidParameter);
  171. EXPECT_THROW(process("perfdhcp -4 -D 1400 -l ethx all"),
  172. isc::InvalidParameter);
  173. }
  174. TEST_F(CommandOptionsTest, ReportDelay) {
  175. CommandOptions& opt = CommandOptions::instance();
  176. process("perfdhcp -r 100 -t 17 -l ethx all");
  177. EXPECT_EQ(17, opt.getReportDelay());
  178. // Negative test cases
  179. // -t must be positive integer
  180. EXPECT_THROW(process("perfdhcp -r 10 -t -8 -l ethx all"),
  181. isc::InvalidParameter);
  182. EXPECT_THROW(process("perfdhcp -r 10 -t 0 -l ethx all"),
  183. isc::InvalidParameter);
  184. EXPECT_THROW(process("perfdhcp -r 10 -t s -l ethx all"),
  185. isc::InvalidParameter);
  186. }
  187. TEST_F(CommandOptionsTest, ClientsNum) {
  188. CommandOptions& opt = CommandOptions::instance();
  189. process("perfdhcp -R 200 -l ethx all");
  190. EXPECT_EQ(200, opt.getClientsNum());
  191. process("perfdhcp -R 0 -l ethx all");
  192. EXPECT_EQ(0, opt.getClientsNum());
  193. // Negative test cases
  194. // Number of clients must be non-negative integer
  195. EXPECT_THROW(process("perfdhcp -R -5 -l ethx all"),
  196. isc::InvalidParameter);
  197. EXPECT_THROW(process("perfdhcp -R gs -l ethx all"),
  198. isc::InvalidParameter);
  199. }
  200. TEST_F(CommandOptionsTest, Base) {
  201. CommandOptions& opt = CommandOptions::instance();
  202. uint8_t mac[6] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60 };
  203. uint8_t duid[14] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  204. 0x01, 0x01, 0x01, 0x10, 0x11, 0x1F, 0x14 };
  205. // Test DUID and MAC together.
  206. EXPECT_NO_THROW(process("perfdhcp -b DUID=0101010101010101010110111F14"
  207. " -b MAC=10::20::30::40::50::60"
  208. " -l 127.0.0.1 all"));
  209. std::vector<uint8_t> v1 = opt.getMacTemplate();
  210. std::vector<uint8_t> v2 = opt.getDuidTemplate();
  211. v2 = opt.getDuidTemplate();
  212. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  213. EXPECT_TRUE(std::equal(v2.begin(), v2.end(), duid));
  214. // Test valid DUID.
  215. EXPECT_NO_THROW(
  216. process("perfdhcp -b duid=0101010101010101010110111F14 -l 127.0.0.1 all")
  217. );
  218. ASSERT_EQ(sizeof(duid) / sizeof(uint8_t), v2.size());
  219. EXPECT_TRUE(std::equal(v2.begin(), v2.end(), duid));
  220. // Test mix of upper/lower case letters.
  221. EXPECT_NO_THROW(process("perfdhcp -b DuiD=0101010101010101010110111F14"
  222. " -b Mac=10::20::30::40::50::60"
  223. " -l 127.0.0.1 all"));
  224. v1 = opt.getMacTemplate();
  225. v2 = opt.getDuidTemplate();
  226. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  227. EXPECT_TRUE(std::equal(v2.begin(), v2.end(), duid));
  228. // Use "ether" instead of "mac".
  229. EXPECT_NO_THROW(process("perfdhcp -b ether=10::20::30::40::50::60"
  230. " -l 127.0.0.1 all"));
  231. v1 = opt.getMacTemplate();
  232. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  233. // Use "ETHER" in upper case.
  234. EXPECT_NO_THROW(process("perfdhcp -b ETHER=10::20::30::40::50::60"
  235. " -l 127.0.0.1 all"));
  236. v1 = opt.getMacTemplate();
  237. EXPECT_TRUE(std::equal(v1.begin(), v1.end(), mac));
  238. // "t" is invalid character in DUID
  239. EXPECT_THROW(process("perfdhcp -6 -l ethx -b "
  240. "duid=010101010101010101t110111F14 all"),
  241. isc::InvalidParameter);
  242. // "3x" is invalid value in MAC address
  243. EXPECT_THROW(process("perfdhcp -b mac=10::2::3x::4::5::6 -l ethx all"),
  244. isc::InvalidParameter);
  245. // Base is not specified
  246. EXPECT_THROW(process("perfdhcp -b -l ethx all"),
  247. isc::InvalidParameter);
  248. // Typo: should be mac= instead of mc=
  249. EXPECT_THROW(process("perfdhcp -l ethx -b mc=00:01:02:03::04:05 all"),
  250. isc::InvalidParameter);
  251. // Too short DUID (< 6).
  252. EXPECT_THROW(process("perfdhcp -l ethx -b duid=00010203 all"),
  253. isc::InvalidParameter);
  254. // Odd number of digits.
  255. EXPECT_THROW(process("perfdhcp -l ethx -b duid=000102030405060 all"),
  256. isc::InvalidParameter);
  257. // Too short MAC (!= 6).
  258. EXPECT_THROW(process("perfdhcp -l ethx -b mac=00:01:02:04 all"),
  259. isc::InvalidParameter);
  260. }
  261. TEST_F(CommandOptionsTest, DropTime) {
  262. CommandOptions& opt = CommandOptions::instance();
  263. process("perfdhcp -l ethx -d 12 all");
  264. ASSERT_EQ(2, opt.getDropTime().size());
  265. EXPECT_DOUBLE_EQ(12, opt.getDropTime()[0]);
  266. EXPECT_DOUBLE_EQ(1, opt.getDropTime()[1]);
  267. process("perfdhcp -l ethx -d 2 -d 4.7 all");
  268. ASSERT_EQ(2, opt.getDropTime().size());
  269. EXPECT_DOUBLE_EQ(2, opt.getDropTime()[0]);
  270. EXPECT_DOUBLE_EQ(4.7, opt.getDropTime()[1]);
  271. // Negative test cases
  272. // Drop time must not be negative
  273. EXPECT_THROW(process("perfdhcp -l ethx -d -2 -d 4.7 all"),
  274. isc::InvalidParameter);
  275. EXPECT_THROW(process("perfdhcp -l ethx -d -9.1 -d 0 all"),
  276. isc::InvalidParameter);
  277. }
  278. TEST_F(CommandOptionsTest, TimeOffset) {
  279. CommandOptions& opt = CommandOptions::instance();
  280. process("perfdhcp -l ethx -T file1.x -T file2.x -E 4 all");
  281. EXPECT_EQ(4, opt.getElapsedTimeOffset());
  282. // Negative test cases
  283. // Argument -E must be used with -T
  284. EXPECT_THROW(process("perfdhcp -l ethx -E 3 -i all"),
  285. isc::InvalidParameter);
  286. // Value in -E not specified
  287. EXPECT_THROW(process("perfdhcp -l ethx -T file.x -E -i all"),
  288. isc::InvalidParameter);
  289. // Value for -E must not be negative
  290. EXPECT_THROW(process("perfdhcp -l ethx -E -3 -T file.x all"),
  291. isc::InvalidParameter);
  292. }
  293. TEST_F(CommandOptionsTest, ExchangeMode) {
  294. CommandOptions& opt = CommandOptions::instance();
  295. process("perfdhcp -l ethx -i all");
  296. EXPECT_EQ(CommandOptions::DO_SA, opt.getExchangeMode());
  297. // Negative test cases
  298. // No template file specified
  299. EXPECT_THROW(process("perfdhcp -i -l ethx -X 3 all"),
  300. isc::InvalidParameter);
  301. // Offsets can't be used in simple exchanges (-i)
  302. EXPECT_THROW(process("perfdhcp -i -l ethx -O 2 -T file.x all"),
  303. isc::InvalidParameter);
  304. EXPECT_THROW(process("perfdhcp -i -l ethx -E 3 -T file.x all"),
  305. isc::InvalidParameter);
  306. EXPECT_THROW(process("perfdhcp -i -l ethx -S 1 -T file.x all"),
  307. isc::InvalidParameter);
  308. EXPECT_THROW(process("perfdhcp -i -l ethx -I 2 -T file.x all"),
  309. isc::InvalidParameter);
  310. }
  311. TEST_F(CommandOptionsTest, Offsets) {
  312. CommandOptions& opt = CommandOptions::instance();
  313. process("perfdhcp -E5 -4 -I 2 -S3 -O 30 -X7 -l ethx "
  314. "-X3 -T file1.x -T file2.x all");
  315. EXPECT_EQ(2, opt.getRequestedIpOffset());
  316. EXPECT_EQ(5, opt.getElapsedTimeOffset());
  317. EXPECT_EQ(3, opt.getServerIdOffset());
  318. ASSERT_EQ(2, opt.getRandomOffset().size());
  319. EXPECT_EQ(30, opt.getRandomOffset()[0]);
  320. EXPECT_EQ(30, opt.getRandomOffset()[1]);
  321. ASSERT_EQ(2, opt.getTransactionIdOffset().size());
  322. EXPECT_EQ(7, opt.getTransactionIdOffset()[0]);
  323. EXPECT_EQ(3, opt.getTransactionIdOffset()[1]);
  324. // Negative test cases
  325. // IP offset/IA_NA offset must be positive
  326. EXPECT_THROW(process("perfdhcp -6 -I 0 -l ethx all"),
  327. isc::InvalidParameter);
  328. EXPECT_THROW(process("perfdhcp -6 -I -4 -l ethx all"),
  329. isc::InvalidParameter);
  330. // TODO - other negative cases
  331. }
  332. TEST_F(CommandOptionsTest, LocalPort) {
  333. CommandOptions& opt = CommandOptions::instance();
  334. process("perfdhcp -l ethx -L 2000 all");
  335. EXPECT_EQ(2000, opt.getLocalPort());
  336. // Negative test cases
  337. // Local port must be between 0..65535
  338. EXPECT_THROW(process("perfdhcp -l ethx -L -2 all"),
  339. isc::InvalidParameter);
  340. EXPECT_THROW(process("perfdhcp -l ethx -L all"),
  341. isc::InvalidParameter);
  342. EXPECT_THROW(process("perfdhcp -l ethx -L 65540 all"),
  343. isc::InvalidParameter);
  344. }
  345. TEST_F(CommandOptionsTest, Preload) {
  346. CommandOptions& opt = CommandOptions::instance();
  347. process("perfdhcp -1 -P 3 -l ethx all");
  348. EXPECT_EQ(3, opt.getPreload());
  349. // Negative test cases
  350. // Number of preload packages must not be negative integer
  351. EXPECT_THROW(process("perfdhcp -P -1 -l ethx all"),
  352. isc::InvalidParameter);
  353. EXPECT_THROW(process("perfdhcp -P -3 -l ethx all"),
  354. isc::InvalidParameter);
  355. }
  356. TEST_F(CommandOptionsTest, Seed) {
  357. CommandOptions& opt = CommandOptions::instance();
  358. process("perfdhcp -6 -P 2 -s 23 -l ethx all");
  359. EXPECT_EQ(23, opt.getSeed());
  360. EXPECT_TRUE(opt.isSeeded());
  361. process("perfdhcp -6 -P 2 -s 0 -l ethx all");
  362. EXPECT_EQ(0, opt.getSeed());
  363. EXPECT_FALSE(opt.isSeeded());
  364. // Negtaive test cases
  365. // Seed must be non-negative integer
  366. EXPECT_THROW(process("perfdhcp -6 -P 2 -s -5 -l ethx all"),
  367. isc::InvalidParameter);
  368. EXPECT_THROW(process("perfdhcp -6 -P 2 -s -l ethx all"),
  369. isc::InvalidParameter);
  370. }
  371. TEST_F(CommandOptionsTest, TemplateFiles) {
  372. CommandOptions& opt = CommandOptions::instance();
  373. process("perfdhcp -T file1.x -l ethx all");
  374. ASSERT_EQ(1, opt.getTemplateFiles().size());
  375. EXPECT_EQ("file1.x", opt.getTemplateFiles()[0]);
  376. process("perfdhcp -T file1.x -s 12 -w start -T file2.x -4 -l ethx all");
  377. ASSERT_EQ(2, opt.getTemplateFiles().size());
  378. EXPECT_EQ("file1.x", opt.getTemplateFiles()[0]);
  379. EXPECT_EQ("file2.x", opt.getTemplateFiles()[1]);
  380. // Negative test cases
  381. // No template file specified
  382. EXPECT_THROW(process("perfdhcp -s 12 -T -l ethx all"),
  383. isc::InvalidParameter);
  384. // Too many template files specified
  385. EXPECT_THROW(process("perfdhcp -s 12 -l ethx -T file.x "
  386. "-T file.x -T file.x all"),
  387. isc::InvalidParameter);
  388. }
  389. TEST_F(CommandOptionsTest, Wrapped) {
  390. CommandOptions& opt = CommandOptions::instance();
  391. process("perfdhcp -B -w start -i -l ethx all");
  392. EXPECT_EQ("start", opt.getWrapped());
  393. // Negative test cases
  394. // Missing command after -w, expected start/stop
  395. EXPECT_THROW(process("perfdhcp -B -i -l ethx -w all"),
  396. isc::InvalidParameter);
  397. }
  398. TEST_F(CommandOptionsTest, Diagnostics) {
  399. CommandOptions& opt = CommandOptions::instance();
  400. process("perfdhcp -l ethx -i -x asTe all");
  401. EXPECT_EQ("asTe", opt.getDiags());
  402. // Negative test cases
  403. // No diagnostics string specified
  404. EXPECT_THROW(process("perfdhcp -l ethx -i -x all"),
  405. isc::InvalidParameter);
  406. }
  407. TEST_F(CommandOptionsTest, Aggressivity) {
  408. CommandOptions& opt = CommandOptions::instance();
  409. process("perfdhcp -a 10 -l 192.168.0.1 all");
  410. EXPECT_EQ(10, opt.getAggressivity());
  411. // Negative test cases
  412. // Aggressivity must be non negative integer
  413. EXPECT_THROW(process("perfdhcp -l ethx -a 0 all"),
  414. isc::InvalidParameter);
  415. EXPECT_THROW(process("perfdhcp -l ethx -a all"),
  416. isc::InvalidParameter);
  417. EXPECT_THROW(process("perfdhcp -a -2 -l ethx -a 3 all"),
  418. isc::InvalidParameter);
  419. }
  420. TEST_F(CommandOptionsTest, MaxDrop) {
  421. CommandOptions& opt = CommandOptions::instance();
  422. process("perfdhcp -D 25 -l ethx -r 10 all");
  423. EXPECT_EQ(25, opt.getMaxDrop()[0]);
  424. process("perfdhcp -D 25 -l ethx -D 15 -r 10 all");
  425. EXPECT_EQ(25, opt.getMaxDrop()[0]);
  426. EXPECT_EQ(15, opt.getMaxDrop()[1]);
  427. process("perfdhcp -D 15% -l ethx -r 10 all");
  428. EXPECT_EQ(15, opt.getMaxDropPercentage()[0]);
  429. process("perfdhcp -D 15% -D25% -l ethx -r 10 all");
  430. EXPECT_EQ(15, opt.getMaxDropPercentage()[0]);
  431. EXPECT_EQ(25, opt.getMaxDropPercentage()[1]);
  432. process("perfdhcp -D 1% -D 99% -l ethx -r 10 all");
  433. EXPECT_EQ(1, opt.getMaxDropPercentage()[0]);
  434. EXPECT_EQ(99, opt.getMaxDropPercentage()[1]);
  435. // Negative test cases
  436. // Too many -D<value> options
  437. EXPECT_THROW(process("perfdhcp -D 0% -D 1 -l ethx -r20 -D 3 all"),
  438. isc::InvalidParameter);
  439. // Too many -D<value%> options
  440. EXPECT_THROW(process("perfdhcp -D 99% -D 13% -l ethx -r20 -D 10% all"),
  441. isc::InvalidParameter);
  442. // Percentage is out of bounds
  443. EXPECT_THROW(process("perfdhcp -D101% -D 13% -l ethx -r20 all"),
  444. isc::InvalidParameter);
  445. EXPECT_THROW(process("perfdhcp -D0% -D 13% -l ethx -r20 all"),
  446. isc::InvalidParameter);
  447. }
  448. TEST_F(CommandOptionsTest, NumRequest) {
  449. CommandOptions& opt = CommandOptions::instance();
  450. process("perfdhcp -n 1000 -r 10 -l ethx all");
  451. EXPECT_EQ(1000, opt.getNumRequests()[0]);
  452. process("perfdhcp -n 5 -r 10 -n 500 -l ethx all");
  453. EXPECT_EQ(5, opt.getNumRequests()[0]);
  454. EXPECT_EQ(500, opt.getNumRequests()[1]);
  455. // Negative test cases
  456. // Too many -n<value> parameters, expected maximum 2
  457. EXPECT_THROW(process("perfdhcp -n 1 -n 2 -l ethx -n3 -r 20 all"),
  458. isc::InvalidParameter);
  459. // Num request must be positive integer
  460. EXPECT_THROW(process("perfdhcp -n 1 -n -22 -l ethx -r 10 all"),
  461. isc::InvalidParameter);
  462. EXPECT_THROW(process("perfdhcp -n 0 -l ethx -r 10 all"),
  463. isc::InvalidParameter);
  464. }
  465. TEST_F(CommandOptionsTest, Period) {
  466. CommandOptions& opt = CommandOptions::instance();
  467. process("perfdhcp -p 120 -l ethx -r 100 all");
  468. EXPECT_EQ(120, opt.getPeriod());
  469. // Negative test cases
  470. // Test period must be positive integer
  471. EXPECT_THROW(process("perfdhcp -p 0 -l ethx -r 50 all"),
  472. isc::InvalidParameter);
  473. EXPECT_THROW(process("perfdhcp -p -3 -l ethx -r 50 all"),
  474. isc::InvalidParameter);
  475. }
  476. TEST_F(CommandOptionsTest, Interface) {
  477. // In order to make this test portable we need to know
  478. // at least one interface name on OS where test is run.
  479. // Interface Manager has ability to detect interfaces.
  480. // Altough we don't call initIsInterface explicitely
  481. // here it is called by CommandOptions object interally
  482. // so this function is covered by the test.
  483. dhcp::IfaceMgr& iface_mgr = dhcp::IfaceMgr::instance();
  484. const dhcp::IfaceMgr::IfaceCollection& ifaces = iface_mgr.getIfaces();
  485. std::string iface_name;
  486. CommandOptions& opt = CommandOptions::instance();
  487. // The local loopback interface should be available.
  488. // If no interface have been found for any reason we should
  489. // not fail this test.
  490. if (ifaces.size() > 0) {
  491. // Get the name of the interface we detected.
  492. iface_name = ifaces.begin()->getName();
  493. // Use the name in the command parser.
  494. ASSERT_NO_THROW(process("perfdhcp -4 -l " + iface_name + " abc"));
  495. // We expect that command parser will detect that argument
  496. // specified along with '-l' is the interface name.
  497. EXPECT_TRUE(opt.isInterface());
  498. // If neither interface nor server is specified then
  499. // exception is expected to be thrown.
  500. EXPECT_THROW(process("perfdhcp -4"), isc::InvalidParameter);
  501. }
  502. }
  503. TEST_F(CommandOptionsTest, Server) {
  504. CommandOptions& opt = CommandOptions::instance();
  505. // There is at least server parameter needed. If server is not
  506. // specified the local interface must be specified.
  507. // The server value equal to 'all' means use broadcast.
  508. ASSERT_NO_THROW(process("perfdhcp all"));
  509. // Once command line is parsed we expect that server name is
  510. // set to broadcast address because 'all' was specified.
  511. EXPECT_TRUE(opt.isBroadcast());
  512. // The broadcast address is 255.255.255.255.
  513. EXPECT_EQ("255.255.255.255", opt.getServerName());
  514. // When all is specified for DHCPv6 mode we expect
  515. // FF02::1:2 as a server name which means All DHCP
  516. // servers and relay agents in local network segment
  517. ASSERT_NO_THROW(process("perfdhcp -6 all"));
  518. EXPECT_EQ("FF02::1:2", opt.getServerName());
  519. // When server='servers' in DHCPv6 mode we expect
  520. // FF05::1:3 as server name which means All DHCP
  521. // servers in local network.
  522. ASSERT_NO_THROW(process("perfdhcp -6 servers"));
  523. EXPECT_EQ("FF05::1:3", opt.getServerName());
  524. // If server name is neither 'all' nor 'servers'
  525. // the given argument value is expected to be
  526. // returned.
  527. ASSERT_NO_THROW(process("perfdhcp -6 abc"));
  528. EXPECT_EQ("abc", opt.getServerName());
  529. }