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 
26     ~PostReporterTest()
27     {
28     }
29 
30     NiceMock<sdbusplus::SdBusMock> bus_mock;
31     sdbusplus::bus_t bus;
32 };
33 
34 TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
35 {
36 
37     EXPECT_CALL(bus_mock,
38                 sd_bus_emit_object_added(IsNull(), StrEq(snoopObject)))
39         .WillOnce(Return(0));
40 
41     PostReporter testReporter(bus, snoopObject, true);
42     testReporter.emit_object_added();
43 }
44 
45 TEST_F(PostReporterTest, AddsObjectWithExpectedName)
46 {
47     EXPECT_CALL(bus_mock,
48                 sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject),
49                                          StrEq(snoopDbus), _, _))
50         .WillOnce(Return(0));
51 
52     PostReporter testReporter(bus, snoopObject, true);
53 }
54 
55 TEST_F(PostReporterTest, ValueReadsDefaultToZero)
56 {
57     PostReporter testReporter(bus, snoopObject, true);
58     EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
59 }
60 
61 TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
62 {
63     PostReporter testReporter(bus, snoopObject, true);
64     secondary_post_code_t secondaryCode = {123, 124, 125};
65     testReporter.value(std::make_tuple(65537, secondaryCode));
66     EXPECT_EQ(65537, std::get<primary_post_code_t>(testReporter.value()));
67     EXPECT_EQ(secondaryCode,
68               std::get<secondary_post_code_t>(testReporter.value()));
69 }
70 
71 TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
72 {
73     PostReporter testReporter(bus, snoopObject, true);
74     secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56};
75     testReporter.value(std::make_tuple(123, secondaryCode));
76     EXPECT_EQ(123, std::get<primary_post_code_t>(testReporter.value()));
77     EXPECT_EQ(secondaryCode,
78               std::get<secondary_post_code_t>(testReporter.value()));
79 
80     secondaryCode = {0, 0, 0, 0, 0};
81     testReporter.value(std::make_tuple(45, secondaryCode));
82     EXPECT_EQ(45, std::get<primary_post_code_t>(testReporter.value()));
83     EXPECT_EQ(secondaryCode,
84               std::get<secondary_post_code_t>(testReporter.value()));
85 
86     secondaryCode = {23, 200, 0, 45, 2};
87     testReporter.value(std::make_tuple(0, secondaryCode));
88     EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
89     EXPECT_EQ(secondaryCode,
90               std::get<secondary_post_code_t>(testReporter.value()));
91 
92     secondaryCode = {10, 40, 0, 35, 78};
93     testReporter.value(std::make_tuple(46, secondaryCode));
94     EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value()));
95     EXPECT_EQ(secondaryCode,
96               std::get<secondary_post_code_t>(testReporter.value()));
97 
98     secondaryCode = {10, 40, 0, 35, 78};
99     testReporter.value(std::make_tuple(46, secondaryCode));
100     EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value()));
101     EXPECT_EQ(secondaryCode,
102               std::get<secondary_post_code_t>(testReporter.value()));
103 }
104 
105 } // namespace
106