csv_lease_file6_unittest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (C) 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 <dhcp/duid.h>
  17. #include <dhcpsrv/csv_lease_file6.h>
  18. #include <dhcpsrv/lease.h>
  19. #include <dhcpsrv/tests/lease_file_io.h>
  20. #include <boost/scoped_ptr.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include <gtest/gtest.h>
  23. #include <sstream>
  24. using namespace isc;
  25. using namespace isc::asiolink;
  26. using namespace isc::dhcp;
  27. using namespace isc::dhcp::test;
  28. using namespace isc::util;
  29. namespace {
  30. // DUID values used by unit tests.
  31. const uint8_t DUID0[] = { 0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf };
  32. const uint8_t DUID1[] = { 1, 1, 1, 1, 0xa, 1, 2, 3, 4, 5 };
  33. /// @brief Test fixture class for @c CSVLeaseFile6 validation.
  34. class CSVLeaseFile6Test : public ::testing::Test {
  35. public:
  36. /// @brief Constructor.
  37. ///
  38. /// Initializes IO for lease file used by unit tests.
  39. CSVLeaseFile6Test();
  40. /// @brief Prepends the absolute path to the file specified
  41. /// as an argument.
  42. ///
  43. /// @param filename Name of the file.
  44. /// @return Absolute path to the test file.
  45. static std::string absolutePath(const std::string& filename);
  46. /// @brief Create DUID object from the binary.
  47. ///
  48. /// @param duid Binary value representing a DUID.
  49. /// @param size Size of the DUID.
  50. /// @return Pointer to the @c DUID object.
  51. DuidPtr makeDUID(const uint8_t* duid, const unsigned int size) const {
  52. return (DuidPtr(new DUID(duid, size)));
  53. }
  54. /// @brief Create lease file that can be parsed by unit tests.
  55. void writeSampleFile() const;
  56. /// @brief Name of the test lease file.
  57. std::string filename_;
  58. /// @brief Object providing access to lease file IO.
  59. LeaseFileIO io_;
  60. };
  61. CSVLeaseFile6Test::CSVLeaseFile6Test()
  62. : filename_(absolutePath("leases6.csv")), io_(filename_) {
  63. }
  64. std::string
  65. CSVLeaseFile6Test::absolutePath(const std::string& filename) {
  66. std::ostringstream s;
  67. s << DHCP_DATA_DIR << "/" << filename;
  68. return (s.str());
  69. }
  70. void
  71. CSVLeaseFile6Test::writeSampleFile() const {
  72. io_.writeFile("address,duid,valid_lifetime,expire,subnet_id,"
  73. "pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,"
  74. "fqdn_rev,hostname,hwaddr\n"
  75. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
  76. "200,200,8,100,0,7,0,1,1,host.example.com\n"
  77. "2001:db8:1::1,,200,200,8,100,0,7,0,1,1,host.example.com\n"
  78. "2001:db8:2::10,01:01:01:01:0a:01:02:03:04:05,300,300,6,150,"
  79. "0,8,0,0,0,\n"
  80. "3000:1::,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,0,200,8,0,2,"
  81. "16,64,0,0,,\n");
  82. }
  83. // This test checks the capability to read and parse leases from the file.
  84. TEST_F(CSVLeaseFile6Test, parse) {
  85. // Create a file to be parsed.
  86. writeSampleFile();
  87. // Open the lease file.
  88. boost::scoped_ptr<CSVLeaseFile6> lf(new CSVLeaseFile6(filename_));
  89. ASSERT_NO_THROW(lf->open());
  90. Lease6Ptr lease;
  91. // Reading first read should be successful.
  92. EXPECT_TRUE(lf->next(lease));
  93. ASSERT_TRUE(lease);
  94. // Verify that the lease attributes are correct.
  95. EXPECT_EQ("2001:db8:1::1", lease->addr_.toText());
  96. ASSERT_TRUE(lease->duid_);
  97. EXPECT_EQ("00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f", lease->duid_->toText());
  98. EXPECT_EQ(200, lease->valid_lft_);
  99. EXPECT_EQ(0, lease->cltt_);
  100. EXPECT_EQ(8, lease->subnet_id_);
  101. EXPECT_EQ(100, lease->preferred_lft_);
  102. EXPECT_EQ(Lease::TYPE_NA, lease->type_);
  103. EXPECT_EQ(7, lease->iaid_);
  104. EXPECT_EQ(0, lease->prefixlen_);
  105. EXPECT_TRUE(lease->fqdn_fwd_);
  106. EXPECT_TRUE(lease->fqdn_rev_);
  107. EXPECT_EQ("host.example.com", lease->hostname_);
  108. // Second lease is malformed - DUID is empty.
  109. EXPECT_FALSE(lf->next(lease));
  110. // Even, parsing previous lease failed, reading the next lease should be
  111. // successful.
  112. EXPECT_TRUE(lf->next(lease));
  113. ASSERT_TRUE(lease);
  114. // Verify that the third lease is correct.
  115. EXPECT_EQ("2001:db8:2::10", lease->addr_.toText());
  116. ASSERT_TRUE(lease->duid_);
  117. EXPECT_EQ("01:01:01:01:0a:01:02:03:04:05", lease->duid_->toText());
  118. EXPECT_EQ(300, lease->valid_lft_);
  119. EXPECT_EQ(0, lease->cltt_);
  120. EXPECT_EQ(6, lease->subnet_id_);
  121. EXPECT_EQ(150, lease->preferred_lft_);
  122. EXPECT_EQ(Lease::TYPE_NA, lease->type_);
  123. EXPECT_EQ(8, lease->iaid_);
  124. EXPECT_EQ(0, lease->prefixlen_);
  125. EXPECT_FALSE(lease->fqdn_fwd_);
  126. EXPECT_FALSE(lease->fqdn_rev_);
  127. EXPECT_TRUE(lease->hostname_.empty());
  128. // Reading the fourth lease should be successful.
  129. EXPECT_TRUE(lf->next(lease));
  130. ASSERT_TRUE(lease);
  131. // Verify that the lease is correct.
  132. EXPECT_EQ("3000:1::", lease->addr_.toText());
  133. ASSERT_TRUE(lease->duid_);
  134. EXPECT_EQ("00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f", lease->duid_->toText());
  135. EXPECT_EQ(0, lease->valid_lft_);
  136. EXPECT_EQ(200, lease->cltt_);
  137. EXPECT_EQ(8, lease->subnet_id_);
  138. EXPECT_EQ(0, lease->preferred_lft_);
  139. EXPECT_EQ(Lease::TYPE_PD, lease->type_);
  140. EXPECT_EQ(16, lease->iaid_);
  141. EXPECT_EQ(64, lease->prefixlen_);
  142. EXPECT_FALSE(lease->fqdn_fwd_);
  143. EXPECT_FALSE(lease->fqdn_rev_);
  144. EXPECT_TRUE(lease->hostname_.empty());
  145. // There are no more leases. Reading should cause no error, but the returned
  146. // lease pointer should be NULL.
  147. EXPECT_TRUE(lf->next(lease));
  148. EXPECT_FALSE(lease);
  149. // We should be able to do it again.
  150. EXPECT_TRUE(lf->next(lease));
  151. EXPECT_FALSE(lease);
  152. }
  153. // This test checks creation of the lease file and writing leases.
  154. TEST_F(CSVLeaseFile6Test, recreate) {
  155. boost::scoped_ptr<CSVLeaseFile6> lf(new CSVLeaseFile6(filename_));
  156. ASSERT_NO_THROW(lf->recreate());
  157. ASSERT_TRUE(io_.exists());
  158. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"),
  159. makeDUID(DUID0, sizeof(DUID0)),
  160. 7, 100, 200, 50, 80, 8, true, true,
  161. "host.example.com"));
  162. lease->cltt_ = 0;
  163. ASSERT_NO_THROW(lf->append(*lease));
  164. lease.reset(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:2::10"),
  165. makeDUID(DUID1, sizeof(DUID1)),
  166. 8, 150, 300, 40, 70, 6, false, false,
  167. "", HWAddrPtr(), 128));
  168. lease->cltt_ = 0;
  169. ASSERT_NO_THROW(lf->append(*lease));
  170. lease.reset(new Lease6(Lease::TYPE_PD, IOAddress("3000:1:1::"),
  171. makeDUID(DUID0, sizeof(DUID0)),
  172. 7, 150, 300, 40, 70, 10, false, false,
  173. "", HWAddrPtr(), 64));
  174. lease->cltt_ = 0;
  175. ASSERT_NO_THROW(lf->append(*lease));
  176. EXPECT_EQ("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  177. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
  178. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
  179. "200,200,8,100,0,7,0,1,1,host.example.com,\n"
  180. "2001:db8:2::10,01:01:01:01:0a:01:02:03:04:05"
  181. ",300,300,6,150,0,8,128,0,0,,\n"
  182. "3000:1:1::,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
  183. "300,300,10,150,2,7,64,0,0,,\n",
  184. io_.readFile());
  185. }
  186. /// @todo Currently we don't check invalid lease attributes, such as invalid
  187. /// lease type, invalid preferred lifetime vs valid lifetime etc. The Lease6
  188. /// should be extended with the function that validates lease attributes. Once
  189. /// this is implemented we should provide more tests for malformed leases
  190. /// in the CSV file. See http://kea.isc.org/ticket/2405.
  191. } // end of anonymous namespace