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