ipc_channel_mojo_unittest.cc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. // Copyright 2014 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 "ipc/ipc_channel_mojo.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <utility>
  9. #include "base/base_paths.h"
  10. #include "base/bind.h"
  11. #include "base/callback_helpers.h"
  12. #include "base/containers/queue.h"
  13. #include "base/files/file.h"
  14. #include "base/files/scoped_temp_dir.h"
  15. #include "base/location.h"
  16. #include "base/memory/platform_shared_memory_region.h"
  17. #include "base/memory/raw_ptr.h"
  18. #include "base/memory/read_only_shared_memory_region.h"
  19. #include "base/memory/shared_memory_mapping.h"
  20. #include "base/memory/unsafe_shared_memory_region.h"
  21. #include "base/memory/writable_shared_memory_region.h"
  22. #include "base/message_loop/message_pump_type.h"
  23. #include "base/path_service.h"
  24. #include "base/pickle.h"
  25. #include "base/run_loop.h"
  26. #include "base/synchronization/waitable_event.h"
  27. #include "base/task/single_thread_task_runner.h"
  28. #include "base/test/bind.h"
  29. #include "base/test/task_environment.h"
  30. #include "base/test/test_io_thread.h"
  31. #include "base/test/test_shared_memory_util.h"
  32. #include "base/test/test_timeouts.h"
  33. #include "base/threading/thread.h"
  34. #include "base/threading/thread_task_runner_handle.h"
  35. #include "build/build_config.h"
  36. #include "ipc/ipc_message.h"
  37. #include "ipc/ipc_message_utils.h"
  38. #include "ipc/ipc_mojo_handle_attachment.h"
  39. #include "ipc/ipc_mojo_message_helper.h"
  40. #include "ipc/ipc_mojo_param_traits.h"
  41. #include "ipc/ipc_sync_channel.h"
  42. #include "ipc/ipc_sync_message.h"
  43. #include "ipc/ipc_test.mojom.h"
  44. #include "ipc/ipc_test_base.h"
  45. #include "ipc/ipc_test_channel_listener.h"
  46. #include "mojo/public/cpp/bindings/associated_receiver.h"
  47. #include "mojo/public/cpp/bindings/associated_remote.h"
  48. #include "mojo/public/cpp/bindings/lib/validation_errors.h"
  49. #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
  50. #include "mojo/public/cpp/system/functions.h"
  51. #include "mojo/public/cpp/system/wait.h"
  52. #include "testing/gtest/include/gtest/gtest.h"
  53. #include "third_party/abseil-cpp/absl/types/optional.h"
  54. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  55. #include "base/file_descriptor_posix.h"
  56. #include "ipc/ipc_platform_file_attachment_posix.h"
  57. #endif
  58. namespace {
  59. void SendString(IPC::Sender* sender, const std::string& str) {
  60. IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
  61. message->WriteString(str);
  62. ASSERT_TRUE(sender->Send(message));
  63. }
  64. void SendValue(IPC::Sender* sender, int32_t value) {
  65. IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
  66. message->WriteInt(value);
  67. ASSERT_TRUE(sender->Send(message));
  68. }
  69. class ListenerThatExpectsOK : public IPC::Listener {
  70. public:
  71. explicit ListenerThatExpectsOK(base::OnceClosure quit_closure)
  72. : received_ok_(false), quit_closure_(std::move(quit_closure)) {}
  73. ~ListenerThatExpectsOK() override = default;
  74. bool OnMessageReceived(const IPC::Message& message) override {
  75. base::PickleIterator iter(message);
  76. std::string should_be_ok;
  77. EXPECT_TRUE(iter.ReadString(&should_be_ok));
  78. EXPECT_EQ(should_be_ok, "OK");
  79. received_ok_ = true;
  80. std::move(quit_closure_).Run();
  81. return true;
  82. }
  83. void OnChannelError() override {
  84. // The connection should be healthy while the listener is waiting
  85. // message. An error can occur after that because the peer
  86. // process dies.
  87. CHECK(received_ok_);
  88. }
  89. static void SendOK(IPC::Sender* sender) { SendString(sender, "OK"); }
  90. private:
  91. bool received_ok_;
  92. base::OnceClosure quit_closure_;
  93. };
  94. class TestListenerBase : public IPC::Listener {
  95. public:
  96. explicit TestListenerBase(base::OnceClosure quit_closure)
  97. : quit_closure_(std::move(quit_closure)) {}
  98. ~TestListenerBase() override = default;
  99. void OnChannelError() override { RunQuitClosure(); }
  100. void set_sender(IPC::Sender* sender) { sender_ = sender; }
  101. IPC::Sender* sender() const { return sender_; }
  102. void RunQuitClosure() {
  103. if (quit_closure_)
  104. std::move(quit_closure_).Run();
  105. }
  106. private:
  107. raw_ptr<IPC::Sender> sender_ = nullptr;
  108. base::OnceClosure quit_closure_;
  109. };
  110. using IPCChannelMojoTest = IPCChannelMojoTestBase;
  111. class TestChannelListenerWithExtraExpectations
  112. : public IPC::TestChannelListener {
  113. public:
  114. TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {}
  115. void OnChannelConnected(int32_t peer_pid) override {
  116. IPC::TestChannelListener::OnChannelConnected(peer_pid);
  117. EXPECT_TRUE(base::kNullProcessId != peer_pid);
  118. is_connected_called_ = true;
  119. }
  120. bool is_connected_called() const { return is_connected_called_; }
  121. private:
  122. bool is_connected_called_;
  123. };
  124. TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
  125. Init("IPCChannelMojoTestClient");
  126. // Set up IPC channel and start client.
  127. TestChannelListenerWithExtraExpectations listener;
  128. CreateChannel(&listener);
  129. listener.Init(sender());
  130. ASSERT_TRUE(ConnectChannel());
  131. IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
  132. base::RunLoop().Run();
  133. channel()->Close();
  134. EXPECT_TRUE(WaitForClientShutdown());
  135. EXPECT_TRUE(listener.is_connected_called());
  136. EXPECT_TRUE(listener.HasSentAll());
  137. DestroyChannel();
  138. }
  139. // A long running process that connects to us
  140. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestClient) {
  141. TestChannelListenerWithExtraExpectations listener;
  142. Connect(&listener);
  143. listener.Init(channel());
  144. IPC::TestChannelListener::SendOneMessage(channel(), "hello from child");
  145. base::RunLoop().Run();
  146. EXPECT_TRUE(listener.is_connected_called());
  147. EXPECT_TRUE(listener.HasSentAll());
  148. Close();
  149. }
  150. class ListenerExpectingErrors : public TestListenerBase {
  151. public:
  152. ListenerExpectingErrors(base::OnceClosure quit_closure)
  153. : TestListenerBase(std::move(quit_closure)), has_error_(false) {}
  154. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  155. void OnChannelError() override {
  156. has_error_ = true;
  157. TestListenerBase::OnChannelError();
  158. }
  159. bool has_error() const { return has_error_; }
  160. private:
  161. bool has_error_;
  162. };
  163. class ListenerThatQuits : public IPC::Listener {
  164. public:
  165. explicit ListenerThatQuits(base::OnceClosure quit_closure)
  166. : quit_closure_(std::move(quit_closure)) {}
  167. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  168. void OnChannelConnected(int32_t peer_pid) override {
  169. std::move(quit_closure_).Run();
  170. }
  171. private:
  172. base::OnceClosure quit_closure_;
  173. };
  174. // A long running process that connects to us.
  175. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoErraticTestClient) {
  176. base::RunLoop run_loop;
  177. ListenerThatQuits listener(run_loop.QuitClosure());
  178. Connect(&listener);
  179. run_loop.Run();
  180. Close();
  181. }
  182. TEST_F(IPCChannelMojoTest, SendFailWithPendingMessages) {
  183. Init("IPCChannelMojoErraticTestClient");
  184. // Set up IPC channel and start client.
  185. base::RunLoop run_loop;
  186. ListenerExpectingErrors listener(run_loop.QuitClosure());
  187. CreateChannel(&listener);
  188. ASSERT_TRUE(ConnectChannel());
  189. // This matches a value in mojo/edk/system/constants.h
  190. const int kMaxMessageNumBytes = 4 * 1024 * 1024;
  191. std::string overly_large_data(kMaxMessageNumBytes, '*');
  192. // This messages are queued as pending.
  193. for (size_t i = 0; i < 10; ++i) {
  194. IPC::TestChannelListener::SendOneMessage(sender(),
  195. overly_large_data.c_str());
  196. }
  197. run_loop.Run();
  198. channel()->Close();
  199. EXPECT_TRUE(WaitForClientShutdown());
  200. EXPECT_TRUE(listener.has_error());
  201. DestroyChannel();
  202. }
  203. class ListenerThatBindsATestStructPasser : public IPC::Listener,
  204. public IPC::mojom::TestStructPasser {
  205. public:
  206. ListenerThatBindsATestStructPasser() = default;
  207. ~ListenerThatBindsATestStructPasser() override = default;
  208. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  209. void OnChannelConnected(int32_t peer_pid) override {}
  210. void OnChannelError() override { NOTREACHED(); }
  211. void OnAssociatedInterfaceRequest(
  212. const std::string& interface_name,
  213. mojo::ScopedInterfaceEndpointHandle handle) override {
  214. CHECK_EQ(interface_name, IPC::mojom::TestStructPasser::Name_);
  215. receiver_.Bind(
  216. mojo::PendingAssociatedReceiver<IPC::mojom::TestStructPasser>(
  217. std::move(handle)));
  218. }
  219. private:
  220. // IPC::mojom::TestStructPasser:
  221. void Pass(IPC::mojom::TestStructPtr) override { NOTREACHED(); }
  222. mojo::AssociatedReceiver<IPC::mojom::TestStructPasser> receiver_{this};
  223. };
  224. class ListenerThatExpectsNoError : public IPC::Listener {
  225. public:
  226. ListenerThatExpectsNoError(base::OnceClosure connect_closure,
  227. base::OnceClosure quit_closure)
  228. : connect_closure_(std::move(connect_closure)),
  229. quit_closure_(std::move(quit_closure)) {}
  230. bool OnMessageReceived(const IPC::Message& message) override {
  231. base::PickleIterator iter(message);
  232. std::string should_be_ok;
  233. EXPECT_TRUE(iter.ReadString(&should_be_ok));
  234. EXPECT_EQ(should_be_ok, "OK");
  235. std::move(quit_closure_).Run();
  236. return true;
  237. }
  238. void OnChannelConnected(int32_t peer_pid) override {
  239. std::move(connect_closure_).Run();
  240. }
  241. void OnChannelError() override { NOTREACHED(); }
  242. private:
  243. base::OnceClosure connect_closure_;
  244. base::OnceClosure quit_closure_;
  245. };
  246. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
  247. IPCChannelMojoNoImplicitChanelClosureClient) {
  248. base::RunLoop wait_to_connect_loop;
  249. base::RunLoop wait_to_quit_loop;
  250. ListenerThatExpectsNoError listener(wait_to_connect_loop.QuitClosure(),
  251. wait_to_quit_loop.QuitClosure());
  252. Connect(&listener);
  253. wait_to_connect_loop.Run();
  254. mojo::AssociatedRemote<IPC::mojom::TestStructPasser> passer;
  255. channel()->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface(
  256. passer.BindNewEndpointAndPassReceiver());
  257. // This avoids hitting DCHECKs in the serialization code meant to stop us from
  258. // making such "mistakes" as the one we're about to make below.
  259. mojo::internal::SerializationWarningObserverForTesting suppress_those_dchecks;
  260. // Send an invalid message. The TestStruct argument is not allowed to be null.
  261. // This will elicit a validation error in the parent process, but should not
  262. // actually disconnect the channel.
  263. passer->Pass(nullptr);
  264. // Wait until the parent says it's OK to quit, so it has time to verify its
  265. // expected behavior.
  266. wait_to_quit_loop.Run();
  267. Close();
  268. }
  269. TEST_F(IPCChannelMojoTest, NoImplicitChannelClosure) {
  270. // Verifies that OnChannelError is not invoked due to conditions other than
  271. // peer closure (e.g. a malformed inbound message). Instead we should always
  272. // be able to handle validation errors via Mojo bad message reporting.
  273. // NOTE: We can't create a RunLoop before Init() is called, but we have to set
  274. // the default ProcessErrorCallback (which we want to reference the RunLoop)
  275. // before Init() launches a child process. Hence the absl::optional here.
  276. absl::optional<base::RunLoop> wait_for_error_loop;
  277. bool process_error_received = false;
  278. mojo::SetDefaultProcessErrorHandler(
  279. base::BindLambdaForTesting([&](const std::string&) {
  280. process_error_received = true;
  281. wait_for_error_loop->Quit();
  282. }));
  283. Init("IPCChannelMojoNoImplicitChanelClosureClient");
  284. wait_for_error_loop.emplace();
  285. ListenerThatBindsATestStructPasser listener;
  286. CreateChannel(&listener);
  287. ASSERT_TRUE(ConnectChannel());
  288. wait_for_error_loop->Run();
  289. EXPECT_TRUE(process_error_received);
  290. mojo::SetDefaultProcessErrorHandler(base::NullCallback());
  291. // Tell the child it can quit and wait for it to shut down.
  292. ListenerThatExpectsOK::SendOK(channel());
  293. EXPECT_TRUE(WaitForClientShutdown());
  294. DestroyChannel();
  295. }
  296. struct TestingMessagePipe {
  297. TestingMessagePipe() {
  298. EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer));
  299. }
  300. mojo::ScopedMessagePipeHandle self;
  301. mojo::ScopedMessagePipeHandle peer;
  302. };
  303. class HandleSendingHelper {
  304. public:
  305. static std::string GetSendingFileContent() { return "Hello"; }
  306. static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) {
  307. std::string content = HandleSendingHelper::GetSendingFileContent();
  308. EXPECT_EQ(MOJO_RESULT_OK,
  309. mojo::WriteMessageRaw(pipe->self.get(), &content[0],
  310. static_cast<uint32_t>(content.size()),
  311. nullptr, 0, 0));
  312. EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo(
  313. message, std::move(pipe->peer)));
  314. }
  315. static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) {
  316. IPC::Message* message =
  317. new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
  318. WritePipe(message, pipe);
  319. ASSERT_TRUE(sender->Send(message));
  320. }
  321. static void ReadReceivedPipe(const IPC::Message& message,
  322. base::PickleIterator* iter) {
  323. mojo::ScopedMessagePipeHandle pipe;
  324. EXPECT_TRUE(
  325. IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe));
  326. std::vector<uint8_t> content;
  327. ASSERT_EQ(MOJO_RESULT_OK,
  328. mojo::Wait(pipe.get(), MOJO_HANDLE_SIGNAL_READABLE));
  329. EXPECT_EQ(MOJO_RESULT_OK,
  330. mojo::ReadMessageRaw(pipe.get(), &content, nullptr, 0));
  331. EXPECT_EQ(std::string(content.begin(), content.end()),
  332. GetSendingFileContent());
  333. }
  334. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  335. static base::FilePath GetSendingFilePath(const base::FilePath& dir_path) {
  336. return dir_path.Append("ListenerThatExpectsFile.txt");
  337. }
  338. static void WriteFile(IPC::Message* message, base::File& file) {
  339. std::string content = GetSendingFileContent();
  340. file.WriteAtCurrentPos(content.data(), content.size());
  341. file.Flush();
  342. message->WriteAttachment(new IPC::internal::PlatformFileAttachment(
  343. base::ScopedFD(file.TakePlatformFile())));
  344. }
  345. static void WriteFileThenSend(IPC::Sender* sender, base::File& file) {
  346. IPC::Message* message =
  347. new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
  348. WriteFile(message, file);
  349. ASSERT_TRUE(sender->Send(message));
  350. }
  351. static void WriteFileAndPipeThenSend(IPC::Sender* sender,
  352. base::File& file,
  353. TestingMessagePipe* pipe) {
  354. IPC::Message* message =
  355. new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
  356. WriteFile(message, file);
  357. WritePipe(message, pipe);
  358. ASSERT_TRUE(sender->Send(message));
  359. }
  360. static void ReadReceivedFile(const IPC::Message& message,
  361. base::PickleIterator* iter) {
  362. scoped_refptr<base::Pickle::Attachment> attachment;
  363. EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
  364. EXPECT_EQ(
  365. IPC::MessageAttachment::Type::PLATFORM_FILE,
  366. static_cast<IPC::MessageAttachment*>(attachment.get())->GetType());
  367. base::File file(
  368. static_cast<IPC::internal::PlatformFileAttachment*>(attachment.get())
  369. ->TakePlatformFile());
  370. std::string content(GetSendingFileContent().size(), ' ');
  371. file.Read(0, &content[0], content.size());
  372. EXPECT_EQ(content, GetSendingFileContent());
  373. }
  374. #endif
  375. };
  376. class ListenerThatExpectsMessagePipe : public TestListenerBase {
  377. public:
  378. ListenerThatExpectsMessagePipe(base::OnceClosure quit_closure)
  379. : TestListenerBase(std::move(quit_closure)) {}
  380. ~ListenerThatExpectsMessagePipe() override = default;
  381. bool OnMessageReceived(const IPC::Message& message) override {
  382. base::PickleIterator iter(message);
  383. HandleSendingHelper::ReadReceivedPipe(message, &iter);
  384. ListenerThatExpectsOK::SendOK(sender());
  385. return true;
  386. }
  387. };
  388. TEST_F(IPCChannelMojoTest, SendMessagePipe) {
  389. Init("IPCChannelMojoTestSendMessagePipeClient");
  390. base::RunLoop run_loop;
  391. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  392. CreateChannel(&listener);
  393. ASSERT_TRUE(ConnectChannel());
  394. TestingMessagePipe pipe;
  395. HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
  396. run_loop.Run();
  397. channel()->Close();
  398. EXPECT_TRUE(WaitForClientShutdown());
  399. DestroyChannel();
  400. }
  401. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendMessagePipeClient) {
  402. base::RunLoop run_loop;
  403. ListenerThatExpectsMessagePipe listener(run_loop.QuitClosure());
  404. Connect(&listener);
  405. listener.set_sender(channel());
  406. run_loop.Run();
  407. Close();
  408. }
  409. void ReadOK(mojo::MessagePipeHandle pipe) {
  410. std::vector<uint8_t> should_be_ok;
  411. CHECK_EQ(MOJO_RESULT_OK, mojo::Wait(pipe, MOJO_HANDLE_SIGNAL_READABLE));
  412. CHECK_EQ(MOJO_RESULT_OK,
  413. mojo::ReadMessageRaw(pipe, &should_be_ok, nullptr, 0));
  414. EXPECT_EQ("OK", std::string(should_be_ok.begin(), should_be_ok.end()));
  415. }
  416. void WriteOK(mojo::MessagePipeHandle pipe) {
  417. std::string ok("OK");
  418. CHECK_EQ(MOJO_RESULT_OK,
  419. mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()),
  420. nullptr, 0, 0));
  421. }
  422. class ListenerThatExpectsMessagePipeUsingParamTrait : public TestListenerBase {
  423. public:
  424. explicit ListenerThatExpectsMessagePipeUsingParamTrait(
  425. base::OnceClosure quit_closure,
  426. bool receiving_valid)
  427. : TestListenerBase(std::move(quit_closure)),
  428. receiving_valid_(receiving_valid) {}
  429. ~ListenerThatExpectsMessagePipeUsingParamTrait() override = default;
  430. bool OnMessageReceived(const IPC::Message& message) override {
  431. base::PickleIterator iter(message);
  432. mojo::MessagePipeHandle handle;
  433. EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter,
  434. &handle));
  435. EXPECT_EQ(handle.is_valid(), receiving_valid_);
  436. if (receiving_valid_) {
  437. ReadOK(handle);
  438. MojoClose(handle.value());
  439. }
  440. ListenerThatExpectsOK::SendOK(sender());
  441. return true;
  442. }
  443. private:
  444. bool receiving_valid_;
  445. };
  446. class ParamTraitMessagePipeClient : public IpcChannelMojoTestClient {
  447. public:
  448. void RunTest(bool receiving_valid_handle) {
  449. base::RunLoop run_loop;
  450. ListenerThatExpectsMessagePipeUsingParamTrait listener(
  451. run_loop.QuitClosure(), receiving_valid_handle);
  452. Connect(&listener);
  453. listener.set_sender(channel());
  454. run_loop.Run();
  455. Close();
  456. }
  457. };
  458. TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) {
  459. Init("ParamTraitValidMessagePipeClient");
  460. base::RunLoop run_loop;
  461. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  462. CreateChannel(&listener);
  463. ASSERT_TRUE(ConnectChannel());
  464. TestingMessagePipe pipe;
  465. std::unique_ptr<IPC::Message> message(new IPC::Message());
  466. IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
  467. pipe.peer.release());
  468. WriteOK(pipe.self.get());
  469. channel()->Send(message.release());
  470. run_loop.Run();
  471. channel()->Close();
  472. EXPECT_TRUE(WaitForClientShutdown());
  473. DestroyChannel();
  474. }
  475. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
  476. ParamTraitValidMessagePipeClient,
  477. ParamTraitMessagePipeClient) {
  478. RunTest(true);
  479. }
  480. TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) {
  481. Init("ParamTraitInvalidMessagePipeClient");
  482. base::RunLoop run_loop;
  483. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  484. CreateChannel(&listener);
  485. ASSERT_TRUE(ConnectChannel());
  486. mojo::MessagePipeHandle invalid_handle;
  487. std::unique_ptr<IPC::Message> message(new IPC::Message());
  488. IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
  489. invalid_handle);
  490. channel()->Send(message.release());
  491. run_loop.Run();
  492. channel()->Close();
  493. EXPECT_TRUE(WaitForClientShutdown());
  494. DestroyChannel();
  495. }
  496. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
  497. ParamTraitInvalidMessagePipeClient,
  498. ParamTraitMessagePipeClient) {
  499. RunTest(false);
  500. }
  501. TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
  502. Init("IPCChannelMojoTestSendOkClient");
  503. base::RunLoop run_loop;
  504. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  505. CreateChannel(&listener);
  506. ASSERT_TRUE(ConnectChannel());
  507. run_loop.Run();
  508. channel()->Close();
  509. ASSERT_FALSE(channel()->Send(new IPC::Message()));
  510. EXPECT_TRUE(WaitForClientShutdown());
  511. DestroyChannel();
  512. }
  513. class ListenerSendingOneOk : public TestListenerBase {
  514. public:
  515. ListenerSendingOneOk(base::OnceClosure quit_closure)
  516. : TestListenerBase(std::move(quit_closure)) {}
  517. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  518. void OnChannelConnected(int32_t peer_pid) override {
  519. ListenerThatExpectsOK::SendOK(sender());
  520. RunQuitClosure();
  521. }
  522. };
  523. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendOkClient) {
  524. base::RunLoop run_loop;
  525. ListenerSendingOneOk listener(run_loop.QuitClosure());
  526. Connect(&listener);
  527. listener.set_sender(channel());
  528. run_loop.Run();
  529. Close();
  530. }
  531. class ListenerWithSimpleAssociatedInterface
  532. : public IPC::Listener,
  533. public IPC::mojom::SimpleTestDriver {
  534. public:
  535. static const int kNumMessages;
  536. explicit ListenerWithSimpleAssociatedInterface(base::OnceClosure quit_closure)
  537. : quit_closure_(std::move(quit_closure)) {}
  538. ~ListenerWithSimpleAssociatedInterface() override = default;
  539. bool OnMessageReceived(const IPC::Message& message) override {
  540. base::PickleIterator iter(message);
  541. int32_t should_be_expected;
  542. EXPECT_TRUE(iter.ReadInt(&should_be_expected));
  543. EXPECT_EQ(should_be_expected, next_expected_value_);
  544. num_messages_received_++;
  545. return true;
  546. }
  547. void OnChannelError() override { CHECK(!quit_closure_); }
  548. void RegisterInterfaceFactory(IPC::Channel* channel) {
  549. channel->GetAssociatedInterfaceSupport()->AddAssociatedInterface(
  550. base::BindRepeating(
  551. &ListenerWithSimpleAssociatedInterface::BindReceiver,
  552. base::Unretained(this)));
  553. }
  554. private:
  555. // IPC::mojom::SimpleTestDriver:
  556. void ExpectValue(int32_t value) override {
  557. next_expected_value_ = value;
  558. }
  559. void GetExpectedValue(GetExpectedValueCallback callback) override {
  560. NOTREACHED();
  561. }
  562. void RequestValue(RequestValueCallback callback) override { NOTREACHED(); }
  563. void RequestQuit(RequestQuitCallback callback) override {
  564. EXPECT_EQ(kNumMessages, num_messages_received_);
  565. std::move(callback).Run();
  566. std::move(quit_closure_).Run();
  567. }
  568. void BindReceiver(
  569. mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
  570. DCHECK(!receiver_.is_bound());
  571. receiver_.Bind(std::move(receiver));
  572. }
  573. int32_t next_expected_value_ = 0;
  574. int num_messages_received_ = 0;
  575. base::OnceClosure quit_closure_;
  576. mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
  577. };
  578. const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000;
  579. class ListenerSendingAssociatedMessages : public IPC::Listener {
  580. public:
  581. explicit ListenerSendingAssociatedMessages(base::OnceClosure quit_closure)
  582. : quit_closure_(std::move(quit_closure)) {}
  583. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  584. void OnChannelConnected(int32_t peer_pid) override {
  585. DCHECK(channel_);
  586. channel_->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface(
  587. driver_.BindNewEndpointAndPassReceiver());
  588. // Send a bunch of interleaved messages, alternating between the associated
  589. // interface and a legacy IPC::Message.
  590. for (int i = 0; i < ListenerWithSimpleAssociatedInterface::kNumMessages;
  591. ++i) {
  592. driver_->ExpectValue(i);
  593. SendValue(channel_, i);
  594. }
  595. driver_->RequestQuit(base::BindOnce(
  596. &ListenerSendingAssociatedMessages::OnQuitAck, base::Unretained(this)));
  597. }
  598. void set_channel(IPC::Channel* channel) { channel_ = channel; }
  599. private:
  600. void OnQuitAck() { std::move(quit_closure_).Run(); }
  601. raw_ptr<IPC::Channel> channel_ = nullptr;
  602. mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver_;
  603. base::OnceClosure quit_closure_;
  604. };
  605. TEST_F(IPCChannelMojoTest, SimpleAssociatedInterface) {
  606. Init("SimpleAssociatedInterfaceClient");
  607. base::RunLoop run_loop;
  608. ListenerWithSimpleAssociatedInterface listener(run_loop.QuitClosure());
  609. CreateChannel(&listener);
  610. ASSERT_TRUE(ConnectChannel());
  611. listener.RegisterInterfaceFactory(channel());
  612. run_loop.Run();
  613. channel()->Close();
  614. EXPECT_TRUE(WaitForClientShutdown());
  615. DestroyChannel();
  616. }
  617. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(SimpleAssociatedInterfaceClient) {
  618. base::RunLoop run_loop;
  619. ListenerSendingAssociatedMessages listener(run_loop.QuitClosure());
  620. Connect(&listener);
  621. listener.set_channel(channel());
  622. run_loop.Run();
  623. Close();
  624. }
  625. class ChannelProxyRunner {
  626. public:
  627. ChannelProxyRunner(mojo::ScopedMessagePipeHandle handle,
  628. bool for_server)
  629. : for_server_(for_server),
  630. handle_(std::move(handle)),
  631. io_thread_("ChannelProxyRunner IO thread"),
  632. never_signaled_(base::WaitableEvent::ResetPolicy::MANUAL,
  633. base::WaitableEvent::InitialState::NOT_SIGNALED) {
  634. }
  635. ChannelProxyRunner(const ChannelProxyRunner&) = delete;
  636. ChannelProxyRunner& operator=(const ChannelProxyRunner&) = delete;
  637. void CreateProxy(IPC::Listener* listener) {
  638. io_thread_.StartWithOptions(
  639. base::Thread::Options(base::MessagePumpType::IO, 0));
  640. proxy_ = IPC::SyncChannel::Create(listener, io_thread_.task_runner(),
  641. base::ThreadTaskRunnerHandle::Get(),
  642. &never_signaled_);
  643. }
  644. void RunProxy() {
  645. std::unique_ptr<IPC::ChannelFactory> factory;
  646. if (for_server_) {
  647. factory = IPC::ChannelMojo::CreateServerFactory(
  648. std::move(handle_), io_thread_.task_runner(),
  649. base::ThreadTaskRunnerHandle::Get());
  650. } else {
  651. factory = IPC::ChannelMojo::CreateClientFactory(
  652. std::move(handle_), io_thread_.task_runner(),
  653. base::ThreadTaskRunnerHandle::Get());
  654. }
  655. proxy_->Init(std::move(factory), true);
  656. }
  657. IPC::ChannelProxy* proxy() { return proxy_.get(); }
  658. private:
  659. const bool for_server_;
  660. mojo::ScopedMessagePipeHandle handle_;
  661. base::Thread io_thread_;
  662. base::WaitableEvent never_signaled_;
  663. std::unique_ptr<IPC::ChannelProxy> proxy_;
  664. };
  665. class IPCChannelProxyMojoTest : public IPCChannelMojoTestBase {
  666. public:
  667. void Init(const std::string& client_name) {
  668. IPCChannelMojoTestBase::Init(client_name);
  669. runner_ = std::make_unique<ChannelProxyRunner>(TakeHandle(), true);
  670. }
  671. void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
  672. void RunProxy() {
  673. runner_->RunProxy();
  674. }
  675. void DestroyProxy() {
  676. runner_.reset();
  677. base::RunLoop().RunUntilIdle();
  678. }
  679. IPC::ChannelProxy* proxy() { return runner_->proxy(); }
  680. private:
  681. std::unique_ptr<ChannelProxyRunner> runner_;
  682. };
  683. class ListenerWithSimpleProxyAssociatedInterface
  684. : public IPC::Listener,
  685. public IPC::mojom::SimpleTestDriver {
  686. public:
  687. static const int kNumMessages;
  688. explicit ListenerWithSimpleProxyAssociatedInterface(
  689. base::OnceClosure quit_closure)
  690. : quit_closure_(std::move(quit_closure)) {}
  691. ~ListenerWithSimpleProxyAssociatedInterface() override = default;
  692. bool OnMessageReceived(const IPC::Message& message) override {
  693. base::PickleIterator iter(message);
  694. int32_t should_be_expected;
  695. EXPECT_TRUE(iter.ReadInt(&should_be_expected));
  696. EXPECT_EQ(should_be_expected, next_expected_value_);
  697. num_messages_received_++;
  698. return true;
  699. }
  700. void OnChannelError() override { CHECK(!quit_closure_); }
  701. void OnAssociatedInterfaceRequest(
  702. const std::string& interface_name,
  703. mojo::ScopedInterfaceEndpointHandle handle) override {
  704. DCHECK_EQ(interface_name, IPC::mojom::SimpleTestDriver::Name_);
  705. receiver_.Bind(
  706. mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver>(
  707. std::move(handle)));
  708. }
  709. bool received_all_messages() const {
  710. return num_messages_received_ == kNumMessages && !quit_closure_;
  711. }
  712. private:
  713. // IPC::mojom::SimpleTestDriver:
  714. void ExpectValue(int32_t value) override {
  715. next_expected_value_ = value;
  716. }
  717. void GetExpectedValue(GetExpectedValueCallback callback) override {
  718. std::move(callback).Run(next_expected_value_);
  719. }
  720. void RequestValue(RequestValueCallback callback) override { NOTREACHED(); }
  721. void RequestQuit(RequestQuitCallback callback) override {
  722. std::move(callback).Run();
  723. receiver_.reset();
  724. std::move(quit_closure_).Run();
  725. }
  726. void BindReceiver(
  727. mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
  728. DCHECK(!receiver_.is_bound());
  729. receiver_.Bind(std::move(receiver));
  730. }
  731. int32_t next_expected_value_ = 0;
  732. int num_messages_received_ = 0;
  733. base::OnceClosure quit_closure_;
  734. mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
  735. };
  736. const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
  737. TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterface) {
  738. Init("ProxyThreadAssociatedInterfaceClient");
  739. base::RunLoop run_loop;
  740. ListenerWithSimpleProxyAssociatedInterface listener(run_loop.QuitClosure());
  741. CreateProxy(&listener);
  742. RunProxy();
  743. run_loop.Run();
  744. EXPECT_TRUE(WaitForClientShutdown());
  745. EXPECT_TRUE(listener.received_all_messages());
  746. DestroyProxy();
  747. }
  748. class ChannelProxyClient {
  749. public:
  750. void Init(mojo::ScopedMessagePipeHandle handle) {
  751. runner_ = std::make_unique<ChannelProxyRunner>(std::move(handle), false);
  752. }
  753. void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
  754. void RunProxy() { runner_->RunProxy(); }
  755. void DestroyProxy() {
  756. runner_.reset();
  757. base::RunLoop().RunUntilIdle();
  758. }
  759. void RequestQuitAndWaitForAck(IPC::mojom::SimpleTestDriver* driver) {
  760. base::RunLoop loop;
  761. driver->RequestQuit(loop.QuitClosure());
  762. loop.Run();
  763. }
  764. IPC::ChannelProxy* proxy() { return runner_->proxy(); }
  765. private:
  766. base::test::SingleThreadTaskEnvironment task_environment_;
  767. std::unique_ptr<ChannelProxyRunner> runner_;
  768. };
  769. class DummyListener : public IPC::Listener {
  770. public:
  771. // IPC::Listener
  772. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  773. };
  774. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
  775. ProxyThreadAssociatedInterfaceClient,
  776. ChannelProxyClient) {
  777. DummyListener listener;
  778. CreateProxy(&listener);
  779. RunProxy();
  780. // Send a bunch of interleaved messages, alternating between the associated
  781. // interface and a legacy IPC::Message.
  782. mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver;
  783. proxy()->GetRemoteAssociatedInterface(
  784. driver.BindNewEndpointAndPassReceiver());
  785. for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages;
  786. ++i) {
  787. driver->ExpectValue(i);
  788. SendValue(proxy(), i);
  789. }
  790. base::RunLoop run_loop;
  791. driver->RequestQuit(run_loop.QuitClosure());
  792. run_loop.Run();
  793. DestroyProxy();
  794. }
  795. class ListenerWithIndirectProxyAssociatedInterface
  796. : public IPC::Listener,
  797. public IPC::mojom::IndirectTestDriver,
  798. public IPC::mojom::PingReceiver {
  799. public:
  800. ListenerWithIndirectProxyAssociatedInterface() = default;
  801. ~ListenerWithIndirectProxyAssociatedInterface() override = default;
  802. // IPC::Listener:
  803. bool OnMessageReceived(const IPC::Message& message) override { return true; }
  804. void OnAssociatedInterfaceRequest(
  805. const std::string& interface_name,
  806. mojo::ScopedInterfaceEndpointHandle handle) override {
  807. DCHECK(!driver_receiver_.is_bound());
  808. DCHECK_EQ(interface_name, IPC::mojom::IndirectTestDriver::Name_);
  809. driver_receiver_.Bind(
  810. mojo::PendingAssociatedReceiver<IPC::mojom::IndirectTestDriver>(
  811. std::move(handle)));
  812. }
  813. void set_ping_handler(const base::RepeatingClosure& handler) {
  814. ping_handler_ = handler;
  815. }
  816. private:
  817. // IPC::mojom::IndirectTestDriver:
  818. void GetPingReceiver(mojo::PendingAssociatedReceiver<IPC::mojom::PingReceiver>
  819. receiver) override {
  820. ping_receiver_receiver_.Bind(std::move(receiver));
  821. }
  822. // IPC::mojom::PingReceiver:
  823. void Ping(PingCallback callback) override {
  824. std::move(callback).Run();
  825. ping_handler_.Run();
  826. }
  827. mojo::AssociatedReceiver<IPC::mojom::IndirectTestDriver> driver_receiver_{
  828. this};
  829. mojo::AssociatedReceiver<IPC::mojom::PingReceiver> ping_receiver_receiver_{
  830. this};
  831. base::RepeatingClosure ping_handler_;
  832. };
  833. TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterfaceIndirect) {
  834. // Tests that we can pipeline interface requests and subsequent messages
  835. // targeting proxy thread bindings, and the channel will still dispatch
  836. // messages appropriately.
  837. Init("ProxyThreadAssociatedInterfaceIndirectClient");
  838. ListenerWithIndirectProxyAssociatedInterface listener;
  839. CreateProxy(&listener);
  840. RunProxy();
  841. base::RunLoop loop;
  842. listener.set_ping_handler(loop.QuitClosure());
  843. loop.Run();
  844. EXPECT_TRUE(WaitForClientShutdown());
  845. DestroyProxy();
  846. }
  847. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
  848. ProxyThreadAssociatedInterfaceIndirectClient,
  849. ChannelProxyClient) {
  850. DummyListener listener;
  851. CreateProxy(&listener);
  852. RunProxy();
  853. // Use an interface requested via another interface. On the remote end both
  854. // interfaces are bound on the proxy thread. This ensures that the Ping
  855. // message we send will still be dispatched properly even though the remote
  856. // endpoint may not have been bound yet by the time the message is initially
  857. // processed on the IO thread.
  858. mojo::AssociatedRemote<IPC::mojom::IndirectTestDriver> driver;
  859. mojo::AssociatedRemote<IPC::mojom::PingReceiver> ping_receiver;
  860. proxy()->GetRemoteAssociatedInterface(
  861. driver.BindNewEndpointAndPassReceiver());
  862. driver->GetPingReceiver(ping_receiver.BindNewEndpointAndPassReceiver());
  863. base::RunLoop loop;
  864. ping_receiver->Ping(loop.QuitClosure());
  865. loop.Run();
  866. DestroyProxy();
  867. }
  868. class ListenerWithSyncAssociatedInterface
  869. : public IPC::Listener,
  870. public IPC::mojom::SimpleTestDriver {
  871. public:
  872. ListenerWithSyncAssociatedInterface() = default;
  873. ~ListenerWithSyncAssociatedInterface() override = default;
  874. void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; }
  875. void RunUntilQuitRequested() {
  876. base::RunLoop loop;
  877. quit_closure_ = loop.QuitClosure();
  878. loop.Run();
  879. }
  880. void CloseBinding() { receiver_.reset(); }
  881. void set_response_value(int32_t response) {
  882. response_value_ = response;
  883. }
  884. private:
  885. // IPC::mojom::SimpleTestDriver:
  886. void ExpectValue(int32_t value) override {
  887. next_expected_value_ = value;
  888. }
  889. void GetExpectedValue(GetExpectedValueCallback callback) override {
  890. std::move(callback).Run(next_expected_value_);
  891. }
  892. void RequestValue(RequestValueCallback callback) override {
  893. std::move(callback).Run(response_value_);
  894. }
  895. void RequestQuit(RequestQuitCallback callback) override {
  896. std::move(quit_closure_).Run();
  897. std::move(callback).Run();
  898. }
  899. // IPC::Listener:
  900. bool OnMessageReceived(const IPC::Message& message) override {
  901. EXPECT_EQ(0u, message.type());
  902. EXPECT_TRUE(message.is_sync());
  903. EXPECT_TRUE(message.should_unblock());
  904. std::unique_ptr<IPC::Message> reply(
  905. IPC::SyncMessage::GenerateReply(&message));
  906. reply->WriteInt(response_value_);
  907. DCHECK(sync_sender_);
  908. EXPECT_TRUE(sync_sender_->Send(reply.release()));
  909. return true;
  910. }
  911. void OnAssociatedInterfaceRequest(
  912. const std::string& interface_name,
  913. mojo::ScopedInterfaceEndpointHandle handle) override {
  914. DCHECK(!receiver_.is_bound());
  915. DCHECK_EQ(interface_name, IPC::mojom::SimpleTestDriver::Name_);
  916. receiver_.Bind(
  917. mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver>(
  918. std::move(handle)));
  919. }
  920. void BindReceiver(
  921. mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
  922. DCHECK(!receiver_.is_bound());
  923. receiver_.Bind(std::move(receiver));
  924. }
  925. IPC::Sender* sync_sender_ = nullptr;
  926. int32_t next_expected_value_ = 0;
  927. int32_t response_value_ = 0;
  928. base::OnceClosure quit_closure_;
  929. mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
  930. };
  931. class SyncReplyReader : public IPC::MessageReplyDeserializer {
  932. public:
  933. explicit SyncReplyReader(int32_t* storage) : storage_(storage) {}
  934. SyncReplyReader(const SyncReplyReader&) = delete;
  935. SyncReplyReader& operator=(const SyncReplyReader&) = delete;
  936. ~SyncReplyReader() override = default;
  937. private:
  938. // IPC::MessageReplyDeserializer:
  939. bool SerializeOutputParameters(const IPC::Message& message,
  940. base::PickleIterator iter) override {
  941. if (!iter.ReadInt(storage_))
  942. return false;
  943. return true;
  944. }
  945. raw_ptr<int32_t> storage_;
  946. };
  947. TEST_F(IPCChannelProxyMojoTest, SyncAssociatedInterface) {
  948. Init("SyncAssociatedInterface");
  949. ListenerWithSyncAssociatedInterface listener;
  950. CreateProxy(&listener);
  951. listener.set_sync_sender(proxy());
  952. RunProxy();
  953. // Run the client's simple sanity check to completion.
  954. listener.RunUntilQuitRequested();
  955. // Verify that we can send a sync IPC and service an incoming sync request
  956. // while waiting on it
  957. listener.set_response_value(42);
  958. mojo::AssociatedRemote<IPC::mojom::SimpleTestClient> client;
  959. proxy()->GetRemoteAssociatedInterface(
  960. client.BindNewEndpointAndPassReceiver());
  961. int32_t received_value;
  962. EXPECT_TRUE(client->RequestValue(&received_value));
  963. EXPECT_EQ(42, received_value);
  964. // Do it again. This time the client will send a classical sync IPC to us
  965. // while we wait.
  966. received_value = 0;
  967. EXPECT_TRUE(client->RequestValue(&received_value));
  968. EXPECT_EQ(42, received_value);
  969. // Now make a classical sync IPC request to the client. It will send a
  970. // sync associated interface message to us while we wait.
  971. received_value = 0;
  972. std::unique_ptr<IPC::SyncMessage> request(
  973. new IPC::SyncMessage(0, 0, IPC::Message::PRIORITY_NORMAL,
  974. new SyncReplyReader(&received_value)));
  975. EXPECT_TRUE(proxy()->Send(request.release()));
  976. EXPECT_EQ(42, received_value);
  977. listener.CloseBinding();
  978. EXPECT_TRUE(WaitForClientShutdown());
  979. DestroyProxy();
  980. }
  981. class SimpleTestClientImpl : public IPC::mojom::SimpleTestClient,
  982. public IPC::Listener {
  983. public:
  984. SimpleTestClientImpl() = default;
  985. SimpleTestClientImpl(const SimpleTestClientImpl&) = delete;
  986. SimpleTestClientImpl& operator=(const SimpleTestClientImpl&) = delete;
  987. ~SimpleTestClientImpl() override = default;
  988. void set_driver(IPC::mojom::SimpleTestDriver* driver) { driver_ = driver; }
  989. void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; }
  990. void WaitForValueRequest() {
  991. run_loop_ = std::make_unique<base::RunLoop>();
  992. run_loop_->Run();
  993. }
  994. void UseSyncSenderForRequest(bool use_sync_sender) {
  995. use_sync_sender_ = use_sync_sender;
  996. }
  997. private:
  998. // IPC::mojom::SimpleTestClient:
  999. void RequestValue(RequestValueCallback callback) override {
  1000. int32_t response = 0;
  1001. if (use_sync_sender_) {
  1002. std::unique_ptr<IPC::SyncMessage> reply(new IPC::SyncMessage(
  1003. 0, 0, IPC::Message::PRIORITY_NORMAL, new SyncReplyReader(&response)));
  1004. EXPECT_TRUE(sync_sender_->Send(reply.release()));
  1005. } else {
  1006. DCHECK(driver_);
  1007. EXPECT_TRUE(driver_->RequestValue(&response));
  1008. }
  1009. std::move(callback).Run(response);
  1010. DCHECK(run_loop_);
  1011. run_loop_->Quit();
  1012. }
  1013. // IPC::Listener:
  1014. bool OnMessageReceived(const IPC::Message& message) override {
  1015. int32_t response;
  1016. DCHECK(driver_);
  1017. EXPECT_TRUE(driver_->RequestValue(&response));
  1018. std::unique_ptr<IPC::Message> reply(
  1019. IPC::SyncMessage::GenerateReply(&message));
  1020. reply->WriteInt(response);
  1021. EXPECT_TRUE(sync_sender_->Send(reply.release()));
  1022. DCHECK(run_loop_);
  1023. run_loop_->Quit();
  1024. return true;
  1025. }
  1026. void OnAssociatedInterfaceRequest(
  1027. const std::string& interface_name,
  1028. mojo::ScopedInterfaceEndpointHandle handle) override {
  1029. DCHECK(!receiver_.is_bound());
  1030. DCHECK_EQ(interface_name, IPC::mojom::SimpleTestClient::Name_);
  1031. receiver_.Bind(
  1032. mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestClient>(
  1033. std::move(handle)));
  1034. }
  1035. bool use_sync_sender_ = false;
  1036. mojo::AssociatedReceiver<IPC::mojom::SimpleTestClient> receiver_{this};
  1037. IPC::Sender* sync_sender_ = nullptr;
  1038. IPC::mojom::SimpleTestDriver* driver_ = nullptr;
  1039. std::unique_ptr<base::RunLoop> run_loop_;
  1040. };
  1041. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(SyncAssociatedInterface,
  1042. ChannelProxyClient) {
  1043. SimpleTestClientImpl client_impl;
  1044. CreateProxy(&client_impl);
  1045. client_impl.set_sync_sender(proxy());
  1046. RunProxy();
  1047. mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver;
  1048. proxy()->GetRemoteAssociatedInterface(
  1049. driver.BindNewEndpointAndPassReceiver());
  1050. client_impl.set_driver(driver.get());
  1051. // Simple sync message sanity check.
  1052. driver->ExpectValue(42);
  1053. int32_t expected_value = 0;
  1054. EXPECT_TRUE(driver->GetExpectedValue(&expected_value));
  1055. EXPECT_EQ(42, expected_value);
  1056. RequestQuitAndWaitForAck(driver.get());
  1057. // Wait for the test driver to perform a sync call test with our own sync
  1058. // associated interface message nested inside.
  1059. client_impl.UseSyncSenderForRequest(false);
  1060. client_impl.WaitForValueRequest();
  1061. // Wait for the test driver to perform a sync call test with our own classical
  1062. // sync IPC nested inside.
  1063. client_impl.UseSyncSenderForRequest(true);
  1064. client_impl.WaitForValueRequest();
  1065. // Wait for the test driver to perform a classical sync IPC request, with our
  1066. // own sync associated interface message nested inside.
  1067. client_impl.UseSyncSenderForRequest(false);
  1068. client_impl.WaitForValueRequest();
  1069. DestroyProxy();
  1070. }
  1071. TEST_F(IPCChannelProxyMojoTest, Pause) {
  1072. // Ensures that pausing a channel elicits the expected behavior when sending
  1073. // messages, unpausing, sending more messages, and then manually flushing.
  1074. // Specifically a sequence like:
  1075. //
  1076. // Connect()
  1077. // Send(A)
  1078. // Pause()
  1079. // Send(B)
  1080. // Send(C)
  1081. // Unpause(false)
  1082. // Send(D)
  1083. // Send(E)
  1084. // Flush()
  1085. //
  1086. // must result in the other end receiving messages A, D, E, B, D; in that
  1087. // order.
  1088. //
  1089. // This behavior is required by some consumers of IPC::Channel, and it is not
  1090. // sufficient to leave this up to the consumer to implement since associated
  1091. // interface requests and messages also need to be queued according to the
  1092. // same policy.
  1093. Init("CreatePausedClient");
  1094. DummyListener listener;
  1095. CreateProxy(&listener);
  1096. RunProxy();
  1097. // This message must be sent immediately since the channel is unpaused.
  1098. SendValue(proxy(), 1);
  1099. proxy()->Pause();
  1100. // These messages must be queued internally since the channel is paused.
  1101. SendValue(proxy(), 2);
  1102. SendValue(proxy(), 3);
  1103. proxy()->Unpause(false /* flush */);
  1104. // These messages must be sent immediately since the channel is unpaused.
  1105. SendValue(proxy(), 4);
  1106. SendValue(proxy(), 5);
  1107. // Now we flush the previously queued messages.
  1108. proxy()->Flush();
  1109. EXPECT_TRUE(WaitForClientShutdown());
  1110. DestroyProxy();
  1111. }
  1112. class ExpectValueSequenceListener : public IPC::Listener {
  1113. public:
  1114. ExpectValueSequenceListener(base::queue<int32_t>* expected_values,
  1115. base::OnceClosure quit_closure)
  1116. : expected_values_(expected_values),
  1117. quit_closure_(std::move(quit_closure)) {}
  1118. ExpectValueSequenceListener(const ExpectValueSequenceListener&) = delete;
  1119. ExpectValueSequenceListener& operator=(const ExpectValueSequenceListener&) =
  1120. delete;
  1121. ~ExpectValueSequenceListener() override = default;
  1122. // IPC::Listener:
  1123. bool OnMessageReceived(const IPC::Message& message) override {
  1124. DCHECK(!expected_values_->empty());
  1125. base::PickleIterator iter(message);
  1126. int32_t should_be_expected;
  1127. EXPECT_TRUE(iter.ReadInt(&should_be_expected));
  1128. EXPECT_EQ(expected_values_->front(), should_be_expected);
  1129. expected_values_->pop();
  1130. if (expected_values_->empty())
  1131. std::move(quit_closure_).Run();
  1132. return true;
  1133. }
  1134. private:
  1135. raw_ptr<base::queue<int32_t>> expected_values_;
  1136. base::OnceClosure quit_closure_;
  1137. };
  1138. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(CreatePausedClient,
  1139. ChannelProxyClient) {
  1140. base::queue<int32_t> expected_values;
  1141. base::RunLoop run_loop;
  1142. ExpectValueSequenceListener listener(&expected_values,
  1143. run_loop.QuitClosure());
  1144. CreateProxy(&listener);
  1145. expected_values.push(1);
  1146. expected_values.push(4);
  1147. expected_values.push(5);
  1148. expected_values.push(2);
  1149. expected_values.push(3);
  1150. RunProxy();
  1151. run_loop.Run();
  1152. EXPECT_TRUE(expected_values.empty());
  1153. DestroyProxy();
  1154. }
  1155. TEST_F(IPCChannelProxyMojoTest, AssociatedRequestClose) {
  1156. Init("DropAssociatedRequest");
  1157. DummyListener listener;
  1158. CreateProxy(&listener);
  1159. RunProxy();
  1160. mojo::AssociatedRemote<IPC::mojom::AssociatedInterfaceVendor> vendor;
  1161. proxy()->GetRemoteAssociatedInterface(
  1162. vendor.BindNewEndpointAndPassReceiver());
  1163. mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> tester;
  1164. vendor->GetTestInterface(tester.BindNewEndpointAndPassReceiver());
  1165. base::RunLoop run_loop;
  1166. tester.set_disconnect_handler(run_loop.QuitClosure());
  1167. run_loop.Run();
  1168. tester.reset();
  1169. proxy()->GetRemoteAssociatedInterface(
  1170. tester.BindNewEndpointAndPassReceiver());
  1171. EXPECT_TRUE(WaitForClientShutdown());
  1172. DestroyProxy();
  1173. }
  1174. class AssociatedInterfaceDroppingListener : public IPC::Listener {
  1175. public:
  1176. AssociatedInterfaceDroppingListener(base::OnceClosure callback)
  1177. : callback_(std::move(callback)) {}
  1178. bool OnMessageReceived(const IPC::Message& message) override { return false; }
  1179. void OnAssociatedInterfaceRequest(
  1180. const std::string& interface_name,
  1181. mojo::ScopedInterfaceEndpointHandle handle) override {
  1182. if (interface_name == IPC::mojom::SimpleTestDriver::Name_)
  1183. std::move(callback_).Run();
  1184. }
  1185. private:
  1186. base::OnceClosure callback_;
  1187. };
  1188. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(DropAssociatedRequest,
  1189. ChannelProxyClient) {
  1190. base::RunLoop run_loop;
  1191. AssociatedInterfaceDroppingListener listener(run_loop.QuitClosure());
  1192. CreateProxy(&listener);
  1193. RunProxy();
  1194. run_loop.Run();
  1195. DestroyProxy();
  1196. }
  1197. #if !BUILDFLAG(IS_APPLE)
  1198. // TODO(wez): On Mac we need to set up a MachPortBroker before we can transfer
  1199. // Mach ports (which underpin Sharedmemory on Mac) across IPC.
  1200. template <class SharedMemoryRegionType>
  1201. class IPCChannelMojoSharedMemoryRegionTypedTest : public IPCChannelMojoTest {};
  1202. struct WritableRegionTraits {
  1203. using RegionType = base::WritableSharedMemoryRegion;
  1204. static const char kClientName[];
  1205. };
  1206. const char WritableRegionTraits::kClientName[] =
  1207. "IPCChannelMojoTestSendWritableSharedMemoryRegionClient";
  1208. struct UnsafeRegionTraits {
  1209. using RegionType = base::UnsafeSharedMemoryRegion;
  1210. static const char kClientName[];
  1211. };
  1212. const char UnsafeRegionTraits::kClientName[] =
  1213. "IPCChannelMojoTestSendUnsafeSharedMemoryRegionClient";
  1214. struct ReadOnlyRegionTraits {
  1215. using RegionType = base::ReadOnlySharedMemoryRegion;
  1216. static const char kClientName[];
  1217. };
  1218. const char ReadOnlyRegionTraits::kClientName[] =
  1219. "IPCChannelMojoTestSendReadOnlySharedMemoryRegionClient";
  1220. typedef ::testing::
  1221. Types<WritableRegionTraits, UnsafeRegionTraits, ReadOnlyRegionTraits>
  1222. AllSharedMemoryRegionTraits;
  1223. TYPED_TEST_SUITE(IPCChannelMojoSharedMemoryRegionTypedTest,
  1224. AllSharedMemoryRegionTraits);
  1225. template <class SharedMemoryRegionType>
  1226. class ListenerThatExpectsSharedMemoryRegion : public TestListenerBase {
  1227. public:
  1228. explicit ListenerThatExpectsSharedMemoryRegion(base::OnceClosure quit_closure)
  1229. : TestListenerBase(std::move(quit_closure)) {}
  1230. bool OnMessageReceived(const IPC::Message& message) override {
  1231. base::PickleIterator iter(message);
  1232. SharedMemoryRegionType region;
  1233. EXPECT_TRUE(IPC::ReadParam(&message, &iter, &region));
  1234. EXPECT_TRUE(region.IsValid());
  1235. // Verify the shared memory region has expected content.
  1236. typename SharedMemoryRegionType::MappingType mapping = region.Map();
  1237. std::string content = HandleSendingHelper::GetSendingFileContent();
  1238. EXPECT_EQ(0, memcmp(mapping.memory(), content.data(), content.size()));
  1239. ListenerThatExpectsOK::SendOK(sender());
  1240. return true;
  1241. }
  1242. };
  1243. TYPED_TEST(IPCChannelMojoSharedMemoryRegionTypedTest, Send) {
  1244. this->Init(TypeParam::kClientName);
  1245. const size_t size = 1004;
  1246. typename TypeParam::RegionType region;
  1247. base::WritableSharedMemoryMapping mapping;
  1248. std::tie(region, mapping) =
  1249. base::CreateMappedRegion<typename TypeParam::RegionType>(size);
  1250. std::string content = HandleSendingHelper::GetSendingFileContent();
  1251. memcpy(mapping.memory(), content.data(), content.size());
  1252. // Create a success listener, and launch the child process.
  1253. base::RunLoop run_loop;
  1254. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  1255. this->CreateChannel(&listener);
  1256. ASSERT_TRUE(this->ConnectChannel());
  1257. // Send the child process an IPC with |shmem| attached, to verify
  1258. // that is is correctly wrapped, transferred and unwrapped.
  1259. IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
  1260. IPC::WriteParam(message, region);
  1261. ASSERT_TRUE(this->channel()->Send(message));
  1262. run_loop.Run();
  1263. this->channel()->Close();
  1264. EXPECT_TRUE(this->WaitForClientShutdown());
  1265. EXPECT_FALSE(region.IsValid());
  1266. this->DestroyChannel();
  1267. }
  1268. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
  1269. IPCChannelMojoTestSendWritableSharedMemoryRegionClient) {
  1270. base::RunLoop run_loop;
  1271. ListenerThatExpectsSharedMemoryRegion<base::WritableSharedMemoryRegion>
  1272. listener(run_loop.QuitClosure());
  1273. Connect(&listener);
  1274. listener.set_sender(channel());
  1275. run_loop.Run();
  1276. Close();
  1277. }
  1278. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
  1279. IPCChannelMojoTestSendUnsafeSharedMemoryRegionClient) {
  1280. base::RunLoop run_loop;
  1281. ListenerThatExpectsSharedMemoryRegion<base::UnsafeSharedMemoryRegion>
  1282. listener(run_loop.QuitClosure());
  1283. Connect(&listener);
  1284. listener.set_sender(channel());
  1285. run_loop.Run();
  1286. Close();
  1287. }
  1288. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
  1289. IPCChannelMojoTestSendReadOnlySharedMemoryRegionClient) {
  1290. base::RunLoop run_loop;
  1291. ListenerThatExpectsSharedMemoryRegion<base::ReadOnlySharedMemoryRegion>
  1292. listener(run_loop.QuitClosure());
  1293. Connect(&listener);
  1294. listener.set_sender(channel());
  1295. run_loop.Run();
  1296. Close();
  1297. }
  1298. #endif // !BUILDFLAG(IS_APPLE)
  1299. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  1300. class ListenerThatExpectsFile : public TestListenerBase {
  1301. public:
  1302. explicit ListenerThatExpectsFile(base::OnceClosure quit_closure)
  1303. : TestListenerBase(std::move(quit_closure)) {}
  1304. bool OnMessageReceived(const IPC::Message& message) override {
  1305. base::PickleIterator iter(message);
  1306. HandleSendingHelper::ReadReceivedFile(message, &iter);
  1307. ListenerThatExpectsOK::SendOK(sender());
  1308. return true;
  1309. }
  1310. };
  1311. TEST_F(IPCChannelMojoTest, SendPlatformFile) {
  1312. Init("IPCChannelMojoTestSendPlatformFileClient");
  1313. base::RunLoop run_loop;
  1314. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  1315. CreateChannel(&listener);
  1316. ASSERT_TRUE(ConnectChannel());
  1317. base::ScopedTempDir temp_dir;
  1318. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  1319. base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.GetPath()),
  1320. base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
  1321. base::File::FLAG_READ);
  1322. HandleSendingHelper::WriteFileThenSend(channel(), file);
  1323. run_loop.Run();
  1324. channel()->Close();
  1325. EXPECT_TRUE(WaitForClientShutdown());
  1326. DestroyChannel();
  1327. }
  1328. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformFileClient) {
  1329. base::RunLoop run_loop;
  1330. ListenerThatExpectsFile listener(run_loop.QuitClosure());
  1331. Connect(&listener);
  1332. listener.set_sender(channel());
  1333. run_loop.Run();
  1334. Close();
  1335. }
  1336. class ListenerThatExpectsFileAndMessagePipe : public TestListenerBase {
  1337. public:
  1338. explicit ListenerThatExpectsFileAndMessagePipe(base::OnceClosure quit_closure)
  1339. : TestListenerBase(std::move(quit_closure)) {}
  1340. ~ListenerThatExpectsFileAndMessagePipe() override = default;
  1341. bool OnMessageReceived(const IPC::Message& message) override {
  1342. base::PickleIterator iter(message);
  1343. HandleSendingHelper::ReadReceivedFile(message, &iter);
  1344. HandleSendingHelper::ReadReceivedPipe(message, &iter);
  1345. ListenerThatExpectsOK::SendOK(sender());
  1346. return true;
  1347. }
  1348. };
  1349. TEST_F(IPCChannelMojoTest, SendPlatformFileAndMessagePipe) {
  1350. Init("IPCChannelMojoTestSendPlatformFileAndMessagePipeClient");
  1351. base::RunLoop run_loop;
  1352. ListenerThatExpectsOK listener(run_loop.QuitClosure());
  1353. CreateChannel(&listener);
  1354. ASSERT_TRUE(ConnectChannel());
  1355. base::ScopedTempDir temp_dir;
  1356. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  1357. base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.GetPath()),
  1358. base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
  1359. base::File::FLAG_READ);
  1360. TestingMessagePipe pipe;
  1361. HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
  1362. run_loop.Run();
  1363. channel()->Close();
  1364. EXPECT_TRUE(WaitForClientShutdown());
  1365. DestroyChannel();
  1366. }
  1367. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
  1368. IPCChannelMojoTestSendPlatformFileAndMessagePipeClient) {
  1369. base::RunLoop run_loop;
  1370. ListenerThatExpectsFileAndMessagePipe listener(run_loop.QuitClosure());
  1371. Connect(&listener);
  1372. listener.set_sender(channel());
  1373. run_loop.Run();
  1374. Close();
  1375. }
  1376. #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  1377. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  1378. const base::ProcessId kMagicChildId = 54321;
  1379. class ListenerThatVerifiesPeerPid : public TestListenerBase {
  1380. public:
  1381. explicit ListenerThatVerifiesPeerPid(base::OnceClosure quit_closure)
  1382. : TestListenerBase(std::move(quit_closure)) {}
  1383. void OnChannelConnected(int32_t peer_pid) override {
  1384. EXPECT_EQ(peer_pid, kMagicChildId);
  1385. RunQuitClosure();
  1386. }
  1387. bool OnMessageReceived(const IPC::Message& message) override {
  1388. NOTREACHED();
  1389. return true;
  1390. }
  1391. };
  1392. // The global PID is only used on systems that use the zygote. Hence, this
  1393. // test is disabled on other platforms.
  1394. TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
  1395. Init("IPCChannelMojoTestVerifyGlobalPidClient");
  1396. base::RunLoop run_loop;
  1397. ListenerThatVerifiesPeerPid listener(run_loop.QuitClosure());
  1398. CreateChannel(&listener);
  1399. ASSERT_TRUE(ConnectChannel());
  1400. run_loop.Run();
  1401. channel()->Close();
  1402. EXPECT_TRUE(WaitForClientShutdown());
  1403. DestroyChannel();
  1404. }
  1405. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient) {
  1406. IPC::Channel::SetGlobalPid(kMagicChildId);
  1407. base::RunLoop run_loop;
  1408. ListenerThatQuits listener(run_loop.QuitClosure());
  1409. Connect(&listener);
  1410. run_loop.Run();
  1411. Close();
  1412. }
  1413. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  1414. } // namespace