blob: 2d0bc72b006a384550cb586c89cd2c4936d10238 [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#include "bmc_epoch.hpp"
2#include "config.h"
Lei YU415b9642017-02-09 11:37:26 +08003#include "types.hpp"
Lei YU7b218792017-02-09 12:10:13 +08004#include "mocked_bmc_time_change_listener.hpp"
Lei YU96232822017-01-20 14:05:46 +08005
Lei YU33752c72018-06-07 17:06:58 +08006#include <gtest/gtest.h>
7#include <memory>
8#include <sdbusplus/bus.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10
Lei YU96232822017-01-20 14:05:46 +080011namespace phosphor
12{
13namespace time
14{
15
Lei YU7b218792017-02-09 12:10:13 +080016using ::testing::_;
Lei YUe7abcdc2017-01-16 15:05:24 +080017using namespace std::chrono;
Lei YU33752c72018-06-07 17:06:58 +080018using InsufficientPermission =
19 sdbusplus::xyz::openbmc_project::Common::Error::InsufficientPermission;
Lei YU7b218792017-02-09 12:10:13 +080020
Lei YU96232822017-01-20 14:05:46 +080021class TestBmcEpoch : public testing::Test
22{
23 public:
Lei YU96232822017-01-20 14:05:46 +080024 sdbusplus::bus::bus bus;
Lei YU7b218792017-02-09 12:10:13 +080025 sd_event* event;
26 MockBmcTimeChangeListener listener;
27 std::unique_ptr<BmcEpoch> bmcEpoch;
Lei YU96232822017-01-20 14:05:46 +080028
29 TestBmcEpoch()
Lei YU7b218792017-02-09 12:10:13 +080030 : bus(sdbusplus::bus::new_default())
Lei YU96232822017-01-20 14:05:46 +080031 {
Lei YU7b218792017-02-09 12:10:13 +080032 // 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 }
38
39 ~TestBmcEpoch()
40 {
41 bus.detach_event();
42 sd_event_unref(event);
Lei YU96232822017-01-20 14:05:46 +080043 }
44
45 // Proxies for BmcEpoch's private members and functions
46 Mode getTimeMode()
47 {
Lei YU7b218792017-02-09 12:10:13 +080048 return bmcEpoch->timeMode;
Lei YU96232822017-01-20 14:05:46 +080049 }
50 Owner getTimeOwner()
51 {
Lei YU7b218792017-02-09 12:10:13 +080052 return bmcEpoch->timeOwner;
Lei YU96232822017-01-20 14:05:46 +080053 }
Lei YUe7abcdc2017-01-16 15:05:24 +080054 void setTimeOwner(Owner owner)
55 {
Lei YU7b218792017-02-09 12:10:13 +080056 bmcEpoch->timeOwner = owner;
Lei YUe7abcdc2017-01-16 15:05:24 +080057 }
58 void setTimeMode(Mode mode)
59 {
Lei YU7b218792017-02-09 12:10:13 +080060 bmcEpoch->timeMode = mode;
61 }
62 void triggerTimeChange()
63 {
64 bmcEpoch->onTimeChange(nullptr,
65 -1,
66 0,
67 bmcEpoch.get());
Lei YUe7abcdc2017-01-16 15:05:24 +080068 }
Lei YU96232822017-01-20 14:05:46 +080069};
70
71TEST_F(TestBmcEpoch, empty)
72{
Lei YU57eab122017-06-16 10:27:30 +080073 // Default mode/owner is MANUAL/BOTH
Lei YUad143542017-07-25 14:27:07 +080074 EXPECT_EQ(Mode::Manual, getTimeMode());
75 EXPECT_EQ(Owner::Both, getTimeOwner());
Lei YU96232822017-01-20 14:05:46 +080076}
77
78TEST_F(TestBmcEpoch, getElapsed)
79{
Lei YU7b218792017-02-09 12:10:13 +080080 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080081 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080082 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080083 EXPECT_GE(t2, t1);
84}
85
Lei YUe7abcdc2017-01-16 15:05:24 +080086TEST_F(TestBmcEpoch, setElapsedNotAllowed)
87{
88 auto epochNow = duration_cast<microseconds>(
89 system_clock::now().time_since_epoch()).count();
Lei YUe7abcdc2017-01-16 15:05:24 +080090
91 // In Host owner, setting time is not allowed
Lei YUad143542017-07-25 14:27:07 +080092 setTimeMode(Mode::Manual);
93 setTimeOwner(Owner::Host);
Lei YU33752c72018-06-07 17:06:58 +080094 EXPECT_THROW(
95 bmcEpoch->elapsed(epochNow),
96 InsufficientPermission);
Lei YUe7abcdc2017-01-16 15:05:24 +080097}
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}