mysql_lease_mgr_unittest.cc 17 KB

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