blob: 4661e20b4bd555e0b83be6dcce4d198c410e78ee [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{
70 EXPECT_EQ(Mode::NTP, getTimeMode());
71 EXPECT_EQ(Owner::BMC, getTimeOwner());
72}
73
74TEST_F(TestBmcEpoch, getElapsed)
75{
Lei YU7b218792017-02-09 12:10:13 +080076 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080077 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080078 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080079 EXPECT_GE(t2, t1);
80}
81
Lei YUe7abcdc2017-01-16 15:05:24 +080082TEST_F(TestBmcEpoch, setElapsedNotAllowed)
83{
84 auto epochNow = duration_cast<microseconds>(
85 system_clock::now().time_since_epoch()).count();
86 // In NTP mode, setting time is not allowed
Lei YU7b218792017-02-09 12:10:13 +080087 auto ret = bmcEpoch->elapsed(epochNow);
Lei YUe7abcdc2017-01-16 15:05:24 +080088 EXPECT_EQ(0, ret);
89
90 // In Host owner, setting time is not allowed
91 setTimeMode(Mode::MANUAL);
92 setTimeOwner(Owner::HOST);
Lei YU7b218792017-02-09 12:10:13 +080093 ret = bmcEpoch->elapsed(epochNow);
Lei YUe7abcdc2017-01-16 15:05:24 +080094 EXPECT_EQ(0, ret);
95}
96
97TEST_F(TestBmcEpoch, setElapsedOK)
98{
99 // TODO: setting time will call sd-bus functions and it will fail on host
100 // if we have gmock for sdbusplus::bus, we can test setElapsed.
101 // But for now we can not test it
102}
Lei YU96232822017-01-20 14:05:46 +0800103
Lei YU7b218792017-02-09 12:10:13 +0800104TEST_F(TestBmcEpoch, onTimeChange)
105{
106 // On BMC time change, the listner is expected to be notified
107 EXPECT_CALL(listener, onBmcTimeChanged(_)).Times(1);
108 triggerTimeChange();
109}
110
Lei YU96232822017-01-20 14:05:46 +0800111}
112}