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("/").filename(), ""); 50 ASSERT_EQ(sdbusplus::message::object_path("").filename(), ""); 51 ASSERT_EQ(sdbusplus::message::object_path("abc").filename(), ""); 52 ASSERT_EQ(sdbusplus::message::object_path("/_2d").filename(), "-"); 53 ASSERT_EQ(sdbusplus::message::object_path("/_20").filename(), " "); 54 ASSERT_EQ(sdbusplus::message::object_path("/_2F").filename(), "/"); 55 } 56 57 TEST(MessageTypes, ObjectPathParent) 58 { 59 ASSERT_EQ(sdbusplus::message::object_path("/abc/def").parent_path(), 60 sdbusplus::message::object_path("/abc")); 61 ASSERT_EQ(sdbusplus::message::object_path("/abc/").parent_path(), 62 sdbusplus::message::object_path("/abc")); 63 ASSERT_EQ(sdbusplus::message::object_path("/abc").parent_path(), 64 sdbusplus::message::object_path("/")); 65 ASSERT_EQ(sdbusplus::message::object_path("/").parent_path(), 66 sdbusplus::message::object_path("/")); 67 } 68 69 TEST(MessageTypes, ObjectPathOperatorSlash) 70 { 71 ASSERT_EQ(sdbusplus::message::object_path("/") / "abc", 72 sdbusplus::message::object_path("/abc")); 73 ASSERT_EQ(sdbusplus::message::object_path("/abc") / "def", 74 sdbusplus::message::object_path("/abc/def")); 75 ASSERT_EQ(sdbusplus::message::object_path("/abc") / "-", 76 sdbusplus::message::object_path("/abc/_2d")); 77 ASSERT_EQ(sdbusplus::message::object_path("/abc") / " ", 78 sdbusplus::message::object_path("/abc/_20")); 79 ASSERT_EQ(sdbusplus::message::object_path("/abc") / "/", 80 sdbusplus::message::object_path("/abc/_2f")); 81 82 // Test the std::string overload. This is largely just for coverage 83 ASSERT_EQ(sdbusplus::message::object_path("/") / std::string("abc"), 84 sdbusplus::message::object_path("/abc")); 85 } 86 87 TEST(MessageTypes, ObjectPathOperatorSlashEqual) 88 { 89 sdbusplus::message::object_path path("/"); 90 path /= "abc"; 91 ASSERT_EQ(path, sdbusplus::message::object_path("/abc")); 92 93 sdbusplus::message::object_path path2("/"); 94 path2 /= std::string("def"); 95 ASSERT_EQ(path2, sdbusplus::message::object_path("/def")); 96 } 97 98 TEST(MessageTypes, Signature) 99 { 100 ASSERT_EQ(dbus_string(sdbusplus::message::signature("sss")), "g"); 101 } 102 103 TEST(MessageTypes, VectorOfString) 104 { 105 std::vector<std::string> s = {"a", "b"}; 106 107 ASSERT_EQ(dbus_string(s), "as"); 108 } 109 110 TEST(MessageTypes, SetOfString) 111 { 112 std::set<std::string> s = {"a", "b"}; 113 114 ASSERT_EQ(dbus_string(s), "as"); 115 } 116