Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
| 2 | #include <gtest/gtest.h> |
| 3 | |
| 4 | #include "bmc_epoch.hpp" |
| 5 | #include "config.h" |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 6 | #include "types.hpp" |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace time |
| 11 | { |
| 12 | |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 13 | using namespace std::chrono; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 14 | class TestBmcEpoch : public testing::Test |
| 15 | { |
| 16 | public: |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 17 | sdbusplus::bus::bus bus; |
| 18 | BmcEpoch bmcEpoch; |
| 19 | |
| 20 | TestBmcEpoch() |
| 21 | : bus(sdbusplus::bus::new_default()), |
| 22 | bmcEpoch(bus, OBJPATH_BMC) |
| 23 | { |
| 24 | // Empty |
| 25 | } |
| 26 | |
| 27 | // Proxies for BmcEpoch's private members and functions |
| 28 | Mode getTimeMode() |
| 29 | { |
| 30 | return bmcEpoch.timeMode; |
| 31 | } |
| 32 | Owner getTimeOwner() |
| 33 | { |
| 34 | return bmcEpoch.timeOwner; |
| 35 | } |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 36 | void setTimeOwner(Owner owner) |
| 37 | { |
| 38 | bmcEpoch.timeOwner = owner; |
| 39 | } |
| 40 | void setTimeMode(Mode mode) |
| 41 | { |
| 42 | bmcEpoch.timeMode = mode; |
| 43 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | TEST_F(TestBmcEpoch, empty) |
| 47 | { |
| 48 | EXPECT_EQ(Mode::NTP, getTimeMode()); |
| 49 | EXPECT_EQ(Owner::BMC, getTimeOwner()); |
| 50 | } |
| 51 | |
| 52 | TEST_F(TestBmcEpoch, getElapsed) |
| 53 | { |
| 54 | auto t1 = bmcEpoch.elapsed(); |
| 55 | EXPECT_NE(0, t1); |
| 56 | auto t2 = bmcEpoch.elapsed(); |
| 57 | EXPECT_GE(t2, t1); |
| 58 | } |
| 59 | |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 60 | TEST_F(TestBmcEpoch, setElapsedNotAllowed) |
| 61 | { |
| 62 | auto epochNow = duration_cast<microseconds>( |
| 63 | system_clock::now().time_since_epoch()).count(); |
| 64 | // In NTP mode, setting time is not allowed |
| 65 | auto ret = bmcEpoch.elapsed(epochNow); |
| 66 | EXPECT_EQ(0, ret); |
| 67 | |
| 68 | // In Host owner, setting time is not allowed |
| 69 | setTimeMode(Mode::MANUAL); |
| 70 | setTimeOwner(Owner::HOST); |
| 71 | ret = bmcEpoch.elapsed(epochNow); |
| 72 | EXPECT_EQ(0, ret); |
| 73 | } |
| 74 | |
| 75 | TEST_F(TestBmcEpoch, setElapsedOK) |
| 76 | { |
| 77 | // TODO: setting time will call sd-bus functions and it will fail on host |
| 78 | // if we have gmock for sdbusplus::bus, we can test setElapsed. |
| 79 | // But for now we can not test it |
| 80 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 81 | |
| 82 | } |
| 83 | } |