1 #include <sdbusplus/bus.hpp> 2 #include <gtest/gtest.h> 3 4 #include "bmc_epoch.hpp" 5 #include "config.h" 6 7 namespace phosphor 8 { 9 namespace time 10 { 11 12 using namespace std::chrono; 13 class TestBmcEpoch : public testing::Test 14 { 15 public: 16 using Mode = EpochBase::Mode; 17 using Owner = EpochBase::Owner; 18 19 sdbusplus::bus::bus bus; 20 BmcEpoch bmcEpoch; 21 22 TestBmcEpoch() 23 : bus(sdbusplus::bus::new_default()), 24 bmcEpoch(bus, OBJPATH_BMC) 25 { 26 // Empty 27 } 28 29 // Proxies for BmcEpoch's private members and functions 30 Mode getTimeMode() 31 { 32 return bmcEpoch.timeMode; 33 } 34 Owner getTimeOwner() 35 { 36 return bmcEpoch.timeOwner; 37 } 38 void setTimeOwner(Owner owner) 39 { 40 bmcEpoch.timeOwner = owner; 41 } 42 void setTimeMode(Mode mode) 43 { 44 bmcEpoch.timeMode = mode; 45 } 46 }; 47 48 TEST_F(TestBmcEpoch, empty) 49 { 50 EXPECT_EQ(Mode::NTP, getTimeMode()); 51 EXPECT_EQ(Owner::BMC, getTimeOwner()); 52 } 53 54 TEST_F(TestBmcEpoch, getElapsed) 55 { 56 auto t1 = bmcEpoch.elapsed(); 57 EXPECT_NE(0, t1); 58 auto t2 = bmcEpoch.elapsed(); 59 EXPECT_GE(t2, t1); 60 } 61 62 TEST_F(TestBmcEpoch, setElapsedNotAllowed) 63 { 64 auto epochNow = duration_cast<microseconds>( 65 system_clock::now().time_since_epoch()).count(); 66 // In NTP mode, setting time is not allowed 67 auto ret = bmcEpoch.elapsed(epochNow); 68 EXPECT_EQ(0, ret); 69 70 // In Host owner, setting time is not allowed 71 setTimeMode(Mode::MANUAL); 72 setTimeOwner(Owner::HOST); 73 ret = bmcEpoch.elapsed(epochNow); 74 EXPECT_EQ(0, ret); 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 } 85 } 86