blob: f182de42ce2b6b3718de6a159ae0cb224860a848 [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#include <sdbusplus/bus.hpp>
2#include <gtest/gtest.h>
Lei YU7b218792017-02-09 12:10:13 +08003#include <memory>
Lei YU96232822017-01-20 14:05:46 +08004
5#include "bmc_epoch.hpp"
6#include "config.h"
Lei YU415b9642017-02-09 11:37:26 +08007#include "types.hpp"
Lei YU7b218792017-02-09 12:10:13 +08008#include "mocked_bmc_time_change_listener.hpp"
Lei YU96232822017-01-20 14:05:46 +08009
10namespace phosphor
11{
12namespace time
13{
14
Lei YU7b218792017-02-09 12:10:13 +080015using ::testing::_;
Lei YUe7abcdc2017-01-16 15:05:24 +080016using namespace std::chrono;
Lei YU7b218792017-02-09 12:10:13 +080017
Lei YU96232822017-01-20 14:05:46 +080018class TestBmcEpoch : public testing::Test
19{
20 public:
Lei YU96232822017-01-20 14:05:46 +080021 sdbusplus::bus::bus bus;
Lei YU7b218792017-02-09 12:10:13 +080022 sd_event* event;
23 MockBmcTimeChangeListener listener;
24 std::unique_ptr<BmcEpoch> bmcEpoch;
Lei YU96232822017-01-20 14:05:46 +080025
26 TestBmcEpoch()
Lei YU7b218792017-02-09 12:10:13 +080027 : bus(sdbusplus::bus::new_default())
Lei YU96232822017-01-20 14:05:46 +080028 {
Lei YU7b218792017-02-09 12:10:13 +080029 // 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);
Lei YU96232822017-01-20 14:05:46 +080040 }
41
42 // Proxies for BmcEpoch's private members and functions
43 Mode getTimeMode()
44 {
Lei YU7b218792017-02-09 12:10:13 +080045 return bmcEpoch->timeMode;
Lei YU96232822017-01-20 14:05:46 +080046 }
47 Owner getTimeOwner()
48 {
Lei YU7b218792017-02-09 12:10:13 +080049 return bmcEpoch->timeOwner;
Lei YU96232822017-01-20 14:05:46 +080050 }
Lei YUe7abcdc2017-01-16 15:05:24 +080051 void setTimeOwner(Owner owner)
52 {
Lei YU7b218792017-02-09 12:10:13 +080053 bmcEpoch->timeOwner = owner;
Lei YUe7abcdc2017-01-16 15:05:24 +080054 }
55 void setTimeMode(Mode mode)
56 {
Lei YU7b218792017-02-09 12:10:13 +080057 bmcEpoch->timeMode = mode;
58 }
59 void triggerTimeChange()
60 {
61 bmcEpoch->onTimeChange(nullptr,
62 -1,
63 0,
64 bmcEpoch.get());
Lei YUe7abcdc2017-01-16 15:05:24 +080065 }
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
71 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{
Lei YU57eab122017-06-16 10:27:30 +080085 setTimeMode(Mode::NTP);
Lei YUe7abcdc2017-01-16 15:05:24 +080086 auto epochNow = duration_cast<microseconds>(
87 system_clock::now().time_since_epoch()).count();
88 // In NTP mode, setting time is not allowed
Lei YU7b218792017-02-09 12:10:13 +080089 auto ret = bmcEpoch->elapsed(epochNow);
Lei YUe7abcdc2017-01-16 15:05:24 +080090 EXPECT_EQ(0, ret);
91
92 // In Host owner, setting time is not allowed
93 setTimeMode(Mode::MANUAL);
94 setTimeOwner(Owner::HOST);
Lei YU7b218792017-02-09 12:10:13 +080095 ret = bmcEpoch->elapsed(epochNow);
Lei YUe7abcdc2017-01-16 15:05:24 +080096 EXPECT_EQ(0, ret);
97}
98
99TEST_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}
Lei YU96232822017-01-20 14:05:46 +0800105
Lei YU7b218792017-02-09 12:10:13 +0800106TEST_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
Lei YU96232822017-01-20 14:05:46 +0800113}
114}