blob: 4eb9cd1950c8f88a6bac3a096a2c973433aef2f2 [file] [log] [blame]
keybuk@google.combf4649a2012-02-15 06:29:06 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng30c5a172016-04-09 07:55:04 +09005#include <memory>
6
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09007#include "base/memory/ref_counted.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09008#include "dbus/bus.h"
9#include "dbus/message.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090010#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090011#include "dbus/object_proxy.h"
12#include "dbus/test_service.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090015namespace dbus {
16
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090017// The end-to-end test exercises the synchronous APIs in ObjectProxy and
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090018// ExportedObject. The test will launch a thread for the service side
19// operations (i.e. ExportedObject side).
20class EndToEndSyncTest : public testing::Test {
21 public:
22 EndToEndSyncTest() {
23 }
24
dcheng7f5750d2014-12-30 03:30:17 +090025 void SetUp() override {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090026 // Start the test service;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090027 TestService::Options options;
28 test_service_.reset(new TestService(options));
satorux@chromium.org01fbb892011-08-20 10:07:17 +090029 ASSERT_TRUE(test_service_->StartService());
30 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
31 ASSERT_FALSE(test_service_->HasDBusThread());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090032
33 // Create the client.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090034 Bus::Options client_bus_options;
35 client_bus_options.bus_type = Bus::SESSION;
36 client_bus_options.connection_type = Bus::PRIVATE;
37 client_bus_ = new Bus(client_bus_options);
keybuk@google.combf4649a2012-02-15 06:29:06 +090038 object_proxy_ = client_bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +090039 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090040 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.org01fbb892011-08-20 10:07:17 +090041 ASSERT_FALSE(client_bus_->HasDBusThread());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090042 }
43
dcheng7f5750d2014-12-30 03:30:17 +090044 void TearDown() override {
satorux@chromium.orgd336d452011-09-02 15:56:23 +090045 test_service_->ShutdownAndBlock();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090046 test_service_->Stop();
47 client_bus_->ShutdownAndBlock();
48 }
49
50 protected:
dcheng30c5a172016-04-09 07:55:04 +090051 std::unique_ptr<TestService> test_service_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090052 scoped_refptr<Bus> client_bus_;
53 ObjectProxy* object_proxy_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090054};
55
56TEST_F(EndToEndSyncTest, Echo) {
57 const std::string kHello = "hello";
58
59 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090060 MethodCall method_call("org.chromium.TestInterface", "Echo");
61 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090062 writer.AppendString(kHello);
63
64 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090065 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
dcheng30c5a172016-04-09 07:55:04 +090066 std::unique_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090067 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
68 ASSERT_TRUE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090069
70 // Check the response. kHello should be echoed back.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090071 MessageReader reader(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090072 std::string returned_message;
73 ASSERT_TRUE(reader.PopString(&returned_message));
74 EXPECT_EQ(kHello, returned_message);
75}
76
77TEST_F(EndToEndSyncTest, Timeout) {
78 const std::string kHello = "hello";
79
80 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090081 MethodCall method_call("org.chromium.TestInterface", "DelayedEcho");
82 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090083 writer.AppendString(kHello);
84
satorux@chromium.org01fbb892011-08-20 10:07:17 +090085 // Call the method with timeout of 0ms.
satorux@chromium.org01fbb892011-08-20 10:07:17 +090086 const int timeout_ms = 0;
dcheng30c5a172016-04-09 07:55:04 +090087 std::unique_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090088 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090089 // Should fail because of timeout.
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090090 ASSERT_FALSE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090091}
92
93TEST_F(EndToEndSyncTest, NonexistentMethod) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090094 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090095
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090096 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
dcheng30c5a172016-04-09 07:55:04 +090097 std::unique_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090098 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
99 ASSERT_FALSE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900100}
101
102TEST_F(EndToEndSyncTest, BrokenMethod) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900103 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900104
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900105 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
dcheng30c5a172016-04-09 07:55:04 +0900106 std::unique_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +0900107 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
108 ASSERT_FALSE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900109}
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900110
111TEST_F(EndToEndSyncTest, InvalidObjectPath) {
112 // Trailing '/' is only allowed for the root path.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900113 const ObjectPath invalid_object_path("/org/chromium/TestObject/");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900114
115 // Replace object proxy with new one.
hashimotof4a4c0d2016-01-05 17:48:03 +0900116 object_proxy_ = client_bus_->GetObjectProxy(test_service_->service_name(),
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900117 invalid_object_path);
118
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900119 MethodCall method_call("org.chromium.TestInterface", "Echo");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900120
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900121 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
dcheng30c5a172016-04-09 07:55:04 +0900122 std::unique_ptr<Response> response(
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900123 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
124 ASSERT_FALSE(response.get());
125}
126
127TEST_F(EndToEndSyncTest, InvalidServiceName) {
128 // Bus name cannot contain '/'.
129 const std::string invalid_service_name = ":1/2";
130
131 // Replace object proxy with new one.
132 object_proxy_ = client_bus_->GetObjectProxy(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900133 invalid_service_name, ObjectPath("org.chromium.TestObject"));
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900134
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900135 MethodCall method_call("org.chromium.TestInterface", "Echo");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900136
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900137 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
dcheng30c5a172016-04-09 07:55:04 +0900138 std::unique_ptr<Response> response(
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900139 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
140 ASSERT_FALSE(response.get());
141}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900142
143} // namespace dbus