lease_mgr_factory_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 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 <asiolink/io_address.h>
  16. #include <dhcpsrv/lease_mgr_factory.h>
  17. #include <exceptions/exceptions.h>
  18. #include <gtest/gtest.h>
  19. #include <iostream>
  20. #include <sstream>
  21. using namespace std;
  22. using namespace isc::dhcp;
  23. // This set of tests only check the parsing functions of LeaseMgrFactory.
  24. // Tests of the LeaseMgr create/instance/destroy are implicitly carried out
  25. // in the tests for the different concrete lease managers (e.g. MySqlLeaseMgr).
  26. namespace {
  27. // empty class for now, but may be extended once Addr6 becomes bigger
  28. class LeaseMgrFactoryTest : public ::testing::Test {
  29. public:
  30. LeaseMgrFactoryTest() {
  31. }
  32. };
  33. // This test checks that a database access string can be parsed correctly.
  34. TEST_F(LeaseMgrFactoryTest, parse) {
  35. LeaseMgr::ParameterMap parameters = LeaseMgrFactory::parse(
  36. "user=me password=forbidden name=kea somethingelse= type=mysql");
  37. EXPECT_EQ(5, parameters.size());
  38. EXPECT_EQ("me", parameters["user"]);
  39. EXPECT_EQ("forbidden", parameters["password"]);
  40. EXPECT_EQ("kea", parameters["name"]);
  41. EXPECT_EQ("mysql", parameters["type"]);
  42. EXPECT_EQ("", parameters["somethingelse"]);
  43. }
  44. // This test checks that an invalid database access string behaves as expected.
  45. TEST_F(LeaseMgrFactoryTest, parseInvalid) {
  46. // No tokens in the string, so we expect no parameters
  47. std::string invalid = "";
  48. LeaseMgr::ParameterMap parameters = LeaseMgrFactory::parse(invalid);
  49. EXPECT_EQ(0, parameters.size());
  50. // With spaces, there are some tokens so we expect invalid parameter
  51. // as there are no equals signs.
  52. invalid = " \t ";
  53. EXPECT_THROW(LeaseMgrFactory::parse(invalid), isc::InvalidParameter);
  54. invalid = " noequalshere ";
  55. EXPECT_THROW(LeaseMgrFactory::parse(invalid), isc::InvalidParameter);
  56. // A single "=" is valid string, but is placed here as the result is
  57. // expected to be nothing.
  58. invalid = "=";
  59. parameters = LeaseMgrFactory::parse(invalid);
  60. EXPECT_EQ(1, parameters.size());
  61. EXPECT_EQ("", parameters[""]);
  62. }
  63. /// @brief redactConfigString test
  64. ///
  65. /// Checks that the redacted configuration string includes the password only
  66. /// as a set of asterisks.
  67. TEST_F(LeaseMgrFactoryTest, redactAccessString) {
  68. LeaseMgr::ParameterMap parameters =
  69. LeaseMgrFactory::parse("user=me password=forbidden name=kea type=mysql");
  70. EXPECT_EQ(4, parameters.size());
  71. EXPECT_EQ("me", parameters["user"]);
  72. EXPECT_EQ("forbidden", parameters["password"]);
  73. EXPECT_EQ("kea", parameters["name"]);
  74. EXPECT_EQ("mysql", parameters["type"]);
  75. // Redact the result. To check, break the redacted string down into its
  76. // components.
  77. std::string redacted = LeaseMgrFactory::redactedAccessString(parameters);
  78. parameters = LeaseMgrFactory::parse(redacted);
  79. EXPECT_EQ(4, parameters.size());
  80. EXPECT_EQ("me", parameters["user"]);
  81. EXPECT_EQ("*****", parameters["password"]);
  82. EXPECT_EQ("kea", parameters["name"]);
  83. EXPECT_EQ("mysql", parameters["type"]);
  84. }
  85. /// @brief redactConfigString test - empty password
  86. ///
  87. /// Checks that the redacted configuration string includes the password only
  88. /// as a set of asterisks, even if the password is null.
  89. TEST_F(LeaseMgrFactoryTest, redactAccessStringEmptyPassword) {
  90. LeaseMgr::ParameterMap parameters =
  91. LeaseMgrFactory::parse("user=me name=kea type=mysql password=");
  92. EXPECT_EQ(4, parameters.size());
  93. EXPECT_EQ("me", parameters["user"]);
  94. EXPECT_EQ("", parameters["password"]);
  95. EXPECT_EQ("kea", parameters["name"]);
  96. EXPECT_EQ("mysql", parameters["type"]);
  97. // Redact the result. To check, break the redacted string down into its
  98. // components.
  99. std::string redacted = LeaseMgrFactory::redactedAccessString(parameters);
  100. parameters = LeaseMgrFactory::parse(redacted);
  101. EXPECT_EQ(4, parameters.size());
  102. EXPECT_EQ("me", parameters["user"]);
  103. EXPECT_EQ("*****", parameters["password"]);
  104. EXPECT_EQ("kea", parameters["name"]);
  105. EXPECT_EQ("mysql", parameters["type"]);
  106. // ... and again to check that the position of the empty password in the
  107. // string does not matter.
  108. parameters = LeaseMgrFactory::parse("user=me password= name=kea type=mysql");
  109. EXPECT_EQ(4, parameters.size());
  110. EXPECT_EQ("me", parameters["user"]);
  111. EXPECT_EQ("", parameters["password"]);
  112. EXPECT_EQ("kea", parameters["name"]);
  113. EXPECT_EQ("mysql", parameters["type"]);
  114. redacted = LeaseMgrFactory::redactedAccessString(parameters);
  115. parameters = LeaseMgrFactory::parse(redacted);
  116. EXPECT_EQ(4, parameters.size());
  117. EXPECT_EQ("me", parameters["user"]);
  118. EXPECT_EQ("*****", parameters["password"]);
  119. EXPECT_EQ("kea", parameters["name"]);
  120. EXPECT_EQ("mysql", parameters["type"]);
  121. }
  122. /// @brief redactConfigString test - no password
  123. ///
  124. /// Checks that the redacted configuration string excludes the password if there
  125. /// was no password to begin with.
  126. TEST_F(LeaseMgrFactoryTest, redactAccessStringNoPassword) {
  127. LeaseMgr::ParameterMap parameters =
  128. LeaseMgrFactory::parse("user=me name=kea type=mysql");
  129. EXPECT_EQ(3, parameters.size());
  130. EXPECT_EQ("me", parameters["user"]);
  131. EXPECT_EQ("kea", parameters["name"]);
  132. EXPECT_EQ("mysql", parameters["type"]);
  133. // Redact the result. To check, break the redacted string down into its
  134. // components.
  135. std::string redacted = LeaseMgrFactory::redactedAccessString(parameters);
  136. parameters = LeaseMgrFactory::parse(redacted);
  137. EXPECT_EQ(3, parameters.size());
  138. EXPECT_EQ("me", parameters["user"]);
  139. EXPECT_EQ("kea", parameters["name"]);
  140. EXPECT_EQ("mysql", parameters["type"]);
  141. }
  142. }; // end of anonymous namespace