blob: e0b81ea09b0d93a920697bdb682fd4d2182ef9c9 [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;
Pavithra Barithaya8af2a1e2023-04-27 00:46:35 -050021 sd_event* event = nullptr;
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);
Pavithra Barithaya864e1732023-04-11 04:30:23 -050029 bmcEpoch = std::make_unique<BmcEpoch>(bus, objpathBmc, manager);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050030 }
Lei YU7b218792017-02-09 12:10:13 +080031
Pavithra Barithayaf93c4052023-04-26 23:28:13 -050032 ~TestBmcEpoch() override
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050033 {
34 bus.detach_event();
35 sd_event_unref(event);
36 }
Pavithra Barithaya06df6542023-04-28 01:18:31 -050037 TestBmcEpoch(const TestBmcEpoch&) = delete;
38 TestBmcEpoch(TestBmcEpoch&&) = delete;
39 TestBmcEpoch& operator=(const TestBmcEpoch&) = delete;
40 TestBmcEpoch& operator=(TestBmcEpoch&&) = delete;
Lei YU96232822017-01-20 14:05:46 +080041};
42
George Liu0a704522020-04-13 14:51:40 +080043TEST_F(TestBmcEpoch, onModeChange)
44{
45 bmcEpoch->onModeChanged(Mode::NTP);
George Liucb421092022-08-16 17:02:31 +080046 EXPECT_EQ(Mode::NTP, manager.getTimeMode());
George Liu0a704522020-04-13 14:51:40 +080047
48 bmcEpoch->onModeChanged(Mode::Manual);
George Liucb421092022-08-16 17:02:31 +080049 EXPECT_EQ(Mode::Manual, manager.getTimeMode());
George Liu0a704522020-04-13 14:51:40 +080050}
51
Lei YU96232822017-01-20 14:05:46 +080052TEST_F(TestBmcEpoch, empty)
53{
George Liu3c2f4492020-04-12 11:35:57 +080054 // Default mode is MANUAL
George Liucb421092022-08-16 17:02:31 +080055 EXPECT_EQ(Mode::Manual, manager.getTimeMode());
Lei YU96232822017-01-20 14:05:46 +080056}
57
58TEST_F(TestBmcEpoch, getElapsed)
59{
Lei YU7b218792017-02-09 12:10:13 +080060 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080061 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080062 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080063 EXPECT_GE(t2, t1);
64}
65
Lei YUe7abcdc2017-01-16 15:05:24 +080066TEST_F(TestBmcEpoch, setElapsedOK)
67{
68 // TODO: setting time will call sd-bus functions and it will fail on host
69 // if we have gmock for sdbusplus::bus, we can test setElapsed.
70 // But for now we can not test it
71}
Lei YU96232822017-01-20 14:05:46 +080072
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050073} // namespace time
74} // namespace phosphor