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 | { |
| 140 | EXPECT_EQ(Mode::NTP, getTimeMode()); |
| 141 | EXPECT_EQ(Owner::BMC, getTimeOwner()); |
| 142 | } |
| 143 | |
| 144 | TEST_F(TestHostEpoch, readDataFileNotExist) |
| 145 | { |
| 146 | // When file does not exist, the default offset shall be 0 |
| 147 | microseconds offset(0); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 148 | auto value = utils::readData<decltype(offset)::rep>(FILE_NOT_EXIST); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 149 | EXPECT_EQ(0, value); |
| 150 | } |
| 151 | |
| 152 | TEST_F(TestHostEpoch, writeAndReadData) |
| 153 | { |
| 154 | // Write offset to file |
| 155 | microseconds offsetToWrite(1234567); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 156 | utils::writeData<decltype(offsetToWrite)::rep>( |
| 157 | FILE_OFFSET, offsetToWrite.count()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 158 | |
| 159 | // Read it back |
| 160 | microseconds offsetToRead; |
| 161 | offsetToRead = microseconds( |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 162 | utils::readData<decltype(offsetToRead)::rep>(FILE_OFFSET)); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 163 | EXPECT_EQ(offsetToWrite, offsetToRead); |
| 164 | } |
| 165 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 166 | TEST_F(TestHostEpoch, setElapsedInNtpBmc) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 167 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 168 | // Set time in NTP/BMC is not allowed |
| 169 | setTimeMode(Mode::NTP); |
| 170 | setTimeOwner(Owner::BMC); |
| 171 | checkSettingTimeNotAllowed(); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 174 | TEST_F(TestHostEpoch, setElapsedInNtpHost) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 175 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 176 | // Set time in NTP/HOST is not allowed |
| 177 | setTimeMode(Mode::NTP); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 178 | setTimeOwner(Owner::HOST); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 179 | checkSettingTimeNotAllowed(); |
| 180 | } |
| 181 | |
| 182 | TEST_F(TestHostEpoch, setElapsedInNtpSplit) |
| 183 | { |
| 184 | // Set time in NTP/SPLIT, offset will be set |
| 185 | setTimeMode(Mode::NTP); |
| 186 | setTimeOwner(Owner::SPLIT); |
| 187 | |
| 188 | checkSetSplitTimeInFuture(); |
| 189 | |
| 190 | // Reset offset |
| 191 | setOffset(USEC_ZERO); |
| 192 | checkSetSplitTimeInPast(); |
| 193 | } |
| 194 | |
| 195 | TEST_F(TestHostEpoch, setElapsedInNtpBoth) |
| 196 | { |
| 197 | // Set time in NTP/BOTH is not allowed |
| 198 | setTimeMode(Mode::NTP); |
| 199 | setTimeOwner(Owner::BOTH); |
| 200 | checkSettingTimeNotAllowed(); |
| 201 | } |
| 202 | |
| 203 | TEST_F(TestHostEpoch, setElapsedInManualBmc) |
| 204 | { |
| 205 | // Set time in MANUAL/BMC is not allowed |
| 206 | setTimeMode(Mode::MANUAL); |
| 207 | setTimeOwner(Owner::BMC); |
| 208 | checkSettingTimeNotAllowed(); |
| 209 | } |
| 210 | |
| 211 | TEST_F(TestHostEpoch, setElapsedInManualHost) |
| 212 | { |
| 213 | // Set time in MANUAL/HOST, time will be set to BMC |
| 214 | // However it requies gmock to test this case |
| 215 | // TODO: when gmock is ready, test this case. |
| 216 | setTimeMode(Mode::MANUAL); |
| 217 | setTimeOwner(Owner::HOST); |
| 218 | } |
| 219 | |
| 220 | TEST_F(TestHostEpoch, setElapsedInManualSplit) |
| 221 | { |
| 222 | // Set to SPLIT owner so that offset will be set |
| 223 | setTimeMode(Mode::MANUAL); |
| 224 | setTimeOwner(Owner::SPLIT); |
| 225 | |
| 226 | checkSetSplitTimeInFuture(); |
| 227 | |
| 228 | // Reset offset |
| 229 | setOffset(USEC_ZERO); |
| 230 | checkSetSplitTimeInPast(); |
| 231 | } |
| 232 | |
| 233 | TEST_F(TestHostEpoch, setElapsedInManualBoth) |
| 234 | { |
| 235 | // Set time in MANUAL/BOTH, time will be set to BMC |
| 236 | // However it requies gmock to test this case |
| 237 | // TODO: when gmock is ready, test this case. |
| 238 | setTimeMode(Mode::MANUAL); |
| 239 | setTimeOwner(Owner::BOTH); |
| 240 | } |
| 241 | |
| 242 | TEST_F(TestHostEpoch, setElapsedInSplitAndBmcTimeIsChanged) |
| 243 | { |
| 244 | // Set to SPLIT owner so that offset will be set |
| 245 | setTimeOwner(Owner::SPLIT); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 246 | |
| 247 | // Get current time, and set future +1min time |
| 248 | auto t1 = hostEpoch.elapsed(); |
| 249 | EXPECT_NE(0, t1); |
| 250 | microseconds diff = 1min; |
| 251 | auto t2 = t1 + diff.count(); |
| 252 | hostEpoch.elapsed(t2); |
| 253 | |
| 254 | // Verify that the offset shall be positive, |
| 255 | // and less or equal to diff, and shall be not too less. |
| 256 | auto offset = getOffset(); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 257 | EXPECT_GT(offset, USEC_ZERO); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 258 | EXPECT_LE(offset, diff); |
| 259 | diff -= delta; |
| 260 | EXPECT_GE(offset, diff); |
| 261 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 262 | // Now BMC time is changed to future +1min |
| 263 | hostEpoch.onBmcTimeChanged(microseconds(t2)); |
| 264 | |
| 265 | // Verify that the offset shall be around zero since it's almost |
| 266 | // the same as BMC time |
| 267 | offset = getOffset(); |
| 268 | if (offset.count() < 0) |
| 269 | { |
| 270 | offset = microseconds(-offset.count()); |
| 271 | } |
| 272 | EXPECT_LE(offset, delta); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 275 | TEST_F(TestHostEpoch, clearOffsetOnOwnerChange) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 276 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 277 | EXPECT_EQ(USEC_ZERO, getOffset()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 278 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 279 | setTimeOwner(Owner::SPLIT); |
| 280 | hostEpoch.onBmcTimeChanged(microseconds(hostEpoch.elapsed()) + 1min); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 281 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 282 | // Now offset shall be non zero |
| 283 | EXPECT_NE(USEC_ZERO, getOffset()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 284 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame^] | 285 | setTimeOwner(Owner::BOTH); |
| 286 | |
| 287 | // Now owner is BOTH, the offset shall be cleared |
| 288 | EXPECT_EQ(USEC_ZERO, getOffset()); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | } |
| 292 | } |