blob: 483e82a4c2abd1054f308fb4afc999ac3e7fa182 [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
3#include <gtest/gtest.h>
Lei YU86d80412017-07-12 13:12:12 +08004#include <xyz/openbmc_project/Common/error.hpp>
Lei YUddd54422017-04-18 16:38:44 +08005
6namespace phosphor
7{
8namespace time
9{
10namespace utils
11{
12
Lei YUad143542017-07-25 14:27:07 +080013using InvalidEnumString = sdbusplus::exception::InvalidEnumString;
Lei YU86d80412017-07-12 13:12:12 +080014
Lei YUddd54422017-04-18 16:38:44 +080015TEST(TestUtil, strToMode)
16{
Lei YUad143542017-07-25 14:27:07 +080017 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 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
32
33TEST(TestUtil, strToOwner)
34{
Lei YUad143542017-07-25 14:27:07 +080035 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 YUddd54422017-04-18 16:38:44 +080043
Lei YUad143542017-07-25 14:27:07 +080044 // 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 YUddd54422017-04-18 16:38:44 +080048}
49
50TEST(TestUtil, modeToStr)
51{
Lei YUad143542017-07-25 14:27:07 +080052 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 YUddd54422017-04-18 16:38:44 +080056
Lei YUad143542017-07-25 14:27:07 +080057 // All unrecognized strings result in exception
58 EXPECT_ANY_THROW(modeToStr(static_cast<Mode>(100)));
Lei YUddd54422017-04-18 16:38:44 +080059}
60
61TEST(TestUtil, ownerToStr)
62{
Lei YUad143542017-07-25 14:27:07 +080063 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 YUddd54422017-04-18 16:38:44 +080071
Lei YUad143542017-07-25 14:27:07 +080072 // All unrecognized strings result in exception
73 EXPECT_ANY_THROW(ownerToStr(static_cast<Owner>(100)));
Lei YUddd54422017-04-18 16:38:44 +080074}
75
76} // namespace utils
77} // namespace time
78} // namespace phosphor