Home | History | Annotate | Download | only in chromeos-dbus-bindings
      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/xml_interface_parser.h"
      6 
      7 #include <base/files/file_path.h>
      8 #include <base/files/file_util.h>
      9 #include <base/files/scoped_temp_dir.h>
     10 #include <gtest/gtest.h>
     11 
     12 #include "chromeos-dbus-bindings/interface.h"
     13 
     14 using std::string;
     15 using std::vector;
     16 using testing::Test;
     17 
     18 namespace chromeos_dbus_bindings {
     19 
     20 namespace {
     21 
     22 const char kBadInterfaceFileContents0[] = "This has no resemblance to XML";
     23 const char kBadInterfaceFileContents1[] = "<node>";
     24 const char kGoodInterfaceFileContents[] = R"literal_string(
     25 <node name="/org/chromium/Test">
     26   <interface name="fi.w1.wpa_supplicant1.Interface">
     27     <method name="Scan">
     28       <arg name="args" type="a{sv}" direction="in"/>
     29       <annotation name="org.chromium.DBus.Method.Kind" value="async"/>
     30       <annotation name="org.chromium.DBus.Method.IncludeDBusMessage"
     31                   value="true"/>
     32     </method>
     33     <method name="GetBlob">
     34       <arg name="name" type="s"/>
     35       <arg name="data" type="ay" direction="out"/>
     36       <annotation name="org.chromium.DBus.Method.Const" value="true"/>
     37     </method>
     38     <property name="Capabilities" type="a{sv}" access="read"/>
     39     <signal name="BSSRemoved">
     40       <arg name="BSS" type="o"/>
     41     </signal>
     42   </interface>
     43   <interface name="DummyInterface" />
     44   <node name="/"/>
     45   <node/>
     46 </node>
     47 )literal_string";
     48 const char kInterfaceName[] = "fi.w1.wpa_supplicant1.Interface";
     49 const char kScanMethod[] = "Scan";
     50 const char kArgsArgument[] = "args";
     51 const char kArrayStringVariantType[] = "a{sv}";
     52 const char kGetBlobMethod[] = "GetBlob";
     53 const char kNameArgument[] = "name";
     54 const char kDataArgument[] = "data";
     55 const char kStringType[] = "s";
     56 const char kArrayByteType[] = "ay";
     57 const char kBssRemovedSignal[] = "BSSRemoved";
     58 const char kBssArgument[] = "BSS";
     59 const char kObjectType[] = "o";
     60 const char kCapabilitiesProperty[] = "Capabilities";
     61 const char kReadAccess[] = "read";
     62 }  // namespace
     63 
     64 class XmlInterfaceParserTest : public Test {
     65  protected:
     66   XmlInterfaceParser parser_;
     67 };
     68 
     69 TEST_F(XmlInterfaceParserTest, BadInputFile) {
     70   EXPECT_FALSE(parser_.ParseXmlInterfaceFile(kBadInterfaceFileContents0, {}));
     71   EXPECT_FALSE(parser_.ParseXmlInterfaceFile(kBadInterfaceFileContents1, {}));
     72 }
     73 
     74 TEST_F(XmlInterfaceParserTest, GoodInputFile) {
     75   EXPECT_TRUE(parser_.ParseXmlInterfaceFile(kGoodInterfaceFileContents,
     76                                             {"DummyInterface"}));
     77   const vector<Interface>& interfaces = parser_.interfaces();
     78   ASSERT_EQ(1u, interfaces.size());
     79   const Interface& interface = interfaces.back();
     80 
     81   EXPECT_EQ(kInterfaceName, interface.name);
     82   EXPECT_EQ("/org/chromium/Test", interface.path);
     83   ASSERT_EQ(2u, interface.methods.size());
     84   ASSERT_EQ(1u, interface.signals.size());
     85 
     86   // <method name="Scan">
     87   EXPECT_EQ(kScanMethod, interface.methods[0].name);
     88   EXPECT_EQ(Interface::Method::Kind::kAsync, interface.methods[0].kind);
     89   EXPECT_FALSE(interface.methods[0].is_const);
     90   EXPECT_TRUE(interface.methods[0].include_dbus_message);
     91   ASSERT_EQ(1u, interface.methods[0].input_arguments.size());
     92 
     93   // <arg name="args" type="a{sv}" direction="in"/>
     94   EXPECT_EQ(kArgsArgument, interface.methods[0].input_arguments[0].name);
     95   EXPECT_EQ(kArrayStringVariantType,
     96             interface.methods[0].input_arguments[0].type);
     97   EXPECT_EQ(0u, interface.methods[0].output_arguments.size());
     98 
     99   // <method name="GetBlob">
    100   EXPECT_EQ(kGetBlobMethod, interface.methods[1].name);
    101   EXPECT_EQ(Interface::Method::Kind::kNormal, interface.methods[1].kind);
    102   EXPECT_TRUE(interface.methods[1].is_const);
    103   EXPECT_FALSE(interface.methods[1].include_dbus_message);
    104   EXPECT_EQ(1u, interface.methods[1].input_arguments.size());
    105   EXPECT_EQ(1u, interface.methods[1].output_arguments.size());
    106 
    107   // <arg name="name" type="s"/>  (direction="in" is implicit)
    108   EXPECT_EQ(kNameArgument, interface.methods[1].input_arguments[0].name);
    109   EXPECT_EQ(kStringType, interface.methods[1].input_arguments[0].type);
    110 
    111   // <arg name="data" type="ay" direction="out"/>
    112   EXPECT_EQ(kDataArgument, interface.methods[1].output_arguments[0].name);
    113   EXPECT_EQ(kArrayByteType, interface.methods[1].output_arguments[0].type);
    114 
    115   // <signal name="BSSRemoved">
    116   EXPECT_EQ(kBssRemovedSignal, interface.signals[0].name);
    117   EXPECT_EQ(1u, interface.signals[0].arguments.size());
    118 
    119   // <arg name="BSS" type="o"/>
    120   EXPECT_EQ(kBssArgument, interface.signals[0].arguments[0].name);
    121   EXPECT_EQ(kObjectType, interface.signals[0].arguments[0].type);
    122 
    123   // <property name="Capabilities" type="s" access="read"/>
    124   EXPECT_EQ(kCapabilitiesProperty, interface.properties[0].name);
    125   EXPECT_EQ(kArrayStringVariantType, interface.properties[0].type);
    126   EXPECT_EQ(kReadAccess, interface.properties[0].access);
    127 }
    128 
    129 }  // namespace chromeos_dbus_bindings
    130