blob: eb116e555f698c282537f99b3f895e9d9451aa0b [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#include "config.h"
2
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05003#include "bmc_epoch.hpp"
4#include "mocked_bmc_time_change_listener.hpp"
5#include "types.hpp"
6
Lei YU33752c72018-06-07 17:06:58 +08007#include <memory>
8#include <sdbusplus/bus.hpp>
Lei YUf6fad822018-07-13 16:35:45 +08009#include <xyz/openbmc_project/Time/error.hpp>
Lei YU33752c72018-06-07 17:06:58 +080010
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050011#include <gtest/gtest.h>
12
Lei YU96232822017-01-20 14:05:46 +080013namespace phosphor
14{
15namespace time
16{
17
Lei YU7b218792017-02-09 12:10:13 +080018using ::testing::_;
Lei YUe7abcdc2017-01-16 15:05:24 +080019using namespace std::chrono;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050020using NotAllowed = sdbusplus::xyz::openbmc_project::Time::Error::NotAllowed;
Lei YU7b218792017-02-09 12:10:13 +080021
Lei YU96232822017-01-20 14:05:46 +080022class TestBmcEpoch : public testing::Test
23{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050024 public:
25 sdbusplus::bus::bus bus;
26 sd_event* event;
27 MockBmcTimeChangeListener listener;
28 std::unique_ptr<BmcEpoch> bmcEpoch;
Lei YU96232822017-01-20 14:05:46 +080029
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050030 TestBmcEpoch() : bus(sdbusplus::bus::new_default())
31 {
32 // BmcEpoch requires sd_event to init
33 sd_event_default(&event);
34 bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
35 bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC);
36 bmcEpoch->setBmcTimeChangeListener(&listener);
37 }
Lei YU7b218792017-02-09 12:10:13 +080038
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050039 ~TestBmcEpoch()
40 {
41 bus.detach_event();
42 sd_event_unref(event);
43 }
Lei YU96232822017-01-20 14:05:46 +080044
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050045 // Proxies for BmcEpoch's private members and functions
46 Mode getTimeMode()
47 {
48 return bmcEpoch->timeMode;
49 }
50 Owner getTimeOwner()
51 {
52 return bmcEpoch->timeOwner;
53 }
54 void setTimeOwner(Owner owner)
55 {
56 bmcEpoch->timeOwner = owner;
57 }
58 void setTimeMode(Mode mode)
59 {
60 bmcEpoch->timeMode = mode;
61 }
62 void triggerTimeChange()
63 {
64 bmcEpoch->onTimeChange(nullptr, -1, 0, bmcEpoch.get());
65 }
Lei YU96232822017-01-20 14:05:46 +080066};
67
68TEST_F(TestBmcEpoch, empty)
69{
Lei YU57eab122017-06-16 10:27:30 +080070 // Default mode/owner is MANUAL/BOTH
Lei YUad143542017-07-25 14:27:07 +080071 EXPECT_EQ(Mode::Manual, getTimeMode());
72 EXPECT_EQ(Owner::Both, getTimeOwner());
Lei YU96232822017-01-20 14:05:46 +080073}
74
75TEST_F(TestBmcEpoch, getElapsed)
76{
Lei YU7b218792017-02-09 12:10:13 +080077 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080078 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080079 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080080 EXPECT_GE(t2, t1);
81}
82
Lei YUe7abcdc2017-01-16 15:05:24 +080083TEST_F(TestBmcEpoch, setElapsedNotAllowed)
84{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050085 auto epochNow =
86 duration_cast<microseconds>(system_clock::now().time_since_epoch())
87 .count();
Lei YUe7abcdc2017-01-16 15:05:24 +080088
89 // In Host owner, setting time is not allowed
Lei YUad143542017-07-25 14:27:07 +080090 setTimeMode(Mode::Manual);
91 setTimeOwner(Owner::Host);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050092 EXPECT_THROW(bmcEpoch->elapsed(epochNow), NotAllowed);
Lei YUe7abcdc2017-01-16 15:05:24 +080093}
94
95TEST_F(TestBmcEpoch, setElapsedOK)
96{
97 // TODO: setting time will call sd-bus functions and it will fail on host
98 // if we have gmock for sdbusplus::bus, we can test setElapsed.
99 // But for now we can not test it
100}
Lei YU96232822017-01-20 14:05:46 +0800101
Lei YU7b218792017-02-09 12:10:13 +0800102TEST_F(TestBmcEpoch, onTimeChange)
103{
104 // On BMC time change, the listner is expected to be notified
105 EXPECT_CALL(listener, onBmcTimeChanged(_)).Times(1);
106 triggerTimeChange();
107}
108
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500109} // namespace time
110} // namespace phosphor