summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/xml_interface_parser_unittest.cc
blob: 8876b160e129e22ced5966f17d592dfd0e05350b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos-dbus-bindings/xml_interface_parser.h"

#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <gtest/gtest.h>

#include "chromeos-dbus-bindings/interface.h"

using std::string;
using std::vector;
using testing::Test;

namespace chromeos_dbus_bindings {

namespace {

const char kBadInterfaceFileContents0[] = "This has no resemblance to XML";
const char kBadInterfaceFileContents1[] = "<node>";
const char kGoodInterfaceFileContents[] = R"literal_string(
<node name="/org/chromium/Test">
  <interface name="fi.w1.wpa_supplicant1.Interface">
    <method name="Scan">
      <arg name="args" type="a{sv}" direction="in"/>
      <annotation name="org.chromium.DBus.Method.Kind" value="async"/>
      <annotation name="org.chromium.DBus.Method.IncludeDBusMessage"
                  value="true"/>
    </method>
    <method name="GetBlob">
      <arg name="name" type="s"/>
      <arg name="data" type="ay" direction="out"/>
      <annotation name="org.chromium.DBus.Method.Const" value="true"/>
    </method>
    <property name="Capabilities" type="a{sv}" access="read"/>
    <signal name="BSSRemoved">
      <arg name="BSS" type="o"/>
    </signal>
  </interface>
  <interface name="DummyInterface" />
  <node name="/"/>
  <node/>
</node>
)literal_string";
const char kInterfaceName[] = "fi.w1.wpa_supplicant1.Interface";
const char kScanMethod[] = "Scan";
const char kArgsArgument[] = "args";
const char kArrayStringVariantType[] = "a{sv}";
const char kGetBlobMethod[] = "GetBlob";
const char kNameArgument[] = "name";
const char kDataArgument[] = "data";
const char kStringType[] = "s";
const char kArrayByteType[] = "ay";
const char kBssRemovedSignal[] = "BSSRemoved";
const char kBssArgument[] = "BSS";
const char kObjectType[] = "o";
const char kCapabilitiesProperty[] = "Capabilities";
const char kReadAccess[] = "read";
}  // namespace

class XmlInterfaceParserTest : public Test {
 protected:
  XmlInterfaceParser parser_;
};

TEST_F(XmlInterfaceParserTest, BadInputFile) {
  EXPECT_FALSE(parser_.ParseXmlInterfaceFile(kBadInterfaceFileContents0, {}));
  EXPECT_FALSE(parser_.ParseXmlInterfaceFile(kBadInterfaceFileContents1, {}));
}

TEST_F(XmlInterfaceParserTest, GoodInputFile) {
  EXPECT_TRUE(parser_.ParseXmlInterfaceFile(kGoodInterfaceFileContents,
                                            {"DummyInterface"}));
  const vector<Interface>& interfaces = parser_.interfaces();
  ASSERT_EQ(1, interfaces.size());
  const Interface& interface = interfaces.back();

  EXPECT_EQ(kInterfaceName, interface.name);
  EXPECT_EQ("/org/chromium/Test", interface.path);
  ASSERT_EQ(2, interface.methods.size());
  ASSERT_EQ(1, interface.signals.size());

  // <method name="Scan">
  EXPECT_EQ(kScanMethod, interface.methods[0].name);
  EXPECT_EQ(Interface::Method::Kind::kAsync, interface.methods[0].kind);
  EXPECT_FALSE(interface.methods[0].is_const);
  EXPECT_TRUE(interface.methods[0].include_dbus_message);
  ASSERT_EQ(1, interface.methods[0].input_arguments.size());

  // <arg name="args" type="a{sv}" direction="in"/>
  EXPECT_EQ(kArgsArgument, interface.methods[0].input_arguments[0].name);
  EXPECT_EQ(kArrayStringVariantType,
            interface.methods[0].input_arguments[0].type);
  EXPECT_EQ(0, interface.methods[0].output_arguments.size());

  // <method name="GetBlob">
  EXPECT_EQ(kGetBlobMethod, interface.methods[1].name);
  EXPECT_EQ(Interface::Method::Kind::kNormal, interface.methods[1].kind);
  EXPECT_TRUE(interface.methods[1].is_const);
  EXPECT_FALSE(interface.methods[1].include_dbus_message);
  EXPECT_EQ(1, interface.methods[1].input_arguments.size());
  EXPECT_EQ(1, interface.methods[1].output_arguments.size());

  // <arg name="name" type="s"/>  (direction="in" is implicit)
  EXPECT_EQ(kNameArgument, interface.methods[1].input_arguments[0].name);
  EXPECT_EQ(kStringType, interface.methods[1].input_arguments[0].type);

  // <arg name="data" type="ay" direction="out"/>
  EXPECT_EQ(kDataArgument, interface.methods[1].output_arguments[0].name);
  EXPECT_EQ(kArrayByteType, interface.methods[1].output_arguments[0].type);

  // <signal name="BSSRemoved">
  EXPECT_EQ(kBssRemovedSignal, interface.signals[0].name);
  EXPECT_EQ(1, interface.signals[0].arguments.size());

  // <arg name="BSS" type="o"/>
  EXPECT_EQ(kBssArgument, interface.signals[0].arguments[0].name);
  EXPECT_EQ(kObjectType, interface.signals[0].arguments[0].type);

  // <property name="Capabilities" type="s" access="read"/>
  EXPECT_EQ(kCapabilitiesProperty, interface.properties[0].name);
  EXPECT_EQ(kArrayStringVariantType, interface.properties[0].type);
  EXPECT_EQ(kReadAccess, interface.properties[0].access);
}

}  // namespace chromeos_dbus_bindings