keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 5 | #include <memory> |
| 6 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 7 | #include "base/memory/ref_counted.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 8 | #include "dbus/bus.h" |
| 9 | #include "dbus/message.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 10 | #include "dbus/object_path.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 11 | #include "dbus/object_proxy.h" |
| 12 | #include "dbus/test_service.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 15 | namespace dbus { |
| 16 | |
satorux@chromium.org | c6ac757 | 2011-09-01 03:02:43 +0900 | [diff] [blame] | 17 | // The end-to-end test exercises the synchronous APIs in ObjectProxy and |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 18 | // ExportedObject. The test will launch a thread for the service side |
| 19 | // operations (i.e. ExportedObject side). |
| 20 | class EndToEndSyncTest : public testing::Test { |
| 21 | public: |
| 22 | EndToEndSyncTest() { |
| 23 | } |
| 24 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 25 | void SetUp() override { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 26 | // Start the test service; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 27 | TestService::Options options; |
| 28 | test_service_.reset(new TestService(options)); |
satorux@chromium.org | 01fbb89 | 2011-08-20 10:07:17 +0900 | [diff] [blame] | 29 | ASSERT_TRUE(test_service_->StartService()); |
| 30 | ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 31 | ASSERT_FALSE(test_service_->HasDBusThread()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 32 | |
| 33 | // Create the client. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 34 | 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.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 38 | object_proxy_ = client_bus_->GetObjectProxy( |
hashimoto | f4a4c0d | 2016-01-05 17:48:03 +0900 | [diff] [blame] | 39 | test_service_->service_name(), |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 40 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | 01fbb89 | 2011-08-20 10:07:17 +0900 | [diff] [blame] | 41 | ASSERT_FALSE(client_bus_->HasDBusThread()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 42 | } |
| 43 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 44 | void TearDown() override { |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 45 | test_service_->ShutdownAndBlock(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 46 | test_service_->Stop(); |
| 47 | client_bus_->ShutdownAndBlock(); |
| 48 | } |
| 49 | |
| 50 | protected: |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 51 | std::unique_ptr<TestService> test_service_; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 52 | scoped_refptr<Bus> client_bus_; |
| 53 | ObjectProxy* object_proxy_; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | TEST_F(EndToEndSyncTest, Echo) { |
| 57 | const std::string kHello = "hello"; |
| 58 | |
| 59 | // Create the method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 60 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 61 | MessageWriter writer(&method_call); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 62 | writer.AppendString(kHello); |
| 63 | |
| 64 | // Call the method. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 65 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 66 | std::unique_ptr<Response> response( |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 67 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 68 | ASSERT_TRUE(response.get()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 69 | |
| 70 | // Check the response. kHello should be echoed back. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 71 | MessageReader reader(response.get()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 72 | std::string returned_message; |
| 73 | ASSERT_TRUE(reader.PopString(&returned_message)); |
| 74 | EXPECT_EQ(kHello, returned_message); |
| 75 | } |
| 76 | |
| 77 | TEST_F(EndToEndSyncTest, Timeout) { |
| 78 | const std::string kHello = "hello"; |
| 79 | |
| 80 | // Create the method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 81 | MethodCall method_call("org.chromium.TestInterface", "DelayedEcho"); |
| 82 | MessageWriter writer(&method_call); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 83 | writer.AppendString(kHello); |
| 84 | |
satorux@chromium.org | 01fbb89 | 2011-08-20 10:07:17 +0900 | [diff] [blame] | 85 | // Call the method with timeout of 0ms. |
satorux@chromium.org | 01fbb89 | 2011-08-20 10:07:17 +0900 | [diff] [blame] | 86 | const int timeout_ms = 0; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 87 | std::unique_ptr<Response> response( |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 88 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 89 | // Should fail because of timeout. |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 90 | ASSERT_FALSE(response.get()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(EndToEndSyncTest, NonexistentMethod) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 94 | MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 95 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 96 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 97 | std::unique_ptr<Response> response( |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 98 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 99 | ASSERT_FALSE(response.get()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(EndToEndSyncTest, BrokenMethod) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 103 | MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 104 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 105 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 106 | std::unique_ptr<Response> response( |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 107 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 108 | ASSERT_FALSE(response.get()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 109 | } |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 110 | |
| 111 | TEST_F(EndToEndSyncTest, InvalidObjectPath) { |
| 112 | // Trailing '/' is only allowed for the root path. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 113 | const ObjectPath invalid_object_path("/org/chromium/TestObject/"); |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 114 | |
| 115 | // Replace object proxy with new one. |
hashimoto | f4a4c0d | 2016-01-05 17:48:03 +0900 | [diff] [blame] | 116 | object_proxy_ = client_bus_->GetObjectProxy(test_service_->service_name(), |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 117 | invalid_object_path); |
| 118 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 119 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 120 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 121 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 122 | std::unique_ptr<Response> response( |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 123 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 124 | ASSERT_FALSE(response.get()); |
| 125 | } |
| 126 | |
| 127 | TEST_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.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 133 | invalid_service_name, ObjectPath("org.chromium.TestObject")); |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 134 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 135 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 136 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 137 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame^] | 138 | std::unique_ptr<Response> response( |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 139 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 140 | ASSERT_FALSE(response.get()); |
| 141 | } |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 142 | |
| 143 | } // namespace dbus |