mysql_lease_mgr_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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_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. void reopen() {
  160. LeaseMgrFactory::destroy();
  161. LeaseMgrFactory::create(validConnectionString());
  162. lmptr_ = &(LeaseMgrFactory::instance());
  163. }
  164. };
  165. /// @brief Check that database can be opened
  166. ///
  167. /// This test checks if the MySqlLeaseMgr can be instantiated. This happens
  168. /// only if the database can be opened. Note that this is not part of the
  169. /// MySqlLeaseMgr test fixure set. This test checks that the database can be
  170. /// opened: the fixtures assume that and check basic operations.
  171. TEST(MySqlOpenTest, OpenDatabase) {
  172. // Schema needs to be created for the test to work.
  173. destroySchema();
  174. createSchema();
  175. // Check that lease manager open the database opens correctly and tidy up.
  176. // If it fails, print the error message.
  177. try {
  178. LeaseMgrFactory::create(validConnectionString());
  179. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  180. LeaseMgrFactory::destroy();
  181. } catch (const isc::Exception& ex) {
  182. FAIL() << "*** ERROR: unable to open database, reason:\n"
  183. << " " << ex.what() << "\n"
  184. << "*** The test environment is broken and must be fixed\n"
  185. << "*** before the MySQL tests will run correctly.\n";
  186. }
  187. // Check that attempting to get an instance of the lease manager when
  188. // none is set throws an exception.
  189. EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
  190. // Check that wrong specification of backend throws an exception.
  191. // (This is really a check on LeaseMgrFactory, but is convenient to
  192. // perform here.)
  193. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  194. NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  195. InvalidParameter);
  196. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  197. INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  198. InvalidType);
  199. // Check that invalid login data causes an exception.
  200. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  201. VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  202. DbOpenError);
  203. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  204. VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
  205. DbOpenError);
  206. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  207. VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  208. DbOpenError);
  209. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  210. VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
  211. DbOpenError);
  212. // Check for missing parameters
  213. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  214. VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  215. NoDatabaseName);
  216. // Tidy up after the test
  217. destroySchema();
  218. }
  219. /// @brief Check the getType() method
  220. ///
  221. /// getType() returns a string giving the type of the backend, which should
  222. /// always be "mysql".
  223. TEST_F(MySqlLeaseMgrTest, getType) {
  224. EXPECT_EQ(std::string("mysql"), lmptr_->getType());
  225. }
  226. /// @brief Check conversion functions
  227. ///
  228. /// The server works using cltt and valid_filetime. In the database, the
  229. /// information is stored as expire_time and valid-lifetime, which are
  230. /// related by
  231. ///
  232. /// expire_time = cltt + valid_lifetime
  233. ///
  234. /// This test checks that the conversion is correct. It does not check that the
  235. /// data is entered into the database correctly, only that the MYSQL_TIME
  236. /// structure used for the entry is correctly set up.
  237. TEST_F(MySqlLeaseMgrTest, checkTimeConversion) {
  238. const time_t cltt = time(NULL);
  239. const uint32_t valid_lft = 86400; // 1 day
  240. struct tm tm_expire;
  241. MYSQL_TIME mysql_expire;
  242. // Work out what the broken-down time will be for one day
  243. // after the current time.
  244. time_t expire_time = cltt + valid_lft;
  245. (void) localtime_r(&expire_time, &tm_expire);
  246. // Convert to the database time
  247. MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, mysql_expire);
  248. // Are the times the same?
  249. EXPECT_EQ(tm_expire.tm_year + 1900, mysql_expire.year);
  250. EXPECT_EQ(tm_expire.tm_mon + 1, mysql_expire.month);
  251. EXPECT_EQ(tm_expire.tm_mday, mysql_expire.day);
  252. EXPECT_EQ(tm_expire.tm_hour, mysql_expire.hour);
  253. EXPECT_EQ(tm_expire.tm_min, mysql_expire.minute);
  254. EXPECT_EQ(tm_expire.tm_sec, mysql_expire.second);
  255. EXPECT_EQ(0, mysql_expire.second_part);
  256. EXPECT_EQ(0, mysql_expire.neg);
  257. // Convert back
  258. time_t converted_cltt = 0;
  259. MySqlLeaseMgr::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
  260. EXPECT_EQ(cltt, converted_cltt);
  261. }
  262. /// @brief Check getName() returns correct database name
  263. TEST_F(MySqlLeaseMgrTest, getName) {
  264. EXPECT_EQ(std::string("keatest"), lmptr_->getName());
  265. }
  266. /// @brief Check that getVersion() returns the expected version
  267. TEST_F(MySqlLeaseMgrTest, checkVersion) {
  268. // Check version
  269. pair<uint32_t, uint32_t> version;
  270. ASSERT_NO_THROW(version = lmptr_->getVersion());
  271. EXPECT_EQ(CURRENT_VERSION_VERSION, version.first);
  272. EXPECT_EQ(CURRENT_VERSION_MINOR, version.second);
  273. }
  274. /// @brief Basic Lease4 Checks
  275. ///
  276. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  277. /// IPv4 address) works.
  278. TEST_F(MySqlLeaseMgrTest, basicLease4) {
  279. testBasicLease4();
  280. }
  281. TEST_F(MySqlLeaseMgrTest, testAddGetDelete6) {
  282. testAddGetDelete6(false);
  283. }
  284. /// @brief Basic Lease4 Checks
  285. ///
  286. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  287. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  288. /// (client-id is optional and may not be present)
  289. TEST_F(MySqlLeaseMgrTest, lease4NullClientId) {
  290. testLease4NullClientId();
  291. }
  292. /// @brief Verify that too long hostname for Lease4 is not accepted.
  293. ///
  294. /// Checks that the it is not possible to create a lease when the hostname
  295. /// length exceeds 255 characters.
  296. TEST_F(MySqlLeaseMgrTest, lease4InvalidHostname) {
  297. testLease4InvalidHostname();
  298. }
  299. /// @brief Basic Lease6 Checks
  300. ///
  301. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  302. /// IPv6 address) works.
  303. TEST_F(MySqlLeaseMgrTest, basicLease6) {
  304. testBasicLease6();
  305. }
  306. /// @brief Verify that too long hostname for Lease6 is not accepted.
  307. ///
  308. /// Checks that the it is not possible to create a lease when the hostname
  309. /// length exceeds 255 characters.
  310. TEST_F(MySqlLeaseMgrTest, lease6InvalidHostname) {
  311. testLease6InvalidHostname();
  312. }
  313. /// @brief Check GetLease4 methods - access by Hardware Address
  314. TEST_F(MySqlLeaseMgrTest, getLease4HWAddr1) {
  315. testGetLease4HWAddr1();
  316. }
  317. /// @brief Check GetLease4 methods - access by Hardware Address
  318. TEST_F(MySqlLeaseMgrTest, getLease4HWAddr2) {
  319. testGetLease4HWAddr2();
  320. }
  321. // @brief Get lease4 by hardware address (2)
  322. //
  323. // Check that the system can cope with getting a hardware address of
  324. // any size.
  325. TEST_F(MySqlLeaseMgrTest, getLease4HWAddrSize) {
  326. testGetLease4HWAddrSize();
  327. }
  328. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  329. ///
  330. /// Adds leases to the database and checks that they can be accessed via
  331. /// a combination of hardware address and subnet ID
  332. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetId) {
  333. testGetLease4HWAddrSubnetId();
  334. }
  335. // @brief Get lease4 by hardware address and subnet ID (2)
  336. //
  337. // Check that the system can cope with getting a hardware address of
  338. // any size.
  339. TEST_F(MySqlLeaseMgrTest, getLease4HWAddrSubnetIdSize) {
  340. testGetLease4HWAddrSubnetIdSize();
  341. }
  342. // This test was derived from memfile.
  343. TEST_F(MySqlLeaseMgrTest, getLease4ClientId) {
  344. testGetLease4ClientId();
  345. }
  346. /// @brief Check GetLease4 methods - access by Client ID
  347. ///
  348. /// Adds leases to the database and checks that they can be accessed via
  349. /// the Client ID.
  350. TEST_F(MySqlLeaseMgrTest, getLease4ClientId2) {
  351. testGetLease4ClientId2();
  352. }
  353. // @brief Get Lease4 by client ID (2)
  354. //
  355. // Check that the system can cope with a client ID of any size.
  356. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSize) {
  357. testGetLease4ClientIdSize();
  358. }
  359. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  360. ///
  361. /// Adds leases to the database and checks that they can be accessed via
  362. /// a combination of client and subnet IDs.
  363. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) {
  364. testGetLease4ClientIdSubnetId();
  365. }
  366. /// @brief Check GetLease6 methods - access by DUID/IAID
  367. ///
  368. /// Adds leases to the database and checks that they can be accessed via
  369. /// a combination of DUID and IAID.
  370. TEST_F(MySqlLeaseMgrTest, getLeases6DuidIaid) {
  371. testGetLeases6DuidIaid();
  372. }
  373. // Check that the system can cope with a DUID of allowed size.
  374. TEST_F(MySqlLeaseMgrTest, getLeases6DuidSize) {
  375. testGetLeases6DuidSize();
  376. }
  377. /// @brief Check that getLease6 methods discriminate by lease type.
  378. ///
  379. /// Adds six leases, two per lease type all with the same duid and iad but
  380. /// with alternating subnet_ids.
  381. /// It then verifies that all of getLeases6() method variants correctly
  382. /// discriminate between the leases based on lease type alone.
  383. TEST_F(MySqlLeaseMgrTest, lease6LeaseTypeCheck) {
  384. testLease6LeaseTypeCheck();
  385. }
  386. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  387. ///
  388. /// Adds leases to the database and checks that they can be accessed via
  389. /// a combination of DIUID and IAID.
  390. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
  391. testGetLease6DuidIaidSubnetId();
  392. }
  393. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  394. testGetLease6DuidIaidSubnetIdSize();
  395. }
  396. /// @brief Lease4 update tests
  397. ///
  398. /// Checks that we are able to update a lease in the database.
  399. TEST_F(MySqlLeaseMgrTest, updateLease4) {
  400. testUpdateLease4();
  401. }
  402. /// @brief Lease6 update tests
  403. ///
  404. /// Checks that we are able to update a lease in the database.
  405. TEST_F(MySqlLeaseMgrTest, updateLease6) {
  406. testUpdateLease6();
  407. }
  408. }; // Of anonymous namespace