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