blob: 9cddea48591100108c0ade483afa7cb0b87d116e [file] [log] [blame]
[email protected]216ed0b2012-02-14 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/memory/ref_counted.h"
6#include "base/memory/scoped_ptr.h"
7#include "dbus/bus.h"
8#include "dbus/message.h"
[email protected]216ed0b2012-02-14 21:29:069#include "dbus/object_path.h"
[email protected]a51076112011-08-17 20:58:1210#include "dbus/object_proxy.h"
11#include "dbus/test_service.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]829f0e4c2011-08-31 18:02:4314// The end-to-end test exercises the synchronous APIs in ObjectProxy and
[email protected]a51076112011-08-17 20:58:1215// ExportedObject. The test will launch a thread for the service side
16// operations (i.e. ExportedObject side).
17class EndToEndSyncTest : public testing::Test {
18 public:
19 EndToEndSyncTest() {
20 }
21
[email protected]ea78b1e2011-08-27 07:26:3422 virtual void SetUp() {
[email protected]a51076112011-08-17 20:58:1223 // Start the test service;
[email protected]12f97662011-08-20 01:07:1724 dbus::TestService::Options options;
25 test_service_.reset(new dbus::TestService(options));
26 ASSERT_TRUE(test_service_->StartService());
27 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
28 ASSERT_FALSE(test_service_->HasDBusThread());
[email protected]a51076112011-08-17 20:58:1229
30 // Create the client.
31 dbus::Bus::Options client_bus_options;
32 client_bus_options.bus_type = dbus::Bus::SESSION;
33 client_bus_options.connection_type = dbus::Bus::PRIVATE;
34 client_bus_ = new dbus::Bus(client_bus_options);
[email protected]216ed0b2012-02-14 21:29:0635 object_proxy_ = client_bus_->GetObjectProxy(
36 "org.chromium.TestService",
37 dbus::ObjectPath("/org/chromium/TestObject"));
[email protected]12f97662011-08-20 01:07:1738 ASSERT_FALSE(client_bus_->HasDBusThread());
[email protected]a51076112011-08-17 20:58:1239 }
40
[email protected]ea78b1e2011-08-27 07:26:3441 virtual void TearDown() {
[email protected]e20d39fe2011-09-02 06:56:2342 test_service_->ShutdownAndBlock();
[email protected]a51076112011-08-17 20:58:1243 test_service_->Stop();
44 client_bus_->ShutdownAndBlock();
45 }
46
47 protected:
48 scoped_ptr<dbus::TestService> test_service_;
49 scoped_refptr<dbus::Bus> client_bus_;
50 dbus::ObjectProxy* object_proxy_;
51};
52
53TEST_F(EndToEndSyncTest, Echo) {
54 const std::string kHello = "hello";
55
56 // Create the method call.
57 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
58 dbus::MessageWriter writer(&method_call);
59 writer.AppendString(kHello);
60
61 // Call the method.
[email protected]a51076112011-08-17 20:58:1262 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]06ead872011-08-24 03:32:0663 scoped_ptr<dbus::Response> response(
64 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
65 ASSERT_TRUE(response.get());
[email protected]a51076112011-08-17 20:58:1266
67 // Check the response. kHello should be echoed back.
[email protected]06ead872011-08-24 03:32:0668 dbus::MessageReader reader(response.get());
[email protected]a51076112011-08-17 20:58:1269 std::string returned_message;
70 ASSERT_TRUE(reader.PopString(&returned_message));
71 EXPECT_EQ(kHello, returned_message);
72}
73
74TEST_F(EndToEndSyncTest, Timeout) {
75 const std::string kHello = "hello";
76
77 // Create the method call.
78 dbus::MethodCall method_call("org.chromium.TestInterface", "DelayedEcho");
79 dbus::MessageWriter writer(&method_call);
80 writer.AppendString(kHello);
81
[email protected]12f97662011-08-20 01:07:1782 // Call the method with timeout of 0ms.
[email protected]12f97662011-08-20 01:07:1783 const int timeout_ms = 0;
[email protected]06ead872011-08-24 03:32:0684 scoped_ptr<dbus::Response> response(
85 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
[email protected]a51076112011-08-17 20:58:1286 // Should fail because of timeout.
[email protected]06ead872011-08-24 03:32:0687 ASSERT_FALSE(response.get());
[email protected]a51076112011-08-17 20:58:1288}
89
90TEST_F(EndToEndSyncTest, NonexistentMethod) {
91 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
92
[email protected]a51076112011-08-17 20:58:1293 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]06ead872011-08-24 03:32:0694 scoped_ptr<dbus::Response> response(
95 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
96 ASSERT_FALSE(response.get());
[email protected]a51076112011-08-17 20:58:1297}
98
99TEST_F(EndToEndSyncTest, BrokenMethod) {
100 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
101
[email protected]a51076112011-08-17 20:58:12102 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]06ead872011-08-24 03:32:06103 scoped_ptr<dbus::Response> response(
104 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
105 ASSERT_FALSE(response.get());
[email protected]a51076112011-08-17 20:58:12106}
[email protected]ca72ff22012-05-23 06:55:22107
108TEST_F(EndToEndSyncTest, InvalidObjectPath) {
109 // Trailing '/' is only allowed for the root path.
110 const dbus::ObjectPath invalid_object_path("/org/chromium/TestObject/");
111
112 // Replace object proxy with new one.
113 object_proxy_ = client_bus_->GetObjectProxy("org.chromium.TestService",
114 invalid_object_path);
115
116 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
117
118 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
119 scoped_ptr<dbus::Response> response(
120 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
121 ASSERT_FALSE(response.get());
122}
123
124TEST_F(EndToEndSyncTest, InvalidServiceName) {
125 // Bus name cannot contain '/'.
126 const std::string invalid_service_name = ":1/2";
127
128 // Replace object proxy with new one.
129 object_proxy_ = client_bus_->GetObjectProxy(
130 invalid_service_name, dbus::ObjectPath("org.chromium.TestObject"));
131
132 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
133
134 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
135 scoped_ptr<dbus::Response> response(
136 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
137 ASSERT_FALSE(response.get());
138}