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