blob: ef0d0d569e08cd6a3533662bbf3e81be928c1098 [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))
Patrick Williams0ea73572023-05-10 07:50:44 -050023 {}
Kun Yi35b6c672018-06-11 20:44:43 -070024
Patrick Williams0ea73572023-05-10 07:50:44 -050025 ~PostReporterTest() {}
Kun Yi35b6c672018-06-11 20:44:43 -070026
27 NiceMock<sdbusplus::SdBusMock> bus_mock;
Patrick Williamsaebf87c2022-07-22 19:26:54 -050028 sdbusplus::bus_t bus;
Kun Yi2ae207c2018-06-11 16:09:46 -070029};
30
Kun Yi35b6c672018-06-11 20:44:43 -070031TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
Kun Yi2ae207c2018-06-11 16:09:46 -070032{
Kun Yi35b6c672018-06-11 20:44:43 -070033 EXPECT_CALL(bus_mock,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053034 sd_bus_emit_object_added(IsNull(), StrEq(snoopObject)))
Kun Yi35b6c672018-06-11 20:44:43 -070035 .WillOnce(Return(0));
36
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053037 PostReporter testReporter(bus, snoopObject, true);
Kun Yi35b6c672018-06-11 20:44:43 -070038 testReporter.emit_object_added();
Kun Yi2ae207c2018-06-11 16:09:46 -070039}
Kun Yi35b6c672018-06-11 20:44:43 -070040
41TEST_F(PostReporterTest, AddsObjectWithExpectedName)
42{
43 EXPECT_CALL(bus_mock,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053044 sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject),
45 StrEq(snoopDbus), _, _))
Kun Yi35b6c672018-06-11 20:44:43 -070046 .WillOnce(Return(0));
47
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053048 PostReporter testReporter(bus, snoopObject, true);
Kun Yi35b6c672018-06-11 20:44:43 -070049}
50
51TEST_F(PostReporterTest, ValueReadsDefaultToZero)
52{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053053 PostReporter testReporter(bus, snoopObject, true);
Manojkiran Edaba5258f2021-02-25 13:23:33 +053054 EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
Kun Yi35b6c672018-06-11 20:44:43 -070055}
56
57TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
58{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053059 PostReporter testReporter(bus, snoopObject, true);
Manojkiran Edaba5258f2021-02-25 13:23:33 +053060 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()));
Kun Yi35b6c672018-06-11 20:44:43 -070065}
66
67TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
68{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053069 PostReporter testReporter(bus, snoopObject, true);
Manojkiran Edaba5258f2021-02-25 13:23:33 +053070 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()));
Kun Yi35b6c672018-06-11 20:44:43 -070099}
100
101} // namespace