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