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 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 17 | const constexpr microseconds USEC_ZERO{0}; |
| 18 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 19 | class TestHostEpoch : public testing::Test |
| 20 | { |
| 21 | public: |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 22 | sdbusplus::bus::bus bus; |
| 23 | HostEpoch hostEpoch; |
| 24 | |
| 25 | static constexpr auto FILE_NOT_EXIST = "path/to/file-not-exist"; |
| 26 | static constexpr auto FILE_OFFSET = "saved_host_offset"; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 27 | const microseconds delta = 2s; |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 28 | |
| 29 | TestHostEpoch() |
| 30 | : bus(sdbusplus::bus::new_default()), |
| 31 | hostEpoch(bus, OBJPATH_HOST) |
| 32 | { |
| 33 | // Make sure the file does not exist |
| 34 | std::remove(FILE_NOT_EXIST); |
| 35 | } |
| 36 | ~TestHostEpoch() |
| 37 | { |
| 38 | // Cleanup test file |
| 39 | std::remove(FILE_OFFSET); |
| 40 | } |
| 41 | |
| 42 | // Proxies for HostEpoch's private members and functions |
| 43 | Mode getTimeMode() |
| 44 | { |
| 45 | return hostEpoch.timeMode; |
| 46 | } |
| 47 | Owner getTimeOwner() |
| 48 | { |
| 49 | return hostEpoch.timeOwner; |
| 50 | } |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 51 | microseconds getOffset() |
| 52 | { |
| 53 | return hostEpoch.offset; |
| 54 | } |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 55 | void setOffset(microseconds us) |
| 56 | { |
| 57 | hostEpoch.offset = us; |
| 58 | } |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 59 | void setTimeOwner(Owner owner) |
| 60 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 61 | hostEpoch.onOwnerChanged(owner); |
| 62 | } |
| 63 | void setTimeMode(Mode mode) |
| 64 | { |
| 65 | hostEpoch.onModeChanged(mode); |
| 66 | } |
| 67 | |
| 68 | void checkSettingTimeNotAllowed() |
| 69 | { |
| 70 | // By default offset shall be 0 |
| 71 | EXPECT_EQ(0, getOffset().count()); |
| 72 | |
| 73 | // Set time is not allowed, |
| 74 | // so verify offset is still 0 after set time |
| 75 | microseconds diff = 1min; |
| 76 | hostEpoch.elapsed(hostEpoch.elapsed() + diff.count()); |
| 77 | EXPECT_EQ(0, getOffset().count()); |
| 78 | // TODO: when gmock is ready, check there is no call to timedatectl |
| 79 | } |
| 80 | |
| 81 | void checkSetSplitTimeInFuture() |
| 82 | { |
| 83 | // Get current time, and set future +1min time |
| 84 | auto t1 = hostEpoch.elapsed(); |
| 85 | EXPECT_NE(0, t1); |
| 86 | microseconds diff = 1min; |
| 87 | auto t2 = t1 + diff.count(); |
| 88 | hostEpoch.elapsed(t2); |
| 89 | |
| 90 | // Verify that the offset shall be positive, |
| 91 | // and less or equal to diff, and shall be not too less. |
| 92 | auto offset = getOffset(); |
| 93 | EXPECT_GT(offset, USEC_ZERO); |
| 94 | EXPECT_LE(offset, diff); |
| 95 | diff -= delta; |
| 96 | EXPECT_GE(offset, diff); |
| 97 | |
| 98 | // Now get time shall be around future +1min time |
| 99 | auto epochNow = duration_cast<microseconds>( |
| 100 | system_clock::now().time_since_epoch()).count(); |
| 101 | auto elapsedGot = hostEpoch.elapsed(); |
| 102 | EXPECT_LT(epochNow, elapsedGot); |
| 103 | auto epochDiff = elapsedGot - epochNow; |
| 104 | diff = 1min; |
| 105 | EXPECT_GT(epochDiff, (diff - delta).count()); |
| 106 | EXPECT_LT(epochDiff, (diff + delta).count()); |
| 107 | } |
| 108 | void checkSetSplitTimeInPast() |
| 109 | { |
| 110 | // Get current time, and set past -1min time |
| 111 | auto t1 = hostEpoch.elapsed(); |
| 112 | EXPECT_NE(0, t1); |
| 113 | microseconds diff = 1min; |
| 114 | auto t2 = t1 - diff.count(); |
| 115 | hostEpoch.elapsed(t2); |
| 116 | |
| 117 | // Verify that the offset shall be negative, and the absolute value |
| 118 | // shall be equal or greater than diff, and shall not be too greater |
| 119 | auto offset = getOffset(); |
| 120 | EXPECT_LT(offset, USEC_ZERO); |
| 121 | offset = -offset; |
| 122 | EXPECT_GE(offset, diff); |
| 123 | diff += 10s; |
| 124 | EXPECT_LE(offset, diff); |
| 125 | |
| 126 | // Now get time shall be around past -1min time |
| 127 | auto epochNow = duration_cast<microseconds>( |
| 128 | system_clock::now().time_since_epoch()).count(); |
| 129 | auto elapsedGot = hostEpoch.elapsed(); |
| 130 | EXPECT_LT(elapsedGot, epochNow); |
| 131 | auto epochDiff = epochNow - elapsedGot; |
| 132 | diff = 1min; |
| 133 | EXPECT_GT(epochDiff, (diff - delta).count()); |
| 134 | EXPECT_LT(epochDiff, (diff + delta).count()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 135 | } |
| 136 | }; |
| 137 | |
| 138 | TEST_F(TestHostEpoch, empty) |
| 139 | { |
Lei YU | 57eab12 | 2017-06-16 10:27:30 +0800 | [diff] [blame] | 140 | // Default mode/owner is MANUAL/BOTH |
| 141 | EXPECT_EQ(Mode::MANUAL, getTimeMode()); |
| 142 | EXPECT_EQ(Owner::BOTH, getTimeOwner()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | TEST_F(TestHostEpoch, readDataFileNotExist) |
| 146 | { |
| 147 | // When file does not exist, the default offset shall be 0 |
| 148 | microseconds offset(0); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 149 | auto value = utils::readData<decltype(offset)::rep>(FILE_NOT_EXIST); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 150 | EXPECT_EQ(0, value); |
| 151 | } |
| 152 | |
| 153 | TEST_F(TestHostEpoch, writeAndReadData) |
| 154 | { |
| 155 | // Write offset to file |
| 156 | microseconds offsetToWrite(1234567); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 157 | utils::writeData<decltype(offsetToWrite)::rep>( |
| 158 | FILE_OFFSET, offsetToWrite.count()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 159 | |
| 160 | // Read it back |
| 161 | microseconds offsetToRead; |
| 162 | offsetToRead = microseconds( |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 163 | utils::readData<decltype(offsetToRead)::rep>(FILE_OFFSET)); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 164 | EXPECT_EQ(offsetToWrite, offsetToRead); |
| 165 | } |
| 166 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 167 | TEST_F(TestHostEpoch, setElapsedInNtpBmc) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 168 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 169 | // Set time in NTP/BMC is not allowed |
| 170 | setTimeMode(Mode::NTP); |
| 171 | setTimeOwner(Owner::BMC); |
| 172 | checkSettingTimeNotAllowed(); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 175 | TEST_F(TestHostEpoch, setElapsedInNtpHost) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 176 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 177 | // Set time in NTP/HOST is not allowed |
| 178 | setTimeMode(Mode::NTP); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 179 | setTimeOwner(Owner::HOST); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 180 | checkSettingTimeNotAllowed(); |
| 181 | } |
| 182 | |
| 183 | TEST_F(TestHostEpoch, setElapsedInNtpSplit) |
| 184 | { |
| 185 | // Set time in NTP/SPLIT, offset will be set |
| 186 | setTimeMode(Mode::NTP); |
| 187 | setTimeOwner(Owner::SPLIT); |
| 188 | |
| 189 | checkSetSplitTimeInFuture(); |
| 190 | |
| 191 | // Reset offset |
| 192 | setOffset(USEC_ZERO); |
| 193 | checkSetSplitTimeInPast(); |
| 194 | } |
| 195 | |
| 196 | TEST_F(TestHostEpoch, setElapsedInNtpBoth) |
| 197 | { |
| 198 | // Set time in NTP/BOTH is not allowed |
| 199 | setTimeMode(Mode::NTP); |
| 200 | setTimeOwner(Owner::BOTH); |
| 201 | checkSettingTimeNotAllowed(); |
| 202 | } |
| 203 | |
| 204 | TEST_F(TestHostEpoch, setElapsedInManualBmc) |
| 205 | { |
| 206 | // Set time in MANUAL/BMC is not allowed |
| 207 | setTimeMode(Mode::MANUAL); |
| 208 | setTimeOwner(Owner::BMC); |
| 209 | checkSettingTimeNotAllowed(); |
| 210 | } |
| 211 | |
| 212 | TEST_F(TestHostEpoch, setElapsedInManualHost) |
| 213 | { |
| 214 | // Set time in MANUAL/HOST, time will be set to BMC |
| 215 | // However it requies gmock to test this case |
| 216 | // TODO: when gmock is ready, test this case. |
| 217 | setTimeMode(Mode::MANUAL); |
| 218 | setTimeOwner(Owner::HOST); |
| 219 | } |
| 220 | |
| 221 | TEST_F(TestHostEpoch, setElapsedInManualSplit) |
| 222 | { |
| 223 | // Set to SPLIT owner so that offset will be set |
| 224 | setTimeMode(Mode::MANUAL); |
| 225 | setTimeOwner(Owner::SPLIT); |
| 226 | |
| 227 | checkSetSplitTimeInFuture(); |
| 228 | |
| 229 | // Reset offset |
| 230 | setOffset(USEC_ZERO); |
| 231 | checkSetSplitTimeInPast(); |
| 232 | } |
| 233 | |
| 234 | TEST_F(TestHostEpoch, setElapsedInManualBoth) |
| 235 | { |
| 236 | // Set time in MANUAL/BOTH, time will be set to BMC |
| 237 | // However it requies gmock to test this case |
| 238 | // TODO: when gmock is ready, test this case. |
| 239 | setTimeMode(Mode::MANUAL); |
| 240 | setTimeOwner(Owner::BOTH); |
| 241 | } |
| 242 | |
| 243 | TEST_F(TestHostEpoch, setElapsedInSplitAndBmcTimeIsChanged) |
| 244 | { |
| 245 | // Set to SPLIT owner so that offset will be set |
| 246 | setTimeOwner(Owner::SPLIT); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 247 | |
| 248 | // Get current time, and set future +1min time |
| 249 | auto t1 = hostEpoch.elapsed(); |
| 250 | EXPECT_NE(0, t1); |
| 251 | microseconds diff = 1min; |
| 252 | auto t2 = t1 + diff.count(); |
| 253 | hostEpoch.elapsed(t2); |
| 254 | |
| 255 | // Verify that the offset shall be positive, |
| 256 | // and less or equal to diff, and shall be not too less. |
| 257 | auto offset = getOffset(); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 258 | EXPECT_GT(offset, USEC_ZERO); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 259 | EXPECT_LE(offset, diff); |
| 260 | diff -= delta; |
| 261 | EXPECT_GE(offset, diff); |
| 262 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 263 | // Now BMC time is changed to future +1min |
| 264 | hostEpoch.onBmcTimeChanged(microseconds(t2)); |
| 265 | |
| 266 | // Verify that the offset shall be around zero since it's almost |
| 267 | // the same as BMC time |
| 268 | offset = getOffset(); |
| 269 | if (offset.count() < 0) |
| 270 | { |
| 271 | offset = microseconds(-offset.count()); |
| 272 | } |
| 273 | EXPECT_LE(offset, delta); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 274 | } |
| 275 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 276 | TEST_F(TestHostEpoch, clearOffsetOnOwnerChange) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 277 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 278 | EXPECT_EQ(USEC_ZERO, getOffset()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 279 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 280 | setTimeOwner(Owner::SPLIT); |
| 281 | hostEpoch.onBmcTimeChanged(microseconds(hostEpoch.elapsed()) + 1min); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 282 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 283 | // Now offset shall be non zero |
| 284 | EXPECT_NE(USEC_ZERO, getOffset()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 285 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 286 | setTimeOwner(Owner::BOTH); |
| 287 | |
| 288 | // Now owner is BOTH, the offset shall be cleared |
| 289 | EXPECT_EQ(USEC_ZERO, getOffset()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | } |
| 293 | } |