mysql_lease_mgr_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/io_address.h>
  8. #include <dhcpsrv/lease_mgr_factory.h>
  9. #include <dhcpsrv/mysql_connection.h>
  10. #include <dhcpsrv/mysql_lease_mgr.h>
  11. #include <dhcpsrv/tests/test_utils.h>
  12. #include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
  13. #include <dhcpsrv/testutils/mysql_schema.h>
  14. #include <exceptions/exceptions.h>
  15. #include <gtest/gtest.h>
  16. #include <algorithm>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <string>
  20. #include <utility>
  21. using namespace isc;
  22. using namespace isc::asiolink;
  23. using namespace isc::dhcp;
  24. using namespace isc::dhcp::test;
  25. using namespace std;
  26. namespace {
  27. /// @brief Test fixture class for testing MySQL Lease Manager
  28. ///
  29. /// Opens the database prior to each test and closes it afterwards.
  30. /// All pending transactions are deleted prior to closure.
  31. class MySqlLeaseMgrTest : public GenericLeaseMgrTest {
  32. public:
  33. /// @brief Constructor
  34. ///
  35. /// Deletes everything from the database and opens it.
  36. MySqlLeaseMgrTest() {
  37. // Ensure schema is the correct one.
  38. destroyMySQLSchema();
  39. createMySQLSchema();
  40. // Connect to the database
  41. try {
  42. LeaseMgrFactory::create(validMySQLConnectionString());
  43. } catch (...) {
  44. std::cerr << "*** ERROR: unable to open database. The test\n"
  45. "*** environment is broken and must be fixed before\n"
  46. "*** the MySQL tests will run correctly.\n"
  47. "*** The reason for the problem is described in the\n"
  48. "*** accompanying exception output.\n";
  49. throw;
  50. }
  51. lmptr_ = &(LeaseMgrFactory::instance());
  52. }
  53. /// @brief Destructor
  54. ///
  55. /// Rolls back all pending transactions. The deletion of lmptr_ will close
  56. /// the database. Then reopen it and delete everything created by the test.
  57. virtual ~MySqlLeaseMgrTest() {
  58. lmptr_->rollback();
  59. LeaseMgrFactory::destroy();
  60. destroyMySQLSchema();
  61. }
  62. /// @brief Reopen the database
  63. ///
  64. /// Closes the database and re-open it. Anything committed should be
  65. /// visible.
  66. ///
  67. /// Parameter is ignored for MySQL backend as the v4 and v6 leases share
  68. /// the same database.
  69. void reopen(Universe) {
  70. LeaseMgrFactory::destroy();
  71. LeaseMgrFactory::create(validMySQLConnectionString());
  72. lmptr_ = &(LeaseMgrFactory::instance());
  73. }
  74. };
  75. /// @brief Check that database can be opened
  76. ///
  77. /// This test checks if the MySqlLeaseMgr can be instantiated. This happens
  78. /// only if the database can be opened. Note that this is not part of the
  79. /// MySqlLeaseMgr test fixure set. This test checks that the database can be
  80. /// opened: the fixtures assume that and check basic operations.
  81. TEST(MySqlOpenTest, OpenDatabase) {
  82. // Schema needs to be created for the test to work.
  83. destroyMySQLSchema(true);
  84. createMySQLSchema(true);
  85. // Check that lease manager open the database opens correctly and tidy up.
  86. // If it fails, print the error message.
  87. try {
  88. LeaseMgrFactory::create(validMySQLConnectionString());
  89. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  90. LeaseMgrFactory::destroy();
  91. } catch (const isc::Exception& ex) {
  92. FAIL() << "*** ERROR: unable to open database, reason:\n"
  93. << " " << ex.what() << "\n"
  94. << "*** The test environment is broken and must be fixed\n"
  95. << "*** before the MySQL tests will run correctly.\n";
  96. }
  97. // Check that lease manager open the database opens correctly with a longer
  98. // timeout. If it fails, print the error message.
  99. try {
  100. string connection_string = validMySQLConnectionString() + string(" ") +
  101. string(VALID_TIMEOUT);
  102. LeaseMgrFactory::create(connection_string);
  103. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  104. LeaseMgrFactory::destroy();
  105. } catch (const isc::Exception& ex) {
  106. FAIL() << "*** ERROR: unable to open database, reason:\n"
  107. << " " << ex.what() << "\n"
  108. << "*** The test environment is broken and must be fixed\n"
  109. << "*** before the MySQL tests will run correctly.\n";
  110. }
  111. // Check that attempting to get an instance of the lease manager when
  112. // none is set throws an exception.
  113. EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
  114. // Check that wrong specification of backend throws an exception.
  115. // (This is really a check on LeaseMgrFactory, but is convenient to
  116. // perform here.)
  117. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  118. NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  119. InvalidParameter);
  120. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  121. INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  122. InvalidType);
  123. // Check that invalid login data causes an exception.
  124. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  125. MYSQL_VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  126. DbOpenError);
  127. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  128. MYSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
  129. DbOpenError);
  130. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  131. MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  132. DbOpenError);
  133. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  134. MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
  135. DbOpenError);
  136. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  137. MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_1)),
  138. DbInvalidTimeout);
  139. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  140. MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_2)),
  141. DbInvalidTimeout);
  142. // Check for missing parameters
  143. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  144. MYSQL_VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  145. NoDatabaseName);
  146. // Tidy up after the test
  147. destroyMySQLSchema(true);
  148. }
  149. /// @brief Check the getType() method
  150. ///
  151. /// getType() returns a string giving the type of the backend, which should
  152. /// always be "mysql".
  153. TEST_F(MySqlLeaseMgrTest, getType) {
  154. EXPECT_EQ(std::string("mysql"), lmptr_->getType());
  155. }
  156. /// @brief Check conversion functions
  157. ///
  158. /// The server works using cltt and valid_filetime. In the database, the
  159. /// information is stored as expire_time and valid-lifetime, which are
  160. /// related by
  161. ///
  162. /// expire_time = cltt + valid_lifetime
  163. ///
  164. /// This test checks that the conversion is correct. It does not check that the
  165. /// data is entered into the database correctly, only that the MYSQL_TIME
  166. /// structure used for the entry is correctly set up.
  167. TEST_F(MySqlLeaseMgrTest, checkTimeConversion) {
  168. const time_t cltt = time(NULL);
  169. const uint32_t valid_lft = 86400; // 1 day
  170. struct tm tm_expire;
  171. MYSQL_TIME mysql_expire;
  172. // Work out what the broken-down time will be for one day
  173. // after the current time.
  174. time_t expire_time = cltt + valid_lft;
  175. (void) localtime_r(&expire_time, &tm_expire);
  176. // Convert to the database time
  177. MySqlConnection::convertToDatabaseTime(cltt, valid_lft, mysql_expire);
  178. // Are the times the same?
  179. EXPECT_EQ(tm_expire.tm_year + 1900, mysql_expire.year);
  180. EXPECT_EQ(tm_expire.tm_mon + 1, mysql_expire.month);
  181. EXPECT_EQ(tm_expire.tm_mday, mysql_expire.day);
  182. EXPECT_EQ(tm_expire.tm_hour, mysql_expire.hour);
  183. EXPECT_EQ(tm_expire.tm_min, mysql_expire.minute);
  184. EXPECT_EQ(tm_expire.tm_sec, mysql_expire.second);
  185. EXPECT_EQ(0, mysql_expire.second_part);
  186. EXPECT_EQ(0, mysql_expire.neg);
  187. // Convert back
  188. time_t converted_cltt = 0;
  189. MySqlConnection::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
  190. EXPECT_EQ(cltt, converted_cltt);
  191. }
  192. /// @brief Check getName() returns correct database name
  193. TEST_F(MySqlLeaseMgrTest, getName) {
  194. EXPECT_EQ(std::string("keatest"), lmptr_->getName());
  195. }
  196. /// @brief Check that getVersion() returns the expected version
  197. TEST_F(MySqlLeaseMgrTest, checkVersion) {
  198. // Check version
  199. pair<uint32_t, uint32_t> version;
  200. ASSERT_NO_THROW(version = lmptr_->getVersion());
  201. EXPECT_EQ(MYSQL_SCHEMA_VERSION_MAJOR, version.first);
  202. EXPECT_EQ(MYSQL_SCHEMA_VERSION_MINOR, version.second);
  203. }
  204. ////////////////////////////////////////////////////////////////////////////////
  205. /// LEASE4 /////////////////////////////////////////////////////////////////////
  206. ////////////////////////////////////////////////////////////////////////////////
  207. /// @brief Basic Lease4 Checks
  208. ///
  209. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  210. /// IPv4 address) works.
  211. TEST_F(MySqlLeaseMgrTest, basicLease4) {
  212. testBasicLease4();
  213. }
  214. /// @brief Check that Lease4 code safely handles invalid dates.
  215. TEST_F(MySqlLeaseMgrTest, maxDate4) {
  216. testMaxDate4();
  217. }
  218. /// @brief Lease4 update tests
  219. ///
  220. /// Checks that we are able to update a lease in the database.
  221. TEST_F(MySqlLeaseMgrTest, updateLease4) {
  222. testUpdateLease4();
  223. }
  224. /// @brief Check GetLease4 methods - access by Hardware Address
  225. TEST_F(MySqlLeaseMgrTest, getLease4HWAddr1) {
  226. testGetLease4HWAddr1();
  227. }
  228. /// @brief Check GetLease4 methods - access by Hardware Address
  229. TEST_F(MySqlLeaseMgrTest, getLease4HWAddr2) {
  230. testGetLease4HWAddr2();
  231. }
  232. // @brief Get lease4 by hardware address (2)
  233. //
  234. // Check that the system can cope with getting a hardware address of
  235. // any size.
  236. TEST_F(MySqlLeaseMgrTest, getLease4HWAddrSize) {
  237. testGetLease4HWAddrSize();
  238. }
  239. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  240. ///
  241. /// Adds leases to the database and checks that they can be accessed via
  242. /// a combination of hardware address and subnet ID
  243. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetId) {
  244. testGetLease4HWAddrSubnetId();
  245. }
  246. // @brief Get lease4 by hardware address and subnet ID (2)
  247. //
  248. // Check that the system can cope with getting a hardware address of
  249. // any size.
  250. TEST_F(MySqlLeaseMgrTest, getLease4HWAddrSubnetIdSize) {
  251. testGetLease4HWAddrSubnetIdSize();
  252. }
  253. // This test was derived from memfile.
  254. TEST_F(MySqlLeaseMgrTest, getLease4ClientId) {
  255. testGetLease4ClientId();
  256. }
  257. /// @brief Check GetLease4 methods - access by Client ID
  258. ///
  259. /// Adds leases to the database and checks that they can be accessed via
  260. /// the Client ID.
  261. TEST_F(MySqlLeaseMgrTest, getLease4ClientId2) {
  262. testGetLease4ClientId2();
  263. }
  264. // @brief Get Lease4 by client ID (2)
  265. //
  266. // Check that the system can cope with a client ID of any size.
  267. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSize) {
  268. testGetLease4ClientIdSize();
  269. }
  270. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  271. ///
  272. /// Adds leases to the database and checks that they can be accessed via
  273. /// a combination of client and subnet IDs.
  274. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) {
  275. testGetLease4ClientIdSubnetId();
  276. }
  277. /// @brief Basic Lease4 Checks
  278. ///
  279. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  280. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  281. /// (client-id is optional and may not be present)
  282. TEST_F(MySqlLeaseMgrTest, lease4NullClientId) {
  283. testLease4NullClientId();
  284. }
  285. /// @brief Verify that too long hostname for Lease4 is not accepted.
  286. ///
  287. /// Checks that the it is not possible to create a lease when the hostname
  288. /// length exceeds 255 characters.
  289. TEST_F(MySqlLeaseMgrTest, lease4InvalidHostname) {
  290. testLease4InvalidHostname();
  291. }
  292. ////////////////////////////////////////////////////////////////////////////////
  293. /// LEASE6 /////////////////////////////////////////////////////////////////////
  294. ////////////////////////////////////////////////////////////////////////////////
  295. // Test checks whether simple add, get and delete operations are possible
  296. // on Lease6
  297. TEST_F(MySqlLeaseMgrTest, testAddGetDelete6) {
  298. testAddGetDelete6(false);
  299. }
  300. /// @brief Basic Lease6 Checks
  301. ///
  302. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  303. /// IPv6 address) works.
  304. TEST_F(MySqlLeaseMgrTest, basicLease6) {
  305. testBasicLease6();
  306. }
  307. /// @brief Check that Lease6 code safely handles invalid dates.
  308. TEST_F(MySqlLeaseMgrTest, maxDate6) {
  309. testMaxDate6();
  310. }
  311. /// @brief Verify that too long hostname for Lease6 is not accepted.
  312. ///
  313. /// Checks that the it is not possible to create a lease when the hostname
  314. /// length exceeds 255 characters.
  315. TEST_F(MySqlLeaseMgrTest, lease6InvalidHostname) {
  316. testLease6InvalidHostname();
  317. }
  318. /// @brief Check GetLease6 methods - access by DUID/IAID
  319. ///
  320. /// Adds leases to the database and checks that they can be accessed via
  321. /// a combination of DUID and IAID.
  322. TEST_F(MySqlLeaseMgrTest, getLeases6DuidIaid) {
  323. testGetLeases6DuidIaid();
  324. }
  325. // Check that the system can cope with a DUID of allowed size.
  326. TEST_F(MySqlLeaseMgrTest, getLeases6DuidSize) {
  327. testGetLeases6DuidSize();
  328. }
  329. /// @brief Check that getLease6 methods discriminate by lease type.
  330. ///
  331. /// Adds six leases, two per lease type all with the same duid and iad but
  332. /// with alternating subnet_ids.
  333. /// It then verifies that all of getLeases6() method variants correctly
  334. /// discriminate between the leases based on lease type alone.
  335. TEST_F(MySqlLeaseMgrTest, lease6LeaseTypeCheck) {
  336. testLease6LeaseTypeCheck();
  337. }
  338. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  339. ///
  340. /// Adds leases to the database and checks that they can be accessed via
  341. /// a combination of DIUID and IAID.
  342. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
  343. testGetLease6DuidIaidSubnetId();
  344. }
  345. // Test checks that getLease6() works with different DUID sizes
  346. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  347. testGetLease6DuidIaidSubnetIdSize();
  348. }
  349. /// @brief Lease6 update tests
  350. ///
  351. /// Checks that we are able to update a lease in the database.
  352. TEST_F(MySqlLeaseMgrTest, updateLease6) {
  353. testUpdateLease6();
  354. }
  355. /// @brief DHCPv4 Lease recreation tests
  356. ///
  357. /// Checks that the lease can be created, deleted and recreated with
  358. /// different parameters. It also checks that the re-created lease is
  359. /// correctly stored in the lease database.
  360. TEST_F(MySqlLeaseMgrTest, testRecreateLease4) {
  361. testRecreateLease4();
  362. }
  363. /// @brief DHCPv6 Lease recreation tests
  364. ///
  365. /// Checks that the lease can be created, deleted and recreated with
  366. /// different parameters. It also checks that the re-created lease is
  367. /// correctly stored in the lease database.
  368. TEST_F(MySqlLeaseMgrTest, testRecreateLease6) {
  369. testRecreateLease6();
  370. }
  371. /// @brief Checks that null DUID is not allowed.
  372. TEST_F(MySqlLeaseMgrTest, nullDuid) {
  373. testNullDuid();
  374. }
  375. /// @brief Tests whether memfile can store and retrieve hardware addresses
  376. TEST_F(MySqlLeaseMgrTest, testLease6Mac) {
  377. testLease6MAC();
  378. }
  379. /// @brief Tests whether memfile can store and retrieve hardware addresses
  380. TEST_F(MySqlLeaseMgrTest, testLease6HWTypeAndSource) {
  381. testLease6HWTypeAndSource();
  382. }
  383. /// @brief Check that the expired DHCPv4 leases can be retrieved.
  384. ///
  385. /// This test adds a number of leases to the lease database and marks
  386. /// some of them as expired. Then it queries for expired leases and checks
  387. /// whether only expired leases are returned, and that they are returned in
  388. /// the order from most to least expired. It also checks that the lease
  389. /// which is marked as 'reclaimed' is not returned.
  390. TEST_F(MySqlLeaseMgrTest, getExpiredLeases4) {
  391. testGetExpiredLeases4();
  392. }
  393. /// @brief Check that the expired DHCPv6 leases can be retrieved.
  394. ///
  395. /// This test adds a number of leases to the lease database and marks
  396. /// some of them as expired. Then it queries for expired leases and checks
  397. /// whether only expired leases are returned, and that they are returned in
  398. /// the order from most to least expired. It also checks that the lease
  399. /// which is marked as 'reclaimed' is not returned.
  400. TEST_F(MySqlLeaseMgrTest, getExpiredLeases6) {
  401. testGetExpiredLeases6();
  402. }
  403. /// @brief Check that expired reclaimed DHCPv6 leases are removed.
  404. TEST_F(MySqlLeaseMgrTest, deleteExpiredReclaimedLeases6) {
  405. testDeleteExpiredReclaimedLeases6();
  406. }
  407. /// @brief Check that expired reclaimed DHCPv4 leases are removed.
  408. TEST_F(MySqlLeaseMgrTest, deleteExpiredReclaimedLeases4) {
  409. testDeleteExpiredReclaimedLeases4();
  410. }
  411. // Verifies that IPv4 lease statistics can be recalculated.
  412. TEST_F(MySqlLeaseMgrTest, recountLeaseStats4) {
  413. testRecountLeaseStats4();
  414. }
  415. // Verifies that IPv6 lease statistics can be recalculated.
  416. TEST_F(MySqlLeaseMgrTest, recountLeaseStats6) {
  417. testRecountLeaseStats6();
  418. }
  419. }; // Of anonymous namespace