Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 3 | #include "bmc_epoch.hpp" |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 4 | #include "types.hpp" |
| 5 | |
Lei YU | 33752c7 | 2018-06-07 17:06:58 +0800 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Lei YU | 33752c7 | 2018-06-07 17:06:58 +0800 | [diff] [blame] | 7 | |
George Liu | c6d3397 | 2020-06-22 10:35:29 +0800 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace time |
| 15 | { |
| 16 | |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 17 | using namespace std::chrono; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 18 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 19 | class TestBmcEpoch : public testing::Test |
| 20 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 21 | public: |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 22 | sdbusplus::bus_t bus; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 23 | sd_event* event; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 24 | std::unique_ptr<BmcEpoch> bmcEpoch; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 25 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 26 | 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 Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 32 | } |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 33 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 34 | ~TestBmcEpoch() |
| 35 | { |
| 36 | bus.detach_event(); |
| 37 | sd_event_unref(event); |
| 38 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 39 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 40 | // Proxies for BmcEpoch's private members and functions |
| 41 | Mode getTimeMode() |
| 42 | { |
| 43 | return bmcEpoch->timeMode; |
| 44 | } |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 45 | void setTimeMode(Mode mode) |
| 46 | { |
| 47 | bmcEpoch->timeMode = mode; |
| 48 | } |
| 49 | void triggerTimeChange() |
| 50 | { |
| 51 | bmcEpoch->onTimeChange(nullptr, -1, 0, bmcEpoch.get()); |
| 52 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 55 | TEST_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 YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 64 | TEST_F(TestBmcEpoch, empty) |
| 65 | { |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 66 | // Default mode is MANUAL |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 67 | EXPECT_EQ(Mode::Manual, getTimeMode()); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST_F(TestBmcEpoch, getElapsed) |
| 71 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 72 | auto t1 = bmcEpoch->elapsed(); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 73 | EXPECT_NE(0, t1); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 74 | auto t2 = bmcEpoch->elapsed(); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 75 | EXPECT_GE(t2, t1); |
| 76 | } |
| 77 | |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 78 | TEST_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 YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 84 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 85 | } // namespace time |
| 86 | } // namespace phosphor |