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