summaryrefslogtreecommitdiffstats
path: root/test/TestHostEpoch.cpp
blob: 7ddf92fc7540dcecc9f9228b496a2d9f9a4327ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "config.h"

#include "host_epoch.hpp"
#include "types.hpp"
#include "utils.hpp"

#include <sdbusplus/bus.hpp>
#include <xyz/openbmc_project/Time/error.hpp>

#include <gtest/gtest.h>

namespace phosphor
{
namespace time
{

using namespace std::chrono;
using namespace std::chrono_literals;
using NotAllowed = sdbusplus::xyz::openbmc_project::Time::Error::NotAllowed;

const constexpr microseconds USEC_ZERO{0};

class TestHostEpoch : public testing::Test
{
  public:
    sdbusplus::bus::bus bus;
    HostEpoch hostEpoch;

    static constexpr auto FILE_NOT_EXIST = "path/to/file-not-exist";
    static constexpr auto FILE_OFFSET = "saved_host_offset";
    const microseconds delta = 2s;

    TestHostEpoch() :
        bus(sdbusplus::bus::new_default()), hostEpoch(bus, OBJPATH_HOST)
    {
        // Make sure the file does not exist
        std::remove(FILE_NOT_EXIST);
    }
    ~TestHostEpoch()
    {
        // Cleanup test file
        std::remove(FILE_OFFSET);
    }

    // Proxies for HostEpoch's private members and functions
    Mode getTimeMode()
    {
        return hostEpoch.timeMode;
    }
    Owner getTimeOwner()
    {
        return hostEpoch.timeOwner;
    }
    microseconds getOffset()
    {
        return hostEpoch.offset;
    }
    void setOffset(microseconds us)
    {
        hostEpoch.offset = us;
    }
    void setTimeOwner(Owner owner)
    {
        hostEpoch.onOwnerChanged(owner);
    }
    void setTimeMode(Mode mode)
    {
        hostEpoch.onModeChanged(mode);
    }

    void checkSettingTimeNotAllowed()
    {
        // By default offset shall be 0
        EXPECT_EQ(0, getOffset().count());

        // Set time is not allowed,
        // so verify offset is still 0 after set time
        microseconds diff = 1min;
        EXPECT_THROW(hostEpoch.elapsed(hostEpoch.elapsed() + diff.count()),
                     NotAllowed);
        EXPECT_EQ(0, getOffset().count());
    }

