xref: /openbmc/phosphor-time-manager/test/TestBmcEpoch.cpp (revision 06df65486f1c86276cc5da6812673bbb7642e1b5)
196232827SLei YU #include "config.h"
296232827SLei YU 
3ab4cc6a5SGunnar Mills #include "bmc_epoch.hpp"
4cb421097SGeorge Liu #include "manager.hpp"
5ab4cc6a5SGunnar Mills #include "types.hpp"
6ab4cc6a5SGunnar Mills 
733752c75SLei YU #include <sdbusplus/bus.hpp>
833752c75SLei YU 
9ab4cc6a5SGunnar Mills #include <gtest/gtest.h>
10ab4cc6a5SGunnar Mills 
1196232827SLei YU namespace phosphor
1296232827SLei YU {
1396232827SLei YU namespace time
1496232827SLei YU {
1596232827SLei YU 
1696232827SLei YU class TestBmcEpoch : public testing::Test
1796232827SLei YU {
1896232827SLei YU   public:
1938679266SPatrick Williams     sdbusplus::bus_t bus;
20cb421097SGeorge Liu     Manager manager;
218af2a1e3SPavithra Barithaya     sd_event* event = nullptr;
227b218796SLei YU     std::unique_ptr<BmcEpoch> bmcEpoch;
2396232827SLei YU 
TestBmcEpoch()24cb421097SGeorge Liu     TestBmcEpoch() : bus(sdbusplus::bus::new_default()), manager(bus)
2596232827SLei YU     {
267b218796SLei YU         // BmcEpoch requires sd_event to init
277b218796SLei YU         sd_event_default(&event);
287b218796SLei YU         bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
29864e173eSPavithra Barithaya         bmcEpoch = std::make_unique<BmcEpoch>(bus, objpathBmc, manager);
307b218796SLei YU     }
317b218796SLei YU 
~TestBmcEpoch()32f93c405fSPavithra Barithaya     ~TestBmcEpoch() override
337b218796SLei YU     {
347b218796SLei YU         bus.detach_event();
357b218796SLei YU         sd_event_unref(event);
3696232827SLei YU     }
37*06df6548SPavithra Barithaya     TestBmcEpoch(const TestBmcEpoch&) = delete;
38*06df6548SPavithra Barithaya     TestBmcEpoch(TestBmcEpoch&&) = delete;
39*06df6548SPavithra Barithaya     TestBmcEpoch& operator=(const TestBmcEpoch&) = delete;
40*06df6548SPavithra Barithaya     TestBmcEpoch& operator=(TestBmcEpoch&&) = delete;
4196232827SLei YU };
4296232827SLei YU 
TEST_F(TestBmcEpoch,onModeChange)430a70452aSGeorge Liu TEST_F(TestBmcEpoch, onModeChange)
440a70452aSGeorge Liu {
450a70452aSGeorge Liu     bmcEpoch->onModeChanged(Mode::NTP);
46cb421097SGeorge Liu     EXPECT_EQ(Mode::NTP, manager.getTimeMode());
470a70452aSGeorge Liu 
480a70452aSGeorge Liu     bmcEpoch->onModeChanged(Mode::Manual);
49cb421097SGeorge Liu     EXPECT_EQ(Mode::Manual, manager.getTimeMode());
500a70452aSGeorge Liu }
510a70452aSGeorge Liu 
TEST_F(TestBmcEpoch,empty)5296232827SLei YU TEST_F(TestBmcEpoch, empty)
5396232827SLei YU {
543c2f4496SGeorge Liu     // Default mode is MANUAL
55cb421097SGeorge Liu     EXPECT_EQ(Mode::Manual, manager.getTimeMode());
5696232827SLei YU }
5796232827SLei YU 
TEST_F(TestBmcEpoch,getElapsed)5896232827SLei YU TEST_F(TestBmcEpoch, getElapsed)
5996232827SLei YU {
607b218796SLei YU     auto t1 = bmcEpoch->elapsed();
6196232827SLei YU     EXPECT_NE(0, t1);
627b218796SLei YU     auto t2 = bmcEpoch->elapsed();
6396232827SLei YU     EXPECT_GE(t2, t1);
6496232827SLei YU }
6596232827SLei YU 
TEST_F(TestBmcEpoch,setElapsedOK)66e7abcdc7SLei YU TEST_F(TestBmcEpoch, setElapsedOK)
67e7abcdc7SLei YU {
68e7abcdc7SLei YU     // TODO: setting time will call sd-bus functions and it will fail on host
69e7abcdc7SLei YU     // if we have gmock for sdbusplus::bus, we can test setElapsed.
70e7abcdc7SLei YU     // But for now we can not test it
71e7abcdc7SLei YU }
7296232827SLei YU 
73ab4cc6a5SGunnar Mills } // namespace time
74ab4cc6a5SGunnar Mills } // namespace phosphor
75