Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
| 2 | #include <gtest/gtest.h> |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 3 | #include <memory> |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 4 | |
| 5 | #include "bmc_epoch.hpp" |
| 6 | #include "config.h" |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 7 | #include "types.hpp" |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 8 | #include "mocked_bmc_time_change_listener.hpp" |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace time |
| 13 | { |
| 14 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 15 | using ::testing::_; |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 16 | using namespace std::chrono; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 17 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 18 | class TestBmcEpoch : public testing::Test |
| 19 | { |
| 20 | public: |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 21 | sdbusplus::bus::bus bus; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 22 | sd_event* event; |
| 23 | MockBmcTimeChangeListener listener; |
| 24 | std::unique_ptr<BmcEpoch> bmcEpoch; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 25 | |
| 26 | TestBmcEpoch() |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 27 | : bus(sdbusplus::bus::new_default()) |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 28 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 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); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // Proxies for BmcEpoch's private members and functions |
| 43 | Mode getTimeMode() |
| 44 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 45 | return bmcEpoch->timeMode; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 46 | } |
| 47 | Owner getTimeOwner() |
| 48 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 49 | return bmcEpoch->timeOwner; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 50 | } |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 51 | void setTimeOwner(Owner owner) |
| 52 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 53 | bmcEpoch->timeOwner = owner; |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 54 | } |
| 55 | void setTimeMode(Mode mode) |
| 56 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 57 | bmcEpoch->timeMode = mode; |
| 58 | } |
| 59 | void triggerTimeChange() |
| 60 | { |
| 61 | bmcEpoch->onTimeChange(nullptr, |
| 62 | -1, |
| 63 | 0, |
| 64 | bmcEpoch.get()); |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 65 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | TEST_F(TestBmcEpoch, empty) |
| 69 | { |
Lei YU | 57eab12 | 2017-06-16 10:27:30 +0800 | [diff] [blame^] | 70 | // Default mode/owner is MANUAL/BOTH |
| 71 | EXPECT_EQ(Mode::MANUAL, getTimeMode()); |
| 72 | EXPECT_EQ(Owner::BOTH, getTimeOwner()); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST_F(TestBmcEpoch, getElapsed) |
| 76 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 77 | auto t1 = bmcEpoch->elapsed(); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 78 | EXPECT_NE(0, t1); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 79 | auto t2 = bmcEpoch->elapsed(); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 80 | EXPECT_GE(t2, t1); |
| 81 | } |
| 82 | |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 83 | TEST_F(TestBmcEpoch, setElapsedNotAllowed) |
| 84 | { |
Lei YU | 57eab12 | 2017-06-16 10:27:30 +0800 | [diff] [blame^] | 85 | setTimeMode(Mode::NTP); |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 86 | auto epochNow = duration_cast<microseconds>( |
| 87 | system_clock::now().time_since_epoch()).count(); |
| 88 | // In NTP mode, setting time is not allowed |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 89 | auto ret = bmcEpoch->elapsed(epochNow); |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 90 | EXPECT_EQ(0, ret); |
| 91 | |
| 92 | // In Host owner, setting time is not allowed |
| 93 | setTimeMode(Mode::MANUAL); |
| 94 | setTimeOwner(Owner::HOST); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 95 | ret = bmcEpoch->elapsed(epochNow); |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 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 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 105 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 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 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 113 | } |
| 114 | } |