xref: /openbmc/phosphor-time-manager/test/TestBmcEpoch.cpp (revision ad14354fc17811ae585f13b1d52a275cf3daff35)
1 #include <sdbusplus/bus.hpp>
2 #include <gtest/gtest.h>
3 #include <memory>
4 
5 #include "bmc_epoch.hpp"
6 #include "config.h"
7 #include "types.hpp"
8 #include "mocked_bmc_time_change_listener.hpp"
9 
10 namespace phosphor
11 {
12 namespace time
13 {
14 
15 using ::testing::_;
16 using namespace std::chrono;
17 
18 class TestBmcEpoch : public testing::Test
19 {
20     public:
21         sdbusplus::bus::bus bus;
22         sd_event* event;
23         MockBmcTimeChangeListener listener;
24         std::unique_ptr<BmcEpoch> bmcEpoch;
25 
26         TestBmcEpoch()
27             : bus(sdbusplus::bus::new_default())
28         {
29             // BmcEpoch requires sd_event to init
30             sd_event_default(&event);
31             bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
32             bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC);
33             bmcEpoch->setBmcTimeChangeListener(&listener);
34         }
35 
36         ~TestBmcEpoch()
37         {
38             bus.detach_event();
39             sd_event_unref(event);
40         }
41 
42         // Proxies for BmcEpoch's private members and functions
43         Mode getTimeMode()
44         {
45             return bmcEpoch->timeMode;
46         }
47         Owner getTimeOwner()
48         {
49             return bmcEpoch->timeOwner;
50         }
51         void setTimeOwner(Owner owner)
52         {
53             bmcEpoch->timeOwner = owner;
54         }
55         void setTimeMode(Mode mode)
56         {
57             bmcEpoch->timeMode = mode;
58         }
59         void triggerTimeChange()
60         {
61             bmcEpoch->onTimeChange(nullptr,
62                                    -1,
63                                    0,
64                                    bmcEpoch.get());
65         }
66 };
67 
68 TEST_F(TestBmcEpoch, empty)
69 {
70     // Default mode/owner is MANUAL/BOTH
71     EXPECT_EQ(Mode::Manual, getTimeMode());
72     EXPECT_EQ(Owner::Both, getTimeOwner());
73 }
74 
75 TEST_F(TestBmcEpoch, getElapsed)
76 {
77     auto t1 = bmcEpoch->elapsed();
78     EXPECT_NE(0, t1);
79     auto t2 = bmcEpoch->elapsed();
80     EXPECT_GE(t2, t1);
81 }
82 
83 TEST_F(TestBmcEpoch, setElapsedNotAllowed)
84 {
85     setTimeMode(Mode::NTP);
86     auto epochNow = duration_cast<microseconds>(
87         system_clock::now().time_since_epoch()).count();
88     // In NTP mode, setting time is not allowed
89     auto ret = bmcEpoch->elapsed(epochNow);
90     EXPECT_EQ(0, ret);
91 
92     // In Host owner, setting time is not allowed
93     setTimeMode(Mode::Manual);
94     setTimeOwner(Owner::Host);
95     ret = bmcEpoch->elapsed(epochNow);
96     EXPECT_EQ(0, ret);
97 }
98 
99 TEST_F(TestBmcEpoch, setElapsedOK)
100 {
101     // TODO: setting time will call sd-bus functions and it will fail on host
102     // if we have gmock for sdbusplus::bus, we can test setElapsed.
103     // But for now we can not test it
104 }
105 
106 TEST_F(TestBmcEpoch, onTimeChange)
107 {
108     // On BMC time change, the listner is expected to be notified
109     EXPECT_CALL(listener, onBmcTimeChanged(_)).Times(1);
110     triggerTimeChange();
111 }
112 
113 }
114 }
115