[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 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/bind.h" |
| 6 | #include "base/memory/scoped_ptr.h" |
| 7 | #include "base/message_loop.h" |
| 8 | #include "base/metrics/histogram.h" |
| 9 | #include "base/metrics/histogram_samples.h" |
| 10 | #include "base/metrics/statistics_recorder.h" |
| 11 | #include "base/test/test_timeouts.h" |
| 12 | #include "base/threading/platform_thread.h" |
| 13 | #include "base/threading/thread_restrictions.h" |
| 14 | #include "dbus/bus.h" |
| 15 | #include "dbus/message.h" |
| 16 | #include "dbus/object_proxy.h" |
| 17 | #include "dbus/test_service.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | // The test for sender verification in ObjectProxy. |
| 21 | class SignalSenderVerificationTest : public testing::Test { |
| 22 | public: |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 23 | SignalSenderVerificationTest() |
| 24 | : on_name_owner_changed_called_(false), |
| 25 | on_ownership_called_(false) { |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | virtual void SetUp() { |
| 29 | base::StatisticsRecorder::Initialize(); |
| 30 | |
| 31 | // Make the main thread not to allow IO. |
| 32 | base::ThreadRestrictions::SetIOAllowed(false); |
| 33 | |
| 34 | // Start the D-Bus thread. |
| 35 | dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 36 | base::Thread::Options thread_options; |
| 37 | thread_options.message_loop_type = MessageLoop::TYPE_IO; |
| 38 | ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
| 39 | |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 40 | // Create the client, using the D-Bus thread. |
| 41 | dbus::Bus::Options bus_options; |
| 42 | bus_options.bus_type = dbus::Bus::SESSION; |
| 43 | bus_options.connection_type = dbus::Bus::PRIVATE; |
| 44 | bus_options.dbus_thread_message_loop_proxy = |
| 45 | dbus_thread_->message_loop_proxy(); |
| 46 | bus_ = new dbus::Bus(bus_options); |
| 47 | object_proxy_ = bus_->GetObjectProxy( |
| 48 | "org.chromium.TestService", |
| 49 | dbus::ObjectPath("/org/chromium/TestObject")); |
| 50 | ASSERT_TRUE(bus_->HasDBusThread()); |
| 51 | |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 52 | object_proxy_->SetNameOwnerChangedCallback( |
| 53 | base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, |
[email protected] | 1c31240 | 2012-11-22 00:30:16 | [diff] [blame] | 54 | base::Unretained(this))); |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 55 | |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 56 | // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 57 | // the remote object. |
| 58 | object_proxy_->ConnectToSignal( |
| 59 | "org.chromium.TestInterface", |
| 60 | "Test", |
| 61 | base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
| 62 | base::Unretained(this)), |
| 63 | base::Bind(&SignalSenderVerificationTest::OnConnected, |
| 64 | base::Unretained(this))); |
| 65 | // Wait until the object proxy is connected to the signal. |
| 66 | message_loop_.Run(); |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 67 | |
| 68 | // Start the test service, using the D-Bus thread. |
| 69 | dbus::TestService::Options options; |
| 70 | options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); |
| 71 | test_service_.reset(new dbus::TestService(options)); |
| 72 | ASSERT_TRUE(test_service_->StartService()); |
| 73 | ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 74 | ASSERT_TRUE(test_service_->HasDBusThread()); |
| 75 | ASSERT_TRUE(test_service_->has_ownership()); |
| 76 | |
| 77 | // Same setup for the second TestService. This service should not have the |
| 78 | // ownership of the name at this point. |
| 79 | test_service2_.reset(new dbus::TestService(options)); |
| 80 | ASSERT_TRUE(test_service2_->StartService()); |
| 81 | ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); |
| 82 | ASSERT_TRUE(test_service2_->HasDBusThread()); |
| 83 | ASSERT_FALSE(test_service2_->has_ownership()); |
| 84 | |
| 85 | // The name should be owned and known at this point. |
| 86 | if (!on_name_owner_changed_called_) |
| 87 | message_loop_.Run(); |
| 88 | ASSERT_FALSE(latest_name_owner_.empty()); |
| 89 | |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | virtual void TearDown() { |
| 93 | bus_->ShutdownOnDBusThreadAndBlock(); |
| 94 | |
| 95 | // Shut down the service. |
| 96 | test_service_->ShutdownAndBlock(); |
| 97 | test_service2_->ShutdownAndBlock(); |
| 98 | |
| 99 | // Reset to the default. |
| 100 | base::ThreadRestrictions::SetIOAllowed(true); |
| 101 | |
| 102 | // Stopping a thread is considered an IO operation, so do this after |
| 103 | // allowing IO. |
| 104 | test_service_->Stop(); |
| 105 | test_service2_->Stop(); |
| 106 | } |
| 107 | |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 108 | void OnOwnership(bool expected, bool success) { |
| 109 | ASSERT_EQ(expected, success); |
| 110 | // PostTask to quit the MessageLoop as this is called from D-Bus thread. |
| 111 | message_loop_.PostTask( |
| 112 | FROM_HERE, |
| 113 | base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, |
| 114 | base::Unretained(this))); |
| 115 | } |
| 116 | |
| 117 | void OnOwnershipInternal() { |
| 118 | on_ownership_called_ = true; |
| 119 | message_loop_.Quit(); |
| 120 | } |
| 121 | |
[email protected] | 1c31240 | 2012-11-22 00:30:16 | [diff] [blame] | 122 | void OnNameOwnerChanged(dbus::Signal* signal) { |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 123 | dbus::MessageReader reader(signal); |
| 124 | std::string name, old_owner, new_owner; |
| 125 | ASSERT_TRUE(reader.PopString(&name)); |
| 126 | ASSERT_TRUE(reader.PopString(&old_owner)); |
| 127 | ASSERT_TRUE(reader.PopString(&new_owner)); |
| 128 | latest_name_owner_ = new_owner; |
[email protected] | 1c31240 | 2012-11-22 00:30:16 | [diff] [blame] | 129 | on_name_owner_changed_called_ = true; |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 130 | message_loop_.Quit(); |
| 131 | } |
| 132 | |
[email protected] | 1c31240 | 2012-11-22 00:30:16 | [diff] [blame] | 133 | protected: |
| 134 | |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 135 | // Called when the "Test" signal is received, in the main thread. |
| 136 | // Copy the string payload to |test_signal_string_|. |
| 137 | void OnTestSignal(dbus::Signal* signal) { |
| 138 | dbus::MessageReader reader(signal); |
| 139 | ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
| 140 | message_loop_.Quit(); |
| 141 | } |
| 142 | |
| 143 | // Called when connected to the signal. |
| 144 | void OnConnected(const std::string& interface_name, |
| 145 | const std::string& signal_name, |
| 146 | bool success) { |
| 147 | ASSERT_TRUE(success); |
| 148 | message_loop_.Quit(); |
| 149 | } |
| 150 | |
| 151 | // Wait for the hey signal to be received. |
| 152 | void WaitForTestSignal() { |
| 153 | // OnTestSignal() will quit the message loop. |
| 154 | message_loop_.Run(); |
| 155 | } |
| 156 | |
| 157 | MessageLoop message_loop_; |
| 158 | scoped_ptr<base::Thread> dbus_thread_; |
| 159 | scoped_refptr<dbus::Bus> bus_; |
| 160 | dbus::ObjectProxy* object_proxy_; |
| 161 | scoped_ptr<dbus::TestService> test_service_; |
| 162 | scoped_ptr<dbus::TestService> test_service2_; |
| 163 | // Text message from "Test" signal. |
| 164 | std::string test_signal_string_; |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 165 | |
| 166 | // The known latest name owner of TestService. Updated in OnNameOwnerChanged. |
| 167 | std::string latest_name_owner_; |
| 168 | |
| 169 | // Boolean flags to record callback calls. |
| 170 | bool on_name_owner_changed_called_; |
| 171 | bool on_ownership_called_; |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { |
| 175 | const char kMessage[] = "hello, world"; |
| 176 | // Send the test signal from the exported object. |
| 177 | test_service_->SendTestSignal(kMessage); |
| 178 | // Receive the signal with the object proxy. The signal is handled in |
| 179 | // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
| 180 | WaitForTestSignal(); |
| 181 | ASSERT_EQ(kMessage, test_signal_string_); |
| 182 | } |
| 183 | |
| 184 | TEST_F(SignalSenderVerificationTest, TestSignalRejected) { |
| 185 | // To make sure the histogram instance is created. |
| 186 | UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0); |
| 187 | base::Histogram* reject_signal_histogram = |
| 188 | base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount"); |
| 189 | scoped_ptr<base::HistogramSamples> samples1( |
| 190 | reject_signal_histogram->SnapshotSamples()); |
| 191 | |
| 192 | const char kNewMessage[] = "hello, new world"; |
| 193 | test_service2_->SendTestSignal(kNewMessage); |
| 194 | |
| 195 | // This test tests that our callback is NOT called by the ObjectProxy. |
| 196 | // Sleep to have message delivered to the client via the D-Bus service. |
| 197 | base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
| 198 | |
| 199 | scoped_ptr<base::HistogramSamples> samples2( |
| 200 | reject_signal_histogram->SnapshotSamples()); |
| 201 | |
| 202 | ASSERT_EQ("", test_signal_string_); |
| 203 | EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); |
| 204 | } |
| 205 | |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 206 | TEST_F(SignalSenderVerificationTest, TestOwnerChanged) { |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 207 | const char kMessage[] = "hello, world"; |
| 208 | |
| 209 | // Send the test signal from the exported object. |
| 210 | test_service_->SendTestSignal(kMessage); |
| 211 | // Receive the signal with the object proxy. The signal is handled in |
| 212 | // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
| 213 | WaitForTestSignal(); |
| 214 | ASSERT_EQ(kMessage, test_signal_string_); |
| 215 | |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 216 | // Release and acquire the name ownership. |
| 217 | // latest_name_owner_ should be non empty as |test_service_| owns the name. |
| 218 | ASSERT_FALSE(latest_name_owner_.empty()); |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 219 | test_service_->ShutdownAndBlock(); |
[email protected] | 6d36c0c | 2012-11-14 11:02:59 | [diff] [blame] | 220 | // OnNameOwnerChanged will PostTask to quit the message loop. |
| 221 | message_loop_.Run(); |
| 222 | // latest_name_owner_ should be empty as the owner is gone. |
| 223 | ASSERT_TRUE(latest_name_owner_.empty()); |
| 224 | |
| 225 | // Reset the flag as NameOwnerChanged is already received in setup. |
| 226 | on_name_owner_changed_called_ = false; |
| 227 | test_service2_->RequestOwnership( |
| 228 | base::Bind(&SignalSenderVerificationTest::OnOwnership, |
| 229 | base::Unretained(this), true)); |
| 230 | // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
| 231 | // but there's no expected order of those 2 event. |
| 232 | message_loop_.Run(); |
| 233 | if (!on_name_owner_changed_called_ || !on_ownership_called_) |
| 234 | message_loop_.Run(); |
| 235 | ASSERT_TRUE(on_name_owner_changed_called_); |
| 236 | ASSERT_TRUE(on_ownership_called_); |
| 237 | |
| 238 | // latest_name_owner_ becomes non empty as the new owner appears. |
| 239 | ASSERT_FALSE(latest_name_owner_.empty()); |
[email protected] | 8bbe31e | 2012-10-29 06:27:33 | [diff] [blame] | 240 | |
| 241 | // Now the second service owns the name. |
| 242 | const char kNewMessage[] = "hello, new world"; |
| 243 | |
| 244 | test_service2_->SendTestSignal(kNewMessage); |
| 245 | WaitForTestSignal(); |
| 246 | ASSERT_EQ(kNewMessage, test_signal_string_); |
| 247 | } |