blob: 0a13769420d6876b7095e17bb85d0ad799406152 [file] [log] [blame]
Patrick Ventureb5754fd2018-09-10 13:13:58 -07001#include "lpcsnoop/snoop.hpp"
2
Kun Yi35b6c672018-06-11 20:44:43 -07003#include <sdbusplus/bus.hpp>
4#include <sdbusplus/test/sdbus_mock.hpp>
Kun Yi2ae207c2018-06-11 16:09:46 -07005
Patrick Ventureb5754fd2018-09-10 13:13:58 -07006#include <gmock/gmock.h>
7#include <gtest/gtest.h>
Kun Yi35b6c672018-06-11 20:44:43 -07008
Patrick Ventureb5754fd2018-09-10 13:13:58 -07009using ::testing::_;
Kun Yi35b6c672018-06-11 20:44:43 -070010using ::testing::IsNull;
11using ::testing::NiceMock;
12using ::testing::Return;
13using ::testing::StrEq;
Kun Yi35b6c672018-06-11 20:44:43 -070014
15namespace
16{
17
18// Fixture for testing class PostReporter
Kun Yi2ae207c2018-06-11 16:09:46 -070019class PostReporterTest : public ::testing::Test
20{
Kun Yi35b6c672018-06-11 20:44:43 -070021 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::bus bus;
Kun Yi2ae207c2018-06-11 16:09:46 -070032};
33
Kun Yi35b6c672018-06-11 20:44:43 -070034TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
Kun Yi2ae207c2018-06-11 16:09:46 -070035{
Kun Yi35b6c672018-06-11 20:44:43 -070036
37 EXPECT_CALL(bus_mock,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053038 sd_bus_emit_object_added(IsNull(), StrEq(snoopObject)))
Kun Yi35b6c672018-06-11 20:44:43 -070039 .WillOnce(Return(0));
40
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053041 PostReporter testReporter(bus, snoopObject, true);
Kun Yi35b6c672018-06-11 20:44:43 -070042 testReporter.emit_object_added();
Kun Yi2ae207c2018-06-11 16:09:46 -070043}
Kun Yi35b6c672018-06-11 20:44:43 -070044
45TEST_F(PostReporterTest, AddsObjectWithExpectedName)
46{
47 EXPECT_CALL(bus_mock,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053048 sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject),
49 StrEq(snoopDbus), _, _))
Kun Yi35b6c672018-06-11 20:44:43 -070050 .WillOnce(Return(0));
51
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053052 PostReporter testReporter(bus, snoopObject, true);
Kun Yi35b6c672018-06-11 20:44:43 -070053}
54
55TEST_F(PostReporterTest, ValueReadsDefaultToZero)
56{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053057 PostReporter testReporter(bus, snoopObject, true);
Manojkiran Edaba5258f2021-02-25 13:23:33 +053058 EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
Kun Yi35b6c672018-06-11 20:44:43 -070059}
60
61TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
62{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053063 PostReporter testReporter(bus, snoopObject, true);
Manojkiran Edaba5258f2021-02-25 13:23:33 +053064 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()));
Kun Yi35b6c672018-06-11 20:44:43 -070069}
70
71TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
72{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053073 PostReporter testReporter(bus, snoopObject, true);
Manojkiran Edaba5258f2021-02-25 13:23:33 +053074 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()));
Kun Yi35b6c672018-06-11 20:44:43 -0700103}
104
105} // namespace