1 #include "utils.hpp"
2 
3 #include <xyz/openbmc_project/Common/error.hpp>
4 
5 #include <gtest/gtest.h>
6 
7 namespace phosphor
8 {
9 namespace time
10 {
11 namespace utils
12 {
13 
14 using InvalidEnumString = sdbusplus::exception::InvalidEnumString;
15 
16 TEST(TestUtil, strToMode)
17 {
18     EXPECT_EQ(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"));
23 
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);
30 }
31 
32 TEST(TestUtil, strToOwner)
33 {
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"));
42 
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);
47 }
48 
49 TEST(TestUtil, modeToStr)
50 {
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));
55 
56     // All unrecognized strings result in exception
57     EXPECT_ANY_THROW(modeToStr(static_cast<Mode>(100)));
58 }
59 
60 TEST(TestUtil, ownerToStr)
61 {
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));
70 
71     // All unrecognized strings result in exception
72     EXPECT_ANY_THROW(ownerToStr(static_cast<Owner>(100)));
73 }
74 
75 } // namespace utils
76 } // namespace time
77 } // namespace phosphor
78