[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [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" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 9 | #include "dbus/object_path.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 10 | #include "dbus/object_proxy.h" |
| 11 | #include "dbus/test_service.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 829f0e4c | 2011-08-31 18:02:43 | [diff] [blame] | 14 | // The end-to-end test exercises the synchronous APIs in ObjectProxy and |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 15 | // ExportedObject. The test will launch a thread for the service side |
| 16 | // operations (i.e. ExportedObject side). |
| 17 | class EndToEndSyncTest : public testing::Test { |
| 18 | public: |
| 19 | EndToEndSyncTest() { |
| 20 | } |
| 21 | |
[email protected] | ea78b1e | 2011-08-27 07:26:34 | [diff] [blame] | 22 | virtual void SetUp() { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 23 | // Start the test service; |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 24 | 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] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 29 | |
| 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] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 35 | object_proxy_ = client_bus_->GetObjectProxy( |
| 36 | "org.chromium.TestService", |
| 37 | dbus::ObjectPath("/org/chromium/TestObject")); |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 38 | ASSERT_FALSE(client_bus_->HasDBusThread()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | ea78b1e | 2011-08-27 07:26:34 | [diff] [blame] | 41 | virtual void TearDown() { |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 42 | test_service_->ShutdownAndBlock(); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 43 | 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 | |
| 53 | TEST_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] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 62 | const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 06ead87 | 2011-08-24 03:32:06 | [diff] [blame] | 63 | scoped_ptr<dbus::Response> response( |
| 64 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 65 | ASSERT_TRUE(response.get()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 66 | |
| 67 | // Check the response. kHello should be echoed back. |
[email protected] | 06ead87 | 2011-08-24 03:32:06 | [diff] [blame] | 68 | dbus::MessageReader reader(response.get()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 69 | std::string returned_message; |
| 70 | ASSERT_TRUE(reader.PopString(&returned_message)); |
| 71 | EXPECT_EQ(kHello, returned_message); |
| 72 | } |
| 73 | |
| 74 | TEST_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] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 82 | // Call the method with timeout of 0ms. |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 83 | const int timeout_ms = 0; |
[email protected] | 06ead87 | 2011-08-24 03:32:06 | [diff] [blame] | 84 | scoped_ptr<dbus::Response> response( |
| 85 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 86 | // Should fail because of timeout. |
[email protected] | 06ead87 | 2011-08-24 03:32:06 | [diff] [blame] | 87 | ASSERT_FALSE(response.get()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(EndToEndSyncTest, NonexistentMethod) { |
| 91 | dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); |
| 92 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 93 | const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 06ead87 | 2011-08-24 03:32:06 | [diff] [blame] | 94 | scoped_ptr<dbus::Response> response( |
| 95 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 96 | ASSERT_FALSE(response.get()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(EndToEndSyncTest, BrokenMethod) { |
| 100 | dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); |
| 101 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 102 | const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 06ead87 | 2011-08-24 03:32:06 | [diff] [blame] | 103 | scoped_ptr<dbus::Response> response( |
| 104 | object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 105 | ASSERT_FALSE(response.get()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 106 | } |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 107 | |
| 108 | TEST_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 | |
| 124 | TEST_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 | } |