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