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