Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 3 | #include <xyz/openbmc_project/Common/error.hpp> |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 4 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 5 | #include <gtest/gtest.h> |
| 6 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace time |
| 10 | { |
| 11 | namespace utils |
| 12 | { |
| 13 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 14 | using InvalidEnumString = sdbusplus::exception::InvalidEnumString; |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 15 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 16 | TEST(TestUtil, strToMode) |
| 17 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 18 | EXPECT_EQ(Mode::NTP, |
| 19 | strToMode("xyz.openbmc_project.Time.Synchronization.Method.NTP")); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 20 | EXPECT_EQ( |
| 21 | Mode::Manual, |
| 22 | strToMode("xyz.openbmc_project.Time.Synchronization.Method.Manual")); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 23 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 24 | // All unrecognized strings result in InvalidEnumString exception |
| 25 | EXPECT_THROW(strToMode(""), InvalidEnumString); |
| 26 | EXPECT_THROW( |
| 27 | strToMode("xyz.openbmc_project.Time.Synchronization.Method.MANUAL"), |
| 28 | InvalidEnumString); |
| 29 | EXPECT_THROW(strToMode("whatever"), InvalidEnumString); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 30 | } |
| 31 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 32 | TEST(TestUtil, strToOwner) |
| 33 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 34 | EXPECT_EQ(Owner::BMC, |
| 35 | strToOwner("xyz.openbmc_project.Time.Owner.Owners.BMC")); |
| 36 | EXPECT_EQ(Owner::Host, |
| 37 | strToOwner("xyz.openbmc_project.Time.Owner.Owners.Host")); |
| 38 | EXPECT_EQ(Owner::Split, |
| 39 | strToOwner("xyz.openbmc_project.Time.Owner.Owners.Split")); |
| 40 | EXPECT_EQ(Owner::Both, |
| 41 | strToOwner("xyz.openbmc_project.Time.Owner.Owners.Both")); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 42 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 43 | // All unrecognized strings result in InvalidEnumString exception |
| 44 | EXPECT_THROW(strToOwner(""), InvalidEnumString); |
| 45 | EXPECT_THROW(strToOwner("Split"), InvalidEnumString); |
| 46 | EXPECT_THROW(strToOwner("xyz"), InvalidEnumString); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(TestUtil, modeToStr) |
| 50 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 51 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP", |
| 52 | modeToStr(Mode::NTP)); |
| 53 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual", |
| 54 | modeToStr(Mode::Manual)); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 55 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 56 | // All unrecognized strings result in exception |
| 57 | EXPECT_ANY_THROW(modeToStr(static_cast<Mode>(100))); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(TestUtil, ownerToStr) |
| 61 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 62 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.BMC", |
| 63 | ownerToStr(Owner::BMC)); |
| 64 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Host", |
| 65 | ownerToStr(Owner::Host)); |
| 66 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split", |
| 67 | ownerToStr(Owner::Split)); |
| 68 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Both", |
| 69 | ownerToStr(Owner::Both)); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 70 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 71 | // All unrecognized strings result in exception |
| 72 | EXPECT_ANY_THROW(ownerToStr(static_cast<Owner>(100))); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace utils |
| 76 | } // namespace time |
| 77 | } // namespace phosphor |