mysql_lease_mgr_unittest.cc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 <exceptions/exceptions.h>
  20. #include <gtest/gtest.h>
  21. #include <algorithm>
  22. #include <iostream>
  23. #include <sstream>
  24. #include <string>
  25. #include <utility>
  26. using namespace isc;
  27. using namespace isc::asiolink;
  28. using namespace isc::dhcp;
  29. using namespace isc::dhcp::test;
  30. using namespace std;
  31. namespace {
  32. // This holds statements to create and destroy the schema.
  33. #include "schema_copy.h"
  34. // Connection strings.
  35. // Database: keatest
  36. // Host: localhost
  37. // Username: keatest
  38. // Password: keatest
  39. const char* VALID_TYPE = "type=mysql";
  40. const char* INVALID_TYPE = "type=unknown";
  41. const char* VALID_NAME = "name=keatest";
  42. const char* INVALID_NAME = "name=invalidname";
  43. const char* VALID_HOST = "host=localhost";
  44. const char* INVALID_HOST = "host=invalidhost";
  45. const char* VALID_USER = "user=keatest";
  46. const char* INVALID_USER = "user=invaliduser";
  47. const char* VALID_PASSWORD = "password=keatest";
  48. const char* INVALID_PASSWORD = "password=invalid";
  49. // Given a combination of strings above, produce a connection string.
  50. string connectionString(const char* type, const char* name, const char* host,
  51. const char* user, const char* password) {
  52. const string space = " ";
  53. string result = "";
  54. if (type != NULL) {
  55. result += string(type);
  56. }
  57. if (name != NULL) {
  58. if (! result.empty()) {
  59. result += space;
  60. }
  61. result += string(name);
  62. }
  63. if (host != NULL) {
  64. if (! result.empty()) {
  65. result += space;
  66. }
  67. result += string(host);
  68. }
  69. if (user != NULL) {
  70. if (! result.empty()) {
  71. result += space;
  72. }
  73. result += string(user);
  74. }
  75. if (password != NULL) {
  76. if (! result.empty()) {
  77. result += space;
  78. }
  79. result += string(password);
  80. }
  81. return (result);
  82. }
  83. // Return valid connection string
  84. string
  85. validConnectionString() {
  86. return (connectionString(VALID_TYPE, VALID_NAME, VALID_HOST,
  87. VALID_USER, VALID_PASSWORD));
  88. }
  89. // @brief Clear everything from the database
  90. //
  91. // There is no error checking in this code: if something fails, one of the
  92. // tests will (should) fall over.
  93. void destroySchema() {
  94. MySqlHolder mysql;
  95. // Open database
  96. (void) mysql_real_connect(mysql, "localhost", "keatest",
  97. "keatest", "keatest", 0, NULL, 0);
  98. // Get rid of everything in it.
  99. for (int i = 0; destroy_statement[i] != NULL; ++i) {
  100. (void) mysql_query(mysql, destroy_statement[i]);
  101. }
  102. }
  103. // @brief Create the Schema
  104. //
  105. // Creates all the tables in what is assumed to be an empty database.
  106. //
  107. // There is no error checking in this code: if it fails, one of the tests
  108. // will fall over.
  109. void createSchema() {
  110. MySqlHolder mysql;
  111. // Open database
  112. (void) mysql_real_connect(mysql, "localhost", "keatest",
  113. "keatest", "keatest", 0, NULL, 0);
  114. // Execute creation statements.
  115. for (int i = 0; create_statement[i] != NULL; ++i) {
  116. (void) mysql_query(mysql, create_statement[i]);
  117. }
  118. }
  119. /// @brief Test fixture class for testing MySQL Lease Manager
  120. ///
  121. /// Opens the database prior to each test and closes it afterwards.
  122. /// All pending transactions are deleted prior to closure.
  123. class MySqlLeaseMgrTest : public GenericLeaseMgrTest {
  124. public:
  125. /// @brief Constructor
  126. ///
  127. /// Deletes everything from the database and opens it.
  128. MySqlLeaseMgrTest() {
  129. // Ensure schema is the correct one.
  130. destroySchema();
  131. createSchema();
  132. // Connect to the database
  133. try {
  134. LeaseMgrFactory::create(validConnectionString());
  135. } catch (...) {
  136. std::cerr << "*** ERROR: unable to open database. The test\n"
  137. "*** environment is broken and must be fixed before\n"
  138. "*** the MySQL tests will run correctly.\n"
  139. "*** The reason for the problem is described in the\n"
  140. "*** accompanying exception output.\n";
  141. throw;
  142. }
  143. lmptr_ = &(LeaseMgrFactory::instance());
  144. }
  145. /// @brief Destructor
  146. ///
  147. /// Rolls back all pending transactions. The deletion of lmptr_ will close
  148. /// the database. Then reopen it and delete everything created by the test.
  149. virtual ~MySqlLeaseMgrTest() {
  150. lmptr_->rollback();
  151. LeaseMgrFactory::destroy();
  152. destroySchema();
  153. }
  154. /// @brief Reopen the database
  155. ///
  156. /// Closes the database and re-open it. Anything committed should be
  157. /// visible.
  158. void reopen() {
  159. LeaseMgrFactory::destroy();
  160. LeaseMgrFactory::create(validConnectionString());
  161. lmptr_ = &(LeaseMgrFactory::instance());
  162. }
  163. };
  164. /// @brief Check that database can be opened
  165. ///
  166. /// This test checks if the MySqlLeaseMgr can be instantiated. This happens
  167. /// only if the database can be opened. Note that this is not part of the
  168. /// MySqlLeaseMgr test fixure set. This test checks that the database can be
  169. /// opened: the fixtures assume that and check basic operations.
  170. TEST(MySqlOpenTest, OpenDatabase) {
  171. // Schema needs to be created for the test to work.
  172. destroySchema();
  173. createSchema();
  174. // Check that lease manager open the database opens correctly and tidy up.
  175. // If it fails, print the error message.
  176. try {
  177. LeaseMgrFactory::create(validConnectionString());
  178. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  179. LeaseMgrFactory::destroy();
  180. } catch (const isc::Exception& ex) {
  181. FAIL() << "*** ERROR: unable to open database, reason:\n"
  182. << " " << ex.what() << "\n"
  183. << "*** The test environment is broken and must be fixed\n"
  184. << "*** before the MySQL tests will run correctly.\n";
  185. }
  186. // Check that attempting to get an instance of the lease manager when
  187. // none is set throws an exception.
  188. EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
  189. // Check that wrong specification of backend throws an exception.
  190. // (This is really a check on LeaseMgrFactory, but is convenient to
  191. // perform here.)
  192. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  193. NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  194. InvalidParameter);
  195. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  196. INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  197. InvalidType);
  198. // Check that invalid login data causes an exception.
  199. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  200. VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  201. DbOpenError);
  202. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  203. VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
  204. DbOpenError);
  205. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  206. VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  207. DbOpenError);
  208. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  209. VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
  210. DbOpenError);
  211. // Check for missing parameters
  212. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  213. VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  214. NoDatabaseName);
  215. // Tidy up after the test
  216. destroySchema();
  217. }
  218. /// @brief Check the getType() method
  219. ///
  220. /// getType() returns a string giving the type of the backend, which should
  221. /// always be "mysql".
  222. TEST_F(MySqlLeaseMgrTest, getType) {
  223. EXPECT_EQ(std::string("mysql"), lmptr_->getType());
  224. }
  225. /// @brief Check conversion functions
  226. ///
  227. /// The server works using cltt and valid_filetime. In the database, the
  228. /// information is stored as expire_time and valid-lifetime, which are
  229. /// related by
  230. ///
  231. /// expire_time = cltt + valid_lifetime
  232. ///
  233. /// This test checks that the conversion is correct. It does not check that the
  234. /// data is entered into the database correctly, only that the MYSQL_TIME
  235. /// structure used for the entry is correctly set up.
  236. TEST_F(MySqlLeaseMgrTest, checkTimeConversion) {
  237. const time_t cltt = time(NULL);
  238. const uint32_t valid_lft = 86400; // 1 day
  239. struct tm tm_expire;
  240. MYSQL_TIME mysql_expire;
  241. // Work out what the broken-down time will be for one day
  242. // after the current time.
  243. time_t expire_time = cltt + valid_lft;
  244. (void) localtime_r(&expire_time, &tm_expire);
  245. // Convert to the database time
  246. MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, mysql_expire);
  247. // Are the times the same?
  248. EXPECT_EQ(tm_expire.tm_year + 1900, mysql_expire.year);
  249. EXPECT_EQ(tm_expire.tm_mon + 1, mysql_expire.month);
  250. EXPECT_EQ(tm_expire.tm_mday, mysql_expire.day);
  251. EXPECT_EQ(tm_expire.tm_hour, mysql_expire.hour);
  252. EXPECT_EQ(tm_expire.tm_min, mysql_expire.minute);
  253. EXPECT_EQ(tm_expire.tm_sec, mysql_expire.second);
  254. EXPECT_EQ(0, mysql_expire.second_part);
  255. EXPECT_EQ(0, mysql_expire.neg);
  256. // Convert back
  257. time_t converted_cltt = 0;
  258. MySqlLeaseMgr::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
  259. EXPECT_EQ(cltt, converted_cltt);
  260. }
  261. /// @brief Check getName() returns correct database name
  262. TEST_F(MySqlLeaseMgrTest, getName) {
  263. EXPECT_EQ(std::string("keatest"), lmptr_->getName());
  264. }
  265. /// @brief Check that getVersion() returns the expected version
  266. TEST_F(MySqlLeaseMgrTest, checkVersion) {
  267. // Check version
  268. pair<uint32_t, uint32_t> version;
  269. ASSERT_NO_THROW(version = lmptr_->getVersion());
  270. EXPECT_EQ(CURRENT_VERSION_VERSION, version.first);
  271. EXPECT_EQ(CURRENT_VERSION_MINOR, version.second);
  272. }
  273. /// @brief Basic Lease4 Checks
  274. ///
  275. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  276. /// IPv4 address) works.
  277. TEST_F(MySqlLeaseMgrTest, basicLease4) {
  278. testBasicLease4();
  279. }
  280. TEST_F(MySqlLeaseMgrTest, testAddGetDelete6) {
  281. testAddGetDelete6(false);
  282. }
  283. /// @brief Basic Lease4 Checks
  284. ///
  285. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  286. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  287. /// (client-id is optional and may not be present)
  288. TEST_F(MySqlLeaseMgrTest, lease4NullClientId) {
  289. testLease4NullClientId();
  290. }
  291. /// @brief Verify that too long hostname for Lease4 is not accepted.
  292. ///
  293. /// Checks that the it is not possible to create a lease when the hostname
  294. /// length exceeds 255 characters.
  295. TEST_F(MySqlLeaseMgrTest, lease4InvalidHostname) {
  296. // Get the leases to be used for the test.
  297. vector<Lease4Ptr> leases = createLeases4();
  298. // Create a dummy hostname, consisting of 255 characters.
  299. leases[1]->hostname_.assign(255, 'a');
  300. ASSERT_TRUE(lmptr_->addLease(leases[1]));
  301. // The new lease must be in the database.
  302. Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
  303. detailCompareLease(leases[1], l_returned);
  304. // Let's delete the lease, so as we can try to add it again with
  305. // invalid hostname.
  306. EXPECT_TRUE(lmptr_->deleteLease(ioaddress4_[1]));
  307. // Create a hostname with 256 characters. It should not be accepted.
  308. leases[1]->hostname_.assign(256, 'a');
  309. EXPECT_THROW(lmptr_->addLease(leases[1]), DbOperationError);
  310. }
  311. /// @brief Basic Lease6 Checks
  312. ///
  313. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  314. /// IPv6 address) works.
  315. TEST_F(MySqlLeaseMgrTest, basicLease6) {
  316. testBasicLease6();
  317. }
  318. /// @brief Verify that too long hostname for Lease6 is not accepted.
  319. ///
  320. /// Checks that the it is not possible to create a lease when the hostname
  321. /// length exceeds 255 characters.
  322. TEST_F(MySqlLeaseMgrTest, lease6InvalidHostname) {
  323. // Get the leases to be used for the test.
  324. vector<Lease6Ptr> leases = createLeases6();
  325. // Create a dummy hostname, consisting of 255 characters.
  326. leases[1]->hostname_.assign(255, 'a');
  327. ASSERT_TRUE(lmptr_->addLease(leases[1]));
  328. // The new lease must be in the database.
  329. Lease6Ptr l_returned = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  330. detailCompareLease(leases[1], l_returned);
  331. // Let's delete the lease, so as we can try to add it again with
  332. // invalid hostname.
  333. EXPECT_TRUE(lmptr_->deleteLease(ioaddress6_[1]));
  334. // Create a hostname with 256 characters. It should not be accepted.
  335. leases[1]->hostname_.assign(256, 'a');
  336. EXPECT_THROW(lmptr_->addLease(leases[1]), DbOperationError);
  337. }
  338. /// @brief Check GetLease4 methods - access by Hardware Address
  339. TEST_F(MySqlLeaseMgrTest, getLease4HWAddr1) {
  340. testGetLease4HWAddr1();
  341. }
  342. /// @brief Check GetLease4 methods - access by Hardware Address
  343. TEST_F(MySqlLeaseMgrTest, getLease4HWAddr2) {
  344. testGetLease4HWAddr2();
  345. }
  346. // @brief Get lease4 by hardware address (2)
  347. //
  348. // Check that the system can cope with getting a hardware address of
  349. // any size.
  350. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSize) {
  351. // Create leases, although we need only one.
  352. vector<Lease4Ptr> leases = createLeases4();
  353. // Now add leases with increasing hardware address size.
  354. for (uint8_t i = 0; i <= HWAddr::MAX_HWADDR_LEN; ++i) {
  355. leases[1]->hwaddr_.resize(i, i);
  356. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  357. /// @todo: Simply use HWAddr directly once 2589 is implemented
  358. Lease4Collection returned =
  359. lmptr_->getLease4(HWAddr(leases[1]->hwaddr_, HTYPE_ETHER));
  360. ASSERT_EQ(1, returned.size());
  361. detailCompareLease(leases[1], *returned.begin());
  362. (void) lmptr_->deleteLease(leases[1]->addr_);
  363. }
  364. // Database should not let us add one that is too big
  365. // (The 42 is a random value put in each byte of the address.)
  366. /// @todo: 2589 will make this test impossible
  367. leases[1]->hwaddr_.resize(HWAddr::MAX_HWADDR_LEN + 100, 42);
  368. EXPECT_THROW(lmptr_->addLease(leases[1]), isc::dhcp::DbOperationError);
  369. }
  370. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  371. ///
  372. /// Adds leases to the database and checks that they can be accessed via
  373. /// a combination of hardware address and subnet ID
  374. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetId) {
  375. // Get the leases to be used for the test and add to the database
  376. vector<Lease4Ptr> leases = createLeases4();
  377. for (int i = 0; i < leases.size(); ++i) {
  378. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  379. }
  380. // Get the leases matching the hardware address of lease 1 and
  381. // subnet ID of lease 1. Result should be a single lease - lease 1.
  382. /// @todo: Simply use HWAddr directly once 2589 is implemented
  383. Lease4Ptr returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_,
  384. HTYPE_ETHER), leases[1]->subnet_id_);
  385. ASSERT_TRUE(returned);
  386. detailCompareLease(leases[1], returned);
  387. // Try for a match to the hardware address of lease 1 and the wrong
  388. // subnet ID.
  389. /// @todo: Simply use HWAddr directly once 2589 is implemented
  390. returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_, HTYPE_ETHER),
  391. leases[1]->subnet_id_ + 1);
  392. EXPECT_FALSE(returned);
  393. // Try for a match to the subnet ID of lease 1 (and lease 4) but
  394. // the wrong hardware address.
  395. vector<uint8_t> invalid_hwaddr(15, 0x77);
  396. /// @todo: Simply use HWAddr directly once 2589 is implemented
  397. returned = lmptr_->getLease4(HWAddr(invalid_hwaddr, HTYPE_ETHER),
  398. leases[1]->subnet_id_);
  399. EXPECT_FALSE(returned);
  400. // Try for a match to an unknown hardware address and an unknown
  401. // subnet ID.
  402. /// @todo: Simply use HWAddr directly once 2589 is implemented
  403. returned = lmptr_->getLease4(HWAddr(invalid_hwaddr, HTYPE_ETHER),
  404. leases[1]->subnet_id_ + 1);
  405. EXPECT_FALSE(returned);
  406. // Add a second lease with the same values as the first and check that
  407. // an attempt to access the database by these parameters throws a
  408. // "multiple records" exception. (We expect there to be only one record
  409. // with that combination, so getting them via getLeaseX() (as opposed
  410. // to getLeaseXCollection() should throw an exception.)
  411. EXPECT_TRUE(lmptr_->deleteLease(leases[2]->addr_));
  412. leases[1]->addr_ = leases[2]->addr_;
  413. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  414. /// @todo: Simply use HWAddr directly once 2589 is implemented
  415. EXPECT_THROW(returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_,
  416. HTYPE_ETHER),
  417. leases[1]->subnet_id_),
  418. isc::dhcp::MultipleRecords);
  419. }
  420. // @brief Get lease4 by hardware address and subnet ID (2)
  421. //
  422. // Check that the system can cope with getting a hardware address of
  423. // any size.
  424. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetIdSize) {
  425. // Create leases, although we need only one.
  426. vector<Lease4Ptr> leases = createLeases4();
  427. // Now add leases with increasing hardware address size and check
  428. // that they can be retrieved.
  429. for (uint8_t i = 0; i <= HWAddr::MAX_HWADDR_LEN; ++i) {
  430. leases[1]->hwaddr_.resize(i, i);
  431. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  432. /// @todo: Simply use HWAddr directly once 2589 is implemented
  433. Lease4Ptr returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_,
  434. HTYPE_ETHER),
  435. leases[1]->subnet_id_);
  436. ASSERT_TRUE(returned);
  437. detailCompareLease(leases[1], returned);
  438. (void) lmptr_->deleteLease(leases[1]->addr_);
  439. }
  440. // Database should not let us add one that is too big
  441. // (The 42 is a random value put in each byte of the address.)
  442. leases[1]->hwaddr_.resize(HWAddr::MAX_HWADDR_LEN + 100, 42);
  443. EXPECT_THROW(lmptr_->addLease(leases[1]), isc::dhcp::DbOperationError);
  444. }
  445. /// @brief Check GetLease4 methods - access by Client ID
  446. ///
  447. /// Adds leases to the database and checks that they can be accessed via
  448. /// the Client ID.
  449. TEST_F(MySqlLeaseMgrTest, getLease4ClientId) {
  450. // Get the leases to be used for the test and add to the database
  451. vector<Lease4Ptr> leases = createLeases4();
  452. for (int i = 0; i < leases.size(); ++i) {
  453. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  454. }
  455. // Get the leases matching the Client ID address of lease 1
  456. Lease4Collection returned = lmptr_->getLease4(*leases[1]->client_id_);
  457. // Should be four leases, matching leases[1], [4], [5] and [6].
  458. ASSERT_EQ(4, returned.size());
  459. // Easiest way to check is to look at the addresses.
  460. vector<string> addresses;
  461. for (Lease4Collection::const_iterator i = returned.begin();
  462. i != returned.end(); ++i) {
  463. addresses.push_back((*i)->addr_.toText());
  464. }
  465. sort(addresses.begin(), addresses.end());
  466. EXPECT_EQ(straddress4_[1], addresses[0]);
  467. EXPECT_EQ(straddress4_[4], addresses[1]);
  468. EXPECT_EQ(straddress4_[5], addresses[2]);
  469. EXPECT_EQ(straddress4_[6], addresses[3]);
  470. // Repeat test with just one expected match
  471. returned = lmptr_->getLease4(*leases[3]->client_id_);
  472. ASSERT_EQ(1, returned.size());
  473. detailCompareLease(leases[3], *returned.begin());
  474. // Check that client-id is NULL
  475. EXPECT_FALSE(leases[7]->client_id_);
  476. HWAddr tmp(leases[7]->hwaddr_, HTYPE_ETHER);
  477. returned = lmptr_->getLease4(tmp);
  478. ASSERT_EQ(1, returned.size());
  479. detailCompareLease(leases[7], *returned.begin());
  480. // Try to get something with invalid client ID
  481. const uint8_t invalid_data[] = {0, 0, 0};
  482. ClientId invalid(invalid_data, sizeof(invalid_data));
  483. returned = lmptr_->getLease4(invalid);
  484. EXPECT_EQ(0, returned.size());
  485. }
  486. // @brief Get Lease4 by client ID (2)
  487. //
  488. // Check that the system can cope with a client ID of any size.
  489. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSize) {
  490. // Create leases, although we need only one.
  491. vector<Lease4Ptr> leases = createLeases4();
  492. // Now add leases with increasing Client ID size can be retrieved.
  493. // For speed, go from 0 to 128 is steps of 16.
  494. // Intermediate client_id_max is to overcome problem if
  495. // ClientId::MAX_CLIENT_ID_LEN is used in an EXPECT_EQ.
  496. int client_id_max = ClientId::MAX_CLIENT_ID_LEN;
  497. EXPECT_EQ(128, client_id_max);
  498. int client_id_min = ClientId::MIN_CLIENT_ID_LEN;
  499. EXPECT_EQ(2, client_id_min); // See RFC2132, section 9.14
  500. for (uint8_t i = client_id_min; i <= client_id_max; i += 16) {
  501. vector<uint8_t> clientid_vec(i, i);
  502. leases[1]->client_id_.reset(new ClientId(clientid_vec));
  503. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  504. Lease4Collection returned = lmptr_->getLease4(*leases[1]->client_id_);
  505. ASSERT_TRUE(returned.size() == 1);
  506. detailCompareLease(leases[1], *returned.begin());
  507. (void) lmptr_->deleteLease(leases[1]->addr_);
  508. }
  509. // Don't bother to check client IDs longer than the maximum -
  510. // these cannot be constructed, and that limitation is tested
  511. // in the DUID/Client ID unit tests.
  512. }
  513. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  514. ///
  515. /// Adds leases to the database and checks that they can be accessed via
  516. /// a combination of client and subnet IDs.
  517. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) {
  518. // Get the leases to be used for the test and add to the database
  519. vector<Lease4Ptr> leases = createLeases4();
  520. for (int i = 0; i < leases.size(); ++i) {
  521. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  522. }
  523. // Get the leases matching the client ID of lease 1 and
  524. // subnet ID of lease 1. Result should be a single lease - lease 1.
  525. Lease4Ptr returned = lmptr_->getLease4(*leases[1]->client_id_,
  526. leases[1]->subnet_id_);
  527. ASSERT_TRUE(returned);
  528. detailCompareLease(leases[1], returned);
  529. // Try for a match to the client ID of lease 1 and the wrong
  530. // subnet ID.
  531. returned = lmptr_->getLease4(*leases[1]->client_id_,
  532. leases[1]->subnet_id_ + 1);
  533. EXPECT_FALSE(returned);
  534. // Try for a match to the subnet ID of lease 1 (and lease 4) but
  535. // the wrong client ID
  536. const uint8_t invalid_data[] = {0, 0, 0};
  537. ClientId invalid(invalid_data, sizeof(invalid_data));
  538. returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_);
  539. EXPECT_FALSE(returned);
  540. // Try for a match to an unknown hardware address and an unknown
  541. // subnet ID.
  542. returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_ + 1);
  543. EXPECT_FALSE(returned);
  544. }
  545. /// @brief Check GetLease6 methods - access by DUID/IAID
  546. ///
  547. /// Adds leases to the database and checks that they can be accessed via
  548. /// a combination of DIUID and IAID.
  549. TEST_F(MySqlLeaseMgrTest, getLeases6DuidIaid) {
  550. // Get the leases to be used for the test.
  551. vector<Lease6Ptr> leases = createLeases6();
  552. ASSERT_LE(6, leases.size()); // Expect to access leases 0 through 5
  553. // Add them to the database
  554. for (int i = 0; i < leases.size(); ++i) {
  555. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  556. }
  557. // Get the leases matching the DUID and IAID of lease[1].
  558. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[1],
  559. *leases[1]->duid_,
  560. leases[1]->iaid_);
  561. // Should be two leases, matching leases[1] and [4].
  562. ASSERT_EQ(2, returned.size());
  563. // Easiest way to check is to look at the addresses.
  564. vector<string> addresses;
  565. for (Lease6Collection::const_iterator i = returned.begin();
  566. i != returned.end(); ++i) {
  567. addresses.push_back((*i)->addr_.toText());
  568. }
  569. sort(addresses.begin(), addresses.end());
  570. EXPECT_EQ(straddress6_[1], addresses[0]);
  571. EXPECT_EQ(straddress6_[4], addresses[1]);
  572. // Check that nothing is returned when either the IAID or DUID match
  573. // nothing.
  574. returned = lmptr_->getLeases6(leasetype6_[1], *leases[1]->duid_,
  575. leases[1]->iaid_ + 1);
  576. EXPECT_EQ(0, returned.size());
  577. // Alter the leases[1] DUID to match nothing in the database.
  578. vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
  579. ++duid_vector[0];
  580. DUID new_duid(duid_vector);
  581. returned = lmptr_->getLeases6(leasetype6_[1], new_duid, leases[1]->iaid_);
  582. EXPECT_EQ(0, returned.size());
  583. }
  584. // @brief Get Lease4 by DUID and IAID (2)
  585. //
  586. // Check that the system can cope with a DUID of any size.
  587. TEST_F(MySqlLeaseMgrTest, getLeases6DuidIaidSize) {
  588. // Create leases, although we need only one.
  589. vector<Lease6Ptr> leases = createLeases6();
  590. // Now add leases with increasing DUID size can be retrieved.
  591. // For speed, go from 0 to 128 is steps of 16.
  592. int duid_max = DUID::MAX_DUID_LEN;
  593. EXPECT_EQ(128, duid_max);
  594. int duid_min = DUID::MIN_DUID_LEN;
  595. EXPECT_EQ(1, duid_min);
  596. for (uint8_t i = duid_min; i <= duid_max; i += 16) {
  597. vector<uint8_t> duid_vec(i, i);
  598. leases[1]->duid_.reset(new DUID(duid_vec));
  599. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  600. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[1],
  601. *leases[1]->duid_,
  602. leases[1]->iaid_);
  603. ASSERT_EQ(1, returned.size());
  604. detailCompareLease(leases[1], *returned.begin());
  605. (void) lmptr_->deleteLease(leases[1]->addr_);
  606. }
  607. // Don't bother to check DUIDs longer than the maximum - these cannot be
  608. // constructed, and that limitation is tested in the DUID/Client ID unit
  609. // tests.
  610. }
  611. /// @brief Check that getLease6 methods discriminate by lease type.
  612. ///
  613. /// Adds six leases, two per lease type all with the same duid and iad but
  614. /// with alternating subnet_ids.
  615. /// It then verifies that all of getLeases6() method variants correctly
  616. /// discriminate between the leases based on lease type alone.
  617. TEST_F(MySqlLeaseMgrTest, lease6LeaseTypeCheck) {
  618. Lease6Ptr empty_lease(new Lease6());
  619. DuidPtr duid(new DUID(vector<uint8_t>(8, 0x77)));
  620. // Initialize unused fields.
  621. empty_lease->t1_ = 0; // Not saved
  622. empty_lease->t2_ = 0; // Not saved
  623. empty_lease->fixed_ = false; // Unused
  624. empty_lease->comments_ = std::string(""); // Unused
  625. empty_lease->iaid_ = 142;
  626. empty_lease->duid_ = DuidPtr(new DUID(*duid));
  627. empty_lease->subnet_id_ = 23;
  628. empty_lease->preferred_lft_ = 100;
  629. empty_lease->valid_lft_ = 100;
  630. empty_lease->cltt_ = 100;
  631. empty_lease->fqdn_fwd_ = true;
  632. empty_lease->fqdn_rev_ = true;
  633. empty_lease->hostname_ = "myhost.example.com.";
  634. empty_lease->prefixlen_ = 4;
  635. // Make Two leases per lease type, all with the same DUID, IAID but
  636. // alternate the subnet_ids.
  637. vector<Lease6Ptr> leases;
  638. for (int i = 0; i < 6; ++i) {
  639. Lease6Ptr lease(new Lease6(*empty_lease));
  640. lease->type_ = leasetype6_[i / 2];
  641. lease->addr_ = IOAddress(straddress6_[i]);
  642. lease->subnet_id_ += (i % 2);
  643. leases.push_back(lease);
  644. EXPECT_TRUE(lmptr_->addLease(lease));
  645. }
  646. // Verify getting a single lease by type and address.
  647. for (int i = 0; i < 6; ++i) {
  648. // Look for exact match for each lease type.
  649. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[i / 2],
  650. leases[i]->addr_);
  651. // We should match one per lease type.
  652. ASSERT_TRUE(returned);
  653. EXPECT_TRUE(*returned == *leases[i]);
  654. // Same address but wrong lease type, should not match.
  655. returned = lmptr_->getLease6(leasetype6_[i / 2 + 1], leases[i]->addr_);
  656. ASSERT_FALSE(returned);
  657. }
  658. // Verify getting a collection of leases by type, DUID, and IAID.
  659. // Iterate over the lease types, asking for leases based on
  660. // lease type, DUID, and IAID.
  661. for (int i = 0; i < 3; ++i) {
  662. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[i],
  663. *duid, 142);
  664. // We should match two per lease type.
  665. ASSERT_EQ(2, returned.size());
  666. // Collection order returned is not guaranteed.
  667. // Easiest way to check is to look at the addresses.
  668. vector<string> addresses;
  669. for (Lease6Collection::const_iterator it = returned.begin();
  670. it != returned.end(); ++it) {
  671. addresses.push_back((*it)->addr_.toText());
  672. }
  673. sort(addresses.begin(), addresses.end());
  674. // Now verify that the lease addresses match.
  675. EXPECT_EQ(addresses[0], leases[(i * 2)]->addr_.toText());
  676. EXPECT_EQ(addresses[1], leases[(i * 2 + 1)]->addr_.toText());
  677. }
  678. // Verify getting a collection of leases by type, DUID, IAID, and subnet id.
  679. // Iterate over the lease types, asking for leases based on
  680. // lease type, DUID, IAID, and subnet_id.
  681. for (int i = 0; i < 3; ++i) {
  682. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[i],
  683. *duid, 142, 23);
  684. // We should match one per lease type.
  685. ASSERT_EQ(1, returned.size());
  686. EXPECT_TRUE(*(returned[0]) == *leases[i * 2]);
  687. }
  688. // Verify getting a single lease by type, duid, iad, and subnet id.
  689. for (int i = 0; i < 6; ++i) {
  690. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[i / 2],
  691. *duid, 142, (23 + (i % 2)));
  692. // We should match one per lease type.
  693. ASSERT_TRUE(returned);
  694. EXPECT_TRUE(*returned == *leases[i]);
  695. }
  696. }
  697. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  698. ///
  699. /// Adds leases to the database and checks that they can be accessed via
  700. /// a combination of DIUID and IAID.
  701. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
  702. // Get the leases to be used for the test and add them to the database.
  703. vector<Lease6Ptr> leases = createLeases6();
  704. for (int i = 0; i < leases.size(); ++i) {
  705. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  706. }
  707. // Get the leases matching the DUID and IAID of lease[1].
  708. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  709. leases[1]->iaid_,
  710. leases[1]->subnet_id_);
  711. ASSERT_TRUE(returned);
  712. EXPECT_TRUE(*returned == *leases[1]);
  713. // Modify each of the three parameters (DUID, IAID, Subnet ID) and
  714. // check that nothing is returned.
  715. returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  716. leases[1]->iaid_ + 1, leases[1]->subnet_id_);
  717. EXPECT_FALSE(returned);
  718. returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  719. leases[1]->iaid_, leases[1]->subnet_id_ + 1);
  720. EXPECT_FALSE(returned);
  721. // Alter the leases[1] DUID to match nothing in the database.
  722. vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
  723. ++duid_vector[0];
  724. DUID new_duid(duid_vector);
  725. returned = lmptr_->getLease6(leasetype6_[1], new_duid, leases[1]->iaid_,
  726. leases[1]->subnet_id_);
  727. EXPECT_FALSE(returned);
  728. }
  729. // @brief Get Lease4 by DUID, IAID & subnet ID (2)
  730. //
  731. // Check that the system can cope with a DUID of any size.
  732. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  733. // Create leases, although we need only one.
  734. vector<Lease6Ptr> leases = createLeases6();
  735. // Now add leases with increasing DUID size can be retrieved.
  736. // For speed, go from 0 to 128 is steps of 16.
  737. int duid_max = DUID::MAX_DUID_LEN;
  738. EXPECT_EQ(128, duid_max);
  739. int duid_min = DUID::MIN_DUID_LEN;
  740. EXPECT_EQ(1, duid_min);
  741. for (uint8_t i = duid_min; i <= duid_max; i += 16) {
  742. vector<uint8_t> duid_vec(i, i);
  743. leases[1]->duid_.reset(new DUID(duid_vec));
  744. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  745. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  746. leases[1]->iaid_,
  747. leases[1]->subnet_id_);
  748. ASSERT_TRUE(returned);
  749. detailCompareLease(leases[1], returned);
  750. (void) lmptr_->deleteLease(leases[1]->addr_);
  751. }
  752. // Don't bother to check DUIDs longer than the maximum - these cannot be
  753. // constructed, and that limitation is tested in the DUID/Client ID unit
  754. // tests.
  755. }
  756. /// @brief Lease4 update tests
  757. ///
  758. /// Checks that we are able to update a lease in the database.
  759. TEST_F(MySqlLeaseMgrTest, updateLease4) {
  760. // Get the leases to be used for the test and add them to the database.
  761. vector<Lease4Ptr> leases = createLeases4();
  762. for (int i = 0; i < leases.size(); ++i) {
  763. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  764. }
  765. // Modify some fields in lease 1 (not the address) and update it.
  766. ++leases[1]->subnet_id_;
  767. leases[1]->valid_lft_ *= 2;
  768. leases[1]->hostname_ = "modified.hostname.";
  769. leases[1]->fqdn_fwd_ = !leases[1]->fqdn_fwd_;
  770. leases[1]->fqdn_rev_ = !leases[1]->fqdn_rev_;;
  771. lmptr_->updateLease4(leases[1]);
  772. // ... and check what is returned is what is expected.
  773. Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
  774. ASSERT_TRUE(l_returned);
  775. detailCompareLease(leases[1], l_returned);
  776. // Alter the lease again and check.
  777. ++leases[1]->subnet_id_;
  778. leases[1]->cltt_ += 6;
  779. lmptr_->updateLease4(leases[1]);
  780. // Explicitly clear the returned pointer before getting new data to ensure
  781. // that the new data is returned.
  782. l_returned.reset();
  783. l_returned = lmptr_->getLease4(ioaddress4_[1]);
  784. ASSERT_TRUE(l_returned);
  785. detailCompareLease(leases[1], l_returned);
  786. // Check we can do an update without changing data.
  787. lmptr_->updateLease4(leases[1]);
  788. l_returned.reset();
  789. l_returned = lmptr_->getLease4(ioaddress4_[1]);
  790. ASSERT_TRUE(l_returned);
  791. detailCompareLease(leases[1], l_returned);
  792. // Try to update the lease with the too long hostname.
  793. leases[1]->hostname_.assign(256, 'a');
  794. EXPECT_THROW(lmptr_->updateLease4(leases[1]), isc::dhcp::DbOperationError);
  795. // Try updating a lease not in the database.
  796. lmptr_->deleteLease(ioaddress4_[2]);
  797. EXPECT_THROW(lmptr_->updateLease4(leases[2]), isc::dhcp::NoSuchLease);
  798. }
  799. /// @brief Lease6 update tests
  800. ///
  801. /// Checks that we are able to update a lease in the database.
  802. TEST_F(MySqlLeaseMgrTest, updateLease6) {
  803. // Get the leases to be used for the test.
  804. vector<Lease6Ptr> leases = createLeases6();
  805. ASSERT_LE(3, leases.size()); // Expect to access leases 0 through 2
  806. // Add a lease to the database and check that the lease is there.
  807. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  808. lmptr_->commit();
  809. Lease6Ptr l_returned = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  810. ASSERT_TRUE(l_returned);
  811. detailCompareLease(leases[1], l_returned);
  812. // Modify some fields in lease 1 (not the address) and update it.
  813. ++leases[1]->iaid_;
  814. leases[1]->type_ = Lease::TYPE_PD;
  815. leases[1]->valid_lft_ *= 2;
  816. leases[1]->hostname_ = "modified.hostname.v6.";
  817. leases[1]->fqdn_fwd_ = !leases[1]->fqdn_fwd_;
  818. leases[1]->fqdn_rev_ = !leases[1]->fqdn_rev_;;
  819. lmptr_->updateLease6(leases[1]);
  820. lmptr_->commit();
  821. // ... and check what is returned is what is expected.
  822. l_returned.reset();
  823. l_returned = lmptr_->getLease6(Lease::TYPE_PD, ioaddress6_[1]);
  824. ASSERT_TRUE(l_returned);
  825. detailCompareLease(leases[1], l_returned);
  826. // Alter the lease again and check.
  827. ++leases[1]->iaid_;
  828. leases[1]->type_ = Lease::TYPE_TA;
  829. leases[1]->cltt_ += 6;
  830. leases[1]->prefixlen_ = 93;
  831. lmptr_->updateLease6(leases[1]);
  832. l_returned.reset();
  833. l_returned = lmptr_->getLease6(Lease::TYPE_TA, ioaddress6_[1]);
  834. ASSERT_TRUE(l_returned);
  835. detailCompareLease(leases[1], l_returned);
  836. // Check we can do an update without changing data.
  837. lmptr_->updateLease6(leases[1]);
  838. l_returned.reset();
  839. l_returned = lmptr_->getLease6(Lease::TYPE_TA, ioaddress6_[1]);
  840. ASSERT_TRUE(l_returned);
  841. detailCompareLease(leases[1], l_returned);
  842. // Try to update the lease with the too long hostname.
  843. leases[1]->hostname_.assign(256, 'a');
  844. EXPECT_THROW(lmptr_->updateLease6(leases[1]), isc::dhcp::DbOperationError);
  845. // Try updating a lease not in the database.
  846. EXPECT_THROW(lmptr_->updateLease6(leases[2]), isc::dhcp::NoSuchLease);
  847. }
  848. }; // Of anonymous namespace