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:
22     PostReporterTest() : bus_mock(), bus(sdbusplus::get_mocked_new(&bus_mock))
23     {}
24 
25     ~PostReporterTest() {}
26 
27     NiceMock<sdbusplus::SdBusMock> bus_mock;
28     sdbusplus::bus_t bus;
29 };
30 
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 
41 TEST_F(PostReporterTest, AddsObjectWithExpectedName)
42 {
43     EXPECT_CALL(bus_mock,
44                 sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject),
45                                          StrEq(snoopDbus), _, _))
46         .WillOnce(Return(0));
47 
48     PostReporter testReporter(bus, snoopObject, true);
49 }
50 
51 TEST_F(PostReporterTest, ValueReadsDefaultToZero)
52 {
53     PostReporter testReporter(bus, snoopObject, true);
54     EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
55 }
56 
57 TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
58 {
59     PostReporter testReporter(bus, snoopObject, true);
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()));
65 }
66 
67 TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
68 {
69     PostReporter testReporter(bus, snoopObject, true);
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()));
99 }
100 
101 } // namespace
102