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