1 #include "lpcsnoop/snoop.hpp"
2
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/test/sdbus_mock.hpp>
5
6 #include <gmock/gmock.h>
7 #include <gtest/gtest.h>
8
9 using ::testing::_;
10 using ::testing::IsNull;
11 using ::testing::NiceMock;
12 using ::testing::Return;
13 using ::testing::StrEq;
14
15 namespace
16 {
17
18 // Fixture for testing class PostReporter
19 class PostReporterTest : public ::testing::Test
20 {
21 protected:
PostReporterTest()22 PostReporterTest() : bus_mock(), bus(sdbusplus::get_mocked_new(&bus_mock))
23 {}
24
~PostReporterTest()25 ~PostReporterTest() {}
26
27 NiceMock<sdbusplus::SdBusMock> bus_mock;
28 sdbusplus::bus_t bus;
29 };
30
TEST_F(PostReporterTest,EmitsObjectsOnExpectedDbusPath)31 TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
32 {
33 EXPECT_CALL(bus_mock,
34 sd_bus_emit_object_added(IsNull(), StrEq(snoopObject)))
35 .WillOnce(Return(0));
36
37 PostReporter testReporter(bus, snoopObject, true);
38 testReporter.emit_object_added();
39 }
40
TEST_F(PostReporterTest,AddsObjectWithExpectedName)41 TEST_F(PostReporterTest, AddsObjectWithExpectedName)
42 {
43 auto slotcb = [](sd_bus*, sd_bus_slot** slot, auto&&...) {
44 *slot = reinterpret_cast<sd_bus_slot*>(0xdefa);
45 return 0;
46 };
47
48 EXPECT_CALL(bus_mock,
49 sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject),
50 StrEq(snoopDbus), _, _))
51 .WillOnce(slotcb);
52
53 PostReporter testReporter(bus, snoopObject, true);
54 }
55
TEST_F(PostReporterTest,ValueReadsDefaultToEmpty)56 TEST_F(PostReporterTest, ValueReadsDefaultToEmpty)
57 {
58 PostReporter testReporter(bus, snoopObject, true);
59 EXPECT_TRUE(std::get<0>(testReporter.value()).empty());
60 }
61
TEST_F(PostReporterTest,SetValueToPositiveValueWorks)62 TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
63 {
64 PostReporter testReporter(bus, snoopObject, true);
65 primary_post_code_t primaryCode = {122, 126, 127};
66 secondary_post_code_t secondaryCode = {123, 124, 125};
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()));
70 }
71
TEST_F(PostReporterTest,SetValueMultipleTimesWorks)72 TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
73 {
74 PostReporter testReporter(bus, snoopObject, true);
75 primary_post_code_t primaryCode = {20, 21, 0, 123};
76 secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56};
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()));
80
81 primaryCode = {44, 45};
82 secondaryCode = {0, 0, 0, 0, 0};
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()));
86
87 primaryCode = {0};
88 secondaryCode = {23, 200, 0, 45, 2};
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()));
92
93 primaryCode = {46};
94 secondaryCode = {10, 40, 0, 35, 78};
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()));
98
99 primaryCode = {46};
100 secondaryCode = {10, 40, 0, 35, 78};
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()));
104 }
105
106 } // namespace
107