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