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