blob: a5fdb33c09e81664478b1ce299793359cd9f5542 [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"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05004#include "types.hpp"
5
Lei YU33752c72018-06-07 17:06:58 +08006#include <sdbusplus/bus.hpp>
Lei YU33752c72018-06-07 17:06:58 +08007
George Liuc6d33972020-06-22 10:35:29 +08008#include <memory>
9
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050010#include <gtest/gtest.h>
11
Lei YU96232822017-01-20 14:05:46 +080012namespace phosphor
13{
14namespace time
15{
16
Lei YUe7abcdc2017-01-16 15:05:24 +080017using namespace std::chrono;
Lei YU7b218792017-02-09 12:10:13 +080018
Lei YU96232822017-01-20 14:05:46 +080019class TestBmcEpoch : public testing::Test
20{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050021 public:
Patrick Williams38679262022-07-22 19:26:55 -050022 sdbusplus::bus_t bus;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050023 sd_event* event;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050024 std::unique_ptr<BmcEpoch> bmcEpoch;
Lei YU96232822017-01-20 14:05:46 +080025
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050026 TestBmcEpoch() : bus(sdbusplus::bus::new_default())
27 {
28 // BmcEpoch requires sd_event to init
29 sd_event_default(&event);
30 bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
31 bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050032 }
Lei YU7b218792017-02-09 12:10:13 +080033
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050034 ~TestBmcEpoch()
35 {
36 bus.detach_event();
37 sd_event_unref(event);
38 }
Lei YU96232822017-01-20 14:05:46 +080039
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050040 // Proxies for BmcEpoch's private members and functions
41 Mode getTimeMode()
42 {
43 return bmcEpoch->timeMode;
44 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050045 void setTimeMode(Mode mode)
46 {
47 bmcEpoch->timeMode = mode;
48 }
49 void triggerTimeChange()
50 {
51 bmcEpoch->onTimeChange(nullptr, -1, 0, bmcEpoch.get());
52 }
Lei YU96232822017-01-20 14:05:46 +080053};
54
George Liu0a704522020-04-13 14:51:40 +080055TEST_F(TestBmcEpoch, onModeChange)
56{
57 bmcEpoch->onModeChanged(Mode::NTP);
58 EXPECT_EQ(Mode::NTP, getTimeMode());
59
60 bmcEpoch->onModeChanged(Mode::Manual);
61 EXPECT_EQ(Mode::Manual, getTimeMode());
62}
63
Lei YU96232822017-01-20 14:05:46 +080064TEST_F(TestBmcEpoch, empty)
65{
George Liu3c2f4492020-04-12 11:35:57 +080066 // Default mode is MANUAL
Lei YUad143542017-07-25 14:27:07 +080067 EXPECT_EQ(Mode::Manual, getTimeMode());
Lei YU96232822017-01-20 14:05:46 +080068}
69
70TEST_F(TestBmcEpoch, getElapsed)
71{
Lei YU7b218792017-02-09 12:10:13 +080072 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080073 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080074 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080075 EXPECT_GE(t2, t1);
76}
77
Lei YUe7abcdc2017-01-16 15:05:24 +080078TEST_F(TestBmcEpoch, setElapsedOK)
79{
80 // TODO: setting time will call sd-bus functions and it will fail on host
81 // if we have gmock for sdbusplus::bus, we can test setElapsed.
82 // But for now we can not test it
83}
Lei YU96232822017-01-20 14:05:46 +080084
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050085} // namespace time
86} // namespace phosphor