blob: db43854b8b8d0f1c6774556beb9e37df555bad99 [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
54TEST_F(TestBmcEpoch, empty)
55{
George Liu3c2f4492020-04-12 11:35:57 +080056 // Default mode is MANUAL
Lei YUad143542017-07-25 14:27:07 +080057 EXPECT_EQ(Mode::Manual, getTimeMode());
Lei YU96232822017-01-20 14:05:46 +080058}
59
60TEST_F(TestBmcEpoch, getElapsed)
61{
Lei YU7b218792017-02-09 12:10:13 +080062 auto t1 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080063 EXPECT_NE(0, t1);
Lei YU7b218792017-02-09 12:10:13 +080064 auto t2 = bmcEpoch->elapsed();
Lei YU96232822017-01-20 14:05:46 +080065 EXPECT_GE(t2, t1);
66}
67
Lei YUe7abcdc2017-01-16 15:05:24 +080068TEST_F(TestBmcEpoch, setElapsedOK)
69{
70 // TODO: setting time will call sd-bus functions and it will fail on host
71 // if we have gmock for sdbusplus::bus, we can test setElapsed.
72 // But for now we can not test it
73}
Lei YU96232822017-01-20 14:05:46 +080074
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050075} // namespace time
76} // namespace phosphor