xref: /openbmc/sdbusplus/test/message/types.cpp (revision 98019e74)
1 #include <sdbusplus/message/types.hpp>
2 #include <sdbusplus/utility/tuple_to_array.hpp>
3 
4 #include <gtest/gtest.h>
5 
6 template <typename... Args>
7 auto dbus_string(Args&&... /*args*/)
8 {
9     return std::string(sdbusplus::utility::tuple_to_array(
10                            sdbusplus::message::types::type_id<Args...>())
11                            .data());
12 }
13 
14 TEST(MessageTypes, Integer)
15 {
16     EXPECT_EQ(dbus_string(1), "i");
17 }
18 
19 TEST(MessageTypes, Double)
20 {
21     EXPECT_EQ(dbus_string(1.0), "d");
22 }
23 
24 TEST(MessageTypes, MultipleParameter)
25 {
26     EXPECT_EQ(dbus_string(false, true), "bb");
27     EXPECT_EQ(dbus_string(1, 2, 3, true, 1.0), "iiibd");
28 }
29 
30 TEST(MessageTypes, StringReferences)
31 {
32     std::string a = "a";
33     std::string b = "b";
34     const char* c = "c";
35 
36     EXPECT_EQ(dbus_string(a, std::move(b), c), "sss");
37 }
38 
39 TEST(MessageTypes, ObjectPath)
40 {
41     EXPECT_EQ(dbus_string(sdbusplus::message::object_path("/asdf")), "o");
42 }
43 
44 TEST(MessageTypes, ObjectPathFilename)
45 {
46     EXPECT_EQ(sdbusplus::message::object_path("/abc/def").filename(), "def");
47     EXPECT_EQ(sdbusplus::message::object_path("/abc/").filename(), "");
48     EXPECT_EQ(sdbusplus::message::object_path("/abc").filename(), "abc");
49     EXPECT_EQ(sdbusplus::message::object_path("/_61bc").filename(), "abc");
50     EXPECT_EQ(sdbusplus::message::object_path("/").filename(), "");
51     EXPECT_EQ(sdbusplus::message::object_path("").filename(), "");
52     EXPECT_EQ(sdbusplus::message::object_path("abc").filename(), "");
53     EXPECT_EQ(sdbusplus::message::object_path("/_2d").filename(), "-");
54     EXPECT_EQ(sdbusplus::message::object_path("/_20").filename(), " ");
55     EXPECT_EQ(sdbusplus::message::object_path("/_2F").filename(), "/");
56     EXPECT_EQ(sdbusplus::message::object_path("/bios_active").filename(),
57               "bios_active");
58 }
59 
60 TEST(MessageTypes, ObjectPathParent)
61 {
62     EXPECT_EQ(sdbusplus::message::object_path("/abc/def").parent_path(),
63               sdbusplus::message::object_path("/abc"));
64     EXPECT_EQ(sdbusplus::message::object_path("/abc/").parent_path(),
65               sdbusplus::message::object_path("/abc"));
66     EXPECT_EQ(sdbusplus::message::object_path("/abc").parent_path(),
67               sdbusplus::message::object_path("/"));
68     EXPECT_EQ(sdbusplus::message::object_path("/").parent_path(),
69               sdbusplus::message::object_path("/"));
70 }
71 
72 TEST(MessageTypes, ObjectPathOperatorSlash)
73 {
74     EXPECT_EQ(sdbusplus::message::object_path("/") / "abc",
75               sdbusplus::message::object_path("/_61bc"));
76     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "def",
77               sdbusplus::message::object_path("/abc/_64ef"));
78     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "-",
79               sdbusplus::message::object_path("/abc/_2d"));
80     EXPECT_EQ(sdbusplus::message::object_path("/abc") / " ",
81               sdbusplus::message::object_path("/abc/_20"));
82     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "/",
83               sdbusplus::message::object_path("/abc/_2f"));
84     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "ab_cd",
85               sdbusplus::message::object_path("/abc/_61b_5fcd"));
86 
87     // Test the std::string overload.  This is largely just for coverage
88     EXPECT_EQ(sdbusplus::message::object_path("/") / std::string("abc"),
89               sdbusplus::message::object_path("/_61bc"));
90 }
91 
92 TEST(MessageTypes, ObjectPathOperatorSlashEqual)
93 {
94     sdbusplus::message::object_path path("/");
95     path /= "abc";
96     EXPECT_EQ(path, sdbusplus::message::object_path("/_61bc"));
97 
98     sdbusplus::message::object_path path2("/");
99     path2 /= std::string("def");
100     EXPECT_EQ(path2, sdbusplus::message::object_path("/_64ef"));
101 }
102 
103 TEST(MessageTypes, Signature)
104 {
105     EXPECT_EQ(dbus_string(sdbusplus::message::signature("sss")), "g");
106 }
107 
108 TEST(MessageTypes, VectorOfString)
109 {
110     std::vector<std::string> s = {"a", "b"};
111 
112     EXPECT_EQ(dbus_string(s), "as");
113 }
114 
115 TEST(MessageTypes, SetOfString)
116 {
117     std::set<std::string> s = {"a", "b"};
118 
119     EXPECT_EQ(dbus_string(s), "as");
120 }
121