    void checkSetSplitTimeInFuture()
    {
        // Get current time, and set future +1min time
        auto t1 = hostEpoch.elapsed();
        EXPECT_NE(0, t1);
        microseconds diff = 1min;
        auto t2 = t1 + diff.count();
        hostEpoch.elapsed(t2);

        // Verify that the offset shall be positive,
        // and less or equal to diff, and shall be not too less.
        auto offset = getOffset();
        EXPECT_GT(offset, USEC_ZERO);
        EXPECT_LE(offset, diff);
        diff -= delta;
        EXPECT_GE(offset, diff);

        // Now get time shall be around future +1min time
        auto epochNow =
            duration_cast<microseconds>(system_clock::now().time_since_epoch())
                .count();
        auto elapsedGot = hostEpoch.elapsed();
        EXPECT_LT(epochNow, elapsedGot);
        auto epochDiff = elapsedGot - epochNow;
        diff = 1min;
        EXPECT_GT(epochDiff, (diff - delta).count());
        EXPECT_LT(epochDiff, (diff + delta).count());
    }
    void checkSetSplitTimeInPast()
    {
        // Get current time, and set past -1min time
        auto t1 = hostEpoch.elapsed();
        EXPECT_NE(0, t1);
        microseconds diff = 1min;
        auto t2 = t1 - diff.count();
        hostEpoch.elapsed(t2);

        // Verify that the offset shall be negative, and the absolute value
        // shall be equal or greater than diff, and shall not be too greater
        auto offset = getOffset();
        EXPECT_LT(offset, USEC_ZERO);
        offset = -offset;
        EXPECT_GE(offset, diff);
        diff += 10s;
        EXPECT_LE(offset, diff);

        // Now get time shall be around past -1min time
        auto epochNow =
            duration_cast<microseconds>(system_clock::now().time_since_epoch())
                .count();
        auto elapsedGot = hostEpoch.elapsed();
        EXPECT_LT(elapsedGot, epochNow);
        auto epochDiff = epochNow - elapsedGot;
        diff = 1min;
        EXPECT_GT(epochDiff, (diff - delta).count());
        EXPECT_LT(epochDiff, (diff + delta).count());
    }
};

TEST_F(TestHostEpoch, empty)
{
    // Default mode/owner is MANUAL/BOTH
    EXPECT_EQ(Mode::Manual, getTimeMode());
    EXPECT_EQ(Owner::Both, getTimeOwner());
}

TEST_F(TestHostEpoch, readDataFileNotExist)
{
    // When file does not exist, the default offset shall be 0
    microseconds offset(0);
    auto value = utils::readData<decltype(offset)::rep>(FILE_NOT_EXIST);
    EXPECT_EQ(0, value);
}

TEST_F(TestHostEpoch, writeAndReadData)
{
    // Write offset to file
    microseconds offsetToWrite(1234567);
    utils::writeData<decltype(offsetToWrite)::rep>(FILE_OFFSET,
                                                   offsetToWrite.count());

    // Read it back
    microseconds offsetToRead;
    offsetToRead =
        microseconds(utils::readData<decltype(offsetToRead)::rep>(FILE_OFFSET));
    EXPECT_EQ(offsetToWrite, offsetToRead);
}

TEST_F(TestHostEpoch, setElapsedInNtpBmc)
{
    // Set time in NTP/BMC is not allowed
    setTimeMode(Mode::NTP);
    setTimeOwner(Owner::BMC);
    checkSettingTimeNotAllowed();
}

TEST_F(TestHostEpoch, setElapsedInNtpHost)
{
    // Set time in NTP/HOST is not allowed
    setTimeMode(Mode::NTP);
    setTimeOwner(Owner::Host);
    checkSettingTimeNotAllowed();
}

TEST_F(TestHostEpoch, setElapsedInNtpSplit)
{
    // Set time in NTP/SPLIT, offset will be set
    setTimeMode(Mode::NTP);
    setTimeOwner(Owner::Split);

    checkSetSplitTimeInFuture();

    // Reset offset
    setOffset(USEC_ZERO);
    checkSetSplitTimeInPast();
}

TEST_F(TestHostEpoch, setElapsedInNtpBoth)
{
    // Set time in NTP/BOTH is not allowed
    setTimeMode(Mode::NTP);
    setTimeOwner(Owner::Both);
    checkSettingTimeNotAllowed();
}

TEST_F(TestHostEpoch, setElapsedInManualBmc)
{
    // Set time in MANUAL/BMC is not allowed
    setTimeMode(Mode::Manual);
    setTimeOwner(Owner::BMC);
    checkSettingTimeNotAllowed();
}

TEST_F(TestHostEpoch, setElapsedInManualHost)
{
    // Set time in MANUAL/HOST, time will be set to BMC
    // However it requies gmock to test this case
    // TODO: when gmock is ready, test this case.
    setTimeMode(Mode::Manual);
    setTimeOwner(Owner::Host);
}

TEST_F(TestHostEpoch, setElapsedInManualSplit)
{
    // Set to SPLIT owner so that offset will be set
    setTimeMode(Mode::Manual);
    setTimeOwner(Owner::Split);

    checkSetSplitTimeInFuture();

    // Reset offset
    setOffset(USEC_ZERO);
    checkSetSplitTimeInPast();
}

TEST_F(TestHostEpoch, setElapsedInManualBoth)
{
    // Set time in MANUAL/BOTH, time will be set to BMC
    // However it requies gmock to test this case
    // TODO: when gmock is ready, test this case.
    setTimeMode(Mode::Manual);
    setTimeOwner(Owner::Both);
}

TEST_F(TestHostEpoch, setElapsedInSplitAndBmcTimeIsChanged)
{
    // Set to SPLIT owner so that offset will be set
    setTimeOwner(Owner::Split);

    // Get current time, and set future +1min time
    auto t1 = hostEpoch.elapsed();
    EXPECT_NE(0, t1);
    microseconds diff = 1min;
    auto t2 = t1 + diff.count();
    hostEpoch.elapsed(t2);

    // Verify that the offset shall be positive,
    // and less or equal to diff, and shall be not too less.
    auto offset = getOffset();
    EXPECT_GT(offset, USEC_ZERO);
    EXPECT_LE(offset, diff);
    diff -= delta;
    EXPECT_GE(offset, diff);

    // Now BMC time is changed to future +1min
    hostEpoch.onBmcTimeChanged(microseconds(t2));

    // Verify that the offset shall be around zero since it's almost
    // the same as BMC time
    offset = getOffset();
    if (offset.count() < 0)
    {
        offset = microseconds(-offset.count());
    }
    EXPECT_LE(offset, delta);
}

TEST_F(TestHostEpoch, clearOffsetOnOwnerChange)
{
    EXPECT_EQ(USEC_ZERO, getOffset());

    setTimeOwner(Owner::Split);
    hostEpoch.onBmcTimeChanged(microseconds(hostEpoch.elapsed()) + 1min);

    // Now offset shall be non zero
    EXPECT_NE(USEC_ZERO, getOffset());

    setTimeOwner(Owner::Both);

    // Now owner is BOTH, the offset shall be cleared
    EXPECT_EQ(USEC_ZERO, getOffset());
}

} // namespace time
} // namespace phosphor
OpenPOWER on IntegriCloud