blob: 696b76f4c68e5ede9ad4693808e6ae7cde2ef632 [file] [log] [blame]
Christopher Wileycec927c2014-04-15 16:26:47 -07001// 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 "buffet/exported_object_manager.h"
6
7#include <base/bind.h>
8#include <dbus/mock_bus.h>
9#include <dbus/mock_exported_object.h>
10#include <dbus/object_manager.h>
11#include <dbus/object_path.h>
12#include <gtest/gtest.h>
13
14using ::testing::AnyNumber;
15using ::testing::InSequence;
16using ::testing::Invoke;
17using ::testing::Return;
18using ::testing::_;
19
20namespace buffet {
21
22namespace dbus_utils {
23
24namespace {
25
26const dbus::ObjectPath kTestPath(std::string("/test/om_path"));
27const dbus::ObjectPath kClaimedTestPath(std::string("/test/claimed_path"));
28const std::string kClaimedInterface("claimed.interface");
29const std::string kTestPropertyName("PropertyName");
30const std::string kTestPropertyValue("PropertyValue");
31
32void WriteTestPropertyDict(dbus::MessageWriter* writer) {
33 dbus::MessageWriter all_properties(nullptr);
34 dbus::MessageWriter each_property(nullptr);
35 writer->OpenArray("{sv}", &all_properties);
36 all_properties.OpenDictEntry(&each_property);
37 each_property.AppendString(kTestPropertyName);
38 each_property.AppendVariantOfString(kTestPropertyValue);
39 all_properties.CloseContainer(&each_property);
40 writer->CloseContainer(&all_properties);
41}
42
43void ReadTestPropertyDict(dbus::MessageReader* reader) {
44 dbus::MessageReader all_properties(nullptr);
45 dbus::MessageReader each_property(nullptr);
46 ASSERT_TRUE(reader->PopArray(&all_properties));
47 ASSERT_TRUE(all_properties.PopDictEntry(&each_property));
48 std::string property_name;
49 std::string property_value;
50 ASSERT_TRUE(each_property.PopString(&property_name));
51 ASSERT_TRUE(each_property.PopVariantOfString(&property_value));
52 EXPECT_FALSE(each_property.HasMoreData());
53 EXPECT_FALSE(all_properties.HasMoreData());
54 EXPECT_EQ(property_name, kTestPropertyName);
55 EXPECT_EQ(property_value, kTestPropertyValue);
56}
57
58void VerifyInterfaceClaimSignal(dbus::Signal* signal) {
59 EXPECT_EQ(signal->GetInterface(),
60 std::string(dbus::kObjectManagerInterface));
61 EXPECT_EQ(signal->GetMember(),
62 std::string(dbus::kObjectManagerInterfacesAdded));
63 // org.freedesktop.DBus.ObjectManager.InterfacesAdded (
64 // OBJPATH object_path,
65 // DICT<STRING,DICT<STRING,VARIANT>> interfaces_and_properties);
66 dbus::MessageReader reader(signal);
67 dbus::MessageReader all_interfaces(nullptr);
68 dbus::MessageReader each_interface(nullptr);
69 dbus::ObjectPath path;
70 ASSERT_TRUE(reader.PopObjectPath(&path));
71 ASSERT_TRUE(reader.PopArray(&all_interfaces));
72 ASSERT_TRUE(all_interfaces.PopDictEntry(&each_interface));
73 std::string interface_name;
74 ASSERT_TRUE(each_interface.PopString(&interface_name));
75 ReadTestPropertyDict(&each_interface);
76 EXPECT_FALSE(each_interface.HasMoreData());
77 EXPECT_FALSE(all_interfaces.HasMoreData());
78 EXPECT_FALSE(reader.HasMoreData());
79 EXPECT_EQ(interface_name, kClaimedInterface);
80 EXPECT_EQ(path, kClaimedTestPath);
81}
82
83void VerifyInterfaceDropSignal(dbus::Signal* signal) {
84 EXPECT_EQ(signal->GetInterface(),
85 std::string(dbus::kObjectManagerInterface));
86 EXPECT_EQ(signal->GetMember(),
87 std::string(dbus::kObjectManagerInterfacesRemoved));
88 // org.freedesktop.DBus.ObjectManager.InterfacesRemoved (
89 // OBJPATH object_path, ARRAY<STRING> interfaces);
90 dbus::MessageReader reader(signal);
91 dbus::MessageReader each_interface(nullptr);
92 dbus::ObjectPath path;
93 ASSERT_TRUE(reader.PopObjectPath(&path));
94 ASSERT_TRUE(reader.PopArray(&each_interface));
95 std::string interface_name;
96 ASSERT_TRUE(each_interface.PopString(&interface_name));
97 EXPECT_FALSE(each_interface.HasMoreData());
98 EXPECT_FALSE(reader.HasMoreData());
99 EXPECT_EQ(interface_name, kClaimedInterface);
100 EXPECT_EQ(path, kClaimedTestPath);
101}
102
103} // namespace
104
105class ExportedObjectManagerTest: public ::testing::Test {
106 public:
107 virtual void SetUp() {
108 dbus::Bus::Options options;
109 options.bus_type = dbus::Bus::SYSTEM;
110 bus_ = new dbus::MockBus(options);
111 // By default, don't worry about threading assertions.
112 EXPECT_CALL(*bus_, AssertOnOriginThread()).Times(AnyNumber());
113 EXPECT_CALL(*bus_, AssertOnDBusThread()).Times(AnyNumber());
114 // Use a mock exported object.
115 mock_exported_object_ = new dbus::MockExportedObject(
116 bus_.get(), kTestPath);
117 EXPECT_CALL(*bus_, GetExportedObject(kTestPath))
118 .Times(1).WillOnce(Return(mock_exported_object_.get()));
119 om_.reset(new ExportedObjectManager(bus_.get(), kTestPath));
120 property_writer_ = base::Bind(&WriteTestPropertyDict);
121 response_storer_ = base::Bind(&ExportedObjectManagerTest::StoreResponse,
122 base::Unretained(this));
123 }
124
125 void StoreResponse(scoped_ptr<dbus::Response> method_response) {
126 last_response_.reset(method_response.release());
127 }
128
129 void CallHandleGetManagedObjects(
130 dbus::MethodCall* method_call,
131 dbus::ExportedObject::ResponseSender sender) {
132 om_->HandleGetManagedObjects(method_call, response_storer_);
133 }
134
135 scoped_refptr<dbus::MockBus> bus_;
136 scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
137 scoped_ptr<ExportedObjectManager> om_;
138 ExportedObjectManager::PropertyWriter property_writer_;
139 dbus::ExportedObject::ResponseSender response_storer_;
140 scoped_ptr<dbus::Response> last_response_;
141};
142
143TEST_F(ExportedObjectManagerTest, ClaimInterfaceSendsSignals) {
144 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
145 .Times(1).WillOnce(Invoke(&VerifyInterfaceClaimSignal));
146 om_->ClaimInterface(kClaimedTestPath, kClaimedInterface, property_writer_);
147}
148
149TEST_F(ExportedObjectManagerTest, ReleaseInterfaceSendsSignals) {
150 InSequence dummy;
151 EXPECT_CALL(*mock_exported_object_, SendSignal(_)).Times(1);
152 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
153 .Times(1).WillOnce(Invoke(&VerifyInterfaceDropSignal));
154 om_->ClaimInterface(kClaimedTestPath, kClaimedInterface, property_writer_);
155 om_->ReleaseInterface(kClaimedTestPath, kClaimedInterface);
156}
157
158TEST_F(ExportedObjectManagerTest, GetManagedObjectsResponseEmptyCorrectness) {
159 dbus::MethodCall method_call(dbus::kObjectManagerInterface,
160 dbus::kObjectManagerGetManagedObjects);
161 method_call.SetSerial(123);
162 CallHandleGetManagedObjects(&method_call, response_storer_);
163 dbus::MessageReader reader(last_response_.get());
164 dbus::MessageReader all_paths(nullptr);
165 ASSERT_TRUE(reader.PopArray(&all_paths));
166 EXPECT_FALSE(reader.HasMoreData());
167}
168
169TEST_F(ExportedObjectManagerTest, GetManagedObjectsResponseCorrectness) {
170 // org.freedesktop.DBus.ObjectManager.GetManagedObjects (
171 // out DICT<OBJPATH,
172 // DICT<STRING,
173 // DICT<STRING,VARIANT>>> )
174 dbus::MethodCall method_call(dbus::kObjectManagerInterface,
175 dbus::kObjectManagerGetManagedObjects);
176 method_call.SetSerial(123);
177 EXPECT_CALL(*mock_exported_object_, SendSignal(_)).Times(1);
178 om_->ClaimInterface(kClaimedTestPath, kClaimedInterface, property_writer_);
179 CallHandleGetManagedObjects(&method_call, response_storer_);
180 dbus::MessageReader reader(last_response_.get());
181 dbus::MessageReader all_paths(nullptr);
182 dbus::MessageReader each_path(nullptr);
183 dbus::MessageReader all_interfaces(nullptr);
184 dbus::MessageReader each_interface(nullptr);
185 ASSERT_TRUE(reader.PopArray(&all_paths));
186 ASSERT_TRUE(all_paths.PopDictEntry(&each_path));
187 dbus::ObjectPath path;
188 ASSERT_TRUE(each_path.PopObjectPath(&path));
189 ASSERT_TRUE(each_path.PopArray(&all_interfaces));
190 ASSERT_TRUE(all_interfaces.PopDictEntry(&each_interface));
191 std::string interface_name;
192 ASSERT_TRUE(each_interface.PopString(&interface_name));
193 ReadTestPropertyDict(&each_interface);
194 EXPECT_FALSE(each_interface.HasMoreData());
195 EXPECT_FALSE(all_interfaces.HasMoreData());
196 EXPECT_FALSE(each_path.HasMoreData());
197 EXPECT_FALSE(all_paths.HasMoreData());
198 EXPECT_FALSE(reader.HasMoreData());
199 EXPECT_EQ(path, kClaimedTestPath);
200 EXPECT_EQ(interface_name, kClaimedInterface);
201}
202
203} // namespace dbus_utils
204
205} // namespace buffet