blob: 47dc9b1207fefc7fc3fb597ad949ea42a4376726 [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
5#include "base/memory/ref_counted.h"
6#include "base/memory/scoped_ptr.h"
7#include "dbus/bus.h"
8#include "dbus/message.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +09009#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090010#include "dbus/object_proxy.h"
11#include "dbus/test_service.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090014namespace dbus {
15
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090016// The end-to-end test exercises the synchronous APIs in ObjectProxy and
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090017// ExportedObject. The test will launch a thread for the service side
18// operations (i.e. ExportedObject side).
19class EndToEndSyncTest : public testing::Test {
20 public:
21 EndToEndSyncTest() {
22 }
23
dcheng7f5750d2014-12-30 03:30:17 +090024 void SetUp() override {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090025 // Start the test service;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090026 TestService::Options options;
27 test_service_.reset(new TestService(options));
satorux@chromium.org01fbb892011-08-20 10:07:17 +090028 ASSERT_TRUE(test_service_->StartService());
29 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
30 ASSERT_FALSE(test_service_->HasDBusThread());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090031
32 // Create the client.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090033 Bus::Options client_bus_options;
34 client_bus_options.bus_type = Bus::SESSION;
35 client_bus_options.connection_type = Bus::PRIVATE;
36 client_bus_ = new Bus(client_bus_options);
keybuk@google.combf4649a2012-02-15 06:29:06 +090037 object_proxy_ = client_bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +090038 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090039 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.org01fbb892011-08-20 10:07:17 +090040 ASSERT_FALSE(client_bus_->HasDBusThread());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090041 }
42
dcheng7f5750d2014-12-30 03:30:17 +090043 void TearDown() override {
satorux@chromium.orgd336d452011-09-02 15:56:23 +090044 test_service_->ShutdownAndBlock();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090045 test_service_->Stop();
46 client_bus_->ShutdownAndBlock();
47 }
48
49 protected:
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090050 scoped_ptr<TestService> test_service_;
51 scoped_refptr<Bus> client_bus_;
52 ObjectProxy* object_proxy_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090053};
54
55TEST_F(EndToEndSyncTest, Echo) {
56 const std::string kHello = "hello";
57
58 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090059 MethodCall method_call("org.chromium.TestInterface", "Echo");
60 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090061 writer.AppendString(kHello);
62
63 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090064 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
65 scoped_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090066 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
67 ASSERT_TRUE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090068
69 // Check the response. kHello should be echoed back.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090070 MessageReader reader(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090071 std::string returned_message;
72 ASSERT_TRUE(reader.PopString(&returned_message));
73 EXPECT_EQ(kHello, returned_message);
74}
75
76TEST_F(EndToEndSyncTest, Timeout) {
77 const std::string kHello = "hello";
78
79 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090080 MethodCall method_call("org.chromium.TestInterface", "DelayedEcho");
81 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090082 writer.AppendString(kHello);
83
satorux@chromium.org01fbb892011-08-20 10:07:17 +090084 // Call the method with timeout of 0ms.
satorux@chromium.org01fbb892011-08-20 10:07:17 +090085 const int timeout_ms = 0;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090086 scoped_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090087 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090088 // Should fail because of timeout.
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090089 ASSERT_FALSE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090090}
91
92TEST_F(EndToEndSyncTest, NonexistentMethod) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090093 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090094
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090095 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
96 scoped_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +090097 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
98 ASSERT_FALSE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090099}
100
101TEST_F(EndToEndSyncTest, BrokenMethod) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900102 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900103
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900104 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
105 scoped_ptr<Response> response(
satorux@chromium.orgffa83a92011-08-24 12:32:06 +0900106 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
107 ASSERT_FALSE(response.get());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900108}
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900109
110TEST_F(EndToEndSyncTest, InvalidObjectPath) {
111 // Trailing '/' is only allowed for the root path.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900112 const ObjectPath invalid_object_path("/org/chromium/TestObject/");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900113
114 // Replace object proxy with new one.
hashimotof4a4c0d2016-01-05 17:48:03 +0900115 object_proxy_ = client_bus_->GetObjectProxy(test_service_->service_name(),
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900116 invalid_object_path);
117
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900118 MethodCall method_call("org.chromium.TestInterface", "Echo");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900119
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900120 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
121 scoped_ptr<Response> response(
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900122 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
123 ASSERT_FALSE(response.get());
124}
125
126TEST_F(EndToEndSyncTest, InvalidServiceName) {
127 // Bus name cannot contain '/'.
128 const std::string invalid_service_name = ":1/2";
129
130 // Replace object proxy with new one.
131 object_proxy_ = client_bus_->GetObjectProxy(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900132 invalid_service_name, ObjectPath("org.chromium.TestObject"));
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900133
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900134 MethodCall method_call("org.chromium.TestInterface", "Echo");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900135
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900136 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
137 scoped_ptr<Response> response(
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900138 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms));
139 ASSERT_FALSE(response.get());
140}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900141
142} // namespace dbus