blob: aee19f9e205675ab5525de51aa8ef345c200e7ba [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#include "config.h"
2
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05003#include "bmc_epoch.hpp"
George Liucb421092022-08-16 17:02:31 +08004#include "manager.hpp"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05005#include "types.hpp"
6
Lei YU33752c72018-06-07 17:06:58 +08007#include <sdbusplus/bus.hpp>
Lei YU33752c72018-06-07 17:06:58 +08008
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05009#include <gtest/gtest.h>
10
Lei YU96232822017-01-20 14:05:46 +080011namespace phosphor
12{
13namespace time
14{
15
16class TestBmcEpoch : public testing::Test
17{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050018 public:
Patrick Williams38679262022-07-22 19:26:55 -050019 sdbusplus::bus_t bus;
George Liucb421092022-08-16 17:02:31 +080020 Manager manager;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050021 sd_event* event;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050022 std::unique_ptr<BmcEpoch> bmcEpoch;
Lei YU96232822017-01-20 14:05:46 +080023
George Liucb421092022-08-16 17:02:31 +080024 TestBmcEpoch() : bus(sdbusplus::bus::new_default()), manager(bus)
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050025 {
26 // BmcEpoch requires sd_event to init
27 sd_event_default(&event);
28 bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
George Liucb421092022-08-16 17:02:31 +080029 bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC, manager);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050030 }
Lei YU7b218792017-02-09 12:10:13 +080031
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050032 ~TestBmcEpoch()
33 {
34 bus.detach_event();
35 sd_event_unref(event);
36 }
Lei YU96232822017-01-20 14:05:46 +080037};
38
George Liu0a704522020-04-13 14:51:40 +080039TEST_F(TestBmcEpoch, onModeChange)
40{
41 bmcEpoch->onModeChanged(Mode::NTP);
George Liucb421092022-08-16 17:02:31 +080042 EXPECT_EQ(Mode::NTP, manager.getTimeMode());
George Liu0a704522020-04-13 14:51:40 +080043
44 bmcEpoch->onModeChanged(Mode::Manual);
George Liucb421092022-08-16 17:02:31 +080045 EXPECT_EQ(Mode::Manual, manager.getTimeMode());
George Liu0a704522020-04-13 14:51:40 +080046}
47
Lei YU96232822017-01-20 14:05:46 +080048TEST_F(TestBmcEpoch, empty)
49{
George Liu3c2f4492020-04-12 11:35:57 +080050 // Default mode is MANUAL
George Liucb421092022-08-16 17:02:31 +080051 EXPECT_EQ(Mode::Manual, manager.getTimeMode());
Lei YU96232822017-01-20 14:05:46 +080052}
53
54TEST_F(TestBmcEpoch, getElapsed)
55{
Lei YU7b218792017-02-09 12:10:13 +080056 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080057 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080058 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080059 EXPECT_GE(t2, t1);
60}
61
Lei YUe7abcdc2017-01-16 15:05:24 +080062TEST_F(TestBmcEpoch, setElapsedOK)
63{
64 // TODO: setting time will call sd-bus functions and it will fail on host
65 // if we have gmock for sdbusplus::bus, we can test setElapsed.
66 // But for now we can not test it
67}
Lei YU96232822017-01-20 14:05:46 +080068
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050069} // namespace time
70} // namespace phosphor