release_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright (C) 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/subnet_id.h>
  21. #include <dhcp4/tests/dhcp4_test_utils.h>
  22. #include <dhcp4/tests/dhcp4_client.h>
  23. #include <stats/stats_mgr.h>
  24. #include <boost/shared_ptr.hpp>
  25. #include <sstream>
  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. using namespace isc::stats;
  32. namespace {
  33. /// @brief Set of JSON configurations used throughout the Release tests.
  34. ///
  35. /// - Configuration 0:
  36. /// - Used for testing Release message processing
  37. /// - 1 subnet: 10.0.0.0/24
  38. /// - 1 pool: 10.0.0.10-10.0.0.100
  39. /// - Router option present: 10.0.0.200 and 10.0.0.201
  40. const char* RELEASE_CONFIGS[] = {
  41. // Configuration 0
  42. "{ \"interfaces-config\": {"
  43. " \"interfaces\": [ \"*\" ]"
  44. "},"
  45. "\"valid-lifetime\": 600,"
  46. "\"subnet4\": [ { "
  47. " \"subnet\": \"10.0.0.0/24\", "
  48. " \"id\": 1,"
  49. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  50. " \"option-data\": [ {"
  51. " \"name\": \"routers\","
  52. " \"code\": 3,"
  53. " \"data\": \"10.0.0.200,10.0.0.201\","
  54. " \"csv-format\": true,"
  55. " \"space\": \"dhcp4\""
  56. " } ]"
  57. " } ]"
  58. "}"
  59. };
  60. /// @brief Test fixture class for testing 4-way (DORA) exchanges.
  61. ///
  62. /// @todo Currently there is a limit number of test cases covered here.
  63. /// In the future it is planned that the tests from the
  64. /// dhcp4_srv_unittest.cc will be migrated here and will use the
  65. /// @c Dhcp4Client class.
  66. class ReleaseTest : public Dhcpv4SrvTest {
  67. public:
  68. enum ExpectedResult {
  69. SHOULD_PASS,
  70. SHOULD_FAIL
  71. };
  72. /// @brief Constructor.
  73. ///
  74. /// Sets up fake interfaces.
  75. ReleaseTest()
  76. : Dhcpv4SrvTest(),
  77. iface_mgr_test_config_(true) {
  78. IfaceMgr::instance().openSockets4();
  79. }
  80. /// @brief Performs 4-way exchange to obtain new lease.
  81. ///
  82. /// @param client Client to be used to obtain a lease.
  83. void acquireLease(Dhcp4Client& client);
  84. /// @brief Tests if the acquired lease is or is not released.
  85. ///
  86. /// @param hw_address_1 HW Address to be used to acquire the lease.
  87. /// @param client_id_1 Client id to be used to acquire the lease.
  88. /// @param hw_address_2 HW Address to be used to release the lease.
  89. /// @param client_id_2 Client id to be used to release the lease.
  90. /// @param expected_result SHOULD_PASS if the lease is expected to
  91. /// be successfully released, or SHOULD_FAIL if the lease is expected
  92. /// to not be released.
  93. void acquireAndRelease(const std::string& hw_address_1,
  94. const std::string& client_id_1,
  95. const std::string& hw_address_2,
  96. const std::string& client_id_2,
  97. ExpectedResult expected_result);
  98. /// @brief Interface Manager's fake configuration control.
  99. IfaceMgrTestConfig iface_mgr_test_config_;
  100. };
  101. void
  102. ReleaseTest::acquireLease(Dhcp4Client& client) {
  103. // Perform 4-way exchange with the server but to not request any
  104. // specific address in the DHCPDISCOVER message.
  105. ASSERT_NO_THROW(client.doDORA());
  106. // Make sure that the server responded.
  107. ASSERT_TRUE(client.getContext().response_);
  108. Pkt4Ptr resp = client.getContext().response_;
  109. // Make sure that the server has responded with DHCPACK.
  110. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  111. // Response must not be relayed.
  112. EXPECT_FALSE(resp->isRelayed());
  113. // Make sure that the server id is present.
  114. EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
  115. // Make sure that the client has got the lease with the requested address.
  116. ASSERT_NE(client.config_.lease_.addr_.toText(), "0.0.0.0");
  117. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(client.config_.lease_.addr_);
  118. ASSERT_TRUE(lease);
  119. }
  120. void
  121. ReleaseTest::acquireAndRelease(const std::string& hw_address_1,
  122. const std::string& client_id_1,
  123. const std::string& hw_address_2,
  124. const std::string& client_id_2,
  125. ExpectedResult expected_result) {
  126. CfgMgr::instance().clear();
  127. Dhcp4Client client(Dhcp4Client::SELECTING);
  128. // Configure DHCP server.
  129. configure(RELEASE_CONFIGS[0], *client.getServer());
  130. // Explicitly set the client id.
  131. client.includeClientId(client_id_1);
  132. // Explicitly set the HW address.
  133. client.setHWAddress(hw_address_1);
  134. // Perform 4-way exchange to obtain a new lease.
  135. acquireLease(client);
  136. std::stringstream name;
  137. // Let's get the subnet-id and generate statistics name out of it
  138. const Subnet4Collection* subnets =
  139. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  140. ASSERT_EQ(1, subnets->size());
  141. name << "subnet[" << subnets->at(0)->getID() << "].assigned-addresses";
  142. ObservationPtr assigned_cnt = StatsMgr::instance().getObservation(name.str());
  143. ASSERT_TRUE(assigned_cnt);
  144. uint64_t before = assigned_cnt->getInteger().first;
  145. // Remember the acquired address.
  146. IOAddress leased_address = client.config_.lease_.addr_;
  147. // Explicitly set the client id for DHCPRELEASE.
  148. client.includeClientId(client_id_2);
  149. // Explicitly set the HW address for DHCPRELEASE.
  150. client.setHWAddress(hw_address_2);
  151. // Send the release and make sure that the lease is removed from the
  152. // server.
  153. ASSERT_NO_THROW(client.doRelease());
  154. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(leased_address);
  155. assigned_cnt = StatsMgr::instance().getObservation(name.str());
  156. ASSERT_TRUE(assigned_cnt);
  157. uint64_t after = assigned_cnt->getInteger().first;
  158. // We check if the release process was successful by checking if the
  159. // lease is in the database. It is expected that it is not present,
  160. // i.e. has been deleted with the release.
  161. if (expected_result == SHOULD_PASS) {
  162. EXPECT_FALSE(lease);
  163. // The removal succeded, so the assigned-addresses statistic should
  164. // be decreased by one
  165. EXPECT_EQ(before, after + 1);
  166. } else {
  167. EXPECT_TRUE(lease);
  168. // The removal failed, so the assigned-address should be the same
  169. // as before
  170. EXPECT_EQ(before, after);
  171. }
  172. }
  173. // This test checks that the client can acquire and release the lease.
  174. TEST_F(ReleaseTest, releaseNoIdentifierChange) {
  175. acquireAndRelease("01:02:03:04:05:06", "12:14",
  176. "01:02:03:04:05:06", "12:14",
  177. SHOULD_PASS);
  178. }
  179. // This test verifies the release correctness in the following case:
  180. // - Client acquires new lease using HW address only
  181. // - Client sends the DHCPRELEASE with valid HW address and without
  182. // client identifier.
  183. // - The server successfully releases the lease.
  184. TEST_F(ReleaseTest, releaseHWAddressOnly) {
  185. acquireAndRelease("01:02:03:04:05:06", "",
  186. "01:02:03:04:05:06", "",
  187. SHOULD_PASS);
  188. }
  189. // This test verifies the release correctness in the following case:
  190. // - Client acquires new lease using the client identifier and HW address
  191. // - Client sends the DHCPRELEASE with valid HW address but with no
  192. // client identifier.
  193. // - The server successfully releases the lease.
  194. TEST_F(ReleaseTest, releaseNoClientId) {
  195. acquireAndRelease("01:02:03:04:05:06", "12:14",
  196. "01:02:03:04:05:06", "",
  197. SHOULD_PASS);
  198. }
  199. // This test verifies the release correctness in the following case:
  200. // - Client acquires new lease using HW address
  201. // - Client sends the DHCPRELEASE with valid HW address and some
  202. // client identifier.
  203. // - The server identifies the lease using HW address and releases
  204. // this lease.
  205. TEST_F(ReleaseTest, releaseNoClientId2) {
  206. acquireAndRelease("01:02:03:04:05:06", "",
  207. "01:02:03:04:05:06", "12:14",
  208. SHOULD_PASS);
  209. }
  210. // This test checks the server's behavior in the following case:
  211. // - Client acquires new lease using the client identifier and HW address
  212. // - Client sends the DHCPRELEASE with the valid HW address but with invalid
  213. // client identifier.
  214. // - The server should not remove the lease.
  215. TEST_F(ReleaseTest, releaseNonMatchingClientId) {
  216. acquireAndRelease("01:02:03:04:05:06", "12:14",
  217. "01:02:03:04:05:06", "12:15:16",
  218. SHOULD_FAIL);
  219. }
  220. // This test checks the server's behavior in the following case:
  221. // - Client acquires new lease using client identifier and HW address
  222. // - Client sends the DHCPRELEASE with the same client identifier but
  223. // different HW address.
  224. // - The server uses client identifier to find the client's lease and
  225. // releases it.
  226. TEST_F(ReleaseTest, releaseNonMatchingHWAddress) {
  227. acquireAndRelease("01:02:03:04:05:06", "12:14",
  228. "06:06:06:06:06:06", "12:14",
  229. SHOULD_PASS);
  230. }
  231. // This test checks the server's behavior in the following case:
  232. // - Client acquires new lease.
  233. // - Client sends DHCPRELEASE with the ciaddr set to a different
  234. // address than it has acquired from the server.
  235. // - Server determines that the client is trying to release a
  236. // wrong address and will refuse to release.
  237. TEST_F(ReleaseTest, releaseNonMatchingIPAddress) {
  238. Dhcp4Client client(Dhcp4Client::SELECTING);
  239. // Configure DHCP server.
  240. configure(RELEASE_CONFIGS[0], *client.getServer());
  241. // Perform 4-way exchange to obtain a new lease.
  242. acquireLease(client);
  243. // Remember the acquired address.
  244. IOAddress leased_address = client.config_.lease_.addr_;
  245. // Modify the client's address to force it to release a different address
  246. // than it has obtained from the server.
  247. client.config_.lease_.addr_ = IOAddress(static_cast<uint32_t>(leased_address) + 1);
  248. // Send DHCPRELEASE and make sure it was unsuccessful, i.e. the lease
  249. // remains in the database.
  250. ASSERT_NO_THROW(client.doRelease());
  251. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(leased_address);
  252. ASSERT_TRUE(lease);
  253. }
  254. // This test verifies that incoming RELEASE from a bad location
  255. // is correctly dropped.
  256. TEST_F(ReleaseTest, releaseNoSubnet) {
  257. Dhcp4Client client(Dhcp4Client::SELECTING);
  258. // Configure DHCP server.
  259. configure(RELEASE_CONFIGS[0], *client.getServer());
  260. // Perform 4-way exchange to obtain a new lease.
  261. acquireLease(client);
  262. // Remember the acquired address.
  263. IOAddress leased_address = client.config_.lease_.addr_;
  264. // Release is as it was relayed
  265. client.useRelay(true);
  266. // Send the release
  267. ASSERT_NO_THROW(client.doRelease());
  268. // Check that the lease was not removed (due to no subnet)
  269. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(leased_address);
  270. EXPECT_TRUE(lease);
  271. }
  272. } // end of anonymous namespace