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