mysql_lease_mgr_unittest.cc 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. // Get the leases to be used for the test.
  290. vector<Lease4Ptr> leases = createLeases4();
  291. // Let's clear client-id pointers
  292. leases[1]->client_id_ = ClientIdPtr();
  293. leases[2]->client_id_ = ClientIdPtr();
  294. leases[3]->client_id_ = ClientIdPtr();
  295. // Start the tests. Add three leases to the database, read them back and
  296. // check they are what we think they are.
  297. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  298. EXPECT_TRUE(lmptr_->addLease(leases[2]));
  299. EXPECT_TRUE(lmptr_->addLease(leases[3]));
  300. lmptr_->commit();
  301. // Reopen the database to ensure that they actually got stored.
  302. reopen();
  303. Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
  304. ASSERT_TRUE(l_returned);
  305. detailCompareLease(leases[1], l_returned);
  306. l_returned = lmptr_->getLease4(ioaddress4_[2]);
  307. ASSERT_TRUE(l_returned);
  308. detailCompareLease(leases[2], l_returned);
  309. l_returned = lmptr_->getLease4(ioaddress4_[3]);
  310. ASSERT_TRUE(l_returned);
  311. detailCompareLease(leases[3], l_returned);
  312. // Check that we can't add a second lease with the same address
  313. EXPECT_FALSE(lmptr_->addLease(leases[1]));
  314. // Check that we can get the lease by HWAddr
  315. HWAddr tmp(leases[2]->hwaddr_, HTYPE_ETHER);
  316. Lease4Collection returned = lmptr_->getLease4(tmp);
  317. ASSERT_EQ(1, returned.size());
  318. detailCompareLease(leases[2], *returned.begin());
  319. l_returned = lmptr_->getLease4(tmp, leases[2]->subnet_id_);
  320. ASSERT_TRUE(l_returned);
  321. detailCompareLease(leases[2], l_returned);
  322. // Check that we can update the lease
  323. // Modify some fields in lease 1 (not the address) and update it.
  324. ++leases[1]->subnet_id_;
  325. leases[1]->valid_lft_ *= 2;
  326. lmptr_->updateLease4(leases[1]);
  327. // ... and check that the lease is indeed updated
  328. l_returned = lmptr_->getLease4(ioaddress4_[1]);
  329. ASSERT_TRUE(l_returned);
  330. detailCompareLease(leases[1], l_returned);
  331. // Delete a lease, check that it's gone, and that we can't delete it
  332. // a second time.
  333. EXPECT_TRUE(lmptr_->deleteLease(ioaddress4_[1]));
  334. l_returned = lmptr_->getLease4(ioaddress4_[1]);
  335. EXPECT_FALSE(l_returned);
  336. EXPECT_FALSE(lmptr_->deleteLease(ioaddress4_[1]));
  337. // Check that the second address is still there.
  338. l_returned = lmptr_->getLease4(ioaddress4_[2]);
  339. ASSERT_TRUE(l_returned);
  340. detailCompareLease(leases[2], l_returned);
  341. }
  342. /// @brief Verify that too long hostname for Lease4 is not accepted.
  343. ///
  344. /// Checks that the it is not possible to create a lease when the hostname
  345. /// length exceeds 255 characters.
  346. TEST_F(MySqlLeaseMgrTest, lease4InvalidHostname) {
  347. // Get the leases to be used for the test.
  348. vector<Lease4Ptr> leases = createLeases4();
  349. // Create a dummy hostname, consisting of 255 characters.
  350. leases[1]->hostname_.assign(255, 'a');
  351. ASSERT_TRUE(lmptr_->addLease(leases[1]));
  352. // The new lease must be in the database.
  353. Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
  354. detailCompareLease(leases[1], l_returned);
  355. // Let's delete the lease, so as we can try to add it again with
  356. // invalid hostname.
  357. EXPECT_TRUE(lmptr_->deleteLease(ioaddress4_[1]));
  358. // Create a hostname with 256 characters. It should not be accepted.
  359. leases[1]->hostname_.assign(256, 'a');
  360. EXPECT_THROW(lmptr_->addLease(leases[1]), DbOperationError);
  361. }
  362. /// @brief Basic Lease6 Checks
  363. ///
  364. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  365. /// IPv6 address) works.
  366. TEST_F(MySqlLeaseMgrTest, basicLease6) {
  367. // Get the leases to be used for the test.
  368. vector<Lease6Ptr> leases = createLeases6();
  369. // Start the tests. Add three leases to the database, read them back and
  370. // check they are what we think they are.
  371. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  372. EXPECT_TRUE(lmptr_->addLease(leases[2]));
  373. EXPECT_TRUE(lmptr_->addLease(leases[3]));
  374. lmptr_->commit();
  375. // Reopen the database to ensure that they actually got stored.
  376. reopen();
  377. Lease6Ptr l_returned = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  378. ASSERT_TRUE(l_returned);
  379. detailCompareLease(leases[1], l_returned);
  380. l_returned = lmptr_->getLease6(leasetype6_[2], ioaddress6_[2]);
  381. ASSERT_TRUE(l_returned);
  382. detailCompareLease(leases[2], l_returned);
  383. l_returned = lmptr_->getLease6(leasetype6_[3], ioaddress6_[3]);
  384. ASSERT_TRUE(l_returned);
  385. detailCompareLease(leases[3], l_returned);
  386. // Check that we can't add a second lease with the same address
  387. EXPECT_FALSE(lmptr_->addLease(leases[1]));
  388. // Delete a lease, check that it's gone, and that we can't delete it
  389. // a second time.
  390. EXPECT_TRUE(lmptr_->deleteLease(ioaddress6_[1]));
  391. l_returned = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  392. EXPECT_FALSE(l_returned);
  393. EXPECT_FALSE(lmptr_->deleteLease(ioaddress6_[1]));
  394. // Check that the second address is still there.
  395. l_returned = lmptr_->getLease6(leasetype6_[2], ioaddress6_[2]);
  396. ASSERT_TRUE(l_returned);
  397. detailCompareLease(leases[2], l_returned);
  398. }
  399. /// @brief Verify that too long hostname for Lease6 is not accepted.
  400. ///
  401. /// Checks that the it is not possible to create a lease when the hostname
  402. /// length exceeds 255 characters.
  403. TEST_F(MySqlLeaseMgrTest, lease6InvalidHostname) {
  404. // Get the leases to be used for the test.
  405. vector<Lease6Ptr> leases = createLeases6();
  406. // Create a dummy hostname, consisting of 255 characters.
  407. leases[1]->hostname_.assign(255, 'a');
  408. ASSERT_TRUE(lmptr_->addLease(leases[1]));
  409. // The new lease must be in the database.
  410. Lease6Ptr l_returned = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  411. detailCompareLease(leases[1], l_returned);
  412. // Let's delete the lease, so as we can try to add it again with
  413. // invalid hostname.
  414. EXPECT_TRUE(lmptr_->deleteLease(ioaddress6_[1]));
  415. // Create a hostname with 256 characters. It should not be accepted.
  416. leases[1]->hostname_.assign(256, 'a');
  417. EXPECT_THROW(lmptr_->addLease(leases[1]), DbOperationError);
  418. }
  419. /// @brief Check GetLease4 methods - access by Hardware Address
  420. ///
  421. /// Adds leases to the database and checks that they can be accessed via
  422. /// a combination of DIUID and IAID.
  423. TEST_F(MySqlLeaseMgrTest, getLease4Hwaddr) {
  424. // Get the leases to be used for the test and add to the database
  425. vector<Lease4Ptr> leases = createLeases4();
  426. for (int i = 0; i < leases.size(); ++i) {
  427. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  428. }
  429. // Get the leases matching the hardware address of lease 1
  430. /// @todo: Simply use HWAddr directly once 2589 is implemented
  431. HWAddr tmp(leases[1]->hwaddr_, HTYPE_ETHER);
  432. Lease4Collection returned = lmptr_->getLease4(tmp);
  433. // Should be three leases, matching leases[1], [3] and [5].
  434. ASSERT_EQ(3, returned.size());
  435. // Easiest way to check is to look at the addresses.
  436. vector<string> addresses;
  437. for (Lease4Collection::const_iterator i = returned.begin();
  438. i != returned.end(); ++i) {
  439. addresses.push_back((*i)->addr_.toText());
  440. }
  441. sort(addresses.begin(), addresses.end());
  442. EXPECT_EQ(straddress4_[1], addresses[0]);
  443. EXPECT_EQ(straddress4_[3], addresses[1]);
  444. EXPECT_EQ(straddress4_[5], addresses[2]);
  445. // Repeat test with just one expected match
  446. /// @todo: Simply use HWAddr directly once 2589 is implemented
  447. returned = lmptr_->getLease4(HWAddr(leases[2]->hwaddr_, HTYPE_ETHER));
  448. ASSERT_EQ(1, returned.size());
  449. detailCompareLease(leases[2], *returned.begin());
  450. // Check that an empty vector is valid
  451. EXPECT_TRUE(leases[7]->hwaddr_.empty());
  452. /// @todo: Simply use HWAddr directly once 2589 is implemented
  453. returned = lmptr_->getLease4(HWAddr(leases[7]->hwaddr_, HTYPE_ETHER));
  454. ASSERT_EQ(1, returned.size());
  455. detailCompareLease(leases[7], *returned.begin());
  456. // Try to get something with invalid hardware address
  457. vector<uint8_t> invalid(6, 0);
  458. returned = lmptr_->getLease4(invalid);
  459. EXPECT_EQ(0, returned.size());
  460. }
  461. // @brief Get lease4 by hardware address (2)
  462. //
  463. // Check that the system can cope with getting a hardware address of
  464. // any size.
  465. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSize) {
  466. // Create leases, although we need only one.
  467. vector<Lease4Ptr> leases = createLeases4();
  468. // Now add leases with increasing hardware address size.
  469. for (uint8_t i = 0; i <= HWAddr::MAX_HWADDR_LEN; ++i) {
  470. leases[1]->hwaddr_.resize(i, i);
  471. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  472. /// @todo: Simply use HWAddr directly once 2589 is implemented
  473. Lease4Collection returned =
  474. lmptr_->getLease4(HWAddr(leases[1]->hwaddr_, HTYPE_ETHER));
  475. ASSERT_EQ(1, returned.size());
  476. detailCompareLease(leases[1], *returned.begin());
  477. (void) lmptr_->deleteLease(leases[1]->addr_);
  478. }
  479. // Database should not let us add one that is too big
  480. // (The 42 is a random value put in each byte of the address.)
  481. /// @todo: 2589 will make this test impossible
  482. leases[1]->hwaddr_.resize(HWAddr::MAX_HWADDR_LEN + 100, 42);
  483. EXPECT_THROW(lmptr_->addLease(leases[1]), isc::dhcp::DbOperationError);
  484. }
  485. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  486. ///
  487. /// Adds leases to the database and checks that they can be accessed via
  488. /// a combination of hardware address and subnet ID
  489. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetId) {
  490. // Get the leases to be used for the test and add to the database
  491. vector<Lease4Ptr> leases = createLeases4();
  492. for (int i = 0; i < leases.size(); ++i) {
  493. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  494. }
  495. // Get the leases matching the hardware address of lease 1 and
  496. // subnet ID of lease 1. Result should be a single lease - lease 1.
  497. /// @todo: Simply use HWAddr directly once 2589 is implemented
  498. Lease4Ptr returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_,
  499. HTYPE_ETHER), leases[1]->subnet_id_);
  500. ASSERT_TRUE(returned);
  501. detailCompareLease(leases[1], returned);
  502. // Try for a match to the hardware address of lease 1 and the wrong
  503. // subnet ID.
  504. /// @todo: Simply use HWAddr directly once 2589 is implemented
  505. returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_, HTYPE_ETHER),
  506. leases[1]->subnet_id_ + 1);
  507. EXPECT_FALSE(returned);
  508. // Try for a match to the subnet ID of lease 1 (and lease 4) but
  509. // the wrong hardware address.
  510. vector<uint8_t> invalid_hwaddr(15, 0x77);
  511. /// @todo: Simply use HWAddr directly once 2589 is implemented
  512. returned = lmptr_->getLease4(HWAddr(invalid_hwaddr, HTYPE_ETHER),
  513. leases[1]->subnet_id_);
  514. EXPECT_FALSE(returned);
  515. // Try for a match to an unknown hardware address and an unknown
  516. // subnet ID.
  517. /// @todo: Simply use HWAddr directly once 2589 is implemented
  518. returned = lmptr_->getLease4(HWAddr(invalid_hwaddr, HTYPE_ETHER),
  519. leases[1]->subnet_id_ + 1);
  520. EXPECT_FALSE(returned);
  521. // Add a second lease with the same values as the first and check that
  522. // an attempt to access the database by these parameters throws a
  523. // "multiple records" exception. (We expect there to be only one record
  524. // with that combination, so getting them via getLeaseX() (as opposed
  525. // to getLeaseXCollection() should throw an exception.)
  526. EXPECT_TRUE(lmptr_->deleteLease(leases[2]->addr_));
  527. leases[1]->addr_ = leases[2]->addr_;
  528. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  529. /// @todo: Simply use HWAddr directly once 2589 is implemented
  530. EXPECT_THROW(returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_,
  531. HTYPE_ETHER),
  532. leases[1]->subnet_id_),
  533. isc::dhcp::MultipleRecords);
  534. }
  535. // @brief Get lease4 by hardware address and subnet ID (2)
  536. //
  537. // Check that the system can cope with getting a hardware address of
  538. // any size.
  539. TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetIdSize) {
  540. // Create leases, although we need only one.
  541. vector<Lease4Ptr> leases = createLeases4();
  542. // Now add leases with increasing hardware address size and check
  543. // that they can be retrieved.
  544. for (uint8_t i = 0; i <= HWAddr::MAX_HWADDR_LEN; ++i) {
  545. leases[1]->hwaddr_.resize(i, i);
  546. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  547. /// @todo: Simply use HWAddr directly once 2589 is implemented
  548. Lease4Ptr returned = lmptr_->getLease4(HWAddr(leases[1]->hwaddr_,
  549. HTYPE_ETHER),
  550. leases[1]->subnet_id_);
  551. ASSERT_TRUE(returned);
  552. detailCompareLease(leases[1], returned);
  553. (void) lmptr_->deleteLease(leases[1]->addr_);
  554. }
  555. // Database should not let us add one that is too big
  556. // (The 42 is a random value put in each byte of the address.)
  557. leases[1]->hwaddr_.resize(HWAddr::MAX_HWADDR_LEN + 100, 42);
  558. EXPECT_THROW(lmptr_->addLease(leases[1]), isc::dhcp::DbOperationError);
  559. }
  560. /// @brief Check GetLease4 methods - access by Client ID
  561. ///
  562. /// Adds leases to the database and checks that they can be accessed via
  563. /// the Client ID.
  564. TEST_F(MySqlLeaseMgrTest, getLease4ClientId) {
  565. // Get the leases to be used for the test and add to the database
  566. vector<Lease4Ptr> leases = createLeases4();
  567. for (int i = 0; i < leases.size(); ++i) {
  568. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  569. }
  570. // Get the leases matching the Client ID address of lease 1
  571. Lease4Collection returned = lmptr_->getLease4(*leases[1]->client_id_);
  572. // Should be four leases, matching leases[1], [4], [5] and [6].
  573. ASSERT_EQ(4, returned.size());
  574. // Easiest way to check is to look at the addresses.
  575. vector<string> addresses;
  576. for (Lease4Collection::const_iterator i = returned.begin();
  577. i != returned.end(); ++i) {
  578. addresses.push_back((*i)->addr_.toText());
  579. }
  580. sort(addresses.begin(), addresses.end());
  581. EXPECT_EQ(straddress4_[1], addresses[0]);
  582. EXPECT_EQ(straddress4_[4], addresses[1]);
  583. EXPECT_EQ(straddress4_[5], addresses[2]);
  584. EXPECT_EQ(straddress4_[6], addresses[3]);
  585. // Repeat test with just one expected match
  586. returned = lmptr_->getLease4(*leases[3]->client_id_);
  587. ASSERT_EQ(1, returned.size());
  588. detailCompareLease(leases[3], *returned.begin());
  589. // Check that client-id is NULL
  590. EXPECT_FALSE(leases[7]->client_id_);
  591. HWAddr tmp(leases[7]->hwaddr_, HTYPE_ETHER);
  592. returned = lmptr_->getLease4(tmp);
  593. ASSERT_EQ(1, returned.size());
  594. detailCompareLease(leases[7], *returned.begin());
  595. // Try to get something with invalid client ID
  596. const uint8_t invalid_data[] = {0, 0, 0};
  597. ClientId invalid(invalid_data, sizeof(invalid_data));
  598. returned = lmptr_->getLease4(invalid);
  599. EXPECT_EQ(0, returned.size());
  600. }
  601. // @brief Get Lease4 by client ID (2)
  602. //
  603. // Check that the system can cope with a client ID of any size.
  604. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSize) {
  605. // Create leases, although we need only one.
  606. vector<Lease4Ptr> leases = createLeases4();
  607. // Now add leases with increasing Client ID size can be retrieved.
  608. // For speed, go from 0 to 128 is steps of 16.
  609. // Intermediate client_id_max is to overcome problem if
  610. // ClientId::MAX_CLIENT_ID_LEN is used in an EXPECT_EQ.
  611. int client_id_max = ClientId::MAX_CLIENT_ID_LEN;
  612. EXPECT_EQ(128, client_id_max);
  613. int client_id_min = ClientId::MIN_CLIENT_ID_LEN;
  614. EXPECT_EQ(2, client_id_min); // See RFC2132, section 9.14
  615. for (uint8_t i = client_id_min; i <= client_id_max; i += 16) {
  616. vector<uint8_t> clientid_vec(i, i);
  617. leases[1]->client_id_.reset(new ClientId(clientid_vec));
  618. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  619. Lease4Collection returned = lmptr_->getLease4(*leases[1]->client_id_);
  620. ASSERT_TRUE(returned.size() == 1);
  621. detailCompareLease(leases[1], *returned.begin());
  622. (void) lmptr_->deleteLease(leases[1]->addr_);
  623. }
  624. // Don't bother to check client IDs longer than the maximum -
  625. // these cannot be constructed, and that limitation is tested
  626. // in the DUID/Client ID unit tests.
  627. }
  628. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  629. ///
  630. /// Adds leases to the database and checks that they can be accessed via
  631. /// a combination of client and subnet IDs.
  632. TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) {
  633. // Get the leases to be used for the test and add to the database
  634. vector<Lease4Ptr> leases = createLeases4();
  635. for (int i = 0; i < leases.size(); ++i) {
  636. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  637. }
  638. // Get the leases matching the client ID of lease 1 and
  639. // subnet ID of lease 1. Result should be a single lease - lease 1.
  640. Lease4Ptr returned = lmptr_->getLease4(*leases[1]->client_id_,
  641. leases[1]->subnet_id_);
  642. ASSERT_TRUE(returned);
  643. detailCompareLease(leases[1], returned);
  644. // Try for a match to the client ID of lease 1 and the wrong
  645. // subnet ID.
  646. returned = lmptr_->getLease4(*leases[1]->client_id_,
  647. leases[1]->subnet_id_ + 1);
  648. EXPECT_FALSE(returned);
  649. // Try for a match to the subnet ID of lease 1 (and lease 4) but
  650. // the wrong client ID
  651. const uint8_t invalid_data[] = {0, 0, 0};
  652. ClientId invalid(invalid_data, sizeof(invalid_data));
  653. returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_);
  654. EXPECT_FALSE(returned);
  655. // Try for a match to an unknown hardware address and an unknown
  656. // subnet ID.
  657. returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_ + 1);
  658. EXPECT_FALSE(returned);
  659. }
  660. /// @brief Check GetLease6 methods - access by DUID/IAID
  661. ///
  662. /// Adds leases to the database and checks that they can be accessed via
  663. /// a combination of DIUID and IAID.
  664. TEST_F(MySqlLeaseMgrTest, getLeases6DuidIaid) {
  665. // Get the leases to be used for the test.
  666. vector<Lease6Ptr> leases = createLeases6();
  667. ASSERT_LE(6, leases.size()); // Expect to access leases 0 through 5
  668. // Add them to the database
  669. for (int i = 0; i < leases.size(); ++i) {
  670. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  671. }
  672. // Get the leases matching the DUID and IAID of lease[1].
  673. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[1],
  674. *leases[1]->duid_,
  675. leases[1]->iaid_);
  676. // Should be two leases, matching leases[1] and [4].
  677. ASSERT_EQ(2, returned.size());
  678. // Easiest way to check is to look at the addresses.
  679. vector<string> addresses;
  680. for (Lease6Collection::const_iterator i = returned.begin();
  681. i != returned.end(); ++i) {
  682. addresses.push_back((*i)->addr_.toText());
  683. }
  684. sort(addresses.begin(), addresses.end());
  685. EXPECT_EQ(straddress6_[1], addresses[0]);
  686. EXPECT_EQ(straddress6_[4], addresses[1]);
  687. // Check that nothing is returned when either the IAID or DUID match
  688. // nothing.
  689. returned = lmptr_->getLeases6(leasetype6_[1], *leases[1]->duid_,
  690. leases[1]->iaid_ + 1);
  691. EXPECT_EQ(0, returned.size());
  692. // Alter the leases[1] DUID to match nothing in the database.
  693. vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
  694. ++duid_vector[0];
  695. DUID new_duid(duid_vector);
  696. returned = lmptr_->getLeases6(leasetype6_[1], new_duid, leases[1]->iaid_);
  697. EXPECT_EQ(0, returned.size());
  698. }
  699. // @brief Get Lease4 by DUID and IAID (2)
  700. //
  701. // Check that the system can cope with a DUID of any size.
  702. TEST_F(MySqlLeaseMgrTest, getLeases6DuidIaidSize) {
  703. // Create leases, although we need only one.
  704. vector<Lease6Ptr> leases = createLeases6();
  705. // Now add leases with increasing DUID size can be retrieved.
  706. // For speed, go from 0 to 128 is steps of 16.
  707. int duid_max = DUID::MAX_DUID_LEN;
  708. EXPECT_EQ(128, duid_max);
  709. int duid_min = DUID::MIN_DUID_LEN;
  710. EXPECT_EQ(1, duid_min);
  711. for (uint8_t i = duid_min; i <= duid_max; i += 16) {
  712. vector<uint8_t> duid_vec(i, i);
  713. leases[1]->duid_.reset(new DUID(duid_vec));
  714. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  715. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[1],
  716. *leases[1]->duid_,
  717. leases[1]->iaid_);
  718. ASSERT_EQ(1, returned.size());
  719. detailCompareLease(leases[1], *returned.begin());
  720. (void) lmptr_->deleteLease(leases[1]->addr_);
  721. }
  722. // Don't bother to check DUIDs longer than the maximum - these cannot be
  723. // constructed, and that limitation is tested in the DUID/Client ID unit
  724. // tests.
  725. }
  726. /// @brief Check that getLease6 methods discriminate by lease type.
  727. ///
  728. /// Adds six leases, two per lease type all with the same duid and iad but
  729. /// with alternating subnet_ids.
  730. /// It then verifies that all of getLeases6() method variants correctly
  731. /// discriminate between the leases based on lease type alone.
  732. TEST_F(MySqlLeaseMgrTest, lease6LeaseTypeCheck) {
  733. Lease6Ptr empty_lease(new Lease6());
  734. DuidPtr duid(new DUID(vector<uint8_t>(8, 0x77)));
  735. // Initialize unused fields.
  736. empty_lease->t1_ = 0; // Not saved
  737. empty_lease->t2_ = 0; // Not saved
  738. empty_lease->fixed_ = false; // Unused
  739. empty_lease->comments_ = std::string(""); // Unused
  740. empty_lease->iaid_ = 142;
  741. empty_lease->duid_ = DuidPtr(new DUID(*duid));
  742. empty_lease->subnet_id_ = 23;
  743. empty_lease->preferred_lft_ = 100;
  744. empty_lease->valid_lft_ = 100;
  745. empty_lease->cltt_ = 100;
  746. empty_lease->fqdn_fwd_ = true;
  747. empty_lease->fqdn_rev_ = true;
  748. empty_lease->hostname_ = "myhost.example.com.";
  749. empty_lease->prefixlen_ = 4;
  750. // Make Two leases per lease type, all with the same DUID, IAID but
  751. // alternate the subnet_ids.
  752. vector<Lease6Ptr> leases;
  753. for (int i = 0; i < 6; ++i) {
  754. Lease6Ptr lease(new Lease6(*empty_lease));
  755. lease->type_ = leasetype6_[i / 2];
  756. lease->addr_ = IOAddress(straddress6_[i]);
  757. lease->subnet_id_ += (i % 2);
  758. leases.push_back(lease);
  759. EXPECT_TRUE(lmptr_->addLease(lease));
  760. }
  761. // Verify getting a single lease by type and address.
  762. for (int i = 0; i < 6; ++i) {
  763. // Look for exact match for each lease type.
  764. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[i / 2],
  765. leases[i]->addr_);
  766. // We should match one per lease type.
  767. ASSERT_TRUE(returned);
  768. EXPECT_TRUE(*returned == *leases[i]);
  769. // Same address but wrong lease type, should not match.
  770. returned = lmptr_->getLease6(leasetype6_[i / 2 + 1], leases[i]->addr_);
  771. ASSERT_FALSE(returned);
  772. }
  773. // Verify getting a collection of leases by type, DUID, and IAID.
  774. // Iterate over the lease types, asking for leases based on
  775. // lease type, DUID, and IAID.
  776. for (int i = 0; i < 3; ++i) {
  777. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[i],
  778. *duid, 142);
  779. // We should match two per lease type.
  780. ASSERT_EQ(2, returned.size());
  781. // Collection order returned is not guaranteed.
  782. // Easiest way to check is to look at the addresses.
  783. vector<string> addresses;
  784. for (Lease6Collection::const_iterator it = returned.begin();
  785. it != returned.end(); ++it) {
  786. addresses.push_back((*it)->addr_.toText());
  787. }
  788. sort(addresses.begin(), addresses.end());
  789. // Now verify that the lease addresses match.
  790. EXPECT_EQ(addresses[0], leases[(i * 2)]->addr_.toText());
  791. EXPECT_EQ(addresses[1], leases[(i * 2 + 1)]->addr_.toText());
  792. }
  793. // Verify getting a collection of leases by type, DUID, IAID, and subnet id.
  794. // Iterate over the lease types, asking for leases based on
  795. // lease type, DUID, IAID, and subnet_id.
  796. for (int i = 0; i < 3; ++i) {
  797. Lease6Collection returned = lmptr_->getLeases6(leasetype6_[i],
  798. *duid, 142, 23);
  799. // We should match one per lease type.
  800. ASSERT_EQ(1, returned.size());
  801. EXPECT_TRUE(*(returned[0]) == *leases[i * 2]);
  802. }
  803. // Verify getting a single lease by type, duid, iad, and subnet id.
  804. for (int i = 0; i < 6; ++i) {
  805. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[i / 2],
  806. *duid, 142, (23 + (i % 2)));
  807. // We should match one per lease type.
  808. ASSERT_TRUE(returned);
  809. EXPECT_TRUE(*returned == *leases[i]);
  810. }
  811. }
  812. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  813. ///
  814. /// Adds leases to the database and checks that they can be accessed via
  815. /// a combination of DIUID and IAID.
  816. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
  817. // Get the leases to be used for the test and add them to the database.
  818. vector<Lease6Ptr> leases = createLeases6();
  819. for (int i = 0; i < leases.size(); ++i) {
  820. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  821. }
  822. // Get the leases matching the DUID and IAID of lease[1].
  823. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  824. leases[1]->iaid_,
  825. leases[1]->subnet_id_);
  826. ASSERT_TRUE(returned);
  827. EXPECT_TRUE(*returned == *leases[1]);
  828. // Modify each of the three parameters (DUID, IAID, Subnet ID) and
  829. // check that nothing is returned.
  830. returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  831. leases[1]->iaid_ + 1, leases[1]->subnet_id_);
  832. EXPECT_FALSE(returned);
  833. returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  834. leases[1]->iaid_, leases[1]->subnet_id_ + 1);
  835. EXPECT_FALSE(returned);
  836. // Alter the leases[1] DUID to match nothing in the database.
  837. vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
  838. ++duid_vector[0];
  839. DUID new_duid(duid_vector);
  840. returned = lmptr_->getLease6(leasetype6_[1], new_duid, leases[1]->iaid_,
  841. leases[1]->subnet_id_);
  842. EXPECT_FALSE(returned);
  843. }
  844. // @brief Get Lease4 by DUID, IAID & subnet ID (2)
  845. //
  846. // Check that the system can cope with a DUID of any size.
  847. TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  848. // Create leases, although we need only one.
  849. vector<Lease6Ptr> leases = createLeases6();
  850. // Now add leases with increasing DUID size can be retrieved.
  851. // For speed, go from 0 to 128 is steps of 16.
  852. int duid_max = DUID::MAX_DUID_LEN;
  853. EXPECT_EQ(128, duid_max);
  854. int duid_min = DUID::MIN_DUID_LEN;
  855. EXPECT_EQ(1, duid_min);
  856. for (uint8_t i = duid_min; i <= duid_max; i += 16) {
  857. vector<uint8_t> duid_vec(i, i);
  858. leases[1]->duid_.reset(new DUID(duid_vec));
  859. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  860. Lease6Ptr returned = lmptr_->getLease6(leasetype6_[1], *leases[1]->duid_,
  861. leases[1]->iaid_,
  862. leases[1]->subnet_id_);
  863. ASSERT_TRUE(returned);
  864. detailCompareLease(leases[1], returned);
  865. (void) lmptr_->deleteLease(leases[1]->addr_);
  866. }
  867. // Don't bother to check DUIDs longer than the maximum - these cannot be
  868. // constructed, and that limitation is tested in the DUID/Client ID unit
  869. // tests.
  870. }
  871. /// @brief Lease4 update tests
  872. ///
  873. /// Checks that we are able to update a lease in the database.
  874. TEST_F(MySqlLeaseMgrTest, updateLease4) {
  875. // Get the leases to be used for the test and add them to the database.
  876. vector<Lease4Ptr> leases = createLeases4();
  877. for (int i = 0; i < leases.size(); ++i) {
  878. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  879. }
  880. // Modify some fields in lease 1 (not the address) and update it.
  881. ++leases[1]->subnet_id_;
  882. leases[1]->valid_lft_ *= 2;
  883. leases[1]->hostname_ = "modified.hostname.";
  884. leases[1]->fqdn_fwd_ = !leases[1]->fqdn_fwd_;
  885. leases[1]->fqdn_rev_ = !leases[1]->fqdn_rev_;;
  886. lmptr_->updateLease4(leases[1]);
  887. // ... and check what is returned is what is expected.
  888. Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
  889. ASSERT_TRUE(l_returned);
  890. detailCompareLease(leases[1], l_returned);
  891. // Alter the lease again and check.
  892. ++leases[1]->subnet_id_;
  893. leases[1]->cltt_ += 6;
  894. lmptr_->updateLease4(leases[1]);
  895. // Explicitly clear the returned pointer before getting new data to ensure
  896. // that the new data is returned.
  897. l_returned.reset();
  898. l_returned = lmptr_->getLease4(ioaddress4_[1]);
  899. ASSERT_TRUE(l_returned);
  900. detailCompareLease(leases[1], l_returned);
  901. // Check we can do an update without changing data.
  902. lmptr_->updateLease4(leases[1]);
  903. l_returned.reset();
  904. l_returned = lmptr_->getLease4(ioaddress4_[1]);
  905. ASSERT_TRUE(l_returned);
  906. detailCompareLease(leases[1], l_returned);
  907. // Try to update the lease with the too long hostname.
  908. leases[1]->hostname_.assign(256, 'a');
  909. EXPECT_THROW(lmptr_->updateLease4(leases[1]), isc::dhcp::DbOperationError);
  910. // Try updating a lease not in the database.
  911. lmptr_->deleteLease(ioaddress4_[2]);
  912. EXPECT_THROW(lmptr_->updateLease4(leases[2]), isc::dhcp::NoSuchLease);
  913. }
  914. /// @brief Lease6 update tests
  915. ///
  916. /// Checks that we are able to update a lease in the database.
  917. TEST_F(MySqlLeaseMgrTest, updateLease6) {
  918. // Get the leases to be used for the test.
  919. vector<Lease6Ptr> leases = createLeases6();
  920. ASSERT_LE(3, leases.size()); // Expect to access leases 0 through 2
  921. // Add a lease to the database and check that the lease is there.
  922. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  923. lmptr_->commit();
  924. Lease6Ptr l_returned = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  925. ASSERT_TRUE(l_returned);
  926. detailCompareLease(leases[1], l_returned);
  927. // Modify some fields in lease 1 (not the address) and update it.
  928. ++leases[1]->iaid_;
  929. leases[1]->type_ = Lease::TYPE_PD;
  930. leases[1]->valid_lft_ *= 2;
  931. leases[1]->hostname_ = "modified.hostname.v6.";
  932. leases[1]->fqdn_fwd_ = !leases[1]->fqdn_fwd_;
  933. leases[1]->fqdn_rev_ = !leases[1]->fqdn_rev_;;
  934. lmptr_->updateLease6(leases[1]);
  935. lmptr_->commit();
  936. // ... and check what is returned is what is expected.
  937. l_returned.reset();
  938. l_returned = lmptr_->getLease6(Lease::TYPE_PD, ioaddress6_[1]);
  939. ASSERT_TRUE(l_returned);
  940. detailCompareLease(leases[1], l_returned);
  941. // Alter the lease again and check.
  942. ++leases[1]->iaid_;
  943. leases[1]->type_ = Lease::TYPE_TA;
  944. leases[1]->cltt_ += 6;
  945. leases[1]->prefixlen_ = 93;
  946. lmptr_->updateLease6(leases[1]);
  947. l_returned.reset();
  948. l_returned = lmptr_->getLease6(Lease::TYPE_TA, ioaddress6_[1]);
  949. ASSERT_TRUE(l_returned);
  950. detailCompareLease(leases[1], l_returned);
  951. // Check we can do an update without changing data.
  952. lmptr_->updateLease6(leases[1]);
  953. l_returned.reset();
  954. l_returned = lmptr_->getLease6(Lease::TYPE_TA, ioaddress6_[1]);
  955. ASSERT_TRUE(l_returned);
  956. detailCompareLease(leases[1], l_returned);
  957. // Try to update the lease with the too long hostname.
  958. leases[1]->hostname_.assign(256, 'a');
  959. EXPECT_THROW(lmptr_->updateLease6(leases[1]), isc::dhcp::DbOperationError);
  960. // Try updating a lease not in the database.
  961. EXPECT_THROW(lmptr_->updateLease6(leases[2]), isc::dhcp::NoSuchLease);
  962. }
  963. }; // Of anonymous namespace