OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/test/mojo_test_base.h" | 5 #include "mojo/edk/test/mojo_test_base.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "mojo/edk/embedder/embedder.h" | 9 #include "mojo/edk/embedder/embedder.h" |
10 #include "mojo/edk/system/handle_signals_state.h" | 10 #include "mojo/edk/system/handle_signals_state.h" |
11 #include "mojo/public/c/system/buffer.h" | 11 #include "mojo/public/c/system/buffer.h" |
12 #include "mojo/public/c/system/data_pipe.h" | 12 #include "mojo/public/c/system/data_pipe.h" |
13 #include "mojo/public/c/system/functions.h" | 13 #include "mojo/public/c/system/functions.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
| 16 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 17 #include "base/mac/mach_port_broker.h" |
| 18 #endif |
| 19 |
16 namespace mojo { | 20 namespace mojo { |
17 namespace edk { | 21 namespace edk { |
18 namespace test { | 22 namespace test { |
19 | 23 |
| 24 |
| 25 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 26 namespace { |
| 27 base::MachPortBroker* g_mach_broker = nullptr; |
| 28 } |
| 29 #endif |
| 30 |
20 MojoTestBase::MojoTestBase() { | 31 MojoTestBase::MojoTestBase() { |
| 32 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 33 if (!g_mach_broker) { |
| 34 g_mach_broker = new base::MachPortBroker("mojo_test"); |
| 35 CHECK(g_mach_broker->Init()); |
| 36 SetMachPortProviderIfNeeded(g_mach_broker); |
| 37 } |
| 38 #endif |
21 } | 39 } |
22 | 40 |
23 MojoTestBase::~MojoTestBase() {} | 41 MojoTestBase::~MojoTestBase() {} |
24 | 42 |
25 MojoTestBase::ClientController& MojoTestBase::StartClient( | 43 MojoTestBase::ClientController& MojoTestBase::StartClient( |
26 const std::string& client_name) { | 44 const std::string& client_name) { |
27 clients_.push_back( | 45 clients_.push_back( |
28 make_scoped_ptr(new ClientController(client_name, this))); | 46 make_scoped_ptr(new ClientController(client_name, this))); |
29 return *clients_.back(); | 47 return *clients_.back(); |
30 } | 48 } |
31 | 49 |
32 MojoTestBase::ClientController::ClientController(const std::string& client_name, | 50 MojoTestBase::ClientController::ClientController(const std::string& client_name, |
33 MojoTestBase* test) | 51 MojoTestBase* test) |
34 : test_(test) | 52 : test_(test) { |
35 #if !defined(OS_IOS) | 53 #if !defined(OS_IOS) |
36 , | 54 #if defined(OS_MACOSX) |
37 pipe_(helper_.StartChild(client_name)) | 55 // This lock needs to be held while launching the child because the Mach port |
| 56 // broker only allows task ports to be received from known child processes. |
| 57 // However, it can only know the child process's pid after the child has |
| 58 // launched. To prevent a race where the child process sends its task port |
| 59 // before the pid has been registered, the lock needs to be held over both |
| 60 // launch and child pid registration. |
| 61 base::AutoLock lock(g_mach_broker->GetLock()); |
38 #endif | 62 #endif |
39 { | 63 pipe_ = helper_.StartChild(client_name); |
| 64 #if defined(OS_MACOSX) |
| 65 g_mach_broker->AddPlaceholderForPid(helper_.test_child().Handle()); |
| 66 #endif |
| 67 #endif |
40 } | 68 } |
41 | 69 |
42 MojoTestBase::ClientController::~ClientController() { | 70 MojoTestBase::ClientController::~ClientController() { |
43 CHECK(was_shutdown_) | 71 CHECK(was_shutdown_) |
44 << "Test clients should be waited on explicitly with WaitForShutdown()."; | 72 << "Test clients should be waited on explicitly with WaitForShutdown()."; |
45 } | 73 } |
46 | 74 |
47 int MojoTestBase::ClientController::WaitForShutdown() { | 75 int MojoTestBase::ClientController::WaitForShutdown() { |
48 was_shutdown_ = true; | 76 was_shutdown_ = true; |
49 #if !defined(OS_IOS) | 77 #if !defined(OS_IOS) |
50 return helper_.WaitForChildShutdown(); | 78 int retval = helper_.WaitForChildShutdown(); |
| 79 #if defined(OS_MACOSX) |
| 80 base::AutoLock lock(g_mach_broker->GetLock()); |
| 81 g_mach_broker->InvalidatePid(helper_.test_child().Handle()); |
| 82 #endif |
| 83 return retval; |
51 #else | 84 #else |
52 NOTREACHED(); | 85 NOTREACHED(); |
53 return 1; | 86 return 1; |
54 #endif | 87 #endif |
55 } | 88 } |
56 | 89 |
57 // static | 90 // static |
58 void MojoTestBase::CloseHandle(MojoHandle h) { | 91 void MojoTestBase::CloseHandle(MojoHandle h) { |
59 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h)); | 92 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h)); |
60 } | 93 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), | 293 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
261 MOJO_RESULT_OK); | 294 MOJO_RESULT_OK); |
262 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); | 295 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); |
263 | 296 |
264 return std::string(buffer.data(), buffer.size()); | 297 return std::string(buffer.data(), buffer.size()); |
265 } | 298 } |
266 | 299 |
267 } // namespace test | 300 } // namespace test |
268 } // namespace edk | 301 } // namespace edk |
269 } // namespace mojo | 302 } // namespace mojo |
OLD | NEW |