Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 1 | #include <boost/container/flat_map.hpp> |
| 2 | #include <sdbusplus/unpack_properties.hpp> |
| 3 | |
| 4 | #include <gmock/gmock.h> |
| 5 | |
| 6 | namespace sdbusplus |
| 7 | { |
| 8 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 9 | struct ThrowingUnpack |
| 10 | { |
| 11 | template <typename... Args> |
| 12 | bool operator()(Args&&... args) const |
| 13 | { |
| 14 | unpackProperties(std::forward<Args>(args)...); |
| 15 | return false; |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | struct NonThrowingUnpack |
| 20 | { |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 21 | struct UnpackError |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 22 | { |
Patrick Williams | a970d83 | 2022-09-08 07:53:40 -0500 | [diff] [blame] | 23 | UnpackError(sdbusplus::UnpackErrorReason r, const std::string& p) : |
| 24 | reason(r), property(p){}; |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 25 | sdbusplus::UnpackErrorReason reason; |
| 26 | std::string property; |
| 27 | }; |
| 28 | |
| 29 | template <typename... Args> |
| 30 | std::optional<UnpackError> operator()(Args&&... args) const |
| 31 | { |
| 32 | std::optional<UnpackError> error; |
| 33 | unpackPropertiesNoThrow( |
Patrick Williams | a970d83 | 2022-09-08 07:53:40 -0500 | [diff] [blame] | 34 | [&error](sdbusplus::UnpackErrorReason reason, |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 35 | const std::string& property) { |
| 36 | error.emplace(reason, property); |
| 37 | }, |
| 38 | std::forward<Args>(args)...); |
| 39 | return error; |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 40 | } |
| 41 | }; |
| 42 | |
| 43 | template <typename A, typename B> |
| 44 | struct TestingTypes |
| 45 | { |
| 46 | using SystemUnderTest = A; |
| 47 | using Container = B; |
| 48 | }; |
| 49 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 50 | using VariantType = std::variant<std::string, uint32_t, float, double>; |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 51 | using ContainerTypes = testing::Types< |
| 52 | TestingTypes<NonThrowingUnpack, |
| 53 | std::vector<std::pair<std::string, VariantType>>>, |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 54 | TestingTypes<ThrowingUnpack, |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 55 | std::vector<std::pair<std::string, VariantType>>>>; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 56 | |
| 57 | template <typename Exception, typename F> |
| 58 | std::optional<Exception> captureException(F&& code) |
| 59 | { |
| 60 | try |
| 61 | { |
| 62 | code(); |
| 63 | } |
| 64 | catch (const Exception& e) |
| 65 | { |
| 66 | return e; |
| 67 | } |
| 68 | |
| 69 | return std::nullopt; |
| 70 | } |
| 71 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 72 | template <typename Params> |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 73 | struct UnpackPropertiesTest : public testing::Test |
| 74 | { |
| 75 | void SetUp() override |
| 76 | { |
| 77 | using namespace std::string_literals; |
| 78 | |
| 79 | data.insert(data.end(), |
| 80 | std::make_pair("Key-1"s, VariantType("string"s))); |
| 81 | data.insert(data.end(), std::make_pair("Key-2"s, VariantType(42.f))); |
| 82 | data.insert(data.end(), std::make_pair("Key-3"s, VariantType(15.))); |
| 83 | } |
| 84 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 85 | typename Params::Container data; |
| 86 | typename Params::SystemUnderTest unpackPropertiesCall; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | TYPED_TEST_SUITE(UnpackPropertiesTest, ContainerTypes); |
| 90 | |
| 91 | TYPED_TEST(UnpackPropertiesTest, returnsValueWhenKeyIsPresentAndTypeMatches) |
| 92 | { |
| 93 | using namespace testing; |
| 94 | |
| 95 | std::string val1; |
| 96 | float val2 = 0.f; |
| 97 | double val3 = 0.; |
| 98 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 99 | EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", |
| 100 | val2, "Key-3", val3)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 101 | |
| 102 | ASSERT_THAT(val1, Eq("string")); |
| 103 | ASSERT_THAT(val2, FloatEq(42.f)); |
| 104 | ASSERT_THAT(val3, DoubleEq(15.)); |
| 105 | } |
| 106 | |
| 107 | TYPED_TEST(UnpackPropertiesTest, |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 108 | unpackDoesntChangeOriginalDataWhenPassedAsNonConstReference) |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 109 | { |
| 110 | using namespace testing; |
| 111 | |
| 112 | std::string val1, val2; |
| 113 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 114 | EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1)); |
| 115 | EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val2)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 116 | |
| 117 | ASSERT_THAT(val1, Eq("string")); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 118 | ASSERT_THAT(val2, Eq("string")); |
| 119 | } |
| 120 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 121 | TYPED_TEST(UnpackPropertiesTest, doesntReportMissingPropertyForOptional) |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 122 | { |
| 123 | using namespace testing; |
| 124 | using namespace std::string_literals; |
| 125 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 126 | std::optional<std::string> val1; |
| 127 | std::optional<std::string> val4; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 128 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 129 | EXPECT_FALSE( |
| 130 | this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-4", val4)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 131 | |
| 132 | ASSERT_THAT(val1, Eq("string")); |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 133 | ASSERT_THAT(val4, Eq(std::nullopt)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 136 | TYPED_TEST(UnpackPropertiesTest, setPresentPointersOnSuccess) |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 137 | { |
| 138 | using namespace testing; |
| 139 | using namespace std::string_literals; |
| 140 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 141 | const std::string* val1 = nullptr; |
| 142 | const float* val2 = nullptr; |
| 143 | const double* val3 = nullptr; |
| 144 | const std::string* val4 = nullptr; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 145 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 146 | EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", |
| 147 | val2, "Key-3", val3, "Key-4", |
| 148 | val4)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 149 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 150 | ASSERT_TRUE(val1 && val2 && val3); |
| 151 | ASSERT_TRUE(!val4); |
| 152 | |
| 153 | ASSERT_THAT(*val1, Eq("string")); |
| 154 | ASSERT_THAT(*val2, FloatEq(42.f)); |
| 155 | ASSERT_THAT(*val3, DoubleEq(15.)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 158 | template <typename Params> |
| 159 | struct UnpackPropertiesThrowingTest : public UnpackPropertiesTest<Params> |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 160 | {}; |
| 161 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 162 | using ContainerTypesThrowing = testing::Types<TestingTypes< |
| 163 | ThrowingUnpack, std::vector<std::pair<std::string, VariantType>>>>; |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 164 | |
| 165 | TYPED_TEST_SUITE(UnpackPropertiesThrowingTest, ContainerTypesThrowing); |
| 166 | |
| 167 | TYPED_TEST(UnpackPropertiesThrowingTest, throwsErrorWhenKeyIsMissing) |
| 168 | { |
| 169 | using namespace testing; |
| 170 | |
| 171 | std::string val1; |
| 172 | float val2 = 0.f; |
| 173 | double val3 = 0.; |
| 174 | |
| 175 | auto error = captureException<exception::UnpackPropertyError>([&] { |
| 176 | this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-4", val2, |
| 177 | "Key-3", val3); |
| 178 | }); |
| 179 | |
| 180 | ASSERT_TRUE(error); |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 181 | ASSERT_THAT(error->reason, Eq(UnpackErrorReason::missingProperty)); |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 182 | ASSERT_THAT(error->propertyName, Eq("Key-4")); |
| 183 | } |
| 184 | |
| 185 | TYPED_TEST(UnpackPropertiesThrowingTest, throwsErrorWhenTypeDoesntMatch) |
| 186 | { |
| 187 | using namespace testing; |
| 188 | |
| 189 | std::string val1; |
| 190 | std::string val2; |
| 191 | double val3 = 0.; |
| 192 | |
| 193 | auto error = captureException<exception::UnpackPropertyError>([&] { |
| 194 | this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", val2, |
| 195 | "Key-3", val3); |
| 196 | }); |
| 197 | |
| 198 | ASSERT_TRUE(error); |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 199 | ASSERT_THAT(error->reason, Eq(UnpackErrorReason::wrongType)); |
| 200 | ASSERT_THAT(error->propertyName, Eq("Key-2")); |
| 201 | } |
| 202 | |
| 203 | TYPED_TEST(UnpackPropertiesThrowingTest, throwsErrorWhenOptionalTypeDoesntMatch) |
| 204 | { |
| 205 | using namespace testing; |
| 206 | |
| 207 | std::optional<std::string> val1; |
| 208 | std::optional<std::string> val2; |
| 209 | |
| 210 | auto error = captureException<exception::UnpackPropertyError>([&] { |
| 211 | this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", val2); |
| 212 | }); |
| 213 | |
| 214 | ASSERT_TRUE(error); |
| 215 | ASSERT_THAT(error->reason, Eq(UnpackErrorReason::wrongType)); |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 216 | ASSERT_THAT(error->propertyName, Eq("Key-2")); |
| 217 | } |
| 218 | |
| 219 | template <typename Params> |
| 220 | struct UnpackPropertiesNonThrowingTest : public UnpackPropertiesTest<Params> |
| 221 | {}; |
| 222 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 223 | using ContainerTypesNonThrowing = testing::Types<TestingTypes< |
| 224 | NonThrowingUnpack, std::vector<std::pair<std::string, VariantType>>>>; |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 225 | |
| 226 | TYPED_TEST_SUITE(UnpackPropertiesNonThrowingTest, ContainerTypesNonThrowing); |
| 227 | |
| 228 | TYPED_TEST(UnpackPropertiesNonThrowingTest, ErrorWhenKeyIsMissing) |
| 229 | { |
| 230 | using namespace testing; |
| 231 | |
| 232 | std::string val1; |
| 233 | float val2 = 0.f; |
| 234 | double val3 = 0.; |
| 235 | |
| 236 | auto badProperty = this->unpackPropertiesCall(this->data, "Key-1", val1, |
| 237 | "Key-4", val2, "Key-3", val3); |
| 238 | |
| 239 | ASSERT_TRUE(badProperty); |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 240 | EXPECT_THAT(badProperty->reason, Eq(UnpackErrorReason::missingProperty)); |
| 241 | EXPECT_THAT(badProperty->property, Eq("Key-4")); |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TYPED_TEST(UnpackPropertiesNonThrowingTest, ErrorWhenTypeDoesntMatch) |
| 245 | { |
| 246 | using namespace testing; |
| 247 | |
| 248 | std::string val1; |
| 249 | std::string val2; |
| 250 | double val3 = 0.; |
| 251 | |
| 252 | auto badProperty = this->unpackPropertiesCall(this->data, "Key-1", val1, |
| 253 | "Key-2", val2, "Key-3", val3); |
| 254 | |
| 255 | ASSERT_TRUE(badProperty); |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 256 | EXPECT_THAT(badProperty->reason, Eq(UnpackErrorReason::wrongType)); |
| 257 | EXPECT_THAT(badProperty->property, Eq("Key-2")); |
| 258 | } |
| 259 | |
| 260 | TYPED_TEST(UnpackPropertiesNonThrowingTest, ErrorWhenOptionalTypeDoesntMatch) |
| 261 | { |
| 262 | using namespace testing; |
| 263 | |
| 264 | std::optional<std::string> val1; |
| 265 | std::optional<std::string> val2; |
| 266 | |
Patrick Williams | 1a25a10 | 2022-09-29 17:18:26 -0500 | [diff] [blame^] | 267 | auto badProperty = this->unpackPropertiesCall(this->data, "Key-1", val1, |
| 268 | "Key-2", val2); |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 269 | |
| 270 | ASSERT_TRUE(badProperty); |
| 271 | EXPECT_THAT(badProperty->reason, Eq(UnpackErrorReason::wrongType)); |
| 272 | EXPECT_THAT(badProperty->property, Eq("Key-2")); |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | template <typename Params> |
| 276 | struct UnpackPropertiesTest_ForVector : public UnpackPropertiesTest<Params> |
| 277 | {}; |
| 278 | |
| 279 | using ContainerTypesVector = testing::Types< |
| 280 | TestingTypes<NonThrowingUnpack, |
| 281 | std::vector<std::pair<std::string, VariantType>>>, |
| 282 | TestingTypes<ThrowingUnpack, |
| 283 | std::vector<std::pair<std::string, VariantType>>>>; |
| 284 | |
| 285 | TYPED_TEST_SUITE(UnpackPropertiesTest_ForVector, ContainerTypesVector); |
| 286 | |
| 287 | TYPED_TEST(UnpackPropertiesTest_ForVector, silentlyDiscardsDuplicatedKeyInData) |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 288 | { |
| 289 | using namespace testing; |
| 290 | using namespace std::string_literals; |
| 291 | |
| 292 | std::string val1; |
| 293 | float val2 = 0.f; |
| 294 | double val3 = 0.; |
| 295 | |
| 296 | this->data.insert(this->data.end(), |
| 297 | std::make_pair("Key-1"s, VariantType("string2"s))); |
| 298 | |
Szymon Dompke | 6d83cf5 | 2021-10-19 16:31:29 +0200 | [diff] [blame] | 299 | EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", |
| 300 | val2, "Key-3", val3)); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 301 | |
| 302 | ASSERT_THAT(val1, Eq("string")); |
| 303 | ASSERT_THAT(val2, FloatEq(42.f)); |
| 304 | ASSERT_THAT(val3, DoubleEq(15.)); |
| 305 | } |
| 306 | |
| 307 | } // namespace sdbusplus |