Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame^] | 1 | #include <sdbusplus/bus.hpp> |
| 2 | #include <gtest/gtest.h> |
| 3 | |
| 4 | #include "host_epoch.hpp" |
| 5 | #include "config.h" |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace time |
| 10 | { |
| 11 | |
| 12 | using namespace std::chrono; |
| 13 | using namespace std::chrono_literals; |
| 14 | |
| 15 | class TestHostEpoch : public testing::Test |
| 16 | { |
| 17 | public: |
| 18 | using Mode = EpochBase::Mode; |
| 19 | using Owner = EpochBase::Owner; |
| 20 | |
| 21 | sdbusplus::bus::bus bus; |
| 22 | HostEpoch hostEpoch; |
| 23 | |
| 24 | static constexpr auto FILE_NOT_EXIST = "path/to/file-not-exist"; |
| 25 | static constexpr auto FILE_OFFSET = "saved_host_offset"; |
| 26 | static constexpr auto delta = 2s; |
| 27 | |
| 28 | TestHostEpoch() |
| 29 | : bus(sdbusplus::bus::new_default()), |
| 30 | hostEpoch(bus, OBJPATH_HOST) |
| 31 | { |
| 32 | // Make sure the file does not exist |
| 33 | std::remove(FILE_NOT_EXIST); |
| 34 | } |
| 35 | ~TestHostEpoch() |
| 36 | { |
| 37 | // Cleanup test file |
| 38 | std::remove(FILE_OFFSET); |
| 39 | } |
| 40 | |
| 41 | // Proxies for HostEpoch's private members and functions |
| 42 | Mode getTimeMode() |
| 43 | { |
| 44 | return hostEpoch.timeMode; |
| 45 | } |
| 46 | Owner getTimeOwner() |
| 47 | { |
| 48 | return hostEpoch.timeOwner; |
| 49 | } |
| 50 | template <typename T> |
| 51 | T readData(const char* fileName) |
| 52 | { |
| 53 | return HostEpoch::readData<T>(fileName); |
| 54 | } |
| 55 | template <typename T> |
| 56 | void writeData(const char* fileName, T&& data) |
| 57 | { |
| 58 | HostEpoch::writeData<T>(fileName, std::forward<T>(data)); |
| 59 | } |
| 60 | microseconds getOffset() |
| 61 | { |
| 62 | return hostEpoch.offset; |
| 63 | } |
| 64 | void setTimeOwner(Owner owner) |
| 65 | { |
| 66 | hostEpoch.timeOwner = owner; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | TEST_F(TestHostEpoch, empty) |
| 71 | { |
| 72 | EXPECT_EQ(Mode::NTP, getTimeMode()); |
| 73 | EXPECT_EQ(Owner::BMC, getTimeOwner()); |
| 74 | } |
| 75 | |
| 76 | TEST_F(TestHostEpoch, readDataFileNotExist) |
| 77 | { |
| 78 | // When file does not exist, the default offset shall be 0 |
| 79 | microseconds offset(0); |
| 80 | auto value = readData<decltype(offset)::rep>(FILE_NOT_EXIST); |
| 81 | EXPECT_EQ(0, value); |
| 82 | } |
| 83 | |
| 84 | TEST_F(TestHostEpoch, writeAndReadData) |
| 85 | { |
| 86 | // Write offset to file |
| 87 | microseconds offsetToWrite(1234567); |
| 88 | writeData<decltype(offsetToWrite)::rep>(FILE_OFFSET, offsetToWrite.count()); |
| 89 | |
| 90 | // Read it back |
| 91 | microseconds offsetToRead; |
| 92 | offsetToRead = microseconds( |
| 93 | readData<decltype(offsetToRead)::rep>(FILE_OFFSET)); |
| 94 | EXPECT_EQ(offsetToWrite, offsetToRead); |
| 95 | } |
| 96 | |
| 97 | TEST_F(TestHostEpoch, setElapsedNotAllowed) |
| 98 | { |
| 99 | // By default offset shall be 0 |
| 100 | EXPECT_EQ(0, getOffset().count()); |
| 101 | |
| 102 | // Set time in BMC mode is not allowed, |
| 103 | // so verify offset is still 0 after set time |
| 104 | microseconds diff = 1min; |
| 105 | hostEpoch.elapsed(hostEpoch.elapsed() + diff.count()); |
| 106 | EXPECT_EQ(0, getOffset().count()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(TestHostEpoch, setElapsedInFutureAndGet) |
| 110 | { |
| 111 | // Set to HOST owner so that we can set elapsed |
| 112 | setTimeOwner(Owner::HOST); |
| 113 | |
| 114 | // Get current time, and set future +1min time |
| 115 | auto t1 = hostEpoch.elapsed(); |
| 116 | EXPECT_NE(0, t1); |
| 117 | microseconds diff = 1min; |
| 118 | auto t2 = t1 + diff.count(); |
| 119 | hostEpoch.elapsed(t2); |
| 120 | |
| 121 | // Verify that the offset shall be positive, |
| 122 | // and less or equal to diff, and shall be not too less. |
| 123 | auto offset = getOffset(); |
| 124 | EXPECT_GT(offset, microseconds(0)); |
| 125 | EXPECT_LE(offset, diff); |
| 126 | diff -= delta; |
| 127 | EXPECT_GE(offset, diff); |
| 128 | |
| 129 | // Now get time shall be around future +1min time |
| 130 | auto epochNow = duration_cast<microseconds>( |
| 131 | system_clock::now().time_since_epoch()).count(); |
| 132 | auto elapsedGot = hostEpoch.elapsed(); |
| 133 | EXPECT_LT(epochNow, elapsedGot); |
| 134 | auto epochDiff = elapsedGot - epochNow; |
| 135 | diff = 1min; |
| 136 | EXPECT_GT(epochDiff, (diff - delta).count()); |
| 137 | EXPECT_LT(epochDiff, (diff + delta).count()); |
| 138 | } |
| 139 | |
| 140 | TEST_F(TestHostEpoch, setElapsedInPastAndGet) |
| 141 | { |
| 142 | // Set to HOST owner so that we can set elapsed |
| 143 | setTimeOwner(Owner::HOST); |
| 144 | |
| 145 | // Get current time, and set past -1min time |
| 146 | auto t1 = hostEpoch.elapsed(); |
| 147 | EXPECT_NE(0, t1); |
| 148 | microseconds diff = 1min; |
| 149 | auto t2 = t1 - diff.count(); |
| 150 | hostEpoch.elapsed(t2); |
| 151 | |
| 152 | // Verify that the offset shall be negative, and the absolute value |
| 153 | // shall be equal or greater than diff, and shall not be too greater |
| 154 | auto offset = getOffset(); |
| 155 | EXPECT_LT(offset, microseconds(0)); |
| 156 | offset = -offset; |
| 157 | EXPECT_GE(offset, diff); |
| 158 | diff += 10s; |
| 159 | EXPECT_LE(offset, diff); |
| 160 | |
| 161 | // Now get time shall be around past -1min time |
| 162 | auto epochNow = duration_cast<microseconds>( |
| 163 | system_clock::now().time_since_epoch()).count(); |
| 164 | auto elapsedGot = hostEpoch.elapsed(); |
| 165 | EXPECT_LT(elapsedGot, epochNow); |
| 166 | auto epochDiff = epochNow - elapsedGot; |
| 167 | diff = 1min; |
| 168 | EXPECT_GT(epochDiff, (diff - delta).count()); |
| 169 | EXPECT_LT(epochDiff, (diff + delta).count()); |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | } |