• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chromeos-dbus-bindings/dbus_signature.h"
6 
7 #include <map>
8 #include <string>
9 
10 #include <dbus/dbus-protocol.h>
11 #include <gtest/gtest.h>
12 
13 using std::map;
14 using std::string;
15 using testing::Test;
16 
17 namespace chromeos_dbus_bindings {
18 
19 namespace {
20 
21 // Failing signatures.
22 const char kEmptySignature[] = "";
23 const char kEmptyDictSignature[] = "a{}";
24 const char kMissingArraryParameterSignature[] = "a";
25 const char kMissingArraryParameterInnerSignature[] = "a{sa}i";
26 const char kOrphanDictSignature[] = "a{s{i}}";
27 const char kTooFewDictMembersSignature[] = "a{s}";
28 const char kTooManyDictMembersSignature[] = "a{sa{i}u}";
29 const char kUnclosedDictOuterSignature[] = "a{s";
30 const char kUnclosedDictInnerSignature[] = "a{a{u}";
31 const char kUnexpectedCloseSignature[] = "a}i{";
32 const char kUnknownSignature[] = "al";
33 
34 // Define an object type name to disambiguate the typenames above.
35 const char kObjectPathTypename[] = "ObjectPathType";
36 
37 }  // namespace
38 
39 class DbusSignatureTest : public Test {
40  protected:
41   DbusSignature signature_;
42 };
43 
TEST_F(DbusSignatureTest,ParseFailures)44 TEST_F(DbusSignatureTest, ParseFailures) {
45   for (const auto& failing_string : { kEmptySignature,
46                                       kEmptyDictSignature,
47                                       kMissingArraryParameterSignature,
48                                       kMissingArraryParameterInnerSignature,
49                                       kOrphanDictSignature,
50                                       kTooFewDictMembersSignature,
51                                       kTooManyDictMembersSignature,
52                                       kUnclosedDictOuterSignature,
53                                       kUnclosedDictInnerSignature,
54                                       kUnexpectedCloseSignature,
55                                       kUnknownSignature }) {
56     string unused_output;
57     EXPECT_FALSE(signature_.Parse(failing_string, &unused_output))
58         << "Expected signature " << failing_string
59         << " to fail but it succeeded";
60   }
61 }
62 
TEST_F(DbusSignatureTest,DefaultObjectPathTypename)63 TEST_F(DbusSignatureTest, DefaultObjectPathTypename) {
64   // The ParseSuccesses test below overrides the default object typename, so
65   // test the default behavior separately.
66   string output;
67   EXPECT_TRUE(signature_.Parse(DBUS_TYPE_OBJECT_PATH_AS_STRING, &output));
68   EXPECT_EQ(DbusSignature::kDefaultObjectPathTypename, output);
69 }
70 
TEST_F(DbusSignatureTest,ParseSuccesses)71 TEST_F(DbusSignatureTest, ParseSuccesses) {
72   const map<string, string> parse_values {
73     // Simple types.
74     { DBUS_TYPE_BOOLEAN_AS_STRING, DbusSignature::kBooleanTypename },
75     { DBUS_TYPE_BYTE_AS_STRING, DbusSignature::kByteTypename },
76     { DBUS_TYPE_DOUBLE_AS_STRING, DbusSignature::kDoubleTypename },
77     { DBUS_TYPE_OBJECT_PATH_AS_STRING, kObjectPathTypename },
78     { DBUS_TYPE_INT16_AS_STRING, DbusSignature::kSigned16Typename },
79     { DBUS_TYPE_INT32_AS_STRING, DbusSignature::kSigned32Typename },
80     { DBUS_TYPE_INT64_AS_STRING, DbusSignature::kSigned64Typename },
81     { DBUS_TYPE_STRING_AS_STRING, DbusSignature::kStringTypename },
82     { DBUS_TYPE_UNIX_FD_AS_STRING, DbusSignature::kUnixFdTypename },
83     { DBUS_TYPE_UINT16_AS_STRING, DbusSignature::kUnsigned16Typename },
84     { DBUS_TYPE_UINT32_AS_STRING, DbusSignature::kUnsigned32Typename },
85     { DBUS_TYPE_UINT64_AS_STRING, DbusSignature::kUnsigned64Typename },
86     { DBUS_TYPE_VARIANT_AS_STRING, DbusSignature::kVariantTypename },
87 
88     // Complex types.
89     { "ab",             "std::vector<bool>" },
90     { "ay",             "std::vector<uint8_t>" },
91     { "aay",            "std::vector<std::vector<uint8_t>>" },
92     { "ao",             "std::vector<ObjectPathType>" },
93     { "a{oa{sa{sv}}}",  "std::map<ObjectPathType, std::map<std::string, "
94                           "brillo::VariantDictionary>>" },
95     { "a{os}",          "std::map<ObjectPathType, std::string>" },
96     { "as",             "std::vector<std::string>" },
97     { "a{ss}",          "std::map<std::string, std::string>" },
98     { "a{sa{ss}}",      "std::map<std::string, std::map<std::string, "
99                           "std::string>>"},
100     { "a{sa{sv}}",      "std::map<std::string, brillo::VariantDictionary>" },
101     { "a{sv}",          "brillo::VariantDictionary" },
102     { "a{sv}Garbage",   "brillo::VariantDictionary" },
103     { "at",             "std::vector<uint64_t>" },
104     { "a{iv}",          "std::map<int32_t, brillo::Any>" },
105     { "(ib)",           "std::tuple<int32_t, bool>" },
106     { "(ibs)",          "std::tuple<int32_t, bool, std::string>" },
107   };
108   signature_.set_object_path_typename(kObjectPathTypename);
109   for (const auto& parse_test : parse_values) {
110     string output;
111     EXPECT_TRUE(signature_.Parse(parse_test.first, &output))
112         << "Expected signature " << parse_test.first
113         << " to succeed but it failed.";
114     EXPECT_EQ(parse_test.second, output)
115         << "Expected typename for " << parse_test.first
116         << " to be " << parse_test.second << " but instead it was " << output;
117   }
118 }
119 
120 }  // namespace chromeos_dbus_bindings
121