xref: /openbmc/sdbusplus/test/message/types.cpp (revision fcff6763)
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     ASSERT_EQ(dbus_string(1), "i");
17 }
18 
19 TEST(MessageTypes, Double)
20 {
21     ASSERT_EQ(dbus_string(1.0), "d");
22 }
23 
24 TEST(MessageTypes, MultipleParameter)
25 {
26     ASSERT_EQ(dbus_string(false, true), "bb");
27     ASSERT_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     ASSERT_EQ(dbus_string(a, std::move(b), c), "sss");
37 }
38 
39 TEST(MessageTypes, ObjectPath)
40 {
41     ASSERT_EQ(dbus_string(sdbusplus::message::object_path("/asdf")), "o");
42 }
43 
44 TEST(MessageTypes, ObjectPathFilename)
45 {
46     ASSERT_EQ(sdbusplus::message::object_path("/abc/def").filename(), "def");
47     ASSERT_EQ(sdbusplus::message::object_path("/abc/").filename(), "");
48     ASSERT_EQ(sdbusplus::message::object_path("/abc").filename(), "abc");
49     ASSERT_EQ(sdbusplus::message::object_path("/_61bc").filename(), "abc");
50     ASSERT_EQ(sdbusplus::message::object_path("/").filename(), "");
51     ASSERT_EQ(sdbusplus::message::object_path("").filename(), "");
52     ASSERT_EQ(sdbusplus::message::object_path("abc").filename(), "");
53     ASSERT_EQ(sdbusplus::message::object_path("/_2d").filename(), "-");
54     ASSERT_EQ(sdbusplus::message::object_path("/_20").filename(), " ");
55     ASSERT_EQ(sdbusplus::message::object_path("/_2F").filename(), "/");
56     ASSERT_EQ(sdbusplus::message::object_path("/bios_active").filename(),
57               "bios_active");
58 }
59 
60 TEST(MessageTypes, ObjectPathParent)
61 {
62     ASSERT_EQ(sdbusplus::message::object_path("/abc/def").parent_path(),
63               sdbusplus::message::object_path("/abc"));
64     ASSERT_EQ(sdbusplus::message::object_path("/abc/").parent_path(),
65               sdbusplus::message::object_path("/abc"));
66     ASSERT_EQ(sdbusplus::message::object_path("/abc").parent_path(),
67               sdbusplus::message::object_path("/"));
68     ASSERT_EQ(sdbusplus::message::object_path("/").parent_path(),
69               sdbusplus::message::object_path("/"));
70 }
71 
72 TEST(MessageTypes, ObjectPathOperatorSlash)
73 {
74     ASSERT_EQ(sdbusplus::message::object_path("/") / "abc",
75               sdbusplus::message::object_path("/_61bc"));
76     ASSERT_EQ(sdbusplus::message::object_path("/abc") / "def",
77               sdbusplus::message::object_path("/abc/_64ef"));
78     ASSERT_EQ(sdbusplus::message::object_path("/abc") / "-",
79               sdbusplus::message::object_path("/abc/_2d"));
80     ASSERT_EQ(sdbusplus::message::object_path("/abc") / " ",
81               sdbusplus::message::object_path("/abc/_20"));
82     ASSERT_EQ(sdbusplus::message::object_path("/abc") / "/",
83               sdbusplus::message::object_path("/abc/_2f"));
84     ASSERT_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     ASSERT_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     ASSERT_EQ(path, sdbusplus::message::object_path("/_61bc"));
97 
98     sdbusplus::message::object_path path2("/");
99     path2 /= std::string("def");
100     ASSERT_EQ(path2, sdbusplus::message::object_path("/_64ef"));
101 }
102 
103 TEST(MessageTypes, Signature)
104 {
105     ASSERT_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     ASSERT_EQ(dbus_string(s), "as");
113 }
114 
115 TEST(MessageTypes, SetOfString)
116 {
117     std::set<std::string> s = {"a", "b"};
118 
119     ASSERT_EQ(dbus_string(s), "as");
120 }
121