dora_unittest.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // Copyright (C) 2014-2015 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 <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <cc/data.h>
  17. #include <dhcp/dhcp4.h>
  18. #include <dhcp/tests/iface_mgr_test_config.h>
  19. #include <dhcpsrv/cfgmgr.h>
  20. #include <dhcpsrv/host.h>
  21. #include <dhcpsrv/host_mgr.h>
  22. #include <dhcpsrv/subnet_id.h>
  23. #include <dhcp4/tests/dhcp4_test_utils.h>
  24. #include <dhcp4/tests/dhcp4_client.h>
  25. #include <boost/shared_ptr.hpp>
  26. using namespace isc;
  27. using namespace isc::asiolink;
  28. using namespace isc::data;
  29. using namespace isc::dhcp;
  30. using namespace isc::dhcp::test;
  31. namespace {
  32. /// @brief Set of JSON configurations used throughout the DORA tests.
  33. ///
  34. /// - Configuration 0:
  35. /// - Used for testing direct traffic
  36. /// - 1 subnet: 10.0.0.0/24
  37. /// - 1 pool: 10.0.0.10-10.0.0.100
  38. /// - Router option present: 10.0.0.200 and 10.0.0.201
  39. /// - Domain Name Server option present: 10.0.0.202, 10.0.0.203.
  40. /// - Log Servers option present: 192.0.2.200 and 192.0.2.201
  41. /// - Quotes Servers option present: 192.0.2.202, 192.0.2.203.
  42. ///
  43. /// - Configuration 1:
  44. /// - Use for testing relayed messages
  45. /// - 1 subnet: 192.0.2.0/24
  46. /// - Router option present: 192.0.2.200 and 192.0.2.201
  47. /// - Domain Name Server option present: 192.0.2.202, 192.0.2.203.
  48. /// - Log Servers option present: 192.0.2.200 and 192.0.2.201
  49. /// - Quotes Servers option present: 192.0.2.202, 192.0.2.203.
  50. ///
  51. /// - Configuration 2:
  52. /// - Use for testing simple scenarios with host reservations
  53. /// - 1 subnet: 10.0.0.0/24
  54. /// - One reservation for the client using MAC address:
  55. /// aa:bb:cc:dd:ee:ff, reserved address 10.0.0.7
  56. const char* DORA_CONFIGS[] = {
  57. // Configuration 0
  58. "{ \"interfaces-config\": {"
  59. " \"interfaces\": [ \"*\" ]"
  60. "},"
  61. "\"valid-lifetime\": 600,"
  62. "\"subnet4\": [ { "
  63. " \"subnet\": \"10.0.0.0/24\", "
  64. " \"id\": 1,"
  65. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  66. " \"option-data\": [ {"
  67. " \"name\": \"routers\","
  68. " \"code\": 3,"
  69. " \"data\": \"10.0.0.200,10.0.0.201\","
  70. " \"csv-format\": true,"
  71. " \"space\": \"dhcp4\""
  72. " },"
  73. " {"
  74. " \"name\": \"domain-name-servers\","
  75. " \"code\": 6,"
  76. " \"data\": \"10.0.0.202,10.0.0.203\","
  77. " \"csv-format\": true,"
  78. " \"space\": \"dhcp4\""
  79. " },"
  80. " {"
  81. " \"name\": \"log-servers\","
  82. " \"code\": 7,"
  83. " \"data\": \"10.0.0.200,10.0.0.201\","
  84. " \"csv-format\": true,"
  85. " \"space\": \"dhcp4\""
  86. " },"
  87. " {"
  88. " \"name\": \"cookie-servers\","
  89. " \"code\": 8,"
  90. " \"data\": \"10.0.0.202,10.0.0.203\","
  91. " \"csv-format\": true,"
  92. " \"space\": \"dhcp4\""
  93. " } ]"
  94. " } ]"
  95. "}",
  96. // Configuration 1
  97. "{ \"interfaces-config\": {"
  98. " \"interfaces\": [ \"*\" ]"
  99. "},"
  100. "\"valid-lifetime\": 600,"
  101. "\"subnet4\": [ { "
  102. " \"subnet\": \"192.0.2.0/24\", "
  103. " \"option-data\": [ {"
  104. " \"name\": \"routers\","
  105. " \"code\": 3,"
  106. " \"data\": \"192.0.2.200,192.0.2.201\","
  107. " \"csv-format\": true,"
  108. " \"space\": \"dhcp4\""
  109. " },"
  110. " {"
  111. " \"name\": \"domain-name-servers\","
  112. " \"code\": 6,"
  113. " \"data\": \"192.0.2.202,192.0.2.203\","
  114. " \"csv-format\": true,"
  115. " \"space\": \"dhcp4\""
  116. " },"
  117. " {"
  118. " \"name\": \"log-servers\","
  119. " \"code\": 7,"
  120. " \"data\": \"10.0.0.200,10.0.0.201\","
  121. " \"csv-format\": true,"
  122. " \"space\": \"dhcp4\""
  123. " },"
  124. " {"
  125. " \"name\": \"cookie-servers\","
  126. " \"code\": 8,"
  127. " \"data\": \"10.0.0.202,10.0.0.203\","
  128. " \"csv-format\": true,"
  129. " \"space\": \"dhcp4\""
  130. " } ]"
  131. " } ]"
  132. "}",
  133. // Configuration 2
  134. "{ \"interfaces-config\": {"
  135. " \"interfaces\": [ \"*\" ]"
  136. "},"
  137. "\"valid-lifetime\": 600,"
  138. "\"subnet4\": [ { "
  139. " \"subnet\": \"10.0.0.0/24\", "
  140. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  141. " \"reservations\": [ "
  142. " {"
  143. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  144. " \"ip-address\": \"10.0.0.7\""
  145. " }"
  146. " ]"
  147. "} ]"
  148. "}"
  149. };
  150. /// @brief Test fixture class for testing 4-way (DORA) exchanges.
  151. ///
  152. /// @todo Currently there is a limit number of test cases covered here.
  153. /// In the future it is planned that the tests from the
  154. /// dhcp4_srv_unittest.cc will be migrated here and will use the
  155. /// @c Dhcp4Client class.
  156. class DORATest : public Dhcpv4SrvTest {
  157. public:
  158. /// @brief Constructor.
  159. ///
  160. /// Sets up fake interfaces.
  161. DORATest()
  162. : Dhcpv4SrvTest(),
  163. iface_mgr_test_config_(true) {
  164. IfaceMgr::instance().openSockets4();
  165. }
  166. /// @brief Interface Manager's fake configuration control.
  167. IfaceMgrTestConfig iface_mgr_test_config_;
  168. };
  169. /// This test verifies that the client in the SELECTING state can get
  170. /// an address when it doesn't request any specific address in the
  171. /// DHCPDISCOVER message.
  172. TEST_F(DORATest, selectingDoNotRequestAddress) {
  173. Dhcp4Client client(Dhcp4Client::SELECTING);
  174. // Configure DHCP server.
  175. configure(DORA_CONFIGS[0], *client.getServer());
  176. // Perform 4-way exchange with the server but to not request any
  177. // specific address in the DHCPDISCOVER message.
  178. ASSERT_NO_THROW(client.doDORA());
  179. // Make sure that the server responded.
  180. ASSERT_TRUE(client.getContext().response_);
  181. Pkt4Ptr resp = client.getContext().response_;
  182. // Make sure that the server has responded with DHCPACK.
  183. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  184. // Response must not be relayed.
  185. EXPECT_FALSE(resp->isRelayed());
  186. // Make sure that the server id is present.
  187. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  188. // Make sure that the client has got the lease with the requested address.
  189. ASSERT_NE(client.config_.lease_.addr_.toText(), "0.0.0.0");
  190. }
  191. /// This test verifies that multiple clients may use the DHCPv4 server
  192. /// and obtain unique leases.
  193. TEST_F(DORATest, selectingMultipleClients) {
  194. Dhcp4Client client(Dhcp4Client::SELECTING);
  195. // Configure DHCP server.
  196. configure(DORA_CONFIGS[0], *client.getServer());
  197. // Get the first lease.
  198. ASSERT_NO_THROW(client.doDORA());
  199. // Make sure that the server responded.
  200. Pkt4Ptr resp = client.getContext().response_;
  201. ASSERT_TRUE(resp);
  202. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  203. // Store the lease.
  204. Lease4 lease1 = client.config_.lease_;
  205. // Get the lease for a different client.
  206. client.modifyHWAddr();
  207. ASSERT_NO_THROW(client.doDORA());
  208. // Make sure that the server responded.
  209. resp = client.getContext().response_;
  210. ASSERT_TRUE(resp);
  211. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  212. // Store the lease.
  213. Lease4 lease2 = client.config_.lease_;
  214. // Get the lease for a different client.
  215. client.modifyHWAddr();
  216. ASSERT_NO_THROW(client.doDORA());
  217. // Make sure that the server responded.
  218. resp = client.getContext().response_;
  219. ASSERT_TRUE(resp);
  220. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  221. // Store the lease.
  222. Lease4 lease3 = client.config_.lease_;
  223. // Make sure that unique addresses have been assigned.
  224. EXPECT_NE(lease1.addr_, lease2.addr_);
  225. EXPECT_NE(lease2.addr_, lease3.addr_);
  226. EXPECT_NE(lease1.addr_, lease3.addr_);
  227. }
  228. // This test verifies that the client in a SELECTING state can request
  229. // a specific address and that this address will be assigned when
  230. // available. It also tests that if the client requests an address which
  231. // is in use the client will get a different address.
  232. TEST_F(DORATest, selectingRequestAddress) {
  233. Dhcp4Client client(Dhcp4Client::SELECTING);
  234. // Configure DHCP server.
  235. configure(DORA_CONFIGS[0], *client.getServer());
  236. // Perform 4-way exchange with the server.
  237. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  238. IOAddress>(new IOAddress("10.0.0.50"))));
  239. // Make sure that the server responded.
  240. ASSERT_TRUE(client.getContext().response_);
  241. Pkt4Ptr resp = client.getContext().response_;
  242. // Make sure that the server has responded with DHCPACK.
  243. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  244. // Response must not be relayed.
  245. EXPECT_FALSE(resp->isRelayed());
  246. // Make sure that the server id is present.
  247. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  248. // Make sure that the client has got the lease with the requested address.
  249. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  250. // Simulate different client requesting the same address.
  251. client.modifyHWAddr();
  252. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  253. IOAddress>(new IOAddress("10.0.0.50"))));
  254. resp = client.getContext().response_;
  255. // Make sure that the server responded.
  256. ASSERT_TRUE(resp);
  257. // Make sure that the server has responded with DHCPACK.
  258. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  259. // Response must not be relayed.
  260. EXPECT_FALSE(resp->isRelayed());
  261. // Make sure that the server id is present.
  262. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  263. // Make sure that the client has got some address.
  264. EXPECT_NE(client.config_.lease_.addr_.toText(), "0.0.0.0");
  265. // Make sure that the client has got a different address than requested
  266. // as the requested one is already in use.
  267. EXPECT_NE(client.config_.lease_.addr_.toText(), "10.0.0.50");
  268. }
  269. // This test verifies that the client will get the address that it has
  270. // been allocated when the client requests a different address.
  271. TEST_F(DORATest, selectingRequestNonMatchingAddress) {
  272. Dhcp4Client client(Dhcp4Client::SELECTING);
  273. // Configure DHCP server.
  274. configure(DORA_CONFIGS[0], *client.getServer());
  275. // Perform 4-way exchange with the server.
  276. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  277. IOAddress>(new IOAddress("10.0.0.50"))));
  278. // Make sure that the server responded.
  279. ASSERT_TRUE(client.getContext().response_);
  280. Pkt4Ptr resp = client.getContext().response_;
  281. // Make sure that the server has responded with DHCPACK.
  282. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  283. // Response must not be relayed.
  284. EXPECT_FALSE(resp->isRelayed());
  285. // Make sure that the server id is present.
  286. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  287. // Make sure that the client has got the lease with the requested address.
  288. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  289. // Let's request a different address. The server should respond with
  290. // the one that the client already has allocated.
  291. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  292. IOAddress>(new IOAddress("10.0.0.80"))));
  293. // Make sure that the server responded.
  294. ASSERT_TRUE(client.getContext().response_);
  295. resp = client.getContext().response_;
  296. // Make sure that the server has responded with DHCPACK.
  297. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  298. // Response must not be relayed.
  299. EXPECT_FALSE(resp->isRelayed());
  300. // Make sure that the server id is present.
  301. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  302. // Make sure that the client has got the lease with the address that
  303. // the client has recorded in the lease database.
  304. EXPECT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  305. }
  306. // Test that the client in the INIT-REBOOT state can request the IP
  307. // address it has and the address is returned. Also, check that if
  308. // if the client requests invalid address the server sends a DHCPNAK.
  309. TEST_F(DORATest, initRebootRequest) {
  310. Dhcp4Client client(Dhcp4Client::SELECTING);
  311. // Configure DHCP server.
  312. configure(DORA_CONFIGS[0], *client.getServer());
  313. // Obtain a lease from the server using the 4-way exchange.
  314. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  315. IOAddress>(new IOAddress("10.0.0.50"))));
  316. // Make sure that the server responded.
  317. ASSERT_TRUE(client.getContext().response_);
  318. Pkt4Ptr resp = client.getContext().response_;
  319. // Make sure that the server has responded with DHCPACK.
  320. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  321. // Response must not be relayed.
  322. EXPECT_FALSE(resp->isRelayed());
  323. // Make sure that the server id is present.
  324. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  325. // Make sure that the client has got the lease with the requested address.
  326. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  327. // Client has a lease in the database. Let's transition the client
  328. // to the INIT_REBOOT state so as the client can request the cached
  329. // lease using the DHCPREQUEST message.
  330. client.setState(Dhcp4Client::INIT_REBOOT);
  331. ASSERT_NO_THROW(client.doRequest());
  332. // Make sure that the server responded.
  333. ASSERT_TRUE(client.getContext().response_);
  334. resp = client.getContext().response_;
  335. // Make sure that the server has responded with DHCPACK.
  336. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  337. // Response must not be relayed.
  338. EXPECT_FALSE(resp->isRelayed());
  339. // Make sure that the server id is present.
  340. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  341. // Make sure that the client has got the lease with the requested address.
  342. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  343. // Try to request a different address than the client has. The server
  344. // should respond with DHCPNAK.
  345. client.config_.lease_.addr_ = IOAddress("10.0.0.30");
  346. ASSERT_NO_THROW(client.doRequest());
  347. // Make sure that the server responded.
  348. ASSERT_TRUE(client.getContext().response_);
  349. resp = client.getContext().response_;
  350. EXPECT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  351. // Try to request from a different client.
  352. client.modifyHWAddr();
  353. ASSERT_NO_THROW(client.doRequest());
  354. // The server should not respond.
  355. EXPECT_FALSE(client.getContext().response_);
  356. }
  357. // Check that the ciaddr returned by the server is correct for DHCPOFFER and
  358. // DHCPNAK according to RFC2131, section 4.3.1.
  359. TEST_F(DORATest, ciaddr) {
  360. Dhcp4Client client(Dhcp4Client::SELECTING);
  361. // Configure DHCP server.
  362. configure(DORA_CONFIGS[0], *client.getServer());
  363. // Force ciaddr of Discover message to be non-zero.
  364. client.ciaddr_.specify(IOAddress("10.0.0.50"));
  365. // Obtain a lease from the server using the 4-way exchange.
  366. ASSERT_NO_THROW(client.doDiscover(boost::shared_ptr<
  367. IOAddress>(new IOAddress("10.0.0.50"))));
  368. // Make sure that the server responded.
  369. ASSERT_TRUE(client.getContext().response_);
  370. Pkt4Ptr resp = client.getContext().response_;
  371. // Make sure that the server has responded with DHCPOFFER.
  372. ASSERT_EQ(DHCPOFFER, static_cast<int>(resp->getType()));
  373. // Make sure ciaddr is not set for DHCPOFFER.
  374. EXPECT_EQ("0.0.0.0", resp->getCiaddr().toText());
  375. // Obtain a lease from the server using the 4-way exchange.
  376. ASSERT_NO_THROW(client.doRequest());
  377. // Make sure that the server responded.
  378. ASSERT_TRUE(client.getContext().response_);
  379. resp = client.getContext().response_;
  380. // Make sure that the server has responded with DHCPACK.
  381. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  382. // Let's transition the client to Renewing state.
  383. client.setState(Dhcp4Client::RENEWING);
  384. // Set the unicast destination address to indicate that it is a renewal.
  385. client.setDestAddress(IOAddress("10.0.0.1"));
  386. ASSERT_NO_THROW(client.doRequest());
  387. // The client is sending invalid ciaddr so the server should send a NAK.
  388. resp = client.getContext().response_;
  389. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  390. // For DHCPACK the ciaddr may be 0 or may be set to the ciaddr value
  391. // from the client's message. Kea sets it to the latter.
  392. EXPECT_EQ("10.0.0.50", resp->getCiaddr().toText());
  393. // Replace the address held by the client. The client will request
  394. // the assignment of this address but the server has a different
  395. // address for this client.
  396. client.ciaddr_.specify(IOAddress("192.168.0.30"));
  397. ASSERT_NO_THROW(client.doRequest());
  398. // The client is sending invalid ciaddr so the server should send a NAK.
  399. resp = client.getContext().response_;
  400. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  401. // For DHCPNAK the ciaddr is always 0 (should not be copied) from the
  402. // client's message.
  403. EXPECT_EQ("0.0.0.0", resp->getCiaddr().toText());
  404. }
  405. TEST_F(DORATest, overlappingClientId) {
  406. Dhcp4Client clientA(Dhcp4Client::SELECTING);
  407. clientA.includeClientId("12:34");
  408. configure(DORA_CONFIGS[0], *clientA.getServer());
  409. ASSERT_NO_THROW(clientA.doDORA());
  410. // Make sure that the server responded.
  411. ASSERT_TRUE(clientA.getContext().response_);
  412. Pkt4Ptr respA = clientA.getContext().response_;
  413. // Make sure that the server has responded with DHCPACK.
  414. ASSERT_EQ(DHCPACK, static_cast<int>(respA->getType()));
  415. Dhcp4Client clientB(clientA.getServer(), Dhcp4Client::SELECTING);
  416. clientB.includeClientId("12:34");
  417. ASSERT_NO_THROW(clientB.doDiscover());
  418. Pkt4Ptr respB = clientB.getContext().response_;
  419. // Make sure that the server has responded with DHCPOFFER.
  420. ASSERT_EQ(DHCPOFFER, static_cast<int>(respB->getType()));
  421. EXPECT_NE(clientA.config_.lease_.addr_, respB->getYiaddr());
  422. }
  423. // This is a simple test for the host reservation. It creates a reservation
  424. // for an address for a single client, identified by the HW address. The
  425. // test verifies that the client using this HW address will obtain a
  426. // lease for the reserved address. It also checks that the client using
  427. // a different HW address will obtain an address from the dynamic pool.
  428. TEST_F(DORATest, reservation) {
  429. // Client A is a one which will have a reservation.
  430. Dhcp4Client clientA(Dhcp4Client::SELECTING);
  431. // Set explicit HW address so as it matches the reservation in the
  432. // configuration used below.
  433. clientA.setHWAddress("aa:bb:cc:dd:ee:ff");
  434. // Configure DHCP server.
  435. configure(DORA_CONFIGS[2], *clientA.getServer());
  436. // Client A performs 4-way exchange and should obtain a reserved
  437. // address.
  438. ASSERT_NO_THROW(clientA.doDORA(boost::shared_ptr<
  439. IOAddress>(new IOAddress("0.0.0.0"))));
  440. // Make sure that the server responded.
  441. ASSERT_TRUE(clientA.getContext().response_);
  442. Pkt4Ptr resp = clientA.getContext().response_;
  443. // Make sure that the server has responded with DHCPACK.
  444. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  445. // Make sure that the client has got the lease for the reserved address.
  446. ASSERT_EQ("10.0.0.7", clientA.config_.lease_.addr_.toText());
  447. // Client B uses the same server as Client A.
  448. Dhcp4Client clientB(clientA.getServer(), Dhcp4Client::SELECTING);
  449. // Client B has no reservation so it should get the lease from
  450. // the dynamic pool.
  451. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  452. IOAddress>(new IOAddress("0.0.0.0"))));
  453. // Make sure that the server responded.
  454. ASSERT_TRUE(clientB.getContext().response_);
  455. resp = clientB.getContext().response_;
  456. // Make sure that the server has responded with DHCPACK.
  457. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  458. // Obtain the subnet to which the returned address belongs.
  459. Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  460. selectSubnet(clientB.config_.lease_.addr_);
  461. ASSERT_TRUE(subnet);
  462. // Make sure that the address has been allocated from the dynamic pool.
  463. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, clientB.config_.lease_.addr_));
  464. }
  465. // This test checks the following scenario:
  466. // 1. Client A performs 4-way exchange and obtains a lease from the dynamic pool.
  467. // 2. Reservation is created for the client A using an address out of the dynamic
  468. // pool.
  469. // 3. Client A renews the lease.
  470. // 4. Server responds with DHCPNAK to indicate that the client should stop using
  471. // an address for which it has a lease. Server doesn't want to renew an
  472. // address for which the client doesn't have a reservation, while it has
  473. // a reservation for a different address.
  474. // 5. Client A receives a DHCPNAK and returns to the DHCP server discovery.
  475. // 6. Client A performs a 4-way exchange with a server and the server allocates
  476. // a reserved address to the Client A.
  477. // 7. Client A renews the allocated address and the server returns a DHCPACK.
  478. // 8. Reservation for the Client A is removed.
  479. // 9. Client A renews the (previously reserved) lease and the server returns
  480. // DHCPNAK because the address in use is neither reserved nor belongs to
  481. // the dynamic pool.
  482. // 10. Client A returns to the DHCP server discovery.
  483. // 11. Client A uses 4-way exchange to obtain a lease from the dynamic pool.
  484. // 12. The new address that the Client A is using is reserved for Client B.
  485. // Client A still holds this address.
  486. // 13. Client B uses 4-way exchange to obtain a new lease.
  487. // 14. The server determines that the Client B has a reservation for the
  488. // address which is in use by Client A and offers an address different
  489. // than reserved.
  490. // 15. Client B requests the allocation of the offered address and the server
  491. // allocates this address.
  492. // 16. Client A renews the lease.
  493. // 17. The server determines that the address that Client A is using is reserved
  494. // for Client B. The server returns DHCPNAK to the Client A.
  495. // 18. Client B uses 4-way exchange to obtain the reserved lease but the lease
  496. // for the Client A hasn't been removed yet. Client B is assigned the same
  497. // address it has been using.
  498. // 19. Client A uses 4-way exchange to allocate a new lease.
  499. // 20. The server allocates a new lease from the dynamic pool but it avoids
  500. // allocating the address reserved for the Client B.
  501. // 21. Client B uses 4-way exchange to obtain a new lease.
  502. // 22. The server finally allocates a reserved address to the Client B.
  503. TEST_F(DORATest, reservationsWithConflicts) {
  504. Dhcp4Client client(Dhcp4Client::SELECTING);
  505. // Configure DHCP server.
  506. configure(DORA_CONFIGS[0], *client.getServer());
  507. // Client A performs 4-way exchange and obtains a lease from the
  508. // dynamic pool.
  509. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  510. IOAddress>(new IOAddress("10.0.0.50"))));
  511. // Make sure that the server responded.
  512. ASSERT_TRUE(client.getContext().response_);
  513. Pkt4Ptr resp = client.getContext().response_;
  514. // Make sure that the server has responded with DHCPACK.
  515. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  516. // Make sure that the client has got the lease with the requested address.
  517. ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText());
  518. configure(DORA_CONFIGS[0], false);
  519. // Reservation is created for the client A using an address out of the
  520. // dynamic pool.
  521. HostPtr host(new Host(&client.getHWAddress()->hwaddr_[0],
  522. client.getHWAddress()->hwaddr_.size(),
  523. Host::IDENT_HWADDR, SubnetID(1),
  524. SubnetID(0), IOAddress("10.0.0.9")));
  525. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  526. CfgMgr::instance().commit();
  527. // Let's transition the client to Renewing state.
  528. client.setState(Dhcp4Client::RENEWING);
  529. // Set the unicast destination address to indicate that it is a renewal.
  530. client.setDestAddress(IOAddress("10.0.0.1"));
  531. ASSERT_NO_THROW(client.doRequest());
  532. // Client should get the DHCPNAK from the server because the client has
  533. // a reservation for a different address that it is trying to renew.
  534. resp = client.getContext().response_;
  535. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  536. // A conforming client would go back to the server discovery.
  537. client.setState(Dhcp4Client::SELECTING);
  538. // Obtain a lease from the server using the 4-way exchange.
  539. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  540. IOAddress>(new IOAddress("0.0.0.0"))));
  541. // Make sure that the server responded.
  542. ASSERT_TRUE(client.getContext().response_);
  543. resp = client.getContext().response_;
  544. // Make sure that the server has responded with DHCPACK with a reserved
  545. // address
  546. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  547. ASSERT_EQ("10.0.0.9", client.config_.lease_.addr_.toText());
  548. // Client A renews the allocated address.
  549. client.setState(Dhcp4Client::RENEWING);
  550. // Set the unicast destination address to indicate that it is a renewal.
  551. client.setDestAddress(IOAddress("10.0.0.1"));
  552. ASSERT_NO_THROW(client.doRequest());
  553. // Make sure the server responded and renewed the client's address.
  554. resp = client.getContext().response_;
  555. ASSERT_EQ("10.0.0.9", client.config_.lease_.addr_.toText());
  556. // By reconfiguring the server, we remove the existing reservations.
  557. configure(DORA_CONFIGS[0]);
  558. // Try to renew the existing lease again.
  559. ASSERT_NO_THROW(client.doRequest());
  560. // The reservation has been removed, so the server should respond with
  561. // a DHCPNAK because the address that the client is using doesn't belong
  562. // to a dynamic pool.
  563. resp = client.getContext().response_;
  564. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  565. // A conforming client would go back to the server discovery.
  566. client.setState(Dhcp4Client::SELECTING);
  567. // Obtain a lease from the server using the 4-way exchange.
  568. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  569. IOAddress>(new IOAddress("0.0.0.0"))));
  570. // Make sure that the server responded.
  571. ASSERT_TRUE(client.getContext().response_);
  572. resp = client.getContext().response_;
  573. // Make sure that the server has responded with DHCPACK.
  574. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  575. // Obtain the subnet to which the returned address belongs.
  576. Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  577. selectSubnet(client.config_.lease_.addr_);
  578. ASSERT_TRUE(subnet);
  579. // Make sure that the address has been allocated from the dynamic pool.
  580. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, client.config_.lease_.addr_));
  581. // Remember the address allocated in the dynamic pool.
  582. IOAddress in_pool_addr = client.config_.lease_.addr_;
  583. // Create Client B.
  584. Dhcp4Client clientB(client.getServer());
  585. clientB.modifyHWAddr();
  586. // Create reservation for the Client B, for the address that the
  587. // Client A is using.
  588. configure(DORA_CONFIGS[0], false);
  589. host.reset(new Host(&clientB.getHWAddress()->hwaddr_[0],
  590. clientB.getHWAddress()->hwaddr_.size(),
  591. Host::IDENT_HWADDR, SubnetID(1),
  592. SubnetID(0), in_pool_addr));
  593. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  594. CfgMgr::instance().commit();
  595. // Client B performs a DHCPDISCOVER.
  596. clientB.setState(Dhcp4Client::SELECTING);
  597. // The server determines that the address reserved for Client B is
  598. // in use by Client A so it offers a different address.
  599. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  600. IOAddress>(new IOAddress("0.0.0.0"))));
  601. ASSERT_TRUE(clientB.getContext().response_);
  602. ASSERT_EQ(DHCPACK, static_cast<int>(clientB.getContext().response_->getType()));
  603. IOAddress client_b_addr = clientB.config_.lease_.addr_;
  604. ASSERT_NE(client_b_addr, in_pool_addr);
  605. // Client A renews the lease.
  606. client.setState(Dhcp4Client::RENEWING);
  607. // Set the unicast destination address to indicate that it is a renewal.
  608. client.setDestAddress(IOAddress(in_pool_addr));
  609. ASSERT_NO_THROW(client.doRequest());
  610. // Client A should get a DHCPNAK because it is using an address reserved
  611. // for Client B.
  612. resp = client.getContext().response_;
  613. ASSERT_EQ(DHCPNAK, static_cast<int>(resp->getType()));
  614. // Client B performs 4-way exchange but still gets an address from the
  615. // dynamic pool, because Client A hasn't obtained a new lease, so it is
  616. // still using an address reserved for Client B.
  617. clientB.setState(Dhcp4Client::SELECTING);
  618. // Obtain a lease from the server using the 4-way exchange.
  619. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  620. IOAddress>(new IOAddress("0.0.0.0"))));
  621. // Make sure that the server responded.
  622. ASSERT_TRUE(clientB.getContext().response_);
  623. ASSERT_EQ(DHCPACK, static_cast<int>(clientB.getContext().response_->getType()));
  624. ASSERT_NE(clientB.config_.lease_.addr_, in_pool_addr);
  625. ASSERT_EQ(client_b_addr, clientB.config_.lease_.addr_);
  626. // Client B renews its lease.
  627. clientB.setState(Dhcp4Client::RENEWING);
  628. clientB.setDestAddress(IOAddress("10.0.0.1"));
  629. ASSERT_NO_THROW(clientB.doRequest());
  630. // The server should renew the client's B lease because the address
  631. // reserved for client B is still in use by the client A.
  632. ASSERT_TRUE(clientB.getContext().response_);
  633. EXPECT_EQ(DHCPACK, static_cast<int>(clientB.getContext().response_->getType()));
  634. ASSERT_NE(clientB.config_.lease_.addr_, in_pool_addr);
  635. ASSERT_EQ(client_b_addr, clientB.config_.lease_.addr_);
  636. // Client A performs 4-way exchange.
  637. client.setState(Dhcp4Client::SELECTING);
  638. // Revert to the broadcast address for the selcting client.
  639. client.setDestAddress(IOAddress::IPV4_BCAST_ADDRESS());
  640. // Obtain a lease from the server using the 4-way exchange.
  641. ASSERT_NO_THROW(client.doDORA(boost::shared_ptr<
  642. IOAddress>(new IOAddress("0.0.0.0"))));
  643. // Make sure that the server responded.
  644. ASSERT_TRUE(client.getContext().response_);
  645. resp = client.getContext().response_;
  646. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  647. // The server should have assigned a different address than the one
  648. // reserved for the Client B.
  649. ASSERT_NE(client.config_.lease_.addr_, in_pool_addr);
  650. subnet = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->
  651. selectSubnet(client.config_.lease_.addr_);
  652. ASSERT_TRUE(subnet);
  653. ASSERT_TRUE(subnet->inPool(Lease::TYPE_V4, client.config_.lease_.addr_));
  654. // Client B renews again.
  655. ASSERT_NO_THROW(clientB.doRequest());
  656. // The client B should now receive the DHCPNAK from the server because
  657. // the reserved address is now available and the client should
  658. // revert to the DHCPDISCOVER to obtain it.
  659. ASSERT_TRUE(clientB.getContext().response_);
  660. EXPECT_EQ(DHCPNAK, static_cast<int>(clientB.getContext().response_->getType()));
  661. // Client B performs 4-way exchange and obtains a lease for the
  662. // reserved address.
  663. clientB.setState(Dhcp4Client::SELECTING);
  664. ASSERT_NO_THROW(clientB.doDORA(boost::shared_ptr<
  665. IOAddress>(new IOAddress("0.0.0.0"))));
  666. // Make sure that the server responded.
  667. ASSERT_TRUE(clientB.getContext().response_);
  668. resp = clientB.getContext().response_;
  669. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  670. ASSERT_EQ(in_pool_addr, clientB.config_.lease_.addr_);
  671. }
  672. } // end of anonymous namespace