bus_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 "dbus/bus.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/files/file_descriptor_watcher_posix.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/message_loop/message_pump_type.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/threading/thread.h"
  14. #include "dbus/exported_object.h"
  15. #include "dbus/object_path.h"
  16. #include "dbus/object_proxy.h"
  17. #include "dbus/scoped_dbus_error.h"
  18. #include "dbus/test_service.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. namespace dbus {
  21. namespace {
  22. // Test helper for BusTest.ListenForServiceOwnerChange that wraps a
  23. // base::RunLoop. At Run() time, the caller pass in the expected number of
  24. // quit calls, and at QuitIfConditionIsSatisified() time, only quit the RunLoop
  25. // if the expected number of quit calls have been reached.
  26. class RunLoopWithExpectedCount {
  27. public:
  28. RunLoopWithExpectedCount() : expected_quit_calls_(0), actual_quit_calls_(0) {}
  29. RunLoopWithExpectedCount(const RunLoopWithExpectedCount&) = delete;
  30. RunLoopWithExpectedCount& operator=(const RunLoopWithExpectedCount&) = delete;
  31. ~RunLoopWithExpectedCount() = default;
  32. void Run(int expected_quit_calls) {
  33. DCHECK_EQ(0, expected_quit_calls_);
  34. DCHECK_EQ(0, actual_quit_calls_);
  35. expected_quit_calls_ = expected_quit_calls;
  36. run_loop_ = std::make_unique<base::RunLoop>();
  37. run_loop_->Run();
  38. }
  39. void QuitIfConditionIsSatisified() {
  40. if (++actual_quit_calls_ != expected_quit_calls_)
  41. return;
  42. run_loop_->Quit();
  43. expected_quit_calls_ = 0;
  44. actual_quit_calls_ = 0;
  45. }
  46. private:
  47. std::unique_ptr<base::RunLoop> run_loop_;
  48. int expected_quit_calls_;
  49. int actual_quit_calls_;
  50. };
  51. // Test helper for BusTest.ListenForServiceOwnerChange.
  52. void OnServiceOwnerChanged(RunLoopWithExpectedCount* run_loop_state,
  53. std::string* service_owner,
  54. int* num_of_owner_changes,
  55. const std::string& new_service_owner) {
  56. *service_owner = new_service_owner;
  57. ++(*num_of_owner_changes);
  58. run_loop_state->QuitIfConditionIsSatisified();
  59. }
  60. } // namespace
  61. TEST(BusTest, GetObjectProxy) {
  62. Bus::Options options;
  63. scoped_refptr<Bus> bus = new Bus(options);
  64. ObjectProxy* object_proxy1 =
  65. bus->GetObjectProxy("org.chromium.TestService",
  66. ObjectPath("/org/chromium/TestObject"));
  67. ASSERT_TRUE(object_proxy1);
  68. // This should return the same object.
  69. ObjectProxy* object_proxy2 =
  70. bus->GetObjectProxy("org.chromium.TestService",
  71. ObjectPath("/org/chromium/TestObject"));
  72. ASSERT_TRUE(object_proxy2);
  73. EXPECT_EQ(object_proxy1, object_proxy2);
  74. // This should not.
  75. ObjectProxy* object_proxy3 =
  76. bus->GetObjectProxy(
  77. "org.chromium.TestService",
  78. ObjectPath("/org/chromium/DifferentTestObject"));
  79. ASSERT_TRUE(object_proxy3);
  80. EXPECT_NE(object_proxy1, object_proxy3);
  81. bus->ShutdownAndBlock();
  82. }
  83. TEST(BusTest, GetObjectProxyIgnoreUnknownService) {
  84. Bus::Options options;
  85. scoped_refptr<Bus> bus = new Bus(options);
  86. ObjectProxy* object_proxy1 =
  87. bus->GetObjectProxyWithOptions(
  88. "org.chromium.TestService",
  89. ObjectPath("/org/chromium/TestObject"),
  90. ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
  91. ASSERT_TRUE(object_proxy1);
  92. // This should return the same object.
  93. ObjectProxy* object_proxy2 =
  94. bus->GetObjectProxyWithOptions(
  95. "org.chromium.TestService",
  96. ObjectPath("/org/chromium/TestObject"),
  97. ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
  98. ASSERT_TRUE(object_proxy2);
  99. EXPECT_EQ(object_proxy1, object_proxy2);
  100. // This should not.
  101. ObjectProxy* object_proxy3 =
  102. bus->GetObjectProxyWithOptions(
  103. "org.chromium.TestService",
  104. ObjectPath("/org/chromium/DifferentTestObject"),
  105. ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
  106. ASSERT_TRUE(object_proxy3);
  107. EXPECT_NE(object_proxy1, object_proxy3);
  108. bus->ShutdownAndBlock();
  109. }
  110. TEST(BusTest, RemoveObjectProxy) {
  111. base::test::SingleThreadTaskEnvironment task_environment;
  112. // Start the D-Bus thread.
  113. base::Thread::Options thread_options;
  114. thread_options.message_pump_type = base::MessagePumpType::IO;
  115. base::Thread dbus_thread("D-Bus thread");
  116. dbus_thread.StartWithOptions(std::move(thread_options));
  117. // Create the bus.
  118. Bus::Options options;
  119. options.dbus_task_runner = dbus_thread.task_runner();
  120. scoped_refptr<Bus> bus = new Bus(options);
  121. ASSERT_FALSE(bus->shutdown_completed());
  122. // Try to remove a non existant object proxy should return false.
  123. ASSERT_FALSE(bus->RemoveObjectProxy("org.chromium.TestService",
  124. ObjectPath("/org/chromium/TestObject"),
  125. base::DoNothing()));
  126. ObjectProxy* object_proxy1 =
  127. bus->GetObjectProxy("org.chromium.TestService",
  128. ObjectPath("/org/chromium/TestObject"));
  129. ASSERT_TRUE(object_proxy1);
  130. // Increment the reference count to the object proxy to avoid destroying it
  131. // while removing the object.
  132. object_proxy1->AddRef();
  133. // Remove the object from the bus. This will invalidate any other usage of
  134. // object_proxy1 other than destroy it. We keep this object for a comparison
  135. // at a later time.
  136. ASSERT_TRUE(bus->RemoveObjectProxy("org.chromium.TestService",
  137. ObjectPath("/org/chromium/TestObject"),
  138. base::DoNothing()));
  139. // This should return a different object because the first object was removed
  140. // from the bus, but not deleted from memory.
  141. ObjectProxy* object_proxy2 =
  142. bus->GetObjectProxy("org.chromium.TestService",
  143. ObjectPath("/org/chromium/TestObject"));
  144. ASSERT_TRUE(object_proxy2);
  145. // Compare the new object with the first object. The first object still exists
  146. // thanks to the increased reference.
  147. EXPECT_NE(object_proxy1, object_proxy2);
  148. // Release object_proxy1.
  149. object_proxy1->Release();
  150. // Shut down synchronously.
  151. bus->ShutdownOnDBusThreadAndBlock();
  152. EXPECT_TRUE(bus->shutdown_completed());
  153. dbus_thread.Stop();
  154. }
  155. TEST(BusTest, GetExportedObject) {
  156. Bus::Options options;
  157. scoped_refptr<Bus> bus = new Bus(options);
  158. ExportedObject* object_proxy1 =
  159. bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
  160. ASSERT_TRUE(object_proxy1);
  161. // This should return the same object.
  162. ExportedObject* object_proxy2 =
  163. bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
  164. ASSERT_TRUE(object_proxy2);
  165. EXPECT_EQ(object_proxy1, object_proxy2);
  166. // This should not.
  167. ExportedObject* object_proxy3 =
  168. bus->GetExportedObject(
  169. ObjectPath("/org/chromium/DifferentTestObject"));
  170. ASSERT_TRUE(object_proxy3);
  171. EXPECT_NE(object_proxy1, object_proxy3);
  172. bus->ShutdownAndBlock();
  173. }
  174. TEST(BusTest, UnregisterExportedObject) {
  175. // Start the D-Bus thread.
  176. base::Thread::Options thread_options;
  177. thread_options.message_pump_type = base::MessagePumpType::IO;
  178. base::Thread dbus_thread("D-Bus thread");
  179. dbus_thread.StartWithOptions(std::move(thread_options));
  180. // Create the bus.
  181. Bus::Options options;
  182. options.dbus_task_runner = dbus_thread.task_runner();
  183. scoped_refptr<Bus> bus = new Bus(options);
  184. ASSERT_FALSE(bus->shutdown_completed());
  185. ExportedObject* object_proxy1 =
  186. bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
  187. ASSERT_TRUE(object_proxy1);
  188. // Increment the reference count to the object proxy to avoid destroying it
  189. // calling UnregisterExportedObject. This ensures the dbus::ExportedObject is
  190. // not freed from memory. See http://crbug.com/137846 for details.
  191. object_proxy1->AddRef();
  192. bus->UnregisterExportedObject(ObjectPath("/org/chromium/TestObject"));
  193. // This should return a new object because the object_proxy1 is still in
  194. // alloc'ed memory.
  195. ExportedObject* object_proxy2 =
  196. bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
  197. ASSERT_TRUE(object_proxy2);
  198. EXPECT_NE(object_proxy1, object_proxy2);
  199. // Release the incremented reference.
  200. object_proxy1->Release();
  201. // Shut down synchronously.
  202. bus->ShutdownOnDBusThreadAndBlock();
  203. EXPECT_TRUE(bus->shutdown_completed());
  204. dbus_thread.Stop();
  205. }
  206. TEST(BusTest, ShutdownAndBlock) {
  207. Bus::Options options;
  208. scoped_refptr<Bus> bus = new Bus(options);
  209. ASSERT_FALSE(bus->shutdown_completed());
  210. // Shut down synchronously.
  211. bus->ShutdownAndBlock();
  212. EXPECT_TRUE(bus->shutdown_completed());
  213. }
  214. TEST(BusTest, ShutdownAndBlockWithDBusThread) {
  215. // Start the D-Bus thread.
  216. base::Thread::Options thread_options;
  217. thread_options.message_pump_type = base::MessagePumpType::IO;
  218. base::Thread dbus_thread("D-Bus thread");
  219. dbus_thread.StartWithOptions(std::move(thread_options));
  220. // Create the bus.
  221. Bus::Options options;
  222. options.dbus_task_runner = dbus_thread.task_runner();
  223. scoped_refptr<Bus> bus = new Bus(options);
  224. ASSERT_FALSE(bus->shutdown_completed());
  225. // Shut down synchronously.
  226. bus->ShutdownOnDBusThreadAndBlock();
  227. EXPECT_TRUE(bus->shutdown_completed());
  228. dbus_thread.Stop();
  229. }
  230. TEST(BusTest, DoubleAddAndRemoveMatch) {
  231. Bus::Options options;
  232. scoped_refptr<Bus> bus = new Bus(options);
  233. ScopedDBusError error;
  234. bus->Connect();
  235. // Adds the same rule twice.
  236. bus->AddMatch(
  237. "type='signal',interface='org.chromium.TestService',path='/'",
  238. error.get());
  239. ASSERT_FALSE(error.is_set());
  240. bus->AddMatch(
  241. "type='signal',interface='org.chromium.TestService',path='/'",
  242. error.get());
  243. ASSERT_FALSE(error.is_set());
  244. // Removes the same rule twice.
  245. ASSERT_TRUE(bus->RemoveMatch(
  246. "type='signal',interface='org.chromium.TestService',path='/'",
  247. error.get()));
  248. ASSERT_FALSE(error.is_set());
  249. // The rule should be still in the bus since it was removed only once.
  250. // A second removal shouldn't give an error.
  251. ASSERT_TRUE(bus->RemoveMatch(
  252. "type='signal',interface='org.chromium.TestService',path='/'",
  253. error.get()));
  254. ASSERT_FALSE(error.is_set());
  255. // A third attemp to remove the same rule should fail.
  256. ASSERT_FALSE(bus->RemoveMatch(
  257. "type='signal',interface='org.chromium.TestService',path='/'",
  258. error.get()));
  259. bus->ShutdownAndBlock();
  260. }
  261. TEST(BusTest, ListenForServiceOwnerChange) {
  262. base::test::SingleThreadTaskEnvironment task_environment(
  263. base::test::SingleThreadTaskEnvironment::MainThreadType::IO);
  264. RunLoopWithExpectedCount run_loop_state;
  265. // Create the bus.
  266. Bus::Options bus_options;
  267. scoped_refptr<Bus> bus = new Bus(bus_options);
  268. // Add a listener.
  269. std::string service_owner1;
  270. int num_of_owner_changes1 = 0;
  271. Bus::ServiceOwnerChangeCallback callback1 =
  272. base::BindRepeating(&OnServiceOwnerChanged, &run_loop_state,
  273. &service_owner1, &num_of_owner_changes1);
  274. bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1);
  275. // This should be a no-op.
  276. bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1);
  277. base::RunLoop().RunUntilIdle();
  278. // Nothing has happened yet. Check initial state.
  279. EXPECT_TRUE(service_owner1.empty());
  280. EXPECT_EQ(0, num_of_owner_changes1);
  281. // Make an ownership change.
  282. ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService",
  283. Bus::REQUIRE_PRIMARY));
  284. run_loop_state.Run(1);
  285. {
  286. // Get the current service owner and check to make sure the listener got
  287. // the right value.
  288. std::string current_service_owner =
  289. bus->GetServiceOwnerAndBlock("org.chromium.TestService",
  290. Bus::REPORT_ERRORS);
  291. ASSERT_FALSE(current_service_owner.empty());
  292. // Make sure the listener heard about the new owner.
  293. EXPECT_EQ(current_service_owner, service_owner1);
  294. // Test the second ListenForServiceOwnerChange() above is indeed a no-op.
  295. EXPECT_EQ(1, num_of_owner_changes1);
  296. }
  297. // Add a second listener.
  298. std::string service_owner2;
  299. int num_of_owner_changes2 = 0;
  300. Bus::ServiceOwnerChangeCallback callback2 =
  301. base::BindRepeating(&OnServiceOwnerChanged, &run_loop_state,
  302. &service_owner2, &num_of_owner_changes2);
  303. bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2);
  304. base::RunLoop().RunUntilIdle();
  305. // Release the ownership and make sure the service owner listeners fire with
  306. // the right values and the right number of times.
  307. ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService"));
  308. run_loop_state.Run(2);
  309. EXPECT_TRUE(service_owner1.empty());
  310. EXPECT_TRUE(service_owner2.empty());
  311. EXPECT_EQ(2, num_of_owner_changes1);
  312. EXPECT_EQ(1, num_of_owner_changes2);
  313. // Unlisten so shutdown can proceed correctly.
  314. bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1);
  315. bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2);
  316. base::RunLoop().RunUntilIdle();
  317. // Shut down synchronously.
  318. bus->ShutdownAndBlock();
  319. EXPECT_TRUE(bus->shutdown_completed());
  320. }
  321. TEST(BusTest, GetConnectionName) {
  322. Bus::Options options;
  323. scoped_refptr<Bus> bus = new Bus(options);
  324. // Connection name is empty since bus is not connected.
  325. EXPECT_FALSE(bus->IsConnected());
  326. EXPECT_TRUE(bus->GetConnectionName().empty());
  327. // Connect bus to D-Bus.
  328. bus->Connect();
  329. // Connection name is not empty after connection is established.
  330. EXPECT_TRUE(bus->IsConnected());
  331. EXPECT_FALSE(bus->GetConnectionName().empty());
  332. // Shut down synchronously.
  333. bus->ShutdownAndBlock();
  334. EXPECT_TRUE(bus->shutdown_completed());
  335. }
  336. } // namespace dbus