blob: 412fc9b9de7bc29fa81234c2f5cc9146a41d01f3 [file] [log] [blame]
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +01001#include "utils/generate_id.hpp"
2
Krzysztof Grobelnya950e422021-12-31 13:49:00 +01003#include <sdbusplus/exception.hpp>
4
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +01005#include <gmock/gmock.h>
6
7using namespace testing;
Krzysztof Grobelnya950e422021-12-31 13:49:00 +01008using namespace std::literals::string_literals;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +01009
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010010class ScenarioBase : public Test
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010011{
12 public:
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010013 std::string_view defaultName = "defName";
14 size_t maxSize = 32;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010015 std::vector<std::string> conflicts;
16};
17
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010018class ScenarioNameProvided : public ScenarioBase
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010019{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010020 public:
21 auto generateId(std::string_view id, std::string_view name) const
22 {
23 return utils::generateId(id, name, defaultName, conflicts, maxSize);
24 }
25};
26
27class TestGenerateIdNameProvided : public ScenarioNameProvided
28{};
29
30TEST_F(TestGenerateIdNameProvided, usesIdWhenProvided)
31{
32 const std::string name = "name";
33
34 EXPECT_THAT(this->generateId("id0", name), Eq(std::pair{"id0"s, name}));
35 EXPECT_THAT(this->generateId("/id1", name), Eq(std::pair{"id1"s, name}));
36 EXPECT_THAT(this->generateId("prefix/id2", name),
37 Eq(std::pair{"prefix/id2"s, name}));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010038}
39
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010040TEST_F(TestGenerateIdNameProvided, usedDefaultWhenNothingProvided)
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010041{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010042 this->defaultName = "def";
43
44 EXPECT_THAT(this->generateId("", ""), Eq(std::pair{"def"s, "def"s}));
45 EXPECT_THAT(this->generateId("/", ""), Eq(std::pair{"def"s, "def"s}));
46 EXPECT_THAT(this->generateId("abc/", ""),
47 Eq(std::pair{"abc/def"s, "def"s}));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010048}
49
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010050class ScenarioNameNotProvided : public ScenarioBase
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010051{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010052 public:
53 auto generateId(std::string_view id, std::string_view name) const
54 {
55 return utils::generateId(id, "", name, conflicts, maxSize);
56 }
57};
58
59class TestGenerateIdNameNotProvided : public ScenarioNameNotProvided
60{};
61
62TEST_F(TestGenerateIdNameNotProvided, usesIdAsNameWhenProvided)
63{
64 EXPECT_THAT(this->generateId("id0", defaultName),
65 Eq(std::pair{"id0"s, "id0"s}));
66 EXPECT_THAT(this->generateId("/id1", defaultName),
67 Eq(std::pair{"id1"s, "id1"s}));
68 EXPECT_THAT(this->generateId("prefix/id2", defaultName),
69 Eq(std::pair{"prefix/id2"s, "id2"s}));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010070}
71
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010072template <class Scenario>
73class TestGenerateId : public Scenario
74{};
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010075
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010076using TestScenarios =
77 ::testing::Types<ScenarioNameProvided, ScenarioNameNotProvided>;
78TYPED_TEST_SUITE(TestGenerateId, TestScenarios);
79
80TEST_F(TestGenerateIdNameNotProvided, leadingSlashDoesntCountToLengthLimit)
81{
82 this->maxSize = 2;
83
84 EXPECT_THAT(this->generateId("12", "default").first, Eq("12"));
85 EXPECT_THAT(this->generateId("/23", "default").first, Eq("23"));
86 EXPECT_THROW(this->generateId("123", "trigger"),
87 sdbusplus::exception::SdBusError);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010088}
89
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010090TYPED_TEST(TestGenerateId, throwsWhenProvidedIdIsTooLong)
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010091{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010092 this->maxSize = 4;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010093
Krzysztof Grobelnya950e422021-12-31 13:49:00 +010094 EXPECT_THROW(this->generateId("12345", "name"),
95 sdbusplus::exception::SdBusError);
96 EXPECT_THROW(this->generateId("12/45", "name"),
97 sdbusplus::exception::SdBusError);
98 EXPECT_THROW(this->generateId("123/", "trigger"),
99 sdbusplus::exception::SdBusError);
100}
101
102TYPED_TEST(TestGenerateId, throwsWhenThereAreNoFreeIds)
103{
104 this->maxSize = 1;
105 this->conflicts = {"n", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
106
107 EXPECT_THROW(this->generateId("", "n"), sdbusplus::exception::SdBusError);
108}
109
110TYPED_TEST(TestGenerateId, throwsWhenIdContainsMoreThanOneSlash)
111{
112 EXPECT_THROW(this->generateId("/12/", "name"),
113 sdbusplus::exception::SdBusError);
114 EXPECT_THROW(this->generateId("12//", "name"),
115 sdbusplus::exception::SdBusError);
116 EXPECT_THROW(this->generateId("12//123", "name"),
117 sdbusplus::exception::SdBusError);
118 EXPECT_THROW(this->generateId("12/12/123", "name"),
119 sdbusplus::exception::SdBusError);
120}
121
122TYPED_TEST(TestGenerateId, usesNamePartOfNameWhenIdNotProvidedAndNameIsTooLong)
123{
124 this->maxSize = 4;
125
126 this->conflicts = {};
127 EXPECT_THAT(this->generateId("", "trigger"),
128 Eq(std::pair{"trig"s, "trigger"s}));
129 EXPECT_THAT(this->generateId("a/", "trigger"),
130 Eq(std::pair{"a/tr"s, "trigger"s}));
131
132 this->conflicts = {"trig"};
133 EXPECT_THAT(this->generateId("", "trigger"),
134 Eq(std::pair{"tri0"s, "trigger"s}));
135
136 this->conflicts = {"trig", "tri0"};
137 EXPECT_THAT(this->generateId("", "trigger"),
138 Eq(std::pair{"tri1"s, "trigger"s}));
139}
140
141TYPED_TEST(TestGenerateId, throwsWhenProvidedIdIsTaken)
142{
143 this->conflicts = {"id", "prefix/id"};
144
145 EXPECT_THROW(this->generateId("id", "name"),
146 sdbusplus::exception::SdBusError);
147 EXPECT_THROW(this->generateId("/id", "name"),
148 sdbusplus::exception::SdBusError);
149 EXPECT_THROW(this->generateId("prefix/id", "name"),
150 sdbusplus::exception::SdBusError);
151}
152
153TYPED_TEST(TestGenerateId, usesNameWhenIdNotProvided)
154{
155 EXPECT_THAT(this->generateId("", "name"), Eq(std::pair{"name"s, "name"s}));
156 EXPECT_THAT(this->generateId("/", "name"), Eq(std::pair{"name"s, "name"s}));
157 EXPECT_THAT(this->generateId("abc/", "name"),
158 Eq(std::pair{"abc/name"s, "name"s}));
159 EXPECT_THAT(this->generateId("123/", "name"),
160 Eq(std::pair{"123/name"s, "name"s}));
161}
162
163TYPED_TEST(TestGenerateId, usesNameWithoutInvalidCharactersWhenIdNotProvided)
164{
165 EXPECT_THAT(this->generateId("", "n#a$/m@e"),
166 Eq(std::pair{"name"s, "n#a$/m@e"s}));
167 EXPECT_THAT(this->generateId("", "n!^aŹ/me"),
168 Eq(std::pair{"name"s, "n!^aŹ/me"s}));
169 EXPECT_THAT(this->generateId("p/", "n!^aŹ/m*(e"),
170 Eq(std::pair{"p/name"s, "n!^aŹ/m*(e"s}));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100171}