csv_lease_file4_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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_file4.h>
  18. #include <dhcpsrv/lease.h>
  19. #include <dhcpsrv/tests/lease_file_io.h>
  20. #include <gtest/gtest.h>
  21. #include <sstream>
  22. using namespace isc;
  23. using namespace isc::asiolink;
  24. using namespace isc::dhcp;
  25. using namespace isc::dhcp::test;
  26. using namespace isc::util;
  27. namespace {
  28. // HWADDR values used by unit tests.
  29. const uint8_t HWADDR0[] = { 0, 1, 2, 3, 4, 5 };
  30. const uint8_t HWADDR1[] = { 0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf };
  31. const uint8_t CLIENTID0[] = { 1, 2, 3, 4 };
  32. const uint8_t CLIENTID1[] = { 0xa, 0xb, 0xc, 0xd };
  33. /// @brief Test fixture class for @c CSVLeaseFile4 validation.
  34. class CSVLeaseFile4Test : public ::testing::Test {
  35. public:
  36. /// @brief Constructor.
  37. ///
  38. /// Initializes IO for lease file used by unit tests.
  39. CSVLeaseFile4Test();
  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 Creates the lease file to be parsed by unit tests.
  47. void writeSampleFile() const;
  48. /// @brief Checks the stats for the file
  49. ///
  50. /// This method is passed a leasefile and the values for the statistics it
  51. /// should have for comparison.
  52. ///
  53. /// @param lease_file A reference to the file we are using
  54. /// @param reads the number of attempted reads
  55. /// @param read_leases the number of valid leases read
  56. /// @param read_errs the number of errors while reading leases
  57. /// @param writes the number of attempted writes
  58. /// @param write_leases the number of leases successfully written
  59. /// @param write_errs the number of errors while writing
  60. void checkStats(CSVLeaseFile4& lease_file,
  61. uint32_t reads, uint32_t read_leases,
  62. uint32_t read_errs, uint32_t writes,
  63. uint32_t write_leases, uint32_t write_errs) const {
  64. EXPECT_EQ(reads, lease_file.getReads());
  65. EXPECT_EQ(read_leases, lease_file.getReadLeases());
  66. EXPECT_EQ(read_errs, lease_file.getReadErrs());
  67. EXPECT_EQ(writes, lease_file.getWrites());
  68. EXPECT_EQ(write_leases, lease_file.getWriteLeases());
  69. EXPECT_EQ(write_errs, lease_file.getWriteErrs());
  70. }
  71. /// @brief Name of the test lease file.
  72. std::string filename_;
  73. /// @brief Object providing access to lease file IO.
  74. LeaseFileIO io_;
  75. /// @brief hardware address 0 (corresponds to HWADDR0 const)
  76. HWAddrPtr hwaddr0_;
  77. /// @brief hardware address 1 (corresponds to HWADDR1 const)
  78. HWAddrPtr hwaddr1_;
  79. };
  80. CSVLeaseFile4Test::CSVLeaseFile4Test()
  81. : filename_(absolutePath("leases4.csv")), io_(filename_) {
  82. hwaddr0_.reset(new HWAddr(HWADDR0, sizeof(HWADDR0), HTYPE_ETHER));
  83. hwaddr1_.reset(new HWAddr(HWADDR1, sizeof(HWADDR1), HTYPE_ETHER));
  84. }
  85. std::string
  86. CSVLeaseFile4Test::absolutePath(const std::string& filename) {
  87. std::ostringstream s;
  88. s << DHCP_DATA_DIR << "/" << filename;
  89. return (s.str());
  90. }
  91. void
  92. CSVLeaseFile4Test::writeSampleFile() const {
  93. io_.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  94. "fqdn_fwd,fqdn_rev,hostname,state\n"
  95. "192.0.2.1,06:07:08:09:0a:bc,,200,200,8,1,1,"
  96. "host.example.com,0\n"
  97. "192.0.2.1,,a:11:01:04,200,200,8,1,1,host.example.com,1\n"
  98. "192.0.3.15,dd:de:ba:0d:1b:2e:3e:4f,0a:00:01:04,100,100,7,"
  99. "0,0,,1\n");
  100. }
  101. // This test checks the capability to read and parse leases from the file.
  102. TEST_F(CSVLeaseFile4Test, parse) {
  103. // Create a file to be parsed.
  104. writeSampleFile();
  105. // Open the lease file.
  106. CSVLeaseFile4 lf(filename_);
  107. ASSERT_NO_THROW(lf.open());
  108. // Verify the counters are cleared
  109. {
  110. SCOPED_TRACE("Check stats are empty");
  111. checkStats(lf, 0, 0, 0, 0, 0, 0);
  112. }
  113. Lease4Ptr lease;
  114. // Reading first read should be successful.
  115. {
  116. SCOPED_TRACE("First lease valid");
  117. EXPECT_TRUE(lf.next(lease));
  118. ASSERT_TRUE(lease);
  119. checkStats(lf, 1, 1, 0, 0, 0, 0);
  120. // Verify that the lease attributes are correct.
  121. EXPECT_EQ("192.0.2.1", lease->addr_.toText());
  122. HWAddr hwaddr1(*lease->hwaddr_);
  123. EXPECT_EQ("06:07:08:09:0a:bc", hwaddr1.toText(false));
  124. EXPECT_FALSE(lease->client_id_);
  125. EXPECT_EQ(200, lease->valid_lft_);
  126. EXPECT_EQ(0, lease->cltt_);
  127. EXPECT_EQ(8, lease->subnet_id_);
  128. EXPECT_TRUE(lease->fqdn_fwd_);
  129. EXPECT_TRUE(lease->fqdn_rev_);
  130. EXPECT_EQ("host.example.com", lease->hostname_);
  131. EXPECT_EQ(Lease::STATE_DEFAULT, lease->state_);
  132. }
  133. // Second lease is malformed - HW address is empty.
  134. {
  135. SCOPED_TRACE("Second lease malformed");
  136. EXPECT_FALSE(lf.next(lease));
  137. checkStats(lf, 2, 1, 1, 0, 0, 0);
  138. }
  139. // Even though parsing previous lease failed, reading the next lease should be
  140. // successful.
  141. {
  142. SCOPED_TRACE("Third lease valid");
  143. EXPECT_TRUE(lf.next(lease));
  144. ASSERT_TRUE(lease);
  145. checkStats(lf, 3, 2, 1, 0, 0, 0);
  146. // Verify that the third lease is correct.
  147. EXPECT_EQ("192.0.3.15", lease->addr_.toText());
  148. HWAddr hwaddr3(*lease->hwaddr_);
  149. EXPECT_EQ("dd:de:ba:0d:1b:2e:3e:4f", hwaddr3.toText(false));
  150. ASSERT_TRUE(lease->client_id_);
  151. EXPECT_EQ("0a:00:01:04", lease->client_id_->toText());
  152. EXPECT_EQ(100, lease->valid_lft_);
  153. EXPECT_EQ(0, lease->cltt_);
  154. EXPECT_EQ(7, lease->subnet_id_);
  155. EXPECT_FALSE(lease->fqdn_fwd_);
  156. EXPECT_FALSE(lease->fqdn_rev_);
  157. EXPECT_TRUE(lease->hostname_.empty());
  158. EXPECT_EQ(Lease::STATE_DECLINED, lease->state_);
  159. }
  160. // There are no more leases. Reading should cause no error, but the returned
  161. // lease pointer should be NULL.
  162. {
  163. SCOPED_TRACE("Fifth read empty");
  164. EXPECT_TRUE(lf.next(lease));
  165. EXPECT_FALSE(lease);
  166. checkStats(lf, 4, 2, 1, 0, 0, 0);
  167. }
  168. // We should be able to do it again.
  169. {
  170. SCOPED_TRACE("Sixth read empty");
  171. EXPECT_TRUE(lf.next(lease));
  172. EXPECT_FALSE(lease);
  173. checkStats(lf, 5, 2, 1, 0, 0, 0);
  174. }
  175. }
  176. // This test checks creation of the lease file and writing leases.
  177. TEST_F(CSVLeaseFile4Test, recreate) {
  178. CSVLeaseFile4 lf(filename_);
  179. ASSERT_NO_THROW(lf.recreate());
  180. ASSERT_TRUE(io_.exists());
  181. // Verify the counters are cleared
  182. checkStats(lf, 0, 0, 0, 0, 0, 0);
  183. // Create first lease, with NULL client id.
  184. Lease4Ptr lease(new Lease4(IOAddress("192.0.3.2"),
  185. hwaddr0_,
  186. NULL, 0,
  187. 200, 50, 80, 0, 8, true, true,
  188. "host.example.com"));
  189. lease->state_ = Lease::STATE_EXPIRED_RECLAIMED;
  190. {
  191. SCOPED_TRACE("First write");
  192. ASSERT_NO_THROW(lf.append(*lease));
  193. checkStats(lf, 0, 0, 0, 1, 1, 0);
  194. }
  195. // Create second lease, with non-NULL client id.
  196. lease.reset(new Lease4(IOAddress("192.0.3.10"),
  197. hwaddr1_,
  198. CLIENTID0, sizeof(CLIENTID0),
  199. 100, 60, 90, 0, 7));
  200. {
  201. SCOPED_TRACE("Second write");
  202. ASSERT_NO_THROW(lf.append(*lease));
  203. checkStats(lf, 0, 0, 0, 2, 2, 0);
  204. }
  205. // Close the lease file.
  206. lf.close();
  207. // Check that the contents of the csv file are correct.
  208. EXPECT_EQ("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  209. "fqdn_fwd,fqdn_rev,hostname,state\n"
  210. "192.0.3.2,00:01:02:03:04:05,,200,200,8,1,1,host.example.com,2\n"
  211. "192.0.3.10,0d:0e:0a:0d:0b:0e:0e:0f,01:02:03:04,100,100,7,0,"
  212. "0,,0\n",
  213. io_.readFile());
  214. }
  215. // Verifies that a schema 1.0 file with records from
  216. // schema 1.0 and 2.0 loads correctly.
  217. TEST_F(CSVLeaseFile4Test, mixedSchemaload) {
  218. // Create mixed schema file
  219. io_.writeFile(
  220. // schema 1.0 header
  221. "address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  222. "fqdn_fwd,fqdn_rev,hostname\n"
  223. // schema 1.0 record
  224. "192.0.2.1,06:07:08:09:1a:bc,,200,200,8,1,1,"
  225. "one.example.com\n"
  226. // schema 2.0 record - has state
  227. "192.0.2.2,06:07:08:09:2a:bc,,200,200,8,1,1,"
  228. "two.example.com,1\n"
  229. // schema 2.0 record - has state
  230. "192.0.2.3,06:07:08:09:3a:bc,,200,200,8,1,1,"
  231. "three.example.com,2\n"
  232. );
  233. // Open the lease file.
  234. CSVLeaseFile4 lf(filename_);
  235. ASSERT_NO_THROW(lf.open());
  236. Lease4Ptr lease;
  237. // Reading first read should be successful.
  238. {
  239. SCOPED_TRACE("First lease valid");
  240. EXPECT_TRUE(lf.next(lease));
  241. ASSERT_TRUE(lease);
  242. // Verify that the lease attributes are correct.
  243. EXPECT_EQ("192.0.2.1", lease->addr_.toText());
  244. HWAddr hwaddr1(*lease->hwaddr_);
  245. EXPECT_EQ("06:07:08:09:1a:bc", hwaddr1.toText(false));
  246. EXPECT_FALSE(lease->client_id_);
  247. EXPECT_EQ(200, lease->valid_lft_);
  248. EXPECT_EQ(0, lease->cltt_);
  249. EXPECT_EQ(8, lease->subnet_id_);
  250. EXPECT_TRUE(lease->fqdn_fwd_);
  251. EXPECT_TRUE(lease->fqdn_rev_);
  252. EXPECT_EQ("one.example.com", lease->hostname_);
  253. // Verify that added state is DEFAULT
  254. EXPECT_EQ(Lease::STATE_DEFAULT, lease->state_);
  255. }
  256. {
  257. SCOPED_TRACE("Second lease valid");
  258. EXPECT_TRUE(lf.next(lease));
  259. ASSERT_TRUE(lease);
  260. // Verify that the lease attributes are correct.
  261. EXPECT_EQ("192.0.2.2", lease->addr_.toText());
  262. HWAddr hwaddr1(*lease->hwaddr_);
  263. EXPECT_EQ("06:07:08:09:2a:bc", hwaddr1.toText(false));
  264. EXPECT_FALSE(lease->client_id_);
  265. EXPECT_EQ(200, lease->valid_lft_);
  266. EXPECT_EQ(0, lease->cltt_);
  267. EXPECT_EQ(8, lease->subnet_id_);
  268. EXPECT_TRUE(lease->fqdn_fwd_);
  269. EXPECT_TRUE(lease->fqdn_rev_);
  270. EXPECT_EQ("two.example.com", lease->hostname_);
  271. EXPECT_EQ(Lease::STATE_DECLINED, lease->state_);
  272. }
  273. {
  274. SCOPED_TRACE("Third lease valid");
  275. EXPECT_TRUE(lf.next(lease));
  276. ASSERT_TRUE(lease);
  277. // Verify that the third lease is correct.
  278. EXPECT_EQ("192.0.2.3", lease->addr_.toText());
  279. HWAddr hwaddr1(*lease->hwaddr_);
  280. EXPECT_EQ("06:07:08:09:3a:bc", hwaddr1.toText(false));
  281. EXPECT_FALSE(lease->client_id_);
  282. EXPECT_EQ(200, lease->valid_lft_);
  283. EXPECT_EQ(0, lease->cltt_);
  284. EXPECT_EQ(8, lease->subnet_id_);
  285. EXPECT_TRUE(lease->fqdn_fwd_);
  286. EXPECT_TRUE(lease->fqdn_rev_);
  287. EXPECT_EQ("three.example.com", lease->hostname_);
  288. EXPECT_EQ(Lease::STATE_EXPIRED_RECLAIMED, lease->state_);
  289. }
  290. }
  291. // Verifies that a lease file with fewer header columns than the
  292. // minimum allowed will not open.
  293. TEST_F(CSVLeaseFile4Test, tooFewHeaderColumns) {
  294. // Create 1.0 file
  295. io_.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  296. "fqdn_fwd,fqdn_rev\n");
  297. // Open the lease file.
  298. CSVLeaseFile4 lf(filename_);
  299. ASSERT_THROW(lf.open(), CSVFileError);
  300. }
  301. // Verifies that a lease file with an unrecognized column header
  302. // will not open.
  303. TEST_F(CSVLeaseFile4Test, invalidHeaderColumn) {
  304. // Create 1.0 file
  305. io_.writeFile("address,hwaddr,BOGUS,valid_lifetime,expire,subnet_id,"
  306. "fqdn_fwd,fqdn_rev,hostname,state\n");
  307. // Open the lease file.
  308. CSVLeaseFile4 lf(filename_);
  309. ASSERT_THROW(lf.open(), CSVFileError);
  310. }
  311. // Verifies that a lease file with more header columns than defined
  312. // columns will downgrade.
  313. TEST_F(CSVLeaseFile4Test, downGrade) {
  314. // Create 2.0 PLUS a column file
  315. io_.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  316. "fqdn_fwd,fqdn_rev,hostname,state,FUTURE_COL\n"
  317. "192.0.2.3,06:07:08:09:3a:bc,,200,200,8,1,1,"
  318. "three.example.com,2,BOGUS\n");
  319. // Lease file should open and report as needing downgrade.
  320. CSVLeaseFile4 lf(filename_);
  321. ASSERT_NO_THROW(lf.open());
  322. EXPECT_TRUE(lf.needsConversion());
  323. EXPECT_EQ(util::VersionedCSVFile::NEEDS_DOWNGRADE,
  324. lf.getInputSchemaState());
  325. Lease4Ptr lease;
  326. {
  327. SCOPED_TRACE("First lease valid");
  328. EXPECT_TRUE(lf.next(lease));
  329. ASSERT_TRUE(lease);
  330. // Verify that the third lease is correct.
  331. EXPECT_EQ("192.0.2.3", lease->addr_.toText());
  332. HWAddr hwaddr1(*lease->hwaddr_);
  333. EXPECT_EQ("06:07:08:09:3a:bc", hwaddr1.toText(false));
  334. EXPECT_FALSE(lease->client_id_);
  335. EXPECT_EQ(200, lease->valid_lft_);
  336. EXPECT_EQ(0, lease->cltt_);
  337. EXPECT_EQ(8, lease->subnet_id_);
  338. EXPECT_TRUE(lease->fqdn_fwd_);
  339. EXPECT_TRUE(lease->fqdn_rev_);
  340. EXPECT_EQ("three.example.com", lease->hostname_);
  341. EXPECT_EQ(Lease::STATE_EXPIRED_RECLAIMED, lease->state_);
  342. }
  343. }
  344. /// @todo Currently we don't check invalid lease attributes, such as invalid
  345. /// lease type, invalid preferred lifetime vs valid lifetime etc. The Lease6
  346. /// should be extended with the function that validates lease attributes. Once
  347. /// this is implemented we should provide more tests for malformed leases
  348. /// in the CSV file. See http://kea.isc.org/ticket/2405.
  349. } // end of anonymous namespace