csv_lease_file6_unittest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (C) 2014-2015 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 Checks the stats for the file
  57. ///
  58. /// This method is passed a leasefile and the values for the statistics it
  59. /// should have for comparison.
  60. ///
  61. /// @param lease_file A reference to the file we are using
  62. /// @param reads the number of attempted reads
  63. /// @param read_leases the number of valid leases read
  64. /// @param read_errs the number of errors while reading leases
  65. /// @param writes the number of attempted writes
  66. /// @param write_leases the number of leases successfully written
  67. /// @param write_errs the number of errors while writing
  68. void checkStats(CSVLeaseFile6& lease_file,
  69. uint32_t reads, uint32_t read_leases,
  70. uint32_t read_errs, uint32_t writes,
  71. uint32_t write_leases, uint32_t write_errs) const {
  72. EXPECT_EQ(reads, lease_file.getReads());
  73. EXPECT_EQ(read_leases, lease_file.getReadLeases());
  74. EXPECT_EQ(read_errs, lease_file.getReadErrs());
  75. EXPECT_EQ(writes, lease_file.getWrites());
  76. EXPECT_EQ(write_leases, lease_file.getWriteLeases());
  77. EXPECT_EQ(write_errs, lease_file.getWriteErrs());
  78. }
  79. /// @brief Name of the test lease file.
  80. std::string filename_;
  81. /// @brief Object providing access to lease file IO.
  82. LeaseFileIO io_;
  83. };
  84. CSVLeaseFile6Test::CSVLeaseFile6Test()
  85. : filename_(absolutePath("leases6.csv")), io_(filename_) {
  86. }
  87. std::string
  88. CSVLeaseFile6Test::absolutePath(const std::string& filename) {
  89. std::ostringstream s;
  90. s << DHCP_DATA_DIR << "/" << filename;
  91. return (s.str());
  92. }
  93. void
  94. CSVLeaseFile6Test::writeSampleFile() const {
  95. io_.writeFile("address,duid,valid_lifetime,expire,subnet_id,"
  96. "pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,"
  97. "fqdn_rev,hostname,hwaddr\n"
  98. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
  99. "200,200,8,100,0,7,0,1,1,host.example.com\n"
  100. "2001:db8:1::1,,200,200,8,100,0,7,0,1,1,host.example.com\n"
  101. "2001:db8:2::10,01:01:01:01:0a:01:02:03:04:05,300,300,6,150,"
  102. "0,8,0,0,0,\n"
  103. "3000:1::,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,0,200,8,0,2,"
  104. "16,64,0,0,,\n");
  105. }
  106. // This test checks the capability to read and parse leases from the file.
  107. TEST_F(CSVLeaseFile6Test, parse) {
  108. // Create a file to be parsed.
  109. writeSampleFile();
  110. // Open the lease file.
  111. boost::scoped_ptr<CSVLeaseFile6> lf(new CSVLeaseFile6(filename_));
  112. ASSERT_NO_THROW(lf->open());
  113. // Verify the counters are cleared
  114. {
  115. SCOPED_TRACE("Check stats are empty");
  116. checkStats(*lf, 0, 0, 0, 0, 0, 0);
  117. }
  118. Lease6Ptr lease;
  119. // Reading first read should be successful.
  120. {
  121. SCOPED_TRACE("First lease valid");
  122. EXPECT_TRUE(lf->next(lease));
  123. ASSERT_TRUE(lease);
  124. checkStats(*lf, 1, 1, 0, 0, 0, 0);
  125. // Verify that the lease attributes are correct.
  126. EXPECT_EQ("2001:db8:1::1", lease->addr_.toText());
  127. ASSERT_TRUE(lease->duid_);
  128. EXPECT_EQ("00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f", lease->duid_->toText());
  129. EXPECT_EQ(200, lease->valid_lft_);
  130. EXPECT_EQ(0, lease->cltt_);
  131. EXPECT_EQ(8, lease->subnet_id_);
  132. EXPECT_EQ(100, lease->preferred_lft_);
  133. EXPECT_EQ(Lease::TYPE_NA, lease->type_);
  134. EXPECT_EQ(7, lease->iaid_);
  135. EXPECT_EQ(0, lease->prefixlen_);
  136. EXPECT_TRUE(lease->fqdn_fwd_);
  137. EXPECT_TRUE(lease->fqdn_rev_);
  138. EXPECT_EQ("host.example.com", lease->hostname_);
  139. }
  140. // Second lease is malformed - DUID is empty.
  141. {
  142. SCOPED_TRACE("Second lease malformed");
  143. EXPECT_FALSE(lf->next(lease));
  144. checkStats(*lf, 2, 1, 1, 0, 0, 0);
  145. }
  146. // Even, parsing previous lease failed, reading the next lease should be
  147. // successful.
  148. {
  149. SCOPED_TRACE("Third lease valid");
  150. EXPECT_TRUE(lf->next(lease));
  151. ASSERT_TRUE(lease);
  152. checkStats(*lf, 3, 2, 1, 0, 0, 0);
  153. // Verify that the third lease is correct.
  154. EXPECT_EQ("2001:db8:2::10", lease->addr_.toText());
  155. ASSERT_TRUE(lease->duid_);
  156. EXPECT_EQ("01:01:01:01:0a:01:02:03:04:05", lease->duid_->toText());
  157. EXPECT_EQ(300, lease->valid_lft_);
  158. EXPECT_EQ(0, lease->cltt_);
  159. EXPECT_EQ(6, lease->subnet_id_);
  160. EXPECT_EQ(150, lease->preferred_lft_);
  161. EXPECT_EQ(Lease::TYPE_NA, lease->type_);
  162. EXPECT_EQ(8, lease->iaid_);
  163. EXPECT_EQ(0, lease->prefixlen_);
  164. EXPECT_FALSE(lease->fqdn_fwd_);
  165. EXPECT_FALSE(lease->fqdn_rev_);
  166. EXPECT_TRUE(lease->hostname_.empty());
  167. }
  168. // Reading the fourth lease should be successful.
  169. {
  170. SCOPED_TRACE("Fourth lease valid");
  171. EXPECT_TRUE(lf->next(lease));
  172. ASSERT_TRUE(lease);
  173. checkStats(*lf, 4, 3, 1, 0, 0, 0);
  174. // Verify that the lease is correct.
  175. EXPECT_EQ("3000:1::", lease->addr_.toText());
  176. ASSERT_TRUE(lease->duid_);
  177. EXPECT_EQ("00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f", lease->duid_->toText());
  178. EXPECT_EQ(0, lease->valid_lft_);
  179. EXPECT_EQ(200, lease->cltt_);
  180. EXPECT_EQ(8, lease->subnet_id_);
  181. EXPECT_EQ(0, lease->preferred_lft_);
  182. EXPECT_EQ(Lease::TYPE_PD, lease->type_);
  183. EXPECT_EQ(16, lease->iaid_);
  184. EXPECT_EQ(64, lease->prefixlen_);
  185. EXPECT_FALSE(lease->fqdn_fwd_);
  186. EXPECT_FALSE(lease->fqdn_rev_);
  187. EXPECT_TRUE(lease->hostname_.empty());
  188. }
  189. // There are no more leases. Reading should cause no error, but the returned
  190. // lease pointer should be NULL.
  191. {
  192. SCOPED_TRACE("Fifth read empty");
  193. EXPECT_TRUE(lf->next(lease));
  194. EXPECT_FALSE(lease);
  195. checkStats(*lf, 5, 3, 1, 0, 0, 0);
  196. }
  197. // We should be able to do it again.
  198. {
  199. SCOPED_TRACE("Sixth read empty");
  200. EXPECT_TRUE(lf->next(lease));
  201. EXPECT_FALSE(lease);
  202. checkStats(*lf, 6, 3, 1, 0, 0, 0);
  203. }
  204. }
  205. // This test checks creation of the lease file and writing leases.
  206. TEST_F(CSVLeaseFile6Test, recreate) {
  207. boost::scoped_ptr<CSVLeaseFile6> lf(new CSVLeaseFile6(filename_));
  208. ASSERT_NO_THROW(lf->recreate());
  209. ASSERT_TRUE(io_.exists());
  210. // Verify the counters are cleared
  211. {
  212. SCOPED_TRACE("Check stats are empty");
  213. checkStats(*lf, 0, 0, 0, 0, 0, 0);
  214. }
  215. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"),
  216. makeDUID(DUID0, sizeof(DUID0)),
  217. 7, 100, 200, 50, 80, 8, true, true,
  218. "host.example.com"));
  219. lease->cltt_ = 0;
  220. {
  221. SCOPED_TRACE("First write");
  222. ASSERT_NO_THROW(lf->append(*lease));
  223. checkStats(*lf, 0, 0, 0, 1, 1, 0);
  224. }
  225. lease.reset(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:2::10"),
  226. makeDUID(DUID1, sizeof(DUID1)),
  227. 8, 150, 300, 40, 70, 6, false, false,
  228. "", HWAddrPtr(), 128));
  229. lease->cltt_ = 0;
  230. {
  231. SCOPED_TRACE("Second write");
  232. ASSERT_NO_THROW(lf->append(*lease));
  233. checkStats(*lf, 0, 0, 0, 2, 2, 0);
  234. }
  235. lease.reset(new Lease6(Lease::TYPE_PD, IOAddress("3000:1:1::"),
  236. makeDUID(DUID0, sizeof(DUID0)),
  237. 7, 150, 300, 40, 70, 10, false, false,
  238. "", HWAddrPtr(), 64));
  239. lease->cltt_ = 0;
  240. {
  241. SCOPED_TRACE("Third write");
  242. ASSERT_NO_THROW(lf->append(*lease));
  243. checkStats(*lf, 0, 0, 0, 3, 3, 0);
  244. }
  245. EXPECT_EQ("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  246. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
  247. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
  248. "200,200,8,100,0,7,0,1,1,host.example.com,\n"
  249. "2001:db8:2::10,01:01:01:01:0a:01:02:03:04:05"
  250. ",300,300,6,150,0,8,128,0,0,,\n"
  251. "3000:1:1::,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
  252. "300,300,10,150,2,7,64,0,0,,\n",
  253. io_.readFile());
  254. }
  255. /// @todo Currently we don't check invalid lease attributes, such as invalid
  256. /// lease type, invalid preferred lifetime vs valid lifetime etc. The Lease6
  257. /// should be extended with the function that validates lease attributes. Once
  258. /// this is implemented we should provide more tests for malformed leases
  259. /// in the CSV file. See http://kea.isc.org/ticket/2405.
  260. } // end of anonymous namespace