blob: da65b3e908bcebac503c2f9a83763dceb68b8d77 [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#include <sdbusplus/bus.hpp>
2#include <gtest/gtest.h>
3
4#include "bmc_epoch.hpp"
5#include "config.h"
6
7namespace phosphor
8{
9namespace time
10{
11
Lei YUe7abcdc2017-01-16 15:05:24 +080012using namespace std::chrono;
Lei YU96232822017-01-20 14:05:46 +080013class TestBmcEpoch : public testing::Test
14{
15 public:
16 using Mode = EpochBase::Mode;
17 using Owner = EpochBase::Owner;
18
19 sdbusplus::bus::bus bus;
20 BmcEpoch bmcEpoch;
21
22 TestBmcEpoch()
23 : bus(sdbusplus::bus::new_default()),
24 bmcEpoch(bus, OBJPATH_BMC)
25 {
26 // Empty
27 }
28
29 // Proxies for BmcEpoch's private members and functions
30 Mode getTimeMode()
31 {
32 return bmcEpoch.timeMode;
33 }
34 Owner getTimeOwner()
35 {
36 return bmcEpoch.timeOwner;
37 }
Lei YUe7abcdc2017-01-16 15:05:24 +080038 void setTimeOwner(Owner owner)
39 {
40 bmcEpoch.timeOwner = owner;
41 }
42 void setTimeMode(Mode mode)
43 {
44 bmcEpoch.timeMode = mode;
45 }
Lei YU96232822017-01-20 14:05:46 +080046};
47
48TEST_F(TestBmcEpoch, empty)
49{
50 EXPECT_EQ(Mode::NTP, getTimeMode());
51 EXPECT_EQ(Owner::BMC, getTimeOwner());
52}
53
54TEST_F(TestBmcEpoch, getElapsed)
55{
56 auto t1 = bmcEpoch.elapsed();
57 EXPECT_NE(0, t1);
58 auto t2 = bmcEpoch.elapsed();
59 EXPECT_GE(t2, t1);
60}
61
Lei YUe7abcdc2017-01-16 15:05:24 +080062TEST_F(TestBmcEpoch, setElapsedNotAllowed)
63{
64 auto epochNow = duration_cast<microseconds>(
65 system_clock::now().time_since_epoch()).count();
66 // In NTP mode, setting time is not allowed
67 auto ret = bmcEpoch.elapsed(epochNow);
68 EXPECT_EQ(0, ret);
69
70 // In Host owner, setting time is not allowed
71 setTimeMode(Mode::MANUAL);
72 setTimeOwner(Owner::HOST);
73 ret = bmcEpoch.elapsed(epochNow);
74 EXPECT_EQ(0, ret);
75}
76
77TEST_F(TestBmcEpoch, setElapsedOK)
78{
79 // TODO: setting time will call sd-bus functions and it will fail on host
80 // if we have gmock for sdbusplus::bus, we can test setElapsed.
81 // But for now we can not test it
82}
Lei YU96232822017-01-20 14:05:46 +080083
84}
85}