end_to_end_async_unittest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. #include <stddef.h>
  5. #include <algorithm>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/bind.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/message_loop/message_pump_type.h"
  13. #include "base/run_loop.h"
  14. #include "base/task/single_thread_task_runner.h"
  15. #include "base/test/task_environment.h"
  16. #include "base/test/test_timeouts.h"
  17. #include "base/threading/thread.h"
  18. #include "base/threading/thread_restrictions.h"
  19. #include "base/time/time.h"
  20. #include "dbus/bus.h"
  21. #include "dbus/message.h"
  22. #include "dbus/object_path.h"
  23. #include "dbus/object_proxy.h"
  24. #include "dbus/test_service.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. #include "third_party/abseil-cpp/absl/types/optional.h"
  27. namespace dbus {
  28. namespace {
  29. // See comments in ObjectProxy::RunResponseCallback() for why the number was
  30. // chosen.
  31. const int kHugePayloadSize = 64 << 20; // 64 MB
  32. } // namespace
  33. // The end-to-end test exercises the asynchronous APIs in ObjectProxy and
  34. // ExportedObject.
  35. class EndToEndAsyncTest : public testing::Test {
  36. public:
  37. void SetUp() override {
  38. // Make the main thread not to allow IO.
  39. disallow_blocking_.emplace();
  40. // Start the D-Bus thread.
  41. dbus_thread_ = std::make_unique<base::Thread>("D-Bus Thread");
  42. base::Thread::Options thread_options;
  43. thread_options.message_pump_type = base::MessagePumpType::IO;
  44. ASSERT_TRUE(dbus_thread_->StartWithOptions(std::move(thread_options)));
  45. // Start the test service, using the D-Bus thread.
  46. TestService::Options options;
  47. options.dbus_task_runner = dbus_thread_->task_runner();
  48. test_service_ = std::make_unique<TestService>(options);
  49. ASSERT_TRUE(test_service_->StartService());
  50. test_service_->WaitUntilServiceIsStarted();
  51. ASSERT_TRUE(test_service_->HasDBusThread());
  52. // Create the client, using the D-Bus thread.
  53. Bus::Options bus_options;
  54. bus_options.bus_type = Bus::SESSION;
  55. bus_options.connection_type = Bus::PRIVATE;
  56. bus_options.dbus_task_runner = dbus_thread_->task_runner();
  57. bus_ = new Bus(bus_options);
  58. object_proxy_ = bus_->GetObjectProxy(
  59. test_service_->service_name(),
  60. ObjectPath("/org/chromium/TestObject"));
  61. ASSERT_TRUE(bus_->HasDBusThread());
  62. // Connect to the "Test" signal of "org.chromium.TestInterface" from
  63. // the remote object.
  64. object_proxy_->ConnectToSignal(
  65. "org.chromium.TestInterface", "Test",
  66. base::BindRepeating(&EndToEndAsyncTest::OnTestSignal,
  67. base::Unretained(this)),
  68. base::BindOnce(&EndToEndAsyncTest::OnConnected,
  69. base::Unretained(this)));
  70. // Wait until the object proxy is connected to the signal.
  71. run_loop_ = std::make_unique<base::RunLoop>();
  72. run_loop_->Run();
  73. // Connect to the "Test2" signal of "org.chromium.TestInterface" from
  74. // the remote object. There was a bug where we were emitting error
  75. // messages like "Requested to remove an unknown match rule: ..." at
  76. // the shutdown of Bus when an object proxy is connected to more than
  77. // one signal of the same interface. See crosbug.com/23382 for details.
  78. object_proxy_->ConnectToSignal(
  79. "org.chromium.TestInterface", "Test2",
  80. base::BindRepeating(&EndToEndAsyncTest::OnTest2Signal,
  81. base::Unretained(this)),
  82. base::BindOnce(&EndToEndAsyncTest::OnConnected,
  83. base::Unretained(this)));
  84. // Wait until the object proxy is connected to the signal.
  85. run_loop_ = std::make_unique<base::RunLoop>();
  86. run_loop_->Run();
  87. // Create a second object proxy for the root object.
  88. root_object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(),
  89. ObjectPath("/"));
  90. ASSERT_TRUE(bus_->HasDBusThread());
  91. // Connect to the "Test" signal of "org.chromium.TestInterface" from
  92. // the root remote object too.
  93. root_object_proxy_->ConnectToSignal(
  94. "org.chromium.TestInterface", "Test",
  95. base::BindRepeating(&EndToEndAsyncTest::OnRootTestSignal,
  96. base::Unretained(this)),
  97. base::BindOnce(&EndToEndAsyncTest::OnConnected,
  98. base::Unretained(this)));
  99. // Wait until the root object proxy is connected to the signal.
  100. run_loop_ = std::make_unique<base::RunLoop>();
  101. run_loop_->Run();
  102. }
  103. void TearDown() override {
  104. bus_->ShutdownOnDBusThreadAndBlock();
  105. // Shut down the service.
  106. test_service_->ShutdownAndBlock();
  107. // Stopping a thread is considered an IO operation, so do this after
  108. // allowing IO.
  109. disallow_blocking_.reset();
  110. test_service_->Stop();
  111. }
  112. protected:
  113. // Replaces the bus with a broken one.
  114. void SetUpBrokenBus() {
  115. // Shut down the existing bus.
  116. bus_->ShutdownOnDBusThreadAndBlock();
  117. // Create new bus with invalid address.
  118. const char kInvalidAddress[] = "";
  119. Bus::Options bus_options;
  120. bus_options.bus_type = Bus::CUSTOM_ADDRESS;
  121. bus_options.address = kInvalidAddress;
  122. bus_options.connection_type = Bus::PRIVATE;
  123. bus_options.dbus_task_runner = dbus_thread_->task_runner();
  124. bus_ = new Bus(bus_options);
  125. ASSERT_TRUE(bus_->HasDBusThread());
  126. // Create new object proxy.
  127. object_proxy_ = bus_->GetObjectProxy(
  128. test_service_->service_name(),
  129. ObjectPath("/org/chromium/TestObject"));
  130. }
  131. // Calls the method asynchronously. OnResponse() will be called once the
  132. // response is received.
  133. void CallMethod(MethodCall* method_call,
  134. int timeout_ms) {
  135. object_proxy_->CallMethod(
  136. method_call, timeout_ms,
  137. base::BindOnce(&EndToEndAsyncTest::OnResponse, base::Unretained(this)));
  138. }
  139. // Calls the method asynchronously. OnResponse() will be called once the
  140. // response is received without error, otherwise OnError() will be called.
  141. void CallMethodWithErrorCallback(MethodCall* method_call,
  142. int timeout_ms) {
  143. object_proxy_->CallMethodWithErrorCallback(
  144. method_call, timeout_ms,
  145. base::BindOnce(&EndToEndAsyncTest::OnResponse, base::Unretained(this)),
  146. base::BindOnce(&EndToEndAsyncTest::OnError, base::Unretained(this)));
  147. }
  148. // Wait for the give number of responses.
  149. void WaitForResponses(size_t num_responses) {
  150. while (response_strings_.size() < num_responses) {
  151. run_loop_ = std::make_unique<base::RunLoop>();
  152. run_loop_->Run();
  153. }
  154. }
  155. // Called when the response is received.
  156. void OnResponse(Response* response) {
  157. // |response| will be deleted on exit of the function. Copy the
  158. // payload to |response_strings_|.
  159. if (response) {
  160. MessageReader reader(response);
  161. std::string response_string;
  162. ASSERT_TRUE(reader.PopString(&response_string));
  163. response_strings_.push_back(response_string);
  164. } else {
  165. response_strings_.push_back(std::string());
  166. }
  167. run_loop_->Quit();
  168. }
  169. // Wait for the given number of errors.
  170. void WaitForErrors(size_t num_errors) {
  171. while (error_names_.size() < num_errors) {
  172. run_loop_ = std::make_unique<base::RunLoop>();
  173. run_loop_->Run();
  174. }
  175. }
  176. // Called when an error is received.
  177. void OnError(ErrorResponse* error) {
  178. // |error| will be deleted on exit of the function. Copy the payload to
  179. // |error_names_|.
  180. if (error) {
  181. ASSERT_NE("", error->GetErrorName());
  182. error_names_.push_back(error->GetErrorName());
  183. } else {
  184. error_names_.push_back(std::string());
  185. }
  186. run_loop_->Quit();
  187. }
  188. // Called when the "Test" signal is received, in the main thread.
  189. // Copy the string payload to |test_signal_string_|.
  190. void OnTestSignal(Signal* signal) {
  191. MessageReader reader(signal);
  192. ASSERT_TRUE(reader.PopString(&test_signal_string_));
  193. run_loop_->Quit();
  194. }
  195. // Called when the "Test" signal is received, in the main thread, by
  196. // the root object proxy. Copy the string payload to
  197. // |root_test_signal_string_|.
  198. void OnRootTestSignal(Signal* signal) {
  199. MessageReader reader(signal);
  200. ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
  201. run_loop_->Quit();
  202. }
  203. // Called when the "Test2" signal is received, in the main thread.
  204. void OnTest2Signal(Signal* signal) {
  205. MessageReader reader(signal);
  206. run_loop_->Quit();
  207. }
  208. // Called when connected to the signal.
  209. void OnConnected(const std::string& interface_name,
  210. const std::string& signal_name,
  211. bool success) {
  212. ASSERT_TRUE(success);
  213. run_loop_->Quit();
  214. }
  215. // Wait for the hey signal to be received.
  216. void WaitForTestSignal() {
  217. // OnTestSignal() will quit the message loop.
  218. run_loop_ = std::make_unique<base::RunLoop>();
  219. run_loop_->Run();
  220. }
  221. base::test::SingleThreadTaskEnvironment task_environment_;
  222. absl::optional<base::ScopedDisallowBlocking> disallow_blocking_;
  223. std::unique_ptr<base::RunLoop> run_loop_;
  224. std::vector<std::string> response_strings_;
  225. std::vector<std::string> error_names_;
  226. std::unique_ptr<base::Thread> dbus_thread_;
  227. scoped_refptr<Bus> bus_;
  228. raw_ptr<ObjectProxy> object_proxy_;
  229. raw_ptr<ObjectProxy> root_object_proxy_;
  230. std::unique_ptr<TestService> test_service_;
  231. // Text message from "Test" signal.
  232. std::string test_signal_string_;
  233. // Text message from "Test" signal delivered to root.
  234. std::string root_test_signal_string_;
  235. };
  236. TEST_F(EndToEndAsyncTest, Echo) {
  237. const char* kHello = "hello";
  238. // Create the method call.
  239. MethodCall method_call("org.chromium.TestInterface", "Echo");
  240. MessageWriter writer(&method_call);
  241. writer.AppendString(kHello);
  242. // Call the method.
  243. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  244. CallMethod(&method_call, timeout_ms);
  245. // Check the response.
  246. WaitForResponses(1);
  247. EXPECT_EQ(kHello, response_strings_[0]);
  248. }
  249. TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
  250. const char* kHello = "hello";
  251. // Create the method call.
  252. MethodCall method_call("org.chromium.TestInterface", "Echo");
  253. MessageWriter writer(&method_call);
  254. writer.AppendString(kHello);
  255. // Call the method.
  256. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  257. CallMethodWithErrorCallback(&method_call, timeout_ms);
  258. // Check the response.
  259. WaitForResponses(1);
  260. EXPECT_EQ(kHello, response_strings_[0]);
  261. EXPECT_TRUE(error_names_.empty());
  262. }
  263. // Call Echo method three times.
  264. TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
  265. const char* kMessages[] = { "foo", "bar", "baz" };
  266. for (size_t i = 0; i < std::size(kMessages); ++i) {
  267. // Create the method call.
  268. MethodCall method_call("org.chromium.TestInterface", "Echo");
  269. MessageWriter writer(&method_call);
  270. writer.AppendString(kMessages[i]);
  271. // Call the method.
  272. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  273. CallMethod(&method_call, timeout_ms);
  274. }
  275. // Check the responses.
  276. WaitForResponses(3);
  277. // Sort as the order of the returned messages is not deterministic.
  278. std::sort(response_strings_.begin(), response_strings_.end());
  279. EXPECT_EQ("bar", response_strings_[0]);
  280. EXPECT_EQ("baz", response_strings_[1]);
  281. EXPECT_EQ("foo", response_strings_[2]);
  282. }
  283. TEST_F(EndToEndAsyncTest, Echo_HugePayload) {
  284. const std::string kHugePayload(kHugePayloadSize, 'o');
  285. // Create the method call with a huge payload.
  286. MethodCall method_call("org.chromium.TestInterface", "Echo");
  287. MessageWriter writer(&method_call);
  288. writer.AppendString(kHugePayload);
  289. // Call the method.
  290. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  291. CallMethod(&method_call, timeout_ms);
  292. // This caused a DCHECK failure before. Ensure that the issue is fixed.
  293. WaitForResponses(1);
  294. EXPECT_EQ(kHugePayload, response_strings_[0]);
  295. }
  296. TEST_F(EndToEndAsyncTest, BrokenBus) {
  297. const char* kHello = "hello";
  298. // Set up a broken bus.
  299. SetUpBrokenBus();
  300. // Create the method call.
  301. MethodCall method_call("org.chromium.TestInterface", "Echo");
  302. MessageWriter writer(&method_call);
  303. writer.AppendString(kHello);
  304. // Call the method.
  305. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  306. CallMethod(&method_call, timeout_ms);
  307. WaitForResponses(1);
  308. // Should fail because of the broken bus.
  309. ASSERT_EQ("", response_strings_[0]);
  310. }
  311. TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) {
  312. const char* kHello = "hello";
  313. // Set up a broken bus.
  314. SetUpBrokenBus();
  315. // Create the method call.
  316. MethodCall method_call("org.chromium.TestInterface", "Echo");
  317. MessageWriter writer(&method_call);
  318. writer.AppendString(kHello);
  319. // Call the method.
  320. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  321. CallMethodWithErrorCallback(&method_call, timeout_ms);
  322. WaitForErrors(1);
  323. // Should fail because of the broken bus.
  324. ASSERT_TRUE(response_strings_.empty());
  325. ASSERT_EQ("", error_names_[0]);
  326. }
  327. TEST_F(EndToEndAsyncTest, Timeout) {
  328. const char* kHello = "hello";
  329. // Create the method call.
  330. MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
  331. MessageWriter writer(&method_call);
  332. writer.AppendString(kHello);
  333. // Call the method with timeout of 0ms.
  334. const int timeout_ms = 0;
  335. CallMethod(&method_call, timeout_ms);
  336. WaitForResponses(1);
  337. // Should fail because of timeout.
  338. ASSERT_EQ("", response_strings_[0]);
  339. }
  340. TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
  341. const char* kHello = "hello";
  342. // Create the method call.
  343. MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
  344. MessageWriter writer(&method_call);
  345. writer.AppendString(kHello);
  346. // Call the method with timeout of 0ms.
  347. const int timeout_ms = 0;
  348. CallMethodWithErrorCallback(&method_call, timeout_ms);
  349. WaitForErrors(1);
  350. // Should fail because of timeout.
  351. ASSERT_TRUE(response_strings_.empty());
  352. ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
  353. }
  354. TEST_F(EndToEndAsyncTest, CancelPendingCalls) {
  355. const char* kHello = "hello";
  356. // Create the method call.
  357. MethodCall method_call("org.chromium.TestInterface", "Echo");
  358. MessageWriter writer(&method_call);
  359. writer.AppendString(kHello);
  360. // Call the method.
  361. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  362. CallMethod(&method_call, timeout_ms);
  363. // Remove the object proxy before receiving the result.
  364. // This results in cancelling the pending method call.
  365. bus_->RemoveObjectProxy(test_service_->service_name(),
  366. ObjectPath("/org/chromium/TestObject"),
  367. base::DoNothing());
  368. // We shouldn't receive any responses. Wait for a while just to make sure.
  369. run_loop_ = std::make_unique<base::RunLoop>();
  370. task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
  371. FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout());
  372. run_loop_->Run();
  373. EXPECT_TRUE(response_strings_.empty());
  374. }
  375. // Tests calling a method that sends its reply asynchronously.
  376. TEST_F(EndToEndAsyncTest, AsyncEcho) {
  377. const char* kHello = "hello";
  378. // Create the method call.
  379. MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
  380. MessageWriter writer(&method_call);
  381. writer.AppendString(kHello);
  382. // Call the method.
  383. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  384. CallMethod(&method_call, timeout_ms);
  385. // Check the response.
  386. WaitForResponses(1);
  387. EXPECT_EQ(kHello, response_strings_[0]);
  388. }
  389. TEST_F(EndToEndAsyncTest, NonexistentMethod) {
  390. MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
  391. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  392. CallMethod(&method_call, timeout_ms);
  393. WaitForResponses(1);
  394. // Should fail because the method is nonexistent.
  395. ASSERT_EQ("", response_strings_[0]);
  396. }
  397. TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
  398. MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
  399. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  400. CallMethodWithErrorCallback(&method_call, timeout_ms);
  401. WaitForErrors(1);
  402. // Should fail because the method is nonexistent.
  403. ASSERT_TRUE(response_strings_.empty());
  404. ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
  405. }
  406. TEST_F(EndToEndAsyncTest, BrokenMethod) {
  407. MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
  408. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  409. CallMethod(&method_call, timeout_ms);
  410. WaitForResponses(1);
  411. // Should fail because the method is broken.
  412. ASSERT_EQ("", response_strings_[0]);
  413. }
  414. TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
  415. MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
  416. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  417. CallMethodWithErrorCallback(&method_call, timeout_ms);
  418. WaitForErrors(1);
  419. // Should fail because the method is broken.
  420. ASSERT_TRUE(response_strings_.empty());
  421. ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
  422. }
  423. TEST_F(EndToEndAsyncTest, InvalidServiceName) {
  424. // Bus name cannot contain '/'.
  425. const std::string invalid_service_name = ":1/2";
  426. // Replace object proxy with new one.
  427. object_proxy_ = bus_->GetObjectProxy(invalid_service_name,
  428. ObjectPath("/org/chromium/TestObject"));
  429. MethodCall method_call("org.chromium.TestInterface", "Echo");
  430. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  431. CallMethodWithErrorCallback(&method_call, timeout_ms);
  432. WaitForErrors(1);
  433. // Should fail because of the invalid bus name.
  434. ASSERT_TRUE(response_strings_.empty());
  435. ASSERT_EQ("", error_names_[0]);
  436. }
  437. TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
  438. const char* kHello = "hello";
  439. // Create the method call.
  440. MethodCall method_call("org.chromium.TestInterface", "Echo");
  441. MessageWriter writer(&method_call);
  442. writer.AppendString(kHello);
  443. // Call the method with an empty callback.
  444. const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
  445. object_proxy_->CallMethod(&method_call, timeout_ms, base::DoNothing());
  446. // Post a delayed task to quit the RunLoop.
  447. run_loop_ = std::make_unique<base::RunLoop>();
  448. task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
  449. FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout());
  450. run_loop_->Run();
  451. // We cannot tell if the empty callback is called, but at least we can
  452. // check if the test does not crash.
  453. }
  454. TEST_F(EndToEndAsyncTest, TestSignal) {
  455. const char kMessage[] = "hello, world";
  456. // Send the test signal from the exported object.
  457. test_service_->SendTestSignal(kMessage);
  458. // Receive the signal with the object proxy. The signal is handled in
  459. // EndToEndAsyncTest::OnTestSignal() in the main thread.
  460. WaitForTestSignal();
  461. ASSERT_EQ(kMessage, test_signal_string_);
  462. }
  463. TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
  464. const char kMessage[] = "hello, world";
  465. // Object proxies are tied to a particular object path, if a signal
  466. // arrives from a different object path like "/" the first object proxy
  467. // |object_proxy_| should not handle it, and should leave it for the root
  468. // object proxy |root_object_proxy_|.
  469. test_service_->SendTestSignalFromRoot(kMessage);
  470. WaitForTestSignal();
  471. // Verify the signal was not received by the specific proxy.
  472. ASSERT_TRUE(test_signal_string_.empty());
  473. // Verify the string WAS received by the root proxy.
  474. ASSERT_EQ(kMessage, root_test_signal_string_);
  475. }
  476. TEST_F(EndToEndAsyncTest, TestHugeSignal) {
  477. const std::string kHugeMessage(kHugePayloadSize, 'o');
  478. // Send the huge signal from the exported object.
  479. test_service_->SendTestSignal(kHugeMessage);
  480. // This caused a DCHECK failure before. Ensure that the issue is fixed.
  481. WaitForTestSignal();
  482. ASSERT_EQ(kHugeMessage, test_signal_string_);
  483. }
  484. class SignalMultipleHandlerTest : public EndToEndAsyncTest {
  485. public:
  486. SignalMultipleHandlerTest() = default;
  487. void SetUp() override {
  488. // Set up base class.
  489. EndToEndAsyncTest::SetUp();
  490. // Connect the root object proxy's signal handler to a new handler
  491. // so that we can verify that a second call to ConnectSignal() delivers
  492. // to both our new handler and the old.
  493. object_proxy_->ConnectToSignal(
  494. "org.chromium.TestInterface", "Test",
  495. base::BindRepeating(&SignalMultipleHandlerTest::OnAdditionalTestSignal,
  496. base::Unretained(this)),
  497. base::BindOnce(&SignalMultipleHandlerTest::OnAdditionalConnected,
  498. base::Unretained(this)));
  499. // Wait until the object proxy is connected to the signal.
  500. run_loop_ = std::make_unique<base::RunLoop>();
  501. run_loop_->Run();
  502. }
  503. protected:
  504. // Called when the "Test" signal is received, in the main thread.
  505. // Copy the string payload to |additional_test_signal_string_|.
  506. void OnAdditionalTestSignal(Signal* signal) {
  507. MessageReader reader(signal);
  508. ASSERT_TRUE(reader.PopString(&additional_test_signal_string_));
  509. run_loop_->Quit();
  510. }
  511. // Called when connected to the signal.
  512. void OnAdditionalConnected(const std::string& interface_name,
  513. const std::string& signal_name,
  514. bool success) {
  515. ASSERT_TRUE(success);
  516. run_loop_->Quit();
  517. }
  518. // Text message from "Test" signal delivered to additional handler.
  519. std::string additional_test_signal_string_;
  520. };
  521. TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) {
  522. const char kMessage[] = "hello, world";
  523. // Send the test signal from the exported object.
  524. test_service_->SendTestSignal(kMessage);
  525. // Receive the signal with the object proxy.
  526. WaitForTestSignal();
  527. // Verify the string WAS received by the original handler.
  528. ASSERT_EQ(kMessage, test_signal_string_);
  529. // Verify the signal WAS ALSO received by the additional handler.
  530. ASSERT_EQ(kMessage, additional_test_signal_string_);
  531. }
  532. } // namespace dbus