1 #include <sdbusplus/bus.hpp> 2 #include <gtest/gtest.h> 3 #include <memory> 4 5 #include "bmc_epoch.hpp" 6 #include "config.h" 7 #include "types.hpp" 8 #include "mocked_bmc_time_change_listener.hpp" 9 10 namespace phosphor 11 { 12 namespace time 13 { 14 15 using ::testing::_; 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 MockBmcTimeChangeListener listener; 24 std::unique_ptr<BmcEpoch> bmcEpoch; 25 26 TestBmcEpoch() 27 : bus(sdbusplus::bus::new_default()) 28 { 29 // BmcEpoch requires sd_event to init 30 sd_event_default(&event); 31 bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL); 32 bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC); 33 bmcEpoch->setBmcTimeChangeListener(&listener); 34 } 35 36 ~TestBmcEpoch() 37 { 38 bus.detach_event(); 39 sd_event_unref(event); 40 } 41 42 // Proxies for BmcEpoch's private members and functions 43 Mode getTimeMode() 44 { 45 return bmcEpoch->timeMode; 46 } 47 Owner getTimeOwner() 48 { 49 return bmcEpoch->timeOwner; 50 } 51 void setTimeOwner(Owner owner) 52 { 53 bmcEpoch->timeOwner = owner; 54 } 55 void setTimeMode(Mode mode) 56 { 57 bmcEpoch->timeMode = mode; 58 } 59 void triggerTimeChange() 60 { 61 bmcEpoch->onTimeChange(nullptr, 62 -1, 63 0, 64 bmcEpoch.get()); 65 } 66 }; 67 68 TEST_F(TestBmcEpoch, empty) 69 { 70 // Default mode/owner is MANUAL/BOTH 71 EXPECT_EQ(Mode::Manual, getTimeMode()); 72 EXPECT_EQ(Owner::Both, getTimeOwner()); 73 } 74 75 TEST_F(TestBmcEpoch, getElapsed) 76 { 77 auto t1 = bmcEpoch->elapsed(); 78 EXPECT_NE(0, t1); 79 auto t2 = bmcEpoch->elapsed(); 80 EXPECT_GE(t2, t1); 81 } 82 83 TEST_F(TestBmcEpoch, setElapsedNotAllowed) 84 { 85 auto epochNow = duration_cast<microseconds>( 86 system_clock::now().time_since_epoch()).count(); 87 88 // In Host owner, setting time is not allowed 89 setTimeMode(Mode::Manual); 90 setTimeOwner(Owner::Host); 91 auto ret = bmcEpoch->elapsed(epochNow); 92 EXPECT_EQ(0, ret); 93 } 94 95 TEST_F(TestBmcEpoch, setElapsedOK) 96 { 97 // TODO: setting time will call sd-bus functions and it will fail on host 98 // if we have gmock for sdbusplus::bus, we can test setElapsed. 99 // But for now we can not test it 100 } 101 102 TEST_F(TestBmcEpoch, onTimeChange) 103 { 104 // On BMC time change, the listner is expected to be notified 105 EXPECT_CALL(listener, onBmcTimeChanged(_)).Times(1); 106 triggerTimeChange(); 107 } 108 109 } 110 } 111