xref: /openbmc/sdbusplus/test/message/types.cpp (revision 08dc23c1)
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("/_").filename(), "");
57     EXPECT_EQ(sdbusplus::message::object_path("/_2").filename(), "");
58     EXPECT_EQ(sdbusplus::message::object_path("/_2y").filename(), "");
59     EXPECT_EQ(sdbusplus::message::object_path("/_y2").filename(), "");
60     EXPECT_EQ(sdbusplus::message::object_path("/bios_active").filename(),
61               "bios_active");
62 }
63 
64 TEST(MessageTypes, ObjectPathParent)
65 {
66     EXPECT_EQ(sdbusplus::message::object_path("/abc/def").parent_path(),
67               sdbusplus::message::object_path("/abc"));
68     EXPECT_EQ(sdbusplus::message::object_path("/abc/").parent_path(),
69               sdbusplus::message::object_path("/abc"));
70     EXPECT_EQ(sdbusplus::message::object_path("/abc").parent_path(),
71               sdbusplus::message::object_path("/"));
72     EXPECT_EQ(sdbusplus::message::object_path("/").parent_path(),
73               sdbusplus::message::object_path("/"));
74 }
75 
76 TEST(MessageTypes, ObjectPathOperatorSlash)
77 {
78     EXPECT_EQ(sdbusplus::message::object_path("/") / "abc",
79               sdbusplus::message::object_path("/abc"));
80     EXPECT_EQ(sdbusplus::message::object_path("/") / "abc",
81               sdbusplus::message::object_path("/abc"));
82     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "def",
83               sdbusplus::message::object_path("/abc/def"));
84     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "-",
85               sdbusplus::message::object_path("/abc/_2d"));
86     EXPECT_EQ(sdbusplus::message::object_path("/abc") / " ",
87               sdbusplus::message::object_path("/abc/_20"));
88     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "/",
89               sdbusplus::message::object_path("/abc/_2f"));
90     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "ab_cd",
91               sdbusplus::message::object_path("/abc/ab_cd"));
92     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "_ab_cd",
93               sdbusplus::message::object_path("/abc/_5fab_5fcd"));
94     EXPECT_EQ(sdbusplus::message::object_path("/abc") / "ab-c_d",
95               sdbusplus::message::object_path("/abc/_61b_2dc_5fd"));
96 
97     // Test the std::string overload.  This is largely just for coverage
98     EXPECT_EQ(sdbusplus::message::object_path("/") / std::string("abc"),
99               sdbusplus::message::object_path("/abc"));
100 }
101 
102 TEST(MessageTypes, ObjectPathOperatorSlashEqual)
103 {
104     sdbusplus::message::object_path path("/");
105     path /= "abc";
106     EXPECT_EQ(path, sdbusplus::message::object_path("/abc"));
107 
108     sdbusplus::message::object_path path2("/");
109     path2 /= std::string("d-ef");
110     EXPECT_EQ(path2, sdbusplus::message::object_path("/_64_2def"));
111 }
112 
113 TEST(MessageTypes, Signature)
114 {
115     EXPECT_EQ(dbus_string(sdbusplus::message::signature("sss")), "g");
116 }
117 
118 TEST(MessageTypes, VectorOfString)
119 {
120     std::vector<std::string> s = {"a", "b"};
121 
122     EXPECT_EQ(dbus_string(s), "as");
123 }
124 
125 TEST(MessageTypes, SetOfString)
126 {
127     std::set<std::string> s = {"a", "b"};
128 
129     EXPECT_EQ(dbus_string(s), "as");
130 }
131