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" |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 4 | #include "manager.hpp" |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 5 | #include "types.hpp" |
| 6 | |
Lei YU | 33752c7 | 2018-06-07 17:06:58 +0800 | [diff] [blame] | 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 | |
| 16 | class TestBmcEpoch : public testing::Test |
| 17 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 18 | public: |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 19 | sdbusplus::bus_t bus; |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 20 | Manager manager; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 21 | sd_event* event; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 22 | std::unique_ptr<BmcEpoch> bmcEpoch; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 23 | |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 24 | TestBmcEpoch() : bus(sdbusplus::bus::new_default()), manager(bus) |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 25 | { |
| 26 | // BmcEpoch requires sd_event to init |
| 27 | sd_event_default(&event); |
| 28 | bus.attach_event(event, SD_EVENT_PRIORITY_NORMAL); |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 29 | bmcEpoch = std::make_unique<BmcEpoch>(bus, OBJPATH_BMC, manager); |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 30 | } |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 31 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 32 | ~TestBmcEpoch() |
| 33 | { |
| 34 | bus.detach_event(); |
| 35 | sd_event_unref(event); |
| 36 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 39 | TEST_F(TestBmcEpoch, onModeChange) |
| 40 | { |
| 41 | bmcEpoch->onModeChanged(Mode::NTP); |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 42 | EXPECT_EQ(Mode::NTP, manager.getTimeMode()); |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 43 | |
| 44 | bmcEpoch->onModeChanged(Mode::Manual); |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 45 | EXPECT_EQ(Mode::Manual, manager.getTimeMode()); |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 46 | } |
| 47 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 48 | TEST_F(TestBmcEpoch, empty) |
| 49 | { |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 50 | // Default mode is MANUAL |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame] | 51 | EXPECT_EQ(Mode::Manual, manager.getTimeMode()); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST_F(TestBmcEpoch, getElapsed) |
| 55 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 56 | auto t1 = bmcEpoch->elapsed(); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 57 | EXPECT_NE(0, t1); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 58 | auto t2 = bmcEpoch->elapsed(); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 59 | EXPECT_GE(t2, t1); |
| 60 | } |
| 61 | |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 62 | TEST_F(TestBmcEpoch, setElapsedOK) |
| 63 | { |
| 64 | // TODO: setting time will call sd-bus functions and it will fail on host |
| 65 | // if we have gmock for sdbusplus::bus, we can test setElapsed. |
| 66 | // But for now we can not test it |
| 67 | } |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 68 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 69 | } // namespace time |
| 70 | } // namespace phosphor |