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