blob: 1cbc852f30039ad43b6bd0a7dc474a23cf055a4d [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
Lei YU86d80412017-07-12 13:12:12 +08003#include <xyz/openbmc_project/Common/error.hpp>
Lei YUddd54422017-04-18 16:38:44 +08004
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05005#include <gtest/gtest.h>
6
Lei YUddd54422017-04-18 16:38:44 +08007namespace phosphor
8{
9namespace time
10{
11namespace utils
12{
13
Lei YUad143542017-07-25 14:27:07 +080014using InvalidEnumString = sdbusplus::exception::InvalidEnumString;
Lei YU86d80412017-07-12 13:12:12 +080015
Lei YUddd54422017-04-18 16:38:44 +080016TEST(TestUtil, strToMode)
17{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050018 EXPECT_EQ(Mode::NTP,
19 strToMode("xyz.openbmc_project.Time.Synchronization.Method.NTP"));
Lei YUad143542017-07-25 14:27:07 +080020 EXPECT_EQ(
21 Mode::Manual,
22 strToMode("xyz.openbmc_project.Time.Synchronization.Method.Manual"));
Lei YUddd54422017-04-18 16:38:44 +080023
Lei YUad143542017-07-25 14:27:07 +080024 // 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 YUddd54422017-04-18 16:38:44 +080030}
31
Lei YUddd54422017-04-18 16:38:44 +080032TEST(TestUtil, strToOwner)
33{
Lei YUad143542017-07-25 14:27:07 +080034 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 YUddd54422017-04-18 16:38:44 +080042
Lei YUad143542017-07-25 14:27:07 +080043 // 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 YUddd54422017-04-18 16:38:44 +080047}
48
49TEST(TestUtil, modeToStr)
50{
Lei YUad143542017-07-25 14:27:07 +080051 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 YUddd54422017-04-18 16:38:44 +080055
Lei YUad143542017-07-25 14:27:07 +080056 // All unrecognized strings result in exception
57 EXPECT_ANY_THROW(modeToStr(static_cast<Mode>(100)));
Lei YUddd54422017-04-18 16:38:44 +080058}
59
60TEST(TestUtil, ownerToStr)
61{
Lei YUad143542017-07-25 14:27:07 +080062 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 YUddd54422017-04-18 16:38:44 +080070
Lei YUad143542017-07-25 14:27:07 +080071 // All unrecognized strings result in exception
72 EXPECT_ANY_THROW(ownerToStr(static_cast<Owner>(100)));
Lei YUddd54422017-04-18 16:38:44 +080073}
74
75} // namespace utils
76} // namespace time
77} // namespace phosphor