Add length limit for user-defined values
Previously, the only limit for most user-defined values like id or name
was the one enforced by dbus, which was fairly big. In order to save
space used by persistent data, new meson options were added, with length
limit for those fields.
This directly impacts following dbus interfaces:
- TriggerManager.AddTrigger:
- Id
- Name
- Reports
- Thresholds (only name of discrete threshold)
- Trigger.Name
- Trigger.Reports
- Trigger.Thresholds (only name of discrete threshold)
- ReportManager.AddReport(FutureVersion):
- Id
- Name
- MetricParameters (metricId)
- Report.Name
- Report.ReadingParameters (metricId)
For Id fields we support 'prefixes', which also are limited, but those
limit are separate. So if limit for prefix is set to 5 and limit for
id/name is set to 5, following Ids are fine:
- 12345/12345
- 12345
and following are not:
- 123456/1234
- 1/123456
Testing done:
- UTs are passing.
- new limits are reflected when calling mentioned dbus interfaces.
Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I29291a1cc56a344d92fb65491c9186fdb90a8529
diff --git a/tests/src/mocks/report_factory_mock.hpp b/tests/src/mocks/report_factory_mock.hpp
index b14bbb7..a043c09 100644
--- a/tests/src/mocks/report_factory_mock.hpp
+++ b/tests/src/mocks/report_factory_mock.hpp
@@ -56,7 +56,7 @@
MOCK_METHOD(void, updateMetrics,
(std::vector<std::shared_ptr<interfaces::Metric>> & metrics,
- bool enabled, const ReadingParameters&),
+ bool enabled, const std::vector<LabeledMetricParameters>&),
(const, override));
MOCK_METHOD(std::unique_ptr<interfaces::Report>, make,
diff --git a/tests/src/params/report_params.hpp b/tests/src/params/report_params.hpp
index 3d32182..458e11b 100644
--- a/tests/src/params/report_params.hpp
+++ b/tests/src/params/report_params.hpp
@@ -9,9 +9,9 @@
class ReportParams final
{
public:
- ReportParams& reportId(std::string val)
+ ReportParams& reportId(std::string_view val)
{
- reportIdProperty = std::move(val);
+ reportIdProperty = val;
return *this;
}
@@ -20,9 +20,9 @@
return reportIdProperty;
}
- ReportParams& reportName(std::string val)
+ ReportParams& reportName(std::string_view val)
{
- reportNameProperty = std::move(val);
+ reportNameProperty = val;
return *this;
}
diff --git a/tests/src/params/trigger_params.hpp b/tests/src/params/trigger_params.hpp
index 65f423e..9f633b8 100644
--- a/tests/src/params/trigger_params.hpp
+++ b/tests/src/params/trigger_params.hpp
@@ -24,9 +24,9 @@
});
}
- TriggerParams& id(std::string val)
+ TriggerParams& id(std::string_view val)
{
- idProperty = std::move(val);
+ idProperty = val;
return *this;
}
@@ -35,9 +35,9 @@
return idProperty;
}
- TriggerParams& name(std::string val)
+ TriggerParams& name(std::string_view val)
{
- nameProperty = std::move(val);
+ nameProperty = val;
return *this;
}
diff --git a/tests/src/test_generate_id.cpp b/tests/src/test_generate_id.cpp
deleted file mode 100644
index dc534dd..0000000
--- a/tests/src/test_generate_id.cpp
+++ /dev/null
@@ -1,172 +0,0 @@
-#include "helpers.hpp"
-#include "utils/generate_id.hpp"
-
-#include <sdbusplus/exception.hpp>
-
-#include <gmock/gmock.h>
-
-using namespace testing;
-using namespace std::literals::string_literals;
-
-class ScenarioBase : public Test
-{
- public:
- std::string_view defaultName = "defName";
- size_t maxSize = 32;
- std::vector<std::string> conflicts;
-};
-
-class ScenarioNameProvided : public ScenarioBase
-{
- public:
- auto generateId(std::string_view id, std::string_view name) const
- {
- return utils::generateId(id, name, defaultName, conflicts, maxSize);
- }
-};
-
-class TestGenerateIdNameProvided : public ScenarioNameProvided
-{};
-
-TEST_F(TestGenerateIdNameProvided, usesIdWhenProvided)
-{
- const std::string name = "name";
-
- EXPECT_THAT(this->generateId("id0", name), Eq(std::pair{"id0"s, name}));
- EXPECT_THAT(this->generateId("/id1", name), Eq(std::pair{"id1"s, name}));
- EXPECT_THAT(this->generateId("prefix/id2", name),
- Eq(std::pair{"prefix/id2"s, name}));
-}
-
-TEST_F(TestGenerateIdNameProvided, usedDefaultWhenNothingProvided)
-{
- this->defaultName = "def";
-
- EXPECT_THAT(this->generateId("", ""), Eq(std::pair{"def"s, "def"s}));
- EXPECT_THAT(this->generateId("/", ""), Eq(std::pair{"def"s, "def"s}));
- EXPECT_THAT(this->generateId("abc/", ""),
- Eq(std::pair{"abc/def"s, "def"s}));
-}
-
-class ScenarioNameNotProvided : public ScenarioBase
-{
- public:
- auto generateId(std::string_view id, std::string_view name) const
- {
- return utils::generateId(id, "", name, conflicts, maxSize);
- }
-};
-
-class TestGenerateIdNameNotProvided : public ScenarioNameNotProvided
-{};
-
-TEST_F(TestGenerateIdNameNotProvided, usesIdAsNameWhenProvided)
-{
- EXPECT_THAT(this->generateId("id0", defaultName),
- Eq(std::pair{"id0"s, "id0"s}));
- EXPECT_THAT(this->generateId("/id1", defaultName),
- Eq(std::pair{"id1"s, "id1"s}));
- EXPECT_THAT(this->generateId("prefix/id2", defaultName),
- Eq(std::pair{"prefix/id2"s, "id2"s}));
-}
-
-template <class Scenario>
-class TestGenerateId : public Scenario
-{};
-
-using TestScenarios =
- ::testing::Types<ScenarioNameProvided, ScenarioNameNotProvided>;
-TYPED_TEST_SUITE(TestGenerateId, TestScenarios);
-
-TEST_F(TestGenerateIdNameNotProvided, leadingSlashDoesntCountToLengthLimit)
-{
- this->maxSize = 2;
-
- EXPECT_THAT(this->generateId("12", "default").first, Eq("12"));
- EXPECT_THAT(this->generateId("/23", "default").first, Eq("23"));
- EXPECT_THROW(this->generateId("123", "trigger"),
- sdbusplus::exception::SdBusError);
-}
-
-TYPED_TEST(TestGenerateId, throwsWhenProvidedIdIsTooLong)
-{
- this->maxSize = 4;
-
- EXPECT_THROW(this->generateId("12345", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("12/45", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("123/", "trigger"),
- sdbusplus::exception::SdBusError);
-}
-
-TYPED_TEST(TestGenerateId, throwsWhenThereAreNoFreeIds)
-{
- this->maxSize = 1;
- this->conflicts = {"n", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
-
- EXPECT_THROW(this->generateId("", "n"), sdbusplus::exception::SdBusError);
-}
-
-TYPED_TEST(TestGenerateId, throwsWhenIdContainsMoreThanOneSlash)
-{
- EXPECT_THROW(this->generateId("/12/", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("12//", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("12//123", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("12/12/123", "name"),
- sdbusplus::exception::SdBusError);
-}
-
-TYPED_TEST(TestGenerateId, usesNamePartOfNameWhenIdNotProvidedAndNameIsTooLong)
-{
- this->maxSize = 4;
-
- this->conflicts = {};
- EXPECT_THAT(this->generateId("", "trigger"),
- Eq(std::pair{"trig"s, "trigger"s}));
- EXPECT_THAT(this->generateId("a/", "trigger"),
- Eq(std::pair{"a/tr"s, "trigger"s}));
-
- this->conflicts = {"trig"};
- EXPECT_THAT(this->generateId("", "trigger"),
- Eq(std::pair{"tri0"s, "trigger"s}));
-
- this->conflicts = {"trig", "tri0"};
- EXPECT_THAT(this->generateId("", "trigger"),
- Eq(std::pair{"tri1"s, "trigger"s}));
-}
-
-TYPED_TEST(TestGenerateId, throwsWhenProvidedIdIsTaken)
-{
- this->conflicts = {"id", "prefix/id"};
-
- EXPECT_THROW(this->generateId("id", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("/id", "name"),
- sdbusplus::exception::SdBusError);
- EXPECT_THROW(this->generateId("prefix/id", "name"),
- sdbusplus::exception::SdBusError);
-}
-
-TYPED_TEST(TestGenerateId, usesNameWhenIdNotProvided)
-{
- EXPECT_THAT(this->generateId("", "name"), Eq(std::pair{"name"s, "name"s}));
- EXPECT_THAT(this->generateId("/", "name"), Eq(std::pair{"name"s, "name"s}));
- EXPECT_THAT(this->generateId("abc/", "name"),
- Eq(std::pair{"abc/name"s, "name"s}));
- EXPECT_THAT(this->generateId("123/", "name"),
- Eq(std::pair{"123/name"s, "name"s}));
-}
-
-TYPED_TEST(TestGenerateId, usesNameWithoutInvalidCharactersWhenIdNotProvided)
-{
- EXPECT_THAT(this->generateId("", "n#a$/m@e"),
- Eq(std::pair{"name"s, "n#a$/m@e"s}));
- EXPECT_THAT(this->generateId("", "n!^aŹ/me"),
- Eq(std::pair{"name"s, "n!^aŹ/me"s}));
- EXPECT_THAT(this->generateId("p/", "n!^aŹ/m*(e"),
- Eq(std::pair{"p/name"s, "n!^aŹ/m*(e"s}));
-}
diff --git a/tests/src/test_make_id_name.cpp b/tests/src/test_make_id_name.cpp
new file mode 100644
index 0000000..e88a8ac
--- /dev/null
+++ b/tests/src/test_make_id_name.cpp
@@ -0,0 +1,189 @@
+#include "helpers.hpp"
+#include "utils/dbus_path_utils.hpp"
+#include "utils/make_id_name.hpp"
+#include "utils/string_utils.hpp"
+
+#include <sdbusplus/exception.hpp>
+
+#include <gmock/gmock.h>
+
+using namespace testing;
+using namespace std::literals::string_literals;
+using namespace utils::string_utils;
+
+class ScenarioBase : public Test
+{
+ public:
+ std::string_view defaultName = "defName";
+ std::vector<std::string> conflicts;
+};
+
+class ScenarioNameProvided : public ScenarioBase
+{
+ public:
+ auto makeIdName(std::string_view id, std::string_view name) const
+ {
+ return utils::makeIdName(id, name, defaultName, conflicts);
+ }
+};
+
+TEST_F(ScenarioNameProvided, throwsWhenProvidedNameIsTooLong)
+{
+ EXPECT_THROW(this->makeIdName("", getTooLongName()),
+ sdbusplus::exception::SdBusError);
+}
+
+class TestMakeIdNameNameProvided : public ScenarioNameProvided
+{};
+
+TEST_F(TestMakeIdNameNameProvided, usesIdWhenProvided)
+{
+ const std::string name = "name";
+
+ EXPECT_THAT(this->makeIdName("id0", name), Eq(std::pair{"id0"s, name}));
+ EXPECT_THAT(this->makeIdName("prefix/id2", name),
+ Eq(std::pair{"prefix/id2"s, name}));
+}
+
+TEST_F(TestMakeIdNameNameProvided, usedDefaultWhenNothingProvided)
+{
+ this->defaultName = "def";
+
+ EXPECT_THAT(this->makeIdName("", ""), Eq(std::pair{"def"s, "def"s}));
+ EXPECT_THAT(this->makeIdName("abc/", ""),
+ Eq(std::pair{"abc/def"s, "def"s}));
+}
+
+TEST_F(TestMakeIdNameNameProvided, usedDefaultWhenNameContainsNoIdChars)
+{
+ this->defaultName = "def";
+ const std::string name = " !";
+
+ EXPECT_THAT(this->makeIdName("", name), Eq(std::pair{"def"s, name}));
+ EXPECT_THAT(this->makeIdName("prefix/", name),
+ Eq(std::pair{"prefix/def"s, name}));
+}
+
+class ScenarioNameNotProvided : public ScenarioBase
+{
+ public:
+ auto makeIdName(std::string_view id, std::string_view name) const
+ {
+ return utils::makeIdName(id, "", name, conflicts);
+ }
+};
+
+class TestMakeIdNameNameNotProvided : public ScenarioNameNotProvided
+{};
+
+TEST_F(TestMakeIdNameNameNotProvided, usesIdAsNameWhenProvided)
+{
+ EXPECT_THAT(this->makeIdName("id0", defaultName),
+ Eq(std::pair{"id0"s, "id0"s}));
+ EXPECT_THAT(this->makeIdName("prefix/id2", defaultName),
+ Eq(std::pair{"prefix/id2"s, "id2"s}));
+}
+
+template <class Scenario>
+class TestMakeIdName : public Scenario
+{};
+
+using TestScenarios =
+ ::testing::Types<ScenarioNameProvided, ScenarioNameNotProvided>;
+TYPED_TEST_SUITE(TestMakeIdName, TestScenarios);
+
+TYPED_TEST(TestMakeIdName, throwsWhenProvidedIdContainsIncorrectCharacters)
+{
+ EXPECT_THROW(this->makeIdName("Id%@%!@%!%()%fooo/Id", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("Id/Id%@%!@%!%()%fooo", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("/123", "trigger"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("/123/", "trigger"),
+ sdbusplus::exception::SdBusError);
+}
+
+TYPED_TEST(TestMakeIdName, throwsWhenProvidedIdContainsTooLongSegment)
+{
+ std::string longPrefix = getTooLongPrefix();
+ std::string longSuffix = getTooLongId();
+ EXPECT_THROW(this->makeIdName(longPrefix + "/", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName(longPrefix + "/Id", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName(longPrefix + "/" + longSuffix, "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("Prefix/" + longSuffix, "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName(longSuffix, "name"),
+ sdbusplus::exception::SdBusError);
+}
+
+TYPED_TEST(TestMakeIdName, throwsWhenProvidedIdOrPrefixTooLong)
+{
+ EXPECT_THROW(this->makeIdName(getTooLongId(), "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName(getTooLongPrefix() + "/Id", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("Prefix/" + getTooLongId(), "trigger"),
+ sdbusplus::exception::SdBusError);
+}
+
+TYPED_TEST(TestMakeIdName, throwsWhenIdContainsMoreThanOneSlash)
+{
+ EXPECT_THROW(this->makeIdName("/12/", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("12//", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("12//123", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("12/12/123", "name"),
+ sdbusplus::exception::SdBusError);
+}
+
+TYPED_TEST(TestMakeIdName, usesNameWhenThereAreConflicts)
+{
+ this->conflicts = {"trigger"};
+ EXPECT_THAT(this->makeIdName("", "trigger"),
+ Eq(std::pair{"trigger0"s, "trigger"s}));
+
+ this->conflicts = {"trigger", "trigger0"};
+ EXPECT_THAT(this->makeIdName("", "trigger"),
+ Eq(std::pair{"trigger1"s, "trigger"s}));
+
+ this->conflicts = {getMaxId()};
+ std::string expectedId = getMaxId();
+ expectedId[expectedId.length() - 1] = '0';
+ EXPECT_THAT(this->makeIdName("", getMaxId()),
+ Eq(std::pair{expectedId, getMaxId()}));
+}
+
+TYPED_TEST(TestMakeIdName, throwsWhenProvidedIdIsTaken)
+{
+ this->conflicts = {"id", "prefix/id"};
+
+ EXPECT_THROW(this->makeIdName("id", "name"),
+ sdbusplus::exception::SdBusError);
+ EXPECT_THROW(this->makeIdName("prefix/id", "name"),
+ sdbusplus::exception::SdBusError);
+}
+
+TYPED_TEST(TestMakeIdName, usesNameWhenIdNotProvided)
+{
+ EXPECT_THAT(this->makeIdName("", "name"), Eq(std::pair{"name"s, "name"s}));
+ EXPECT_THAT(this->makeIdName("abc/", "name"),
+ Eq(std::pair{"abc/name"s, "name"s}));
+ EXPECT_THAT(this->makeIdName("123/", "name"),
+ Eq(std::pair{"123/name"s, "name"s}));
+}
+
+TYPED_TEST(TestMakeIdName, usesNameWithoutInvalidCharactersWhenIdNotProvided)
+{
+ EXPECT_THAT(this->makeIdName("", "n#a$/m@e"),
+ Eq(std::pair{"name"s, "n#a$/m@e"s}));
+ EXPECT_THAT(this->makeIdName("", "n!^aŹ/me"),
+ Eq(std::pair{"name"s, "n!^aŹ/me"s}));
+ EXPECT_THAT(this->makeIdName("p/", "n!^aŹ/m*(e"),
+ Eq(std::pair{"p/name"s, "n!^aŹ/m*(e"s}));
+}
diff --git a/tests/src/test_report.cpp b/tests/src/test_report.cpp
index ac2419d..14239a9 100644
--- a/tests/src/test_report.cpp
+++ b/tests/src/test_report.cpp
@@ -16,6 +16,7 @@
#include "utils/conv_container.hpp"
#include "utils/dbus_path_utils.hpp"
#include "utils/messanger.hpp"
+#include "utils/string_utils.hpp"
#include "utils/transform.hpp"
#include "utils/tstring.hpp"
@@ -277,6 +278,29 @@
Eq(newParams));
}
+TEST_F(TestReport, setReadingParametersWithTooLongMetricId)
+{
+ const ReadingParameters currentValue =
+ toReadingParameters(defaultParams().metricParameters());
+
+ ReadingParameters newParams = toReadingParameters(
+ std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
+ {LabeledSensorInfo{"Service",
+ "/xyz/openbmc_project/sensors/power/psu",
+ "NewMetadata123"}},
+ OperationType::avg,
+ utils::string_utils::getTooLongId(),
+ CollectionTimeScope::startup,
+ CollectionDuration(250ms)}}});
+
+ changeProperty<ReadingParameters>(
+ sut->getPath(), "ReadingParametersFutureVersion",
+ {.valueBefore = Eq(currentValue),
+ .newValue = newParams,
+ .ec = Eq(boost::system::errc::invalid_argument),
+ .valueAfter = Eq(currentValue)});
+}
+
TEST_F(TestReport, setReportingTypeWithValidNewType)
{
changeProperty<std::string>(
diff --git a/tests/src/test_report_manager.cpp b/tests/src/test_report_manager.cpp
index 5d0d4a5..6ce0f1f 100644
--- a/tests/src/test_report_manager.cpp
+++ b/tests/src/test_report_manager.cpp
@@ -8,7 +8,10 @@
#include "report.hpp"
#include "report_manager.hpp"
#include "utils/conversion.hpp"
+#include "utils/dbus_path_utils.hpp"
+#include "utils/string_utils.hpp"
#include "utils/transform.hpp"
+#include "utils/tstring.hpp"
using namespace testing;
using namespace std::string_literals;
@@ -162,7 +165,7 @@
TEST_F(TestReportManager, addReportWithMaxLengthId)
{
- std::string reportId(ReportManager::maxReportIdLength, 'z');
+ std::string reportId = utils::string_utils::getMaxId();
reportParams.reportId(reportId);
reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
@@ -172,13 +175,142 @@
EXPECT_THAT(path, Eq("/"s + reportId));
}
-TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName)
+TEST_F(TestReportManager, addReportWithMaxLengthPrefix)
+{
+ std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId";
+ reportParams.reportId(reportId);
+ reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq("/"s + reportId));
+}
+
+TEST_F(TestReportManager, addReportWithMaxLengthName)
+{
+ reportParams.reportName(utils::string_utils::getMaxName());
+ reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
+}
+
+TEST_F(TestReportManager, addReportWithMaxLengthMetricId)
+{
+ namespace ts = utils::tstring;
+ std::vector<LabeledMetricParameters> newMetricParams{
+ {LabeledMetricParameters{
+ {LabeledSensorInfo{"Service",
+ "/xyz/openbmc_project/sensors/power/p1",
+ "metadata1"}},
+ OperationType::avg,
+ utils::string_utils::getMaxId(),
+ CollectionTimeScope::point,
+ CollectionDuration(Milliseconds(0u))}}};
+
+ reportParams.metricParameters(newMetricParams);
+ reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
+}
+
+TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongFullId)
{
reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
.Times(0);
reportParams.reportId(
- std::string(ReportManager::maxReportIdLength + 1, 'z'));
+ std::string(utils::constants::maxReportFullIdLength + 1, 'z'));
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongId)
+{
+ reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ reportParams.reportId(utils::string_utils::getTooLongId());
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongPrefix)
+{
+ reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ reportParams.reportId(utils::string_utils::getTooLongPrefix() + "/MyId");
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestReportManager, DISABLED_failToAddReportWithTooManyPrefixes)
+{
+ reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ std::string reportId;
+ for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++)
+ {
+ reportId += "prefix/";
+ }
+ reportId += "MyId";
+
+ reportParams.reportId(reportId);
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName)
+{
+ reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ reportParams.reportName(utils::string_utils::getTooLongName());
+
+ auto [ec, path] = addReport(reportParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongMetricId)
+{
+ namespace ts = utils::tstring;
+
+ std::vector<LabeledMetricParameters> newMetricParams{
+ {LabeledMetricParameters{
+ {LabeledSensorInfo{"Service",
+ "/xyz/openbmc_project/sensors/power/p1",
+ "metadata1"}},
+ OperationType::avg,
+ utils::string_utils::getTooLongId(),
+ CollectionTimeScope::point,
+ CollectionDuration(Milliseconds(0u))}}};
+
+ reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ reportParams.metricParameters(newMetricParams);
auto [ec, path] = addReport(reportParams);
diff --git a/tests/src/test_trigger.cpp b/tests/src/test_trigger.cpp
index 5c41d97..9db2632 100644
--- a/tests/src/test_trigger.cpp
+++ b/tests/src/test_trigger.cpp
@@ -12,7 +12,9 @@
#include "trigger.hpp"
#include "trigger_manager.hpp"
#include "utils/conversion_trigger.hpp"
+#include "utils/dbus_path_utils.hpp"
#include "utils/messanger.hpp"
+#include "utils/string_utils.hpp"
#include "utils/transform.hpp"
#include "utils/tstring.hpp"
@@ -113,6 +115,26 @@
property, newValue);
}
+ template <class T>
+ struct ChangePropertyParams
+ {
+ Matcher<T> valueBefore = _;
+ T newValue;
+ Matcher<boost::system::error_code> ec =
+ Eq(boost::system::errc::success);
+ Matcher<T> valueAfter = Eq(newValue);
+ };
+
+ template <class T>
+ static void changeProperty(const std::string& path,
+ const std::string& property,
+ ChangePropertyParams<T> p)
+ {
+ ASSERT_THAT(getProperty<T>(path, property), p.valueBefore);
+ ASSERT_THAT(setProperty<T>(path, property, p.newValue), p.ec);
+ EXPECT_THAT(getProperty<T>(path, property), p.valueAfter);
+ }
+
boost::system::error_code deleteTrigger(const std::string& path)
{
std::promise<boost::system::error_code> methodPromise;
@@ -233,6 +255,34 @@
Eq(boost::system::errc::invalid_argument));
}
+TEST_F(
+ TestTrigger,
+ DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongPrefix)
+{
+ std::vector<object_path> newPropertyVal{
+ object_path("/xyz/openbmc_project/Telemetry/Reports/" +
+ utils::string_utils::getTooLongPrefix() + "/MyReport")};
+
+ EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
+
+ EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
+ Eq(boost::system::errc::invalid_argument));
+}
+
+TEST_F(
+ TestTrigger,
+ DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongId)
+{
+ std::vector<object_path> newPropertyVal{
+ object_path("/xyz/openbmc_project/Telemetry/Reports/Prefix/" +
+ utils::string_utils::getTooLongId())};
+
+ EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
+
+ EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
+ Eq(boost::system::errc::invalid_argument));
+}
+
TEST_F(TestTrigger,
DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)
{
@@ -270,6 +320,36 @@
Eq(boost::system::errc::success));
}
+TEST_F(TestTrigger, setThresholdParamsWithTooLongDiscreteName)
+{
+ const TriggerThresholdParams currentValue =
+ std::visit(utils::FromLabeledThresholdParamConversion(),
+ triggerParams.thresholdParams());
+
+ TriggerThresholdParams newThresholds =
+ std::vector<discrete::ThresholdParam>({std::make_tuple(
+ utils::string_utils::getTooLongName(), "OK", 10, "12.3")});
+
+ changeProperty<TriggerThresholdParams>(
+ sut->getPath(), "Thresholds",
+ {.valueBefore = Eq(currentValue),
+ .newValue = newThresholds,
+ .ec = Eq(boost::system::errc::invalid_argument),
+ .valueAfter = Eq(currentValue)});
+}
+
+TEST_F(TestTrigger, setNameTooLong)
+{
+ std::string currentValue = TriggerParams().name();
+
+ changeProperty<std::string>(
+ sut->getPath(), "Name",
+ {.valueBefore = Eq(currentValue),
+ .newValue = utils::string_utils::getTooLongName(),
+ .ec = Eq(boost::system::errc::invalid_argument),
+ .valueAfter = Eq(currentValue)});
+}
+
TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
{
const auto& labeledParamsBase =
diff --git a/tests/src/test_trigger_manager.cpp b/tests/src/test_trigger_manager.cpp
index 58d0df1..48c76ea 100644
--- a/tests/src/test_trigger_manager.cpp
+++ b/tests/src/test_trigger_manager.cpp
@@ -7,14 +7,18 @@
#include "trigger.hpp"
#include "trigger_manager.hpp"
#include "utils/conversion_trigger.hpp"
+#include "utils/dbus_path_utils.hpp"
+#include "utils/string_utils.hpp"
#include "utils/transform.hpp"
using namespace testing;
using sdbusplus::message::object_path;
+using namespace std::literals::string_literals;
class TestTriggerManager : public Test
{
public:
+ TriggerParams triggerParams;
std::pair<boost::system::error_code, std::string>
addTrigger(const TriggerParams& params)
{
@@ -207,33 +211,157 @@
TEST_F(TestTriggerManager, addTriggerWithoutIdAndWithLongNameTwice)
{
- addTrigger(TriggerParams().id("").name(
- std::string(2 * TriggerManager::maxTriggerIdLength, 'z')));
+ std::string longName = utils::string_utils::getMaxName();
+ addTrigger(TriggerParams().id("").name(longName));
- auto [ec, path] = addTrigger(TriggerParams().id("").name(
- std::string(2 * TriggerManager::maxTriggerIdLength, 'z')));
+ auto [ec, path] = addTrigger(TriggerParams().id("").name(longName));
EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
EXPECT_THAT(path, Not(Eq("")));
}
TEST_F(TestTriggerManager, addTriggerWithMaxLengthId)
{
- auto triggerId = std::string(TriggerManager::maxTriggerIdLength, 'z');
- auto triggerParams = TriggerParams().id(triggerId);
-
- triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock))
- .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
+ std::string reportId = utils::string_utils::getMaxId();
+ triggerParams.id(reportId);
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
auto [ec, path] = addTrigger(triggerParams);
EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
- EXPECT_THAT(path, Eq(triggerMock.getPath()));
+ EXPECT_THAT(path, Eq("/"s + reportId));
+}
+
+TEST_F(TestTriggerManager, addTriggerWithMaxLengthPrefix)
+{
+ std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId";
+ triggerParams.id(reportId);
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq("/"s + reportId));
+}
+
+TEST_F(TestTriggerManager, addTriggerWithMaxLengthName)
+{
+ triggerParams.name(utils::string_utils::getMaxName());
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq("/"s + triggerParams.id()));
+}
+
+TEST_F(TestTriggerManager, addTriggerWithMaxLengthDiscreteThresholdName)
+{
+ namespace ts = utils::tstring;
+
+ triggerParams =
+ TriggerParams()
+ .id("DiscreteTrigger")
+ .name("My Discrete Trigger")
+ .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
+ discrete::LabeledThresholdParam{
+ utils::string_utils::getMaxName(),
+ discrete::Severity::warning, Milliseconds(10).count(),
+ "15.2"}});
+
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq("/"s + triggerParams.id()));
+}
+
+TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWithTooLongFullId)
+{
+ triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ triggerParams.id(
+ std::string(utils::constants::maxReportFullIdLength + 1, 'z'));
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
}
TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWithTooLongId)
{
- auto triggerId = std::string(TriggerManager::maxTriggerIdLength + 1, 'z');
- auto triggerParams = TriggerParams().id(triggerId);
+ triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ triggerParams.id(utils::string_utils::getTooLongId());
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWithTooLongPrefix)
+{
+ triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ triggerParams.id(utils::string_utils::getTooLongPrefix() + "/MyId");
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWithTooManyPrefixes)
+{
+ triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ std::string reportId;
+ for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++)
+ {
+ reportId += "prefix/";
+ }
+ reportId += "MyId";
+
+ triggerParams.id(reportId);
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWithTooLongName)
+{
+ triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
+ .Times(0);
+
+ triggerParams.name(utils::string_utils::getTooLongName());
+
+ auto [ec, path] = addTrigger(triggerParams);
+
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWithTooLongMetricId)
+{
+ namespace ts = utils::tstring;
+
+ triggerParams =
+ TriggerParams()
+ .id("DiscreteTrigger")
+ .name("My Discrete Trigger")
+ .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
+ discrete::LabeledThresholdParam{
+ utils::string_utils::getTooLongName(),
+ discrete::Severity::warning, Milliseconds(10).count(),
+ "15.2"}});
triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
.Times(0);
diff --git a/tests/src/utils/string_utils.cpp b/tests/src/utils/string_utils.cpp
new file mode 100644
index 0000000..a39afca
--- /dev/null
+++ b/tests/src/utils/string_utils.cpp
@@ -0,0 +1,74 @@
+#include "utils/string_utils.hpp"
+
+#include "utils/dbus_path_utils.hpp"
+
+#include <cmath>
+
+namespace details
+{
+constexpr std::string_view allowedCharactersInId =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
+
+std::string repeat(size_t n)
+{
+ std::string result;
+ for (size_t i = 0; i < n; i++)
+ {
+ result += allowedCharactersInId;
+ }
+ return result;
+}
+
+std::string getString(size_t length)
+{
+ return details::repeat(
+ std::ceil(static_cast<double>(length) /
+ static_cast<double>(allowedCharactersInId.length())))
+ .substr(0, length);
+}
+
+std::string getStringWithSpaces(size_t length)
+{
+ std::string result = getString(length);
+ size_t idx = 1;
+ while (idx < length)
+ {
+ result[idx] = ' ';
+ idx += 5;
+ }
+ return result;
+}
+} // namespace details
+
+namespace utils::string_utils
+{
+std::string getMaxPrefix()
+{
+ return details::getString(constants::maxPrefixLength);
+}
+
+std::string getMaxId()
+{
+ return details::getString(constants::maxIdNameLength);
+}
+
+std::string getMaxName()
+{
+ return details::getStringWithSpaces(constants::maxIdNameLength);
+}
+
+std::string getTooLongPrefix()
+{
+ return details::getString(constants::maxPrefixLength + 1);
+}
+
+std::string getTooLongId()
+{
+ return details::getString(constants::maxIdNameLength + 1);
+}
+
+std::string getTooLongName()
+{
+ return details::getStringWithSpaces(constants::maxIdNameLength + 1);
+}
+} // namespace utils::string_utils
\ No newline at end of file
diff --git a/tests/src/utils/string_utils.hpp b/tests/src/utils/string_utils.hpp
new file mode 100644
index 0000000..90f802c
--- /dev/null
+++ b/tests/src/utils/string_utils.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <string>
+
+namespace utils::string_utils
+{
+std::string getMaxPrefix();
+std::string getMaxId();
+std::string getMaxName();
+std::string getTooLongPrefix();
+std::string getTooLongId();
+std::string getTooLongName();
+} // namespace utils::string_utils
\ No newline at end of file