blob: 5ed1f3c3817ed62df1647801d04e6f2febf29fa5 [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 <memory>
7#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
Lei YUe7abcdc2017-01-16 15:05:24 +080016using namespace std::chrono;
Lei YU7b218792017-02-09 12:10:13 +080017
Lei YU96232822017-01-20 14:05:46 +080018class TestBmcEpoch : public testing::Test
19{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050020 public:
21 sdbusplus::bus::bus bus;
22 sd_event* event;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050023 std::unique_ptr<BmcEpoch> bmcEpoch;
Lei YU96232822017-01-20 14:05:46 +080024
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050025 TestBmcEpoch() : bus(sdbusplus::bus::new_default())
26 {
27 // BmcEpoch requires sd_event to init
28 sd_event_default(&event);
29 bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL);
30 bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050031 }
Lei YU7b218792017-02-09 12:10:13 +080032
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050033 ~TestBmcEpoch()
34 {
35 bus.detach_event();
36 sd_event_unref(event);
37 }
Lei YU96232822017-01-20 14:05:46 +080038
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050039 // Proxies for BmcEpoch's private members and functions
40 Mode getTimeMode()
41 {
42 return bmcEpoch->timeMode;
43 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050044 void setTimeMode(Mode mode)
45 {
46 bmcEpoch->timeMode = mode;
47 }
48 void triggerTimeChange()
49 {
50 bmcEpoch->onTimeChange(nullptr, -1, 0, bmcEpoch.get());
51 }
Lei YU96232822017-01-20 14:05:46 +080052};
53
George Liu0a704522020-04-13 14:51:40 +080054TEST_F(TestBmcEpoch, onModeChange)
55{
56 bmcEpoch->onModeChanged(Mode::NTP);
57 EXPECT_EQ(Mode::NTP, getTimeMode());
58
59 bmcEpoch->onModeChanged(Mode::Manual);
60 EXPECT_EQ(Mode::Manual, getTimeMode());
61}
62
Lei YU96232822017-01-20 14:05:46 +080063TEST_F(TestBmcEpoch, empty)
64{
George Liu3c2f4492020-04-12 11:35:57 +080065 // Default mode is MANUAL
Lei YUad143542017-07-25 14:27:07 +080066 EXPECT_EQ(Mode::Manual, getTimeMode());
Lei YU96232822017-01-20 14:05:46 +080067}
68
69TEST_F(TestBmcEpoch, getElapsed)
70{
Lei YU7b218792017-02-09 12:10:13 +080071 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080072 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080073 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080074 EXPECT_GE(t2, t1);
75}
76
Lei YUe7abcdc2017-01-16 15:05:24 +080077TEST_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}
Lei YU96232822017-01-20 14:05:46 +080083
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050084} // namespace time
85} // namespace phosphor