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,ValueReadsDefaultToZero)56 TEST_F(PostReporterTest, ValueReadsDefaultToZero)
57 {
58     PostReporter testReporter(bus, snoopObject, true);
59     EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
60 }
61 
TEST_F(PostReporterTest,SetValueToPositiveValueWorks)62 TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
63 {
64     PostReporter testReporter(bus, snoopObject, true);
65     secondary_post_code_t secondaryCode = {123, 124, 125};
66     testReporter.value(std::make_tuple(65537, secondaryCode));
67     EXPECT_EQ(65537, std::get<primary_post_code_t>(testReporter.value()));
68     EXPECT_EQ(secondaryCode,
69               std::get<secondary_post_code_t>(testReporter.value()));
70 }
71 
TEST_F(PostReporterTest,SetValueMultipleTimesWorks)72 TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
73 {
74     PostReporter testReporter(bus, snoopObject, true);
75     secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56};
76     testReporter.value(std::make_tuple(123, secondaryCode));
77     EXPECT_EQ(123, std::get<primary_post_code_t>(testReporter.value()));
78     EXPECT_EQ(secondaryCode,
79               std::get<secondary_post_code_t>(testReporter.value()));
80 
81     secondaryCode = {0, 0, 0, 0, 0};
82     testReporter.value(std::make_tuple(45, secondaryCode));
83     EXPECT_EQ(45, std::get<primary_post_code_t>(testReporter.value()));
84     EXPECT_EQ(secondaryCode,
85               std::get<secondary_post_code_t>(testReporter.value()));
86 
87     secondaryCode = {23, 200, 0, 45, 2};
88     testReporter.value(std::make_tuple(0, secondaryCode));
89     EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
90     EXPECT_EQ(secondaryCode,
91               std::get<secondary_post_code_t>(testReporter.value()));
92 
93     secondaryCode = {10, 40, 0, 35, 78};
94     testReporter.value(std::make_tuple(46, secondaryCode));
95     EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value()));
96     EXPECT_EQ(secondaryCode,
97               std::get<secondary_post_code_t>(testReporter.value()));
98 
99     secondaryCode = {10, 40, 0, 35, 78};
100     testReporter.value(std::make_tuple(46, secondaryCode));
101     EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value()));
102     EXPECT_EQ(secondaryCode,
103               std::get<secondary_post_code_t>(testReporter.value()));
104 }
105 
106 } // namespace
107