blob: 4a4f6954896acbba689e6c89d8e6dbd65c101849 [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{
Willy Tufad26602024-06-24 19:24:17 +000043 auto slotcb = [](sd_bus*, sd_bus_slot** slot, auto&&...) {
44 *slot = reinterpret_cast<sd_bus_slot*>(0xdefa);
45 return 0;
46 };
47
Kun Yi35b6c672018-06-11 20:44:43 -070048 EXPECT_CALL(bus_mock,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053049 sd_bus_add_object_vtable(IsNull(), _, StrEq(snoopObject),
50 StrEq(snoopDbus), _, _))
Willy Tufad26602024-06-24 19:24:17 +000051 .WillOnce(slotcb);
Kun Yi35b6c672018-06-11 20:44:43 -070052
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053053 PostReporter testReporter(bus, snoopObject, true);
Kun Yi35b6c672018-06-11 20:44:43 -070054}
55
Cosmo Choufe514952024-09-05 01:08:58 +080056TEST_F(PostReporterTest, ValueReadsDefaultToEmpty)
Kun Yi35b6c672018-06-11 20:44:43 -070057{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053058 PostReporter testReporter(bus, snoopObject, true);
Cosmo Choufe514952024-09-05 01:08:58 +080059 EXPECT_TRUE(std::get<0>(testReporter.value()).empty());
Kun Yi35b6c672018-06-11 20:44:43 -070060}
61
62TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
63{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053064 PostReporter testReporter(bus, snoopObject, true);
Cosmo Choufe514952024-09-05 01:08:58 +080065 primary_post_code_t primaryCode = {122, 126, 127};
Manojkiran Edaba5258f2021-02-25 13:23:33 +053066 secondary_post_code_t secondaryCode = {123, 124, 125};
Cosmo Choufe514952024-09-05 01:08:58 +080067 testReporter.value(std::make_tuple(primaryCode, secondaryCode));
68 EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
69 EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
Kun Yi35b6c672018-06-11 20:44:43 -070070}
71
72TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
73{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053074 PostReporter testReporter(bus, snoopObject, true);
Cosmo Choufe514952024-09-05 01:08:58 +080075 primary_post_code_t primaryCode = {20, 21, 0, 123};
Manojkiran Edaba5258f2021-02-25 13:23:33 +053076 secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56};
Cosmo Choufe514952024-09-05 01:08:58 +080077 testReporter.value(std::make_tuple(primaryCode, secondaryCode));
78 EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
79 EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
Manojkiran Edaba5258f2021-02-25 13:23:33 +053080
Cosmo Choufe514952024-09-05 01:08:58 +080081 primaryCode = {44, 45};
Manojkiran Edaba5258f2021-02-25 13:23:33 +053082 secondaryCode = {0, 0, 0, 0, 0};
Cosmo Choufe514952024-09-05 01:08:58 +080083 testReporter.value(std::make_tuple(primaryCode, secondaryCode));
84 EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
85 EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
Manojkiran Edaba5258f2021-02-25 13:23:33 +053086
Cosmo Choufe514952024-09-05 01:08:58 +080087 primaryCode = {0};
Manojkiran Edaba5258f2021-02-25 13:23:33 +053088 secondaryCode = {23, 200, 0, 45, 2};
Cosmo Choufe514952024-09-05 01:08:58 +080089 testReporter.value(std::make_tuple(primaryCode, secondaryCode));
90 EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
91 EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
Manojkiran Edaba5258f2021-02-25 13:23:33 +053092
Cosmo Choufe514952024-09-05 01:08:58 +080093 primaryCode = {46};
Manojkiran Edaba5258f2021-02-25 13:23:33 +053094 secondaryCode = {10, 40, 0, 35, 78};
Cosmo Choufe514952024-09-05 01:08:58 +080095 testReporter.value(std::make_tuple(primaryCode, secondaryCode));
96 EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
97 EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
Manojkiran Edaba5258f2021-02-25 13:23:33 +053098
Cosmo Choufe514952024-09-05 01:08:58 +080099 primaryCode = {46};
Manojkiran Edaba5258f2021-02-25 13:23:33 +0530100 secondaryCode = {10, 40, 0, 35, 78};
Cosmo Choufe514952024-09-05 01:08:58 +0800101 testReporter.value(std::make_tuple(primaryCode, secondaryCode));
102 EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
103 EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
Kun Yi35b6c672018-06-11 20:44:43 -0700104}
105
106} // namespace