|
@@ -16,6 +16,7 @@
|
|
|
|
|
|
#include <asiolink/io_address.h>
|
|
|
#include <dhcpsrv/lease_mgr_factory.h>
|
|
|
+#include <exceptions/exceptions.h>
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
@@ -37,16 +38,86 @@ public:
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-// This test checks if the LeaseMgr can be instantiated and that it
|
|
|
-// parses parameters string properly.
|
|
|
+// This test checks that a database access string can be parsed correctly.
|
|
|
TEST_F(LeaseMgrFactoryTest, parse) {
|
|
|
|
|
|
- std::map<std::string, std::string> parameters = LeaseMgrFactory::parse(
|
|
|
- "param1=value1 param2=value2 param3=value3");
|
|
|
+ LeaseMgr::ParameterMap parameters = LeaseMgrFactory::parse(
|
|
|
+ "user=me password=forbidden name=kea somethingelse= type=mysql");
|
|
|
|
|
|
- EXPECT_EQ("value1", parameters["param1"]);
|
|
|
- EXPECT_EQ("value2", parameters["param2"]);
|
|
|
- EXPECT_TRUE(parameters.find("type") == parameters.end());
|
|
|
+ EXPECT_EQ(5, parameters.size());
|
|
|
+ EXPECT_EQ("me", parameters["user"]);
|
|
|
+ EXPECT_EQ("forbidden", parameters["password"]);
|
|
|
+ EXPECT_EQ("kea", parameters["name"]);
|
|
|
+ EXPECT_EQ("mysql", parameters["type"]);
|
|
|
+ EXPECT_EQ("", parameters["somethingelse"]);
|
|
|
+}
|
|
|
+
|
|
|
+// This test checks that an invalid database access string behaves as expected.
|
|
|
+TEST_F(LeaseMgrFactoryTest, parseInvalid) {
|
|
|
+
|
|
|
+ // No tokens in the string, so we expect no parameters
|
|
|
+ std::string invalid = "";
|
|
|
+ LeaseMgr::ParameterMap parameters = LeaseMgrFactory::parse(invalid);
|
|
|
+ EXPECT_EQ(0, parameters.size());
|
|
|
+
|
|
|
+ // With spaces, there are some tokens so we expect invalid parameter
|
|
|
+ // as there are no equals signs.
|
|
|
+ invalid = " \t ";
|
|
|
+ EXPECT_THROW(LeaseMgrFactory::parse(invalid), isc::InvalidParameter);
|
|
|
+
|
|
|
+ invalid = " noequalshere ";
|
|
|
+ EXPECT_THROW(LeaseMgrFactory::parse(invalid), isc::InvalidParameter);
|
|
|
+
|
|
|
+ // A single "=" is valid string, but is placed here as the result is
|
|
|
+ // expected to be nothing.
|
|
|
+ invalid = "=";
|
|
|
+ parameters = LeaseMgrFactory::parse(invalid);
|
|
|
+ EXPECT_EQ(1, parameters.size());
|
|
|
+ EXPECT_EQ("", parameters[""]);
|
|
|
+}
|
|
|
+
|
|
|
+/// @brief redactConfigString test
|
|
|
+///
|
|
|
+/// Checks that the redacted configuration string includes the password only
|
|
|
+/// as a set of asterisks.
|
|
|
+TEST(LeaseMgr, redactAccessString) {
|
|
|
+
|
|
|
+ // To check the redacted string, break it down into its component
|
|
|
+ // parameters and check the results.
|
|
|
+ LeaseMgr::ParameterMap parameters = LeaseMgrFactory::parse(
|
|
|
+ "user=me password=forbidden name=kea type=mysql");
|
|
|
+ std::string redacted = LeaseMgrFactory::redactedAccessString(parameters);
|
|
|
+
|
|
|
+ // ... and break the redacted string down into its components.
|
|
|
+ parameters = LeaseMgrFactory::parse(redacted);
|
|
|
+
|
|
|
+ EXPECT_EQ(4, parameters.size());
|
|
|
+ EXPECT_EQ("me", parameters["user"]);
|
|
|
+ EXPECT_EQ("*****", parameters["password"]);
|
|
|
+ EXPECT_EQ("kea", parameters["name"]);
|
|
|
+ EXPECT_EQ("mysql", parameters["type"]);
|
|
|
+
|
|
|
+ // Do the same, but check it works for an empty password.
|
|
|
+ parameters = LeaseMgrFactory::parse(
|
|
|
+ "user=me password=forbidden name=kea type=mysql");
|
|
|
+ redacted = LeaseMgrFactory::redactedAccessString(parameters);
|
|
|
+ parameters = LeaseMgrFactory::parse(redacted);
|
|
|
+
|
|
|
+ EXPECT_EQ(4, parameters.size());
|
|
|
+ EXPECT_EQ("me", parameters["user"]);
|
|
|
+ EXPECT_EQ("*****", parameters["password"]);
|
|
|
+ EXPECT_EQ("kea", parameters["name"]);
|
|
|
+ EXPECT_EQ("mysql", parameters["type"]);
|
|
|
+
|
|
|
+ // Do the same, but check it works in the absence of a password token
|
|
|
+ parameters = LeaseMgrFactory::parse("user=me name=kea type=mysql");
|
|
|
+ redacted = LeaseMgrFactory::redactedAccessString(parameters);
|
|
|
+ parameters = LeaseMgrFactory::parse(redacted);
|
|
|
+
|
|
|
+ EXPECT_EQ(3, parameters.size());
|
|
|
+ EXPECT_EQ("me", parameters["user"]);
|
|
|
+ EXPECT_EQ("kea", parameters["name"]);
|
|
|
+ EXPECT_EQ("mysql", parameters["type"]);
|
|
|
}
|
|
|
|
|
|
}; // end of anonymous namespace
|