1 #include "config.h" 2 3 #include "bmc_epoch.hpp" 4 #include "types.hpp" 5 6 #include <memory> 7 #include <sdbusplus/bus.hpp> 8 9 #include <gtest/gtest.h> 10 11 namespace phosphor 12 { 13 namespace time 14 { 15 16 using namespace std::chrono; 17 18 class TestBmcEpoch : public testing::Test 19 { 20 public: 21 sdbusplus::bus::bus bus; 22 sd_event* event; 23 std::unique_ptr<BmcEpoch> bmcEpoch; 24 25 TestBmcEpoch() : bus(sdbusplus::bus::new_default()) 26 { 27 // BmcEpoch requires sd_event to init 28 sd_event_default(&event); 29 bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL); 30 bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC); 31 } 32 33 ~TestBmcEpoch() 34 { 35 bus.detach_event(); 36 sd_event_unref(event); 37 } 38 39 // Proxies for BmcEpoch's private members and functions 40 Mode getTimeMode() 41 { 42 return bmcEpoch->timeMode; 43 } 44 void setTimeMode(Mode mode) 45 { 46 bmcEpoch->timeMode = mode; 47 } 48 void triggerTimeChange() 49 { 50 bmcEpoch->onTimeChange(nullptr, -1, 0, bmcEpoch.get()); 51 } 52 }; 53 54 TEST_F(TestBmcEpoch, onModeChange) 55 { 56 bmcEpoch->onModeChanged(Mode::NTP); 57 EXPECT_EQ(Mode::NTP, getTimeMode()); 58 59 bmcEpoch->onModeChanged(Mode::Manual); 60 EXPECT_EQ(Mode::Manual, getTimeMode()); 61 } 62 63 TEST_F(TestBmcEpoch, empty) 64 { 65 // Default mode is MANUAL 66 EXPECT_EQ(Mode::Manual, getTimeMode()); 67 } 68 69 TEST_F(TestBmcEpoch, getElapsed) 70 { 71 auto t1 = bmcEpoch->elapsed(); 72 EXPECT_NE(0, t1); 73 auto t2 = bmcEpoch->elapsed(); 74 EXPECT_GE(t2, t1); 75 } 76 77 TEST_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 } 83 84 } // namespace time 85 } // namespace phosphor 86