Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 1 | #include "lpcsnoop/snoop.hpp" |
| 2 | |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 5 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 6 | #include <gmock/gmock.h> |
| 7 | #include <gtest/gtest.h> |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 8 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 9 | using ::testing::_; |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 10 | using ::testing::IsNull; |
| 11 | using ::testing::NiceMock; |
| 12 | using ::testing::Return; |
| 13 | using ::testing::StrEq; |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | // Fixture for testing class PostReporter |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 19 | class PostReporterTest : public ::testing::Test |
| 20 | { |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 21 | protected: |
| 22 | PostReporterTest() : bus_mock(), bus(sdbusplus::get_mocked_new(&bus_mock)) |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 23 | {} |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 24 | |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 25 | ~PostReporterTest() {} |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 26 | |
| 27 | NiceMock<sdbusplus::SdBusMock> bus_mock; |
Patrick Williams | aebf87c | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 28 | sdbusplus::bus_t bus; |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 31 | TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath) |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 32 | { |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 33 | EXPECT_CALL(bus_mock, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 34 | sd_bus_emit_object_added(IsNull(), StrEq(snoopObject))) |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 35 | .WillOnce(Return(0)); |
| 36 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 37 | PostReporter testReporter(bus, snoopObject, true); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 38 | testReporter.emit_object_added(); |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 39 | } |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 40 | |
| 41 | TEST_F(PostReporterTest, AddsObjectWithExpectedName) |
| 42 | { |
| 43 | EXPECT_CALL(bus_mock, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 44 | sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject), |
| 45 | StrEq(snoopDbus), _, _)) |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 46 | .WillOnce(Return(0)); |
| 47 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 48 | PostReporter testReporter(bus, snoopObject, true); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST_F(PostReporterTest, ValueReadsDefaultToZero) |
| 52 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 53 | PostReporter testReporter(bus, snoopObject, true); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 54 | EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value())); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST_F(PostReporterTest, SetValueToPositiveValueWorks) |
| 58 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 59 | PostReporter testReporter(bus, snoopObject, true); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 60 | secondary_post_code_t secondaryCode = {123, 124, 125}; |
| 61 | testReporter.value(std::make_tuple(65537, secondaryCode)); |
| 62 | EXPECT_EQ(65537, std::get<primary_post_code_t>(testReporter.value())); |
| 63 | EXPECT_EQ(secondaryCode, |
| 64 | std::get<secondary_post_code_t>(testReporter.value())); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST_F(PostReporterTest, SetValueMultipleTimesWorks) |
| 68 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 69 | PostReporter testReporter(bus, snoopObject, true); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 70 | secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56}; |
| 71 | testReporter.value(std::make_tuple(123, secondaryCode)); |
| 72 | EXPECT_EQ(123, std::get<primary_post_code_t>(testReporter.value())); |
| 73 | EXPECT_EQ(secondaryCode, |
| 74 | std::get<secondary_post_code_t>(testReporter.value())); |
| 75 | |
| 76 | secondaryCode = {0, 0, 0, 0, 0}; |
| 77 | testReporter.value(std::make_tuple(45, secondaryCode)); |
| 78 | EXPECT_EQ(45, std::get<primary_post_code_t>(testReporter.value())); |
| 79 | EXPECT_EQ(secondaryCode, |
| 80 | std::get<secondary_post_code_t>(testReporter.value())); |
| 81 | |
| 82 | secondaryCode = {23, 200, 0, 45, 2}; |
| 83 | testReporter.value(std::make_tuple(0, secondaryCode)); |
| 84 | EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value())); |
| 85 | EXPECT_EQ(secondaryCode, |
| 86 | std::get<secondary_post_code_t>(testReporter.value())); |
| 87 | |
| 88 | secondaryCode = {10, 40, 0, 35, 78}; |
| 89 | testReporter.value(std::make_tuple(46, secondaryCode)); |
| 90 | EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value())); |
| 91 | EXPECT_EQ(secondaryCode, |
| 92 | std::get<secondary_post_code_t>(testReporter.value())); |
| 93 | |
| 94 | secondaryCode = {10, 40, 0, 35, 78}; |
| 95 | testReporter.value(std::make_tuple(46, secondaryCode)); |
| 96 | EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value())); |
| 97 | EXPECT_EQ(secondaryCode, |
| 98 | std::get<secondary_post_code_t>(testReporter.value())); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } // namespace |