mysql_lease_mgr_unittest.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // Copyright (C) 2011-2012 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 <iostream>
  16. #include <sstream>
  17. #include <utility>
  18. #include <string>
  19. #include <gtest/gtest.h>
  20. #include <asiolink/io_address.h>
  21. #include <dhcp/lease_mgr_factory.h>
  22. #include <dhcp/mysql_lease_mgr.h>
  23. using namespace isc;
  24. using namespace isc::asiolink;
  25. using namespace isc::dhcp;
  26. using namespace std;
  27. namespace {
  28. // Creation of the schema
  29. #include "schema_copy.h"
  30. // IPv6 addresseses
  31. const char* ADDRESS_0 = "2001:db8::0";
  32. const char* ADDRESS_1 = "2001:db8::1";
  33. const char* ADDRESS_2 = "2001:db8::2";
  34. const char* ADDRESS_3 = "2001:db8::3";
  35. const char* ADDRESS_4 = "2001:db8::4";
  36. const char* ADDRESS_5 = "2001:db8::5";
  37. const char* ADDRESS_6 = "2001:db8::6";
  38. const char* ADDRESS_7 = "2001:db8::7";
  39. // Connection strings. Assume:
  40. // Database: keatest
  41. // Username: keatest
  42. // Password: keatest
  43. const char* VALID_TYPE = "type=mysql";
  44. const char* INVALID_TYPE = "type=unknown";
  45. const char* VALID_NAME = "name=keatest";
  46. const char* INVALID_NAME = "name=invalidname";
  47. const char* VALID_HOST = "host=localhost";
  48. const char* INVALID_HOST = "host=invalidhost";
  49. const char* VALID_USER = "user=keatest";
  50. const char* INVALID_USER = "user=invaliduser";
  51. const char* VALID_PASSWORD = "password=keatest";
  52. const char* INVALID_PASSWORD = "password=invalid";
  53. // Given a combination of strings above, produce a connection string.
  54. string connectionString(const char* type, const char* name, const char* host,
  55. const char* user, const char* password) {
  56. const string space = " ";
  57. string result = "";
  58. if (type != NULL) {
  59. result += string(type);
  60. }
  61. if (name != NULL) {
  62. if (! result.empty()) {
  63. result += space;
  64. }
  65. result += string(name);
  66. }
  67. if (host != NULL) {
  68. if (! result.empty()) {
  69. result += space;
  70. }
  71. result += string(host);
  72. }
  73. if (user != NULL) {
  74. if (! result.empty()) {
  75. result += space;
  76. }
  77. result += string(user);
  78. }
  79. if (password != NULL) {
  80. if (! result.empty()) {
  81. result += space;
  82. }
  83. result += string(password);
  84. }
  85. return (result);
  86. }
  87. // Return valid connection string
  88. string
  89. validConnectionString() {
  90. return (connectionString(VALID_TYPE, VALID_NAME, VALID_HOST,
  91. VALID_USER, VALID_PASSWORD));
  92. }
  93. // @brief Clear everything from the database
  94. //
  95. // There is no error checking in this code: if something fails, one of the
  96. // tests will fall over.
  97. void destroySchema() {
  98. // Initialise
  99. MYSQL handle;
  100. (void) mysql_init(&handle);
  101. // Open database
  102. (void) mysql_real_connect(&handle, "localhost", "keatest",
  103. "keatest", "keatest", 0, NULL, 0);
  104. // Get rid of everything in it.
  105. for (int i = 0; destroy_statement[i] != NULL; ++i) {
  106. (void) mysql_query(&handle, destroy_statement[i]);
  107. }
  108. // ... and close
  109. (void) mysql_close(&handle);
  110. }
  111. // @brief Create the Schema
  112. //
  113. // Creates all the tables in what is assumed to be an empty database.
  114. //
  115. // There is no error checking in this code: if it fails, one of the tests
  116. // will fall over.
  117. void createSchema() {
  118. // Initialise
  119. MYSQL handle;
  120. (void) mysql_init(&handle);
  121. // Open database
  122. (void) mysql_real_connect(&handle, "localhost", "keatest",
  123. "keatest", "keatest", 0, NULL, 0);
  124. // Get rid of everything in it.
  125. for (int i = 0; create_statement[i] != NULL; ++i) {
  126. (void) mysql_query(&handle, create_statement[i]);
  127. }
  128. // ... and close
  129. (void) mysql_close(&handle);
  130. }
  131. // Note: Doxygen "///" not used - even though Doxygen is used to
  132. // document class and methods - to avoid the comments appearing
  133. // in the programming manual.
  134. // @brief Test Fixture Class
  135. //
  136. // Opens the database prior to each test and closes it afterwards.
  137. // All pending transactions are deleted prior to closure.
  138. class MySqlLeaseMgrTest : public ::testing::Test {
  139. public:
  140. // @brief Constructor
  141. //
  142. // Deletes everything from the database and opens it.
  143. MySqlLeaseMgrTest() :
  144. L0_ADDRESS(ADDRESS_0), L0_IOADDRESS(L0_ADDRESS),
  145. L1_ADDRESS(ADDRESS_1), L1_IOADDRESS(L1_ADDRESS),
  146. L2_ADDRESS(ADDRESS_2), L2_IOADDRESS(L2_ADDRESS),
  147. L3_ADDRESS(ADDRESS_3), L3_IOADDRESS(L3_ADDRESS),
  148. L4_ADDRESS(ADDRESS_4), L4_IOADDRESS(L4_ADDRESS),
  149. L5_ADDRESS(ADDRESS_5), L5_IOADDRESS(L5_ADDRESS),
  150. L6_ADDRESS(ADDRESS_6), L6_IOADDRESS(L6_ADDRESS),
  151. L7_ADDRESS(ADDRESS_7), L7_IOADDRESS(L7_ADDRESS)
  152. {
  153. destroySchema();
  154. createSchema();
  155. LeaseMgrFactory::create(validConnectionString());
  156. lmptr_ = &(LeaseMgrFactory::instance());
  157. }
  158. // @brief Destructor
  159. //
  160. // Rolls back all pending transactions. The deletion of the
  161. // lmptr_ member variable will close the database. Then
  162. // reopen it and delete everything created by the test.
  163. virtual ~MySqlLeaseMgrTest() {
  164. lmptr_->rollback();
  165. LeaseMgrFactory::destroy();
  166. destroySchema();
  167. }
  168. // @brief Reopen the database
  169. //
  170. // Closes the database and re-open it. Anything committed should be
  171. // visible.
  172. void reopen() {
  173. LeaseMgrFactory::destroy();
  174. LeaseMgrFactory::create(validConnectionString());
  175. lmptr_ = &(LeaseMgrFactory::instance());
  176. }
  177. // @brief Initialize Lease6 Fields
  178. //
  179. // Returns a pointer to a Lease6 structure. Different values are put
  180. // in the lease according to the address passed.
  181. //
  182. // This is just a convenience function for the test methods.
  183. //
  184. // @param address Address to use for the initialization
  185. //
  186. // @return Lease6Ptr. This will not point to anything if the initialization
  187. // failed (e.g. unknown address).
  188. Lease6Ptr initializeLease6(std::string address) {
  189. Lease6Ptr lease(new Lease6());
  190. // Set the address of the lease
  191. lease->addr_ = IOAddress(address);
  192. // Initialize unused fields.
  193. lease->t1_ = 0; // Not saved
  194. lease->t2_ = 0; // Not saved
  195. lease->fixed_ = false; // Unused
  196. lease->hostname_ = std::string(""); // Unused
  197. lease->fqdn_fwd_ = false; // Unused
  198. lease->fqdn_rev_ = false; // Unused
  199. lease->comments_ = std::string(""); // Unused
  200. // Set the other parameters. For historical reasons, L0_ADDRESS is not used.
  201. if (address == L0_ADDRESS) {
  202. lease->type_ = Lease6::LEASE_IA_TA;
  203. lease->prefixlen_ = 4;
  204. lease->iaid_ = 142;
  205. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x77)));
  206. lease->preferred_lft_ = 900; // Preferred lifetime
  207. lease->valid_lft_ = 8677; // Actual lifetime
  208. lease->cltt_ = 168256; // Current time of day
  209. lease->subnet_id_ = 23; // Arbitrary number
  210. } else if (address == L1_ADDRESS) {
  211. lease->type_ = Lease6::LEASE_IA_TA;
  212. lease->prefixlen_ = 0;
  213. lease->iaid_ = 42;
  214. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
  215. lease->preferred_lft_ = 3600; // Preferred lifetime
  216. lease->valid_lft_ = 3677; // Actual lifetime
  217. lease->cltt_ = 123456; // Current time of day
  218. lease->subnet_id_ = 73; // Arbitrary number
  219. } else if (address == L2_ADDRESS) {
  220. lease->type_ = Lease6::LEASE_IA_PD;
  221. lease->prefixlen_ = 7;
  222. lease->iaid_ = 89;
  223. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x3a)));
  224. lease->preferred_lft_ = 1800; // Preferred lifetime
  225. lease->valid_lft_ = 5412; // Actual lifetime
  226. lease->cltt_ = 234567; // Current time of day
  227. lease->subnet_id_ = 73; // Same as for L1_ADDRESS
  228. } else if (address == L3_ADDRESS) {
  229. lease->type_ = Lease6::LEASE_IA_NA;
  230. lease->prefixlen_ = 28;
  231. lease->iaid_ = 0xfffffffe;
  232. vector<uint8_t> duid;
  233. for (uint8_t i = 31; i < 126; ++i) {
  234. duid.push_back(i);
  235. }
  236. lease->duid_ = boost::shared_ptr<DUID>(new DUID(duid));
  237. // The times used in the next tests are deliberately restricted - we
  238. // should be able to cope with valid lifetimes up to 0xffffffff.
  239. // However, this will lead to overflows.
  240. // @TODO: test overflow conditions when code has been fixed
  241. lease->preferred_lft_ = 7200; // Preferred lifetime
  242. lease->valid_lft_ = 7000; // Actual lifetime
  243. lease->cltt_ = 234567; // Current time of day
  244. lease->subnet_id_ = 37; // Different from L1 and L2
  245. } else if (address == L4_ADDRESS) {
  246. // Same DUID and IAID as L1_ADDRESS
  247. lease->type_ = Lease6::LEASE_IA_PD;
  248. lease->prefixlen_ = 15;
  249. lease->iaid_ = 42;
  250. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
  251. lease->preferred_lft_ = 4800; // Preferred lifetime
  252. lease->valid_lft_ = 7736; // Actual lifetime
  253. lease->cltt_ = 222456; // Current time of day
  254. lease->subnet_id_ = 75; // Arbitrary number
  255. } else if (address == L5_ADDRESS) {
  256. // Same DUID and IAID as L1_ADDRESS
  257. lease->type_ = Lease6::LEASE_IA_PD;
  258. lease->prefixlen_ = 24;
  259. lease->iaid_ = 42;
  260. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
  261. lease->preferred_lft_ = 5400; // Preferred lifetime
  262. lease->valid_lft_ = 7832; // Actual lifetime
  263. lease->cltt_ = 227476; // Current time of day
  264. lease->subnet_id_ = 175; // Arbitrary number
  265. } else if (address == L6_ADDRESS) {
  266. // Same DUID as L1_ADDRESS
  267. lease->type_ = Lease6::LEASE_IA_PD;
  268. lease->prefixlen_ = 24;
  269. lease->iaid_ = 93;
  270. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
  271. lease->preferred_lft_ = 5400; // Preferred lifetime
  272. lease->valid_lft_ = 1832; // Actual lifetime
  273. lease->cltt_ = 627476; // Current time of day
  274. lease->subnet_id_ = 112; // Arbitrary number
  275. } else if (address == L7_ADDRESS) {
  276. // Same IAID as L1_ADDRESS
  277. lease->type_ = Lease6::LEASE_IA_PD;
  278. lease->prefixlen_ = 24;
  279. lease->iaid_ = 42;
  280. lease->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0xe5)));
  281. lease->preferred_lft_ = 5600; // Preferred lifetime
  282. lease->valid_lft_ = 7975; // Actual lifetime
  283. lease->cltt_ = 213876; // Current time of day
  284. lease->subnet_id_ = 19; // Arbitrary number
  285. } else {
  286. // Unknown address, return an empty pointer.
  287. lease.reset();
  288. }
  289. return (lease);
  290. }
  291. // @brief Creates Leases for the test
  292. //
  293. // Creates all leases for the test and checks that they are different.
  294. //
  295. // @return vector<Lease6Ptr> Vector of pointers to leases
  296. vector<Lease6Ptr> createLeases6() {
  297. // Create leases
  298. vector<Lease6Ptr> leases;
  299. leases.push_back(initializeLease6(L0_ADDRESS));
  300. leases.push_back(initializeLease6(L1_ADDRESS));
  301. leases.push_back(initializeLease6(L2_ADDRESS));
  302. leases.push_back(initializeLease6(L3_ADDRESS));
  303. leases.push_back(initializeLease6(L4_ADDRESS));
  304. leases.push_back(initializeLease6(L5_ADDRESS));
  305. leases.push_back(initializeLease6(L6_ADDRESS));
  306. leases.push_back(initializeLease6(L7_ADDRESS));
  307. EXPECT_EQ(8, leases.size());
  308. // Check they were created
  309. for (int i = 0; i < leases.size(); ++i) {
  310. EXPECT_TRUE(leases[i]);
  311. }
  312. // Check they are different
  313. for (int i = 0; i < (leases.size() - 1); ++i) {
  314. for (int j = (i + 1); j < leases.size(); ++j) {
  315. EXPECT_TRUE(leases[i] != leases[j]);
  316. }
  317. }
  318. return (leases);
  319. }
  320. // Member variables
  321. LeaseMgr* lmptr_; // Pointer to the lease manager
  322. string L0_ADDRESS; // String form of address 1
  323. IOAddress L0_IOADDRESS; // IOAddress form of L1_ADDRESS
  324. string L1_ADDRESS; // String form of address 1
  325. IOAddress L1_IOADDRESS; // IOAddress form of L1_ADDRESS
  326. string L2_ADDRESS; // String form of address 2
  327. IOAddress L2_IOADDRESS; // IOAddress form of L2_ADDRESS
  328. string L3_ADDRESS; // String form of address 3
  329. IOAddress L3_IOADDRESS; // IOAddress form of L3_ADDRESS
  330. string L4_ADDRESS; // String form of address 4
  331. IOAddress L4_IOADDRESS; // IOAddress form of L4_ADDRESS
  332. string L5_ADDRESS; // String form of address 5
  333. IOAddress L5_IOADDRESS; // IOAddress form of L5_ADDRESS
  334. string L6_ADDRESS; // String form of address 6
  335. IOAddress L6_IOADDRESS; // IOAddress form of L6_ADDRESS
  336. string L7_ADDRESS; // String form of address 7
  337. IOAddress L7_IOADDRESS; // IOAddress form of L7_ADDRESS
  338. };
  339. // @brief Check that Database Can Be Opened
  340. //
  341. // This test checks if the MySqlLeaseMgr can be instantiated. This happens
  342. // only if the database can be opened. Note that this is not part of the
  343. // MySqlLeaseMgr test fixure set. This test checks that the database can be
  344. // opened: the fixtures assume that and check basic operations.
  345. TEST(MySqlOpenTest, OpenDatabase) {
  346. // Schema needs to be created for the test to work.
  347. destroySchema();
  348. createSchema();
  349. // Check that lease manager open the database opens correctly and tidy up.
  350. // If it fails, print the error message.
  351. try {
  352. LeaseMgrFactory::create(validConnectionString());
  353. EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
  354. LeaseMgrFactory::destroy();
  355. } catch (const isc::Exception& ex) {
  356. FAIL() << "*** ERROR: unable to open database, reason:\n"
  357. << " " << ex.what()
  358. << "*** The test environment is broken and must be fixed\n"
  359. << "*** before the MySQL tests will run correctly.\n";
  360. }
  361. // Check that attempting to get an instance of the lease manager when
  362. // none is set throws an exception.
  363. EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
  364. // Check that wrong specification of backend throws an exception.
  365. // (This is really a check on LeaseMgrFactory, but is convenient to
  366. // perform here.)
  367. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  368. NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  369. InvalidParameter);
  370. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  371. INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  372. InvalidType);
  373. // Check that invalid login data causes an exception.
  374. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  375. VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
  376. DbOpenError);
  377. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  378. VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
  379. DbOpenError);
  380. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  381. VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  382. DbOpenError);
  383. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  384. VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
  385. DbOpenError);
  386. // Check for missing parameters
  387. EXPECT_THROW(LeaseMgrFactory::create(connectionString(
  388. VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
  389. NoDatabaseName);
  390. // Tidy up after the test
  391. destroySchema();
  392. }
  393. // @brief Check conversion functions
  394. TEST_F(MySqlLeaseMgrTest, CheckTimeConversion) {
  395. const time_t cltt = time(NULL);
  396. const uint32_t valid_lft = 86400; // 1 day
  397. struct tm tm_expire;
  398. MYSQL_TIME mysql_expire;
  399. // Work out what the broken-down time will be for one day
  400. // after the current time.
  401. time_t expire_time = cltt + valid_lft;
  402. (void) localtime_r(&expire_time, &tm_expire);
  403. // Convert to the database time
  404. MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, mysql_expire);
  405. // Are the times the same?
  406. EXPECT_EQ(tm_expire.tm_year + 1900, mysql_expire.year);
  407. EXPECT_EQ(tm_expire.tm_mon + 1, mysql_expire.month);
  408. EXPECT_EQ(tm_expire.tm_mday, mysql_expire.day);
  409. EXPECT_EQ(tm_expire.tm_hour, mysql_expire.hour);
  410. EXPECT_EQ(tm_expire.tm_min, mysql_expire.minute);
  411. EXPECT_EQ(tm_expire.tm_sec, mysql_expire.second);
  412. EXPECT_EQ(0, mysql_expire.second_part);
  413. EXPECT_EQ(0, mysql_expire.neg);
  414. // Convert back
  415. time_t converted_cltt = 0;
  416. MySqlLeaseMgr::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
  417. EXPECT_EQ(cltt, converted_cltt);
  418. }
  419. // @brief Check getName() returns correct database name
  420. TEST_F(MySqlLeaseMgrTest, getName) {
  421. EXPECT_EQ(std::string("keatest"), lmptr_->getName());
  422. // @TODO: check for the negative
  423. }
  424. // @brief Check that getVersion() works
  425. TEST_F(MySqlLeaseMgrTest, CheckVersion) {
  426. // Check version
  427. pair<uint32_t, uint32_t> version;
  428. ASSERT_NO_THROW(version = lmptr_->getVersion());
  429. EXPECT_EQ(CURRENT_VERSION_VERSION, version.first);
  430. EXPECT_EQ(CURRENT_VERSION_MINOR, version.second);
  431. }
  432. void
  433. detailCompareLease6(const Lease6Ptr& first, const Lease6Ptr& second) {
  434. EXPECT_EQ(first->type_, second->type_);
  435. // Compare address strings - odd things happen when they are different
  436. // as the EXPECT_EQ appears to call the operator uint32_t() function,
  437. // which causes an exception to be thrown for IPv6 addresses.
  438. EXPECT_EQ(first->addr_.toText(), second->addr_.toText());
  439. EXPECT_EQ(first->prefixlen_, second->prefixlen_);
  440. EXPECT_EQ(first->iaid_, second->iaid_);
  441. EXPECT_TRUE(*first->duid_ == *second->duid_);
  442. EXPECT_EQ(first->preferred_lft_, second->preferred_lft_);
  443. EXPECT_EQ(first->valid_lft_, second->valid_lft_);
  444. EXPECT_EQ(first->cltt_, second->cltt_);
  445. EXPECT_EQ(first->subnet_id_, second->subnet_id_);
  446. }
  447. // @brief Check individual Lease6 methods
  448. //
  449. // Checks that the add/update/delete works. All are done within one
  450. // test so that "rollback" can be used to remove trace of the tests
  451. // from the database.
  452. //
  453. // Tests where a collection of leases can be returned are in the test
  454. // Lease6Collection.
  455. TEST_F(MySqlLeaseMgrTest, BasicLease6) {
  456. // Get the leases to be used for the test.
  457. vector<Lease6Ptr> leases = createLeases6();
  458. // Start the tests. Add three leases to the database, read them back and
  459. // check they are what we think they are.
  460. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  461. EXPECT_TRUE(lmptr_->addLease(leases[2]));
  462. EXPECT_TRUE(lmptr_->addLease(leases[3]));
  463. lmptr_->commit();
  464. // Reopen the database to ensure that they actually got stored.
  465. reopen();
  466. Lease6Ptr l_returned = lmptr_->getLease6(L1_IOADDRESS);
  467. EXPECT_TRUE(l_returned);
  468. detailCompareLease6(leases[1], l_returned);
  469. l_returned = lmptr_->getLease6(L2_IOADDRESS);
  470. EXPECT_TRUE(l_returned);
  471. detailCompareLease6(leases[2], l_returned);
  472. l_returned = lmptr_->getLease6(L3_IOADDRESS);
  473. EXPECT_TRUE(l_returned);
  474. detailCompareLease6(leases[3], l_returned);
  475. // Check that we can't add a second lease with the same address
  476. EXPECT_FALSE(lmptr_->addLease(leases[1]));
  477. // Delete a lease, check that it's gone, and that we can't delete it
  478. // a second time.
  479. EXPECT_TRUE(lmptr_->deleteLease6(L1_IOADDRESS));
  480. l_returned = lmptr_->getLease6(L1_IOADDRESS);
  481. EXPECT_FALSE(l_returned);
  482. EXPECT_FALSE(lmptr_->deleteLease6(L1_IOADDRESS));
  483. // Check that the second address is still there.
  484. l_returned = lmptr_->getLease6(L2_IOADDRESS);
  485. EXPECT_TRUE(l_returned);
  486. detailCompareLease6(leases[2], l_returned);
  487. }
  488. // @brief Check GetLease6 methods - Access by DUID/IAID
  489. //
  490. // Adds leases to the database and checks that they can be accessed via
  491. // a combination of DIUID and IAID.
  492. TEST_F(MySqlLeaseMgrTest, GetLease6Extended1) {
  493. // Get the leases to be used for the test.
  494. vector<Lease6Ptr> leases = createLeases6();
  495. EXPECT_LE(6, leases.size()); // Expect to access leases 0 through 5
  496. // Add them to the database
  497. for (int i = 0; i < leases.size(); ++i) {
  498. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  499. }
  500. // Get the leases matching the DUID and IAID of lease[1].
  501. Lease6Collection returned = lmptr_->getLease6(*leases[1]->duid_,
  502. leases[1]->iaid_);
  503. // Should be three leases, matching leases[1], [4] and [5].
  504. ASSERT_EQ(3, returned.size());
  505. // Easiest way to check is to look at the addresses.
  506. vector<string> addresses;
  507. for (Lease6Collection::const_iterator i = returned.begin();
  508. i != returned.end(); ++i) {
  509. addresses.push_back((*i)->addr_.toText());
  510. }
  511. sort(addresses.begin(), addresses.end());
  512. EXPECT_EQ(L1_ADDRESS, addresses[0]);
  513. EXPECT_EQ(L4_ADDRESS, addresses[1]);
  514. EXPECT_EQ(L5_ADDRESS, addresses[2]);
  515. // Check that nothing is returned when either the IAID or DUID match
  516. // nothing.
  517. returned = lmptr_->getLease6(*leases[1]->duid_, leases[1]->iaid_ + 1);
  518. EXPECT_EQ(0, returned.size());
  519. // Alter the leases[1] DUID to match nothing in the database.
  520. vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
  521. ++duid_vector[0];
  522. DUID new_duid(duid_vector);
  523. returned = lmptr_->getLease6(new_duid, leases[1]->iaid_);
  524. EXPECT_EQ(0, returned.size());
  525. }
  526. // @brief Check GetLease6 methods - Access by DUID/IAID/SubnetID
  527. //
  528. // Adds leases to the database and checks that they can be accessed via
  529. // a combination of DIUID and IAID.
  530. TEST_F(MySqlLeaseMgrTest, GetLease6Extended2) {
  531. // Get the leases to be used for the test.
  532. vector<Lease6Ptr> leases = createLeases6();
  533. EXPECT_LE(6, leases.size()); // Expect to access leases 0 through 5
  534. // Add them to the database
  535. for (int i = 0; i < leases.size(); ++i) {
  536. EXPECT_TRUE(lmptr_->addLease(leases[i]));
  537. }
  538. // Get the leases matching the DUID and IAID of lease[1].
  539. Lease6Ptr returned = lmptr_->getLease6(*leases[1]->duid_,
  540. leases[1]->iaid_,
  541. leases[1]->subnet_id_);
  542. ASSERT_TRUE(returned);
  543. EXPECT_TRUE(*returned == *leases[1]);
  544. // Modify each of the three parameters (DUID, IAID, Subnet ID) and
  545. // check that nothing is returned.
  546. returned = lmptr_->getLease6(*leases[1]->duid_, leases[1]->iaid_ + 1,
  547. leases[1]->subnet_id_);
  548. EXPECT_FALSE(returned);
  549. returned = lmptr_->getLease6(*leases[1]->duid_, leases[1]->iaid_,
  550. leases[1]->subnet_id_ + 1);
  551. EXPECT_FALSE(returned);
  552. // Alter the leases[1] DUID to match nothing in the database.
  553. vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
  554. ++duid_vector[0];
  555. DUID new_duid(duid_vector);
  556. returned = lmptr_->getLease6(new_duid, leases[1]->iaid_,
  557. leases[1]->subnet_id_);
  558. EXPECT_FALSE(returned);
  559. }
  560. // @brief Lease6 Update Tests
  561. //
  562. // Checks that we are able to update a lease in the database.
  563. TEST_F(MySqlLeaseMgrTest, UpdateLease6) {
  564. // Get the leases to be used for the test.
  565. vector<Lease6Ptr> leases = createLeases6();
  566. EXPECT_LE(3, leases.size()); // Expect to access leases 0 through 5
  567. // Add a lease to the database and check that the lease is there.
  568. EXPECT_TRUE(lmptr_->addLease(leases[1]));
  569. lmptr_->commit();
  570. reopen();
  571. Lease6Ptr l_returned = lmptr_->getLease6(L1_IOADDRESS);
  572. EXPECT_TRUE(l_returned);
  573. detailCompareLease6(leases[1], l_returned);
  574. // Modify some fields in lease 1 (not the address) and update it.
  575. ++leases[1]->iaid_;
  576. leases[1]->type_ = Lease6::LEASE_IA_PD;
  577. leases[1]->valid_lft_ *= 2;
  578. lmptr_->updateLease6(leases[1]);
  579. lmptr_->commit();
  580. reopen();
  581. // ... and check what is returned is what is expected.
  582. l_returned.reset();
  583. l_returned = lmptr_->getLease6(L1_IOADDRESS);
  584. EXPECT_TRUE(l_returned);
  585. detailCompareLease6(leases[1], l_returned);
  586. // Alter the lease again and check.
  587. ++leases[1]->iaid_;
  588. leases[1]->type_ = Lease6::LEASE_IA_TA;
  589. leases[1]->cltt_ += 6;
  590. leases[1]->prefixlen_ = 93;
  591. lmptr_->updateLease6(leases[1]);
  592. l_returned.reset();
  593. l_returned = lmptr_->getLease6(L1_IOADDRESS);
  594. EXPECT_TRUE(l_returned);
  595. detailCompareLease6(leases[1], l_returned);
  596. // Check we can do an update without changing data.
  597. lmptr_->updateLease6(leases[1]);
  598. l_returned.reset();
  599. l_returned = lmptr_->getLease6(L1_IOADDRESS);
  600. EXPECT_TRUE(l_returned);
  601. detailCompareLease6(leases[1], l_returned);
  602. // Try updating a lease not in the database.
  603. EXPECT_THROW(lmptr_->updateLease6(leases[2]), isc::dhcp::NoSuchLease);
  604. }
  605. }; // Of anonymous namespace