Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 1 | #include "utils/generate_id.hpp" |
| 2 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 3 | #include <sdbusplus/exception.hpp> |
| 4 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 5 | #include <gmock/gmock.h> |
| 6 | |
| 7 | using namespace testing; |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 8 | using namespace std::literals::string_literals; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 9 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 10 | class ScenarioBase : public Test |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 11 | { |
| 12 | public: |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 13 | std::string_view defaultName = "defName"; |
| 14 | size_t maxSize = 32; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 15 | std::vector<std::string> conflicts; |
| 16 | }; |
| 17 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 18 | class ScenarioNameProvided : public ScenarioBase |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 19 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 20 | 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 | |
| 27 | class TestGenerateIdNameProvided : public ScenarioNameProvided |
| 28 | {}; |
| 29 | |
| 30 | TEST_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 Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 40 | TEST_F(TestGenerateIdNameProvided, usedDefaultWhenNothingProvided) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 41 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 42 | 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 Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 50 | class ScenarioNameNotProvided : public ScenarioBase |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 51 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 52 | 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 | |
| 59 | class TestGenerateIdNameNotProvided : public ScenarioNameNotProvided |
| 60 | {}; |
| 61 | |
| 62 | TEST_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 Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 72 | template <class Scenario> |
| 73 | class TestGenerateId : public Scenario |
| 74 | {}; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 75 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 76 | using TestScenarios = |
| 77 | ::testing::Types<ScenarioNameProvided, ScenarioNameNotProvided>; |
| 78 | TYPED_TEST_SUITE(TestGenerateId, TestScenarios); |
| 79 | |
| 80 | TEST_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 Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 90 | TYPED_TEST(TestGenerateId, throwsWhenProvidedIdIsTooLong) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 91 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 92 | this->maxSize = 4; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 93 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame^] | 94 | 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 | |
| 102 | TYPED_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 | |
| 110 | TYPED_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 | |
| 122 | TYPED_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 | |
| 141 | TYPED_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 | |
| 153 | TYPED_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 | |
| 163 | TYPED_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 Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 171 | } |