pgsql_lease_mgr_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Copyright (C) 2014-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/pgsql_lease_mgr.h>
  10. #include <dhcpsrv/tests/test_utils.h>
  11. #include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
  12. #include <dhcpsrv/testutils/pgsql_schema.h>
  13. #include <exceptions/exceptions.h>
  14. #include <gtest/gtest.h>
  15. using namespace isc;
  16. using namespace isc::asiolink;
  17. using namespace isc::dhcp;
  18. using namespace isc::dhcp::test;
  19. using namespace std;
  20. namespace {
  21. /// @brief Test fixture class for testing PostgreSQL Lease Manager
  22. ///
  23. /// Opens the database prior to each test and closes it afterwards.
  24. /// All pending transactions are deleted prior to closure.
  25. class PgSqlLeaseMgrTest : public GenericLeaseMgrTest {
  26. public:
  27. /// @brief Constructor
  28. ///
  29. /// Deletes everything from the database and opens it.
  30. PgSqlLeaseMgrTest() {
  31. // Ensure schema is the correct one.
  32. destroyPgSQLSchema();
  33. createPgSQLSchema();
  34. // Connect to the database
  35. try {
  36. LeaseMgrFactory::create(validPgSQLConnectionString());
  37. } catch (...) {
  38. std::cerr << "*** ERROR: unable to open database. The test\n"
  39. "*** environment is broken and must be fixed before\n"
  40. "*** the PostgreSQL tests will run correctly.\n"
  41. "*** The reason for the problem is described in the\n"
  42. "*** accompanying exception output.\n";
  43. throw;
  44. }
  45. lmptr_ = &(LeaseMgrFactory::instance());
  46. }
  47. /// @brief Destructor
  48. ///
  49. /// Rolls back all pending transactions. The deletion of lmptr_ will close
  50. /// the database. Then reopen it and delete everything created by the test.
  51. virtual ~PgSqlLeaseMgrTest() {
  52. lmptr_->rollback();
  53. LeaseMgrFactory::destroy();
  54. destroyPgSQLSchema();
  55. }
  56. /// @brief Reopen the database
  57. ///
  58. /// Closes the database and re-open it. Anything committed should be
  59. /// visible.
  60. ///
  61. /// Parameter is ignored for Postgres backend as the v4 and v6 leases share
  62. /// the same database.
  63. void reopen(Universe) {
  64. LeaseMgrFactory::destroy();
  65. LeaseMgrFactory::create(validPgSQLConnectionString());
  66. lmptr_ = &(LeaseMgrFactory::instance());
  67. }
  68. };
  69. /// @brief Check that database can be opened
  70. ///
  71. /// This test checks if the PgSqlLeaseMgr can be instantiated. This happens
  72. /// only if the database can be opened. Note that this is not part of the
  73. /// PgSqlLeaseMgr test fixure set. This test checks that the database can be
  74. /// opened: the fixtures assume that and check basic operations.
  75. TEST(PgSqlOpenTest, OpenDatabase) {
  76. // Schema needs to be created for the test to work.
  77. destroyPgSQLSchema(true);
  78. createPgSQLSchema(true);
  79. // Check that lease manager open the database opens correctly and tidy up.
  80. // If it fails, print the error message.
  81. try {
  82. LeaseMgrFactory::create(validPgSQLConnectionString());
  83. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  84. LeaseMgrFactory::destroy();
  85. } catch (const isc::Exception& ex) {
  86. FAIL() << "*** ERROR: unable to open database, reason:\n"
  87. << " " << ex.what() << "\n"
  88. << "*** The test environment is broken and must be fixed\n"
  89. << "*** before the PostgreSQL tests will run correctly.\n";
  90. }
  91. // Check that lease manager open the database opens correctly with a longer
  92. // timeout. If it fails, print the error message.
  93. try {
  94. string connection_string = validPgSQLConnectionString() + string(" ") +
  95. string(VALID_TIMEOUT);
  96. LeaseMgrFactory::create(connection_string);
  97. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  98. LeaseMgrFactory::destroy();
  99. } catch (const isc::Exception& ex) {
  100. FAIL() << "*** ERROR: unable to open database, reason:\n"
  101. << " " << ex.what() << "\n"
  102. << "*** The test environment is broken and must be fixed\n"
  103. << "*** before the PostgreSQL tests will run correctly.\n";
  104. }
  105. // Check that attempting to get an instance of the lease manager when
  106. // none is set throws an exception.
  107. EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
  108. // Check that wrong specification of backend throws an exception.
  109. // (This is really a check on LeaseMgrFactory, but is convenient to
  110. // perform here.)
  111. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  112. NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  113. InvalidParameter);
  114. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  115. INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  116. InvalidType);
  117. // Check that invalid login data causes an exception.
  118. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  119. PGSQL_VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  120. DbOpenError);
  121. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  122. PGSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
  123. DbOpenError);
  124. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  125. PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  126. DbOpenError);
  127. // This test might fail if 'auth-method' in PostgresSQL host-based authentication
  128. // file (/var/lib/pgsql/9.4/data/pg_hba.conf) is set to 'trust',
  129. // which allows logging without password. 'Auth-method' should be changed to 'password'.
  130. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  131. PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
  132. DbOpenError);
  133. // Check for invalid timeouts
  134. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  135. PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_1)),
  136. DbInvalidTimeout);
  137. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  138. PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_2)),
  139. DbInvalidTimeout);
  140. // Check for missing parameters
  141. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  142. PGSQL_VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  143. NoDatabaseName);
  144. // Tidy up after the test
  145. destroyPgSQLSchema(true);
  146. }
  147. /// @brief Check the getType() method
  148. ///
  149. /// getType() returns a string giving the type of the backend, which should
  150. /// always be "postgresql".
  151. TEST_F(PgSqlLeaseMgrTest, getType) {
  152. EXPECT_EQ(std::string("postgresql"), lmptr_->getType());
  153. }
  154. /// @brief Check getName() returns correct database name
  155. TEST_F(PgSqlLeaseMgrTest, getName) {
  156. EXPECT_EQ(std::string("keatest"), lmptr_->getName());
  157. }
  158. /// @brief Check that getVersion() returns the expected version
  159. TEST_F(PgSqlLeaseMgrTest, checkVersion) {
  160. // Check version
  161. pair<uint32_t, uint32_t> version;
  162. ASSERT_NO_THROW(version = lmptr_->getVersion());
  163. EXPECT_EQ(PG_SCHEMA_VERSION_MAJOR, version.first);
  164. EXPECT_EQ(PG_SCHEMA_VERSION_MINOR, version.second);
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////
  167. /// LEASE4 /////////////////////////////////////////////////////////////////////
  168. ////////////////////////////////////////////////////////////////////////////////
  169. /// @brief Basic Lease4 Checks
  170. ///
  171. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  172. /// IPv4 address) works.
  173. TEST_F(PgSqlLeaseMgrTest, basicLease4) {
  174. testBasicLease4();
  175. }
  176. /// @brief Check that Lease4 code safely handles invalid dates.
  177. TEST_F(PgSqlLeaseMgrTest, maxDate4) {
  178. testMaxDate4();
  179. }
  180. /// @brief Lease4 update tests
  181. ///
  182. /// Checks that we are able to update a lease in the database.
  183. TEST_F(PgSqlLeaseMgrTest, updateLease4) {
  184. testUpdateLease4();
  185. }
  186. /// @brief Check GetLease4 methods - access by Hardware Address
  187. TEST_F(PgSqlLeaseMgrTest, getLease4HWAddr1) {
  188. testGetLease4HWAddr1();
  189. }
  190. /// @brief Check GetLease4 methods - access by Hardware Address
  191. TEST_F(PgSqlLeaseMgrTest, getLease4HWAddr2) {
  192. testGetLease4HWAddr2();
  193. }
  194. // @brief Get lease4 by hardware address (2)
  195. //
  196. // Check that the system can cope with getting a hardware address of
  197. // any size.
  198. TEST_F(PgSqlLeaseMgrTest, getLease4HWAddrSize) {
  199. testGetLease4HWAddrSize();
  200. }
  201. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  202. ///
  203. /// Adds leases to the database and checks that they can be accessed via
  204. /// a combination of hardware address and subnet ID
  205. TEST_F(PgSqlLeaseMgrTest, getLease4HwaddrSubnetId) {
  206. testGetLease4HWAddrSubnetId();
  207. }
  208. // @brief Get lease4 by hardware address and subnet ID (2)
  209. //
  210. // Check that the system can cope with getting a hardware address of
  211. // any size.
  212. TEST_F(PgSqlLeaseMgrTest, getLease4HWAddrSubnetIdSize) {
  213. testGetLease4HWAddrSubnetIdSize();
  214. }
  215. // This test was derived from memfile.
  216. TEST_F(PgSqlLeaseMgrTest, getLease4ClientId) {
  217. testGetLease4ClientId();
  218. }
  219. /// @brief Check GetLease4 methods - access by Client ID
  220. ///
  221. /// Adds leases to the database and checks that they can be accessed via
  222. /// the Client ID.
  223. TEST_F(PgSqlLeaseMgrTest, getLease4ClientId2) {
  224. testGetLease4ClientId2();
  225. }
  226. // @brief Get Lease4 by client ID (2)
  227. //
  228. // Check that the system can cope with a client ID of any size.
  229. TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSize) {
  230. testGetLease4ClientIdSize();
  231. }
  232. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  233. ///
  234. /// Adds leases to the database and checks that they can be accessed via
  235. /// a combination of client and subnet IDs.
  236. TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSubnetId) {
  237. testGetLease4ClientIdSubnetId();
  238. }
  239. /// @brief Basic Lease4 Checks
  240. ///
  241. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  242. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  243. /// (client-id is optional and may not be present)
  244. TEST_F(PgSqlLeaseMgrTest, lease4NullClientId) {
  245. testLease4NullClientId();
  246. }
  247. /// @brief Verify that too long hostname for Lease4 is not accepted.
  248. ///
  249. /// Checks that the it is not possible to create a lease when the hostname
  250. /// length exceeds 255 characters.
  251. TEST_F(PgSqlLeaseMgrTest, lease4InvalidHostname) {
  252. testLease4InvalidHostname();
  253. }
  254. /// @brief Check that the expired DHCPv4 leases can be retrieved.
  255. ///
  256. /// This test adds a number of leases to the lease database and marks
  257. /// some of them as expired. Then it queries for expired leases and checks
  258. /// whether only expired leases are returned, and that they are returned in
  259. /// the order from most to least expired. It also checks that the lease
  260. /// which is marked as 'reclaimed' is not returned.
  261. TEST_F(PgSqlLeaseMgrTest, getExpiredLeases4) {
  262. testGetExpiredLeases4();
  263. }
  264. /// @brief Check that expired reclaimed DHCPv4 leases are removed.
  265. TEST_F(PgSqlLeaseMgrTest, deleteExpiredReclaimedLeases4) {
  266. testDeleteExpiredReclaimedLeases4();
  267. }
  268. ////////////////////////////////////////////////////////////////////////////////
  269. /// LEASE6 /////////////////////////////////////////////////////////////////////
  270. ////////////////////////////////////////////////////////////////////////////////
  271. // Test checks whether simple add, get and delete operations are possible
  272. // on Lease6
  273. TEST_F(PgSqlLeaseMgrTest, testAddGetDelete6) {
  274. testAddGetDelete6(false);
  275. }
  276. /// @brief Basic Lease6 Checks
  277. ///
  278. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  279. /// IPv6 address) works.
  280. TEST_F(PgSqlLeaseMgrTest, basicLease6) {
  281. testBasicLease6();
  282. }
  283. /// @brief Check that Lease6 code safely handles invalid dates.
  284. TEST_F(PgSqlLeaseMgrTest, maxDate6) {
  285. testMaxDate6();
  286. }
  287. /// @brief Verify that too long hostname for Lease6 is not accepted.
  288. ///
  289. /// Checks that the it is not possible to create a lease when the hostname
  290. /// length exceeds 255 characters.
  291. TEST_F(PgSqlLeaseMgrTest, lease6InvalidHostname) {
  292. testLease6InvalidHostname();
  293. }
  294. /// @brief Check GetLease6 methods - access by DUID/IAID
  295. ///
  296. /// Adds leases to the database and checks that they can be accessed via
  297. /// a combination of DUID and IAID.
  298. TEST_F(PgSqlLeaseMgrTest, getLeases6DuidIaid) {
  299. testGetLeases6DuidIaid();
  300. }
  301. // Check that the system can cope with a DUID of allowed size.
  302. TEST_F(PgSqlLeaseMgrTest, getLeases6DuidSize) {
  303. testGetLeases6DuidSize();
  304. }
  305. /// @brief Check that getLease6 methods discriminate by lease type.
  306. ///
  307. /// Adds six leases, two per lease type all with the same duid and iad but
  308. /// with alternating subnet_ids.
  309. /// It then verifies that all of getLeases6() method variants correctly
  310. /// discriminate between the leases based on lease type alone.
  311. TEST_F(PgSqlLeaseMgrTest, lease6LeaseTypeCheck) {
  312. testLease6LeaseTypeCheck();
  313. }
  314. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  315. ///
  316. /// Adds leases to the database and checks that they can be accessed via
  317. /// a combination of DIUID and IAID.
  318. TEST_F(PgSqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
  319. testGetLease6DuidIaidSubnetId();
  320. }
  321. // Test checks that getLease6() works with different DUID sizes
  322. TEST_F(PgSqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  323. testGetLease6DuidIaidSubnetIdSize();
  324. }
  325. /// @brief Lease6 update tests
  326. ///
  327. /// Checks that we are able to update a lease in the database.
  328. TEST_F(PgSqlLeaseMgrTest, updateLease6) {
  329. testUpdateLease6();
  330. }
  331. TEST_F(PgSqlLeaseMgrTest, nullDuid) {
  332. testNullDuid();
  333. }
  334. /// @brief Check that the expired DHCPv6 leases can be retrieved.
  335. ///
  336. /// This test adds a number of leases to the lease database and marks
  337. /// some of them as expired. Then it queries for expired leases and checks
  338. /// whether only expired leases are returned, and that they are returned in
  339. /// the order from most to least expired. It also checks that the lease
  340. /// which is marked as 'reclaimed' is not returned.
  341. TEST_F(PgSqlLeaseMgrTest, getExpiredLeases6) {
  342. testGetExpiredLeases6();
  343. }
  344. // Verifies that IPv4 lease statistics can be recalculated.
  345. TEST_F(PgSqlLeaseMgrTest, recountLeaseStats4) {
  346. testRecountLeaseStats4();
  347. }
  348. // Verifies that IPv6 lease statistics can be recalculated.
  349. TEST_F(PgSqlLeaseMgrTest, recountLeaseStats6) {
  350. testRecountLeaseStats6();
  351. }
  352. // Tests that leases from specific subnet can be removed.
  353. TEST_F(PgSqlLeaseMgrTest, DISABLED_wipeLeases4) {
  354. testWipeLeases4();
  355. }
  356. // Tests that leases from specific subnet can be removed.
  357. TEST_F(PgSqlLeaseMgrTest, DISABLED_wipeLeases6) {
  358. testWipeLeases6();
  359. }
  360. }; // namespace