xref: /openbmc/phosphor-time-manager/test/TestBmcEpoch.cpp (revision 81194a82facd5d5b94808ce5b58cafdbffa68642)
1 #include "config.h"
2 
3 #include "bmc_epoch.hpp"
4 #include "types.hpp"
5 
6 #include <sdbusplus/bus.hpp>
7 
8 #include <gtest/gtest.h>
9 
10 namespace phosphor
11 {
12 namespace time
13 {
14 
15 class TestBmcEpoch : public testing::Test
16 {
17   public:
18     sdbusplus::bus_t bus;
19     sd_event* event;
20     std::unique_ptr<BmcEpoch> bmcEpoch;
21 
22     TestBmcEpoch() : bus(sdbusplus::bus::new_default())
23     {
24         // BmcEpoch requires sd_event to init
25         sd_event_default(&event);
26         bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
27         bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC);
28     }
29 
30     ~TestBmcEpoch()
31     {
32         bus.detach_event();
33         sd_event_unref(event);
34     }
35 
36     // Proxies for BmcEpoch's private members and functions
37     Mode getTimeMode()
38     {
39         return bmcEpoch->timeMode;
40     }
41     void setTimeMode(Mode mode)
42     {
43         bmcEpoch->timeMode = mode;
44     }
45 };
46 
47 TEST_F(TestBmcEpoch, onModeChange)
48 {
49     bmcEpoch->onModeChanged(Mode::NTP);
50     EXPECT_EQ(Mode::NTP, getTimeMode());
51 
52     bmcEpoch->onModeChanged(Mode::Manual);
53     EXPECT_EQ(Mode::Manual, getTimeMode());
54 }
55 
56 TEST_F(TestBmcEpoch, empty)
57 {
58     // Default mode is MANUAL
59     EXPECT_EQ(Mode::Manual, getTimeMode());
60 }
61 
62 TEST_F(TestBmcEpoch, getElapsed)
63 {
64     auto t1 = bmcEpoch->elapsed();
65     EXPECT_NE(0, t1);
66     auto t2 = bmcEpoch->elapsed();
67     EXPECT_GE(t2, t1);
68 }
69 
70 TEST_F(TestBmcEpoch, setElapsedOK)
71 {
72     // TODO: setting time will call sd-bus functions and it will fail on host
73     // if we have gmock for sdbusplus::bus, we can test setElapsed.
74     // But for now we can not test it
75 }
76 
77 } // namespace time
78 } // namespace phosphor
79