mysql_lease_mgr_unittest.cc 44 KB

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