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/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