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