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 | { |
Willy Tu | fad2660 | 2024-06-24 19:24:17 +0000 | [diff] [blame] | 43 | auto slotcb = [](sd_bus*, sd_bus_slot** slot, auto&&...) { |
| 44 | *slot = reinterpret_cast<sd_bus_slot*>(0xdefa); |
| 45 | return 0; |
| 46 | }; |
| 47 | |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 48 | EXPECT_CALL(bus_mock, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 49 | sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject), |
| 50 | StrEq(snoopDbus), _, _)) |
Willy Tu | fad2660 | 2024-06-24 19:24:17 +0000 | [diff] [blame] | 51 | .WillOnce(slotcb); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 52 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 53 | PostReporter testReporter(bus, snoopObject, true); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 56 | TEST_F(PostReporterTest, ValueReadsDefaultToEmpty) |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 57 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 58 | PostReporter testReporter(bus, snoopObject, true); |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 59 | EXPECT_TRUE(std::get<0>(testReporter.value()).empty()); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST_F(PostReporterTest, SetValueToPositiveValueWorks) |
| 63 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 64 | PostReporter testReporter(bus, snoopObject, true); |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 65 | primary_post_code_t primaryCode = {122, 126, 127}; |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 66 | secondary_post_code_t secondaryCode = {123, 124, 125}; |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 67 | testReporter.value(std::make_tuple(primaryCode, secondaryCode)); |
| 68 | EXPECT_EQ(primaryCode, std::get<0>(testReporter.value())); |
| 69 | EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value())); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(PostReporterTest, SetValueMultipleTimesWorks) |
| 73 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 74 | PostReporter testReporter(bus, snoopObject, true); |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 75 | primary_post_code_t primaryCode = {20, 21, 0, 123}; |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 76 | secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56}; |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 77 | testReporter.value(std::make_tuple(primaryCode, secondaryCode)); |
| 78 | EXPECT_EQ(primaryCode, std::get<0>(testReporter.value())); |
| 79 | EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value())); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 80 | |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 81 | primaryCode = {44, 45}; |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 82 | secondaryCode = {0, 0, 0, 0, 0}; |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 83 | testReporter.value(std::make_tuple(primaryCode, secondaryCode)); |
| 84 | EXPECT_EQ(primaryCode, std::get<0>(testReporter.value())); |
| 85 | EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value())); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 86 | |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 87 | primaryCode = {0}; |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 88 | secondaryCode = {23, 200, 0, 45, 2}; |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 89 | testReporter.value(std::make_tuple(primaryCode, secondaryCode)); |
| 90 | EXPECT_EQ(primaryCode, std::get<0>(testReporter.value())); |
| 91 | EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value())); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 92 | |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 93 | primaryCode = {46}; |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 94 | secondaryCode = {10, 40, 0, 35, 78}; |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 95 | testReporter.value(std::make_tuple(primaryCode, secondaryCode)); |
| 96 | EXPECT_EQ(primaryCode, std::get<0>(testReporter.value())); |
| 97 | EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value())); |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 98 | |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 99 | primaryCode = {46}; |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 100 | secondaryCode = {10, 40, 0, 35, 78}; |
Cosmo Chou | fe51495 | 2024-09-05 01:08:58 +0800 | [diff] [blame^] | 101 | testReporter.value(std::make_tuple(primaryCode, secondaryCode)); |
| 102 | EXPECT_EQ(primaryCode, std::get<0>(testReporter.value())); |
| 103 | EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value())); |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace |