OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
18 #include "base/files/scoped_file.h" | 18 #include "base/files/scoped_file.h" |
19 #include "base/files/scoped_temp_dir.h" | 19 #include "base/files/scoped_temp_dir.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
| 21 #include "base/message_loop/message_loop.h" |
| 22 #include "base/run_loop.h" |
21 #include "base/strings/string_split.h" | 23 #include "base/strings/string_split.h" |
22 #include "build/build_config.h" | 24 #include "build/build_config.h" |
23 #include "mojo/edk/embedder/platform_channel_pair.h" | 25 #include "mojo/edk/embedder/platform_channel_pair.h" |
24 #include "mojo/edk/embedder/scoped_platform_handle.h" | 26 #include "mojo/edk/embedder/scoped_platform_handle.h" |
25 #include "mojo/edk/system/handle_signals_state.h" | 27 #include "mojo/edk/system/handle_signals_state.h" |
26 #include "mojo/edk/system/test_utils.h" | 28 #include "mojo/edk/system/test_utils.h" |
27 #include "mojo/edk/test/mojo_test_base.h" | 29 #include "mojo/edk/test/mojo_test_base.h" |
28 #include "mojo/edk/test/test_utils.h" | 30 #include "mojo/edk/test/test_utils.h" |
29 #include "mojo/public/c/system/buffer.h" | 31 #include "mojo/public/c/system/buffer.h" |
30 #include "mojo/public/c/system/functions.h" | 32 #include "mojo/public/c/system/functions.h" |
31 #include "mojo/public/c/system/types.h" | 33 #include "mojo/public/c/system/types.h" |
| 34 #include "mojo/public/cpp/system/watcher.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
33 | 36 |
34 | 37 |
35 namespace mojo { | 38 namespace mojo { |
36 namespace edk { | 39 namespace edk { |
37 namespace { | 40 namespace { |
38 | 41 |
39 class MultiprocessMessagePipeTest : public test::MojoTestBase { | 42 class MultiprocessMessagePipeTest : public test::MojoTestBase { |
40 protected: | 43 protected: |
41 // Convenience class for tests which will control command-driven children. | 44 // Convenience class for tests which will control command-driven children. |
(...skipping 1245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1287 | 1290 |
1288 // Create a new pipe using our end of the channel. | 1291 // Create a new pipe using our end of the channel. |
1289 ScopedMessagePipeHandle pipe = | 1292 ScopedMessagePipeHandle pipe = |
1290 edk::CreateMessagePipe(platform_channel.PassServerHandle()); | 1293 edk::CreateMessagePipe(platform_channel.PassServerHandle()); |
1291 | 1294 |
1292 // Ensure that we can read and write on the new pipe. | 1295 // Ensure that we can read and write on the new pipe. |
1293 VerifyEcho(pipe.get().value(), "goodbye"); | 1296 VerifyEcho(pipe.get().value(), "goodbye"); |
1294 END_CHILD() | 1297 END_CHILD() |
1295 } | 1298 } |
1296 | 1299 |
| 1300 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(MessagePipeStatusChangeInTransitClient, |
| 1301 MultiprocessMessagePipeTest, parent) { |
| 1302 // This test verifies that peer closure is detectable through various |
| 1303 // mechanisms when it races with handle transfer. |
| 1304 MojoHandle handles[4]; |
| 1305 EXPECT_EQ("o_O", ReadMessageWithHandles(parent, handles, 4)); |
| 1306 |
| 1307 // Wait on handle 0 using MojoWait. |
| 1308 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(handles[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 1309 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 1310 |
| 1311 base::MessageLoop message_loop; |
| 1312 |
| 1313 // Wait on handle 1 using a Watcher. |
| 1314 { |
| 1315 base::RunLoop run_loop; |
| 1316 Watcher watcher; |
| 1317 watcher.Start(Handle(handles[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 1318 base::Bind([] (base::RunLoop* loop, MojoResult result) { |
| 1319 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 1320 loop->Quit(); |
| 1321 }, &run_loop)); |
| 1322 run_loop.Run(); |
| 1323 } |
| 1324 |
| 1325 // Wait on handle 2 by polling with MojoReadMessage. |
| 1326 MojoResult result; |
| 1327 do { |
| 1328 result = MojoReadMessage(handles[2], nullptr, nullptr, nullptr, nullptr, |
| 1329 MOJO_READ_MESSAGE_FLAG_NONE); |
| 1330 } while (result == MOJO_RESULT_SHOULD_WAIT); |
| 1331 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 1332 |
| 1333 // Wait on handle 3 by polling with MojoWriteMessage. |
| 1334 do { |
| 1335 result = MojoWriteMessage(handles[3], nullptr, 0, nullptr, 0, |
| 1336 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 1337 } while (result == MOJO_RESULT_OK); |
| 1338 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 1339 |
| 1340 for (size_t i = 0; i < 4; ++i) |
| 1341 CloseHandle(handles[i]); |
| 1342 } |
| 1343 |
| 1344 TEST_F(MultiprocessMessagePipeTest, MessagePipeStatusChangeInTransit) { |
| 1345 MojoHandle local_handles[4]; |
| 1346 MojoHandle sent_handles[4]; |
| 1347 for (size_t i = 0; i < 4; ++i) |
| 1348 CreateMessagePipe(&local_handles[i], &sent_handles[i]); |
| 1349 |
| 1350 RUN_CHILD_ON_PIPE(MessagePipeStatusChangeInTransitClient, child) |
| 1351 // Send 4 handles and let their transfer race with their peers' closure. |
| 1352 WriteMessageWithHandles(child, "o_O", sent_handles, 4); |
| 1353 for (size_t i = 0; i < 4; ++i) |
| 1354 CloseHandle(local_handles[i]); |
| 1355 END_CHILD() |
| 1356 } |
| 1357 |
1297 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(BadMessageClient, MultiprocessMessagePipeTest, | 1358 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(BadMessageClient, MultiprocessMessagePipeTest, |
1298 parent) { | 1359 parent) { |
1299 MojoHandle pipe; | 1360 MojoHandle pipe; |
1300 EXPECT_EQ("hi", ReadMessageWithHandles(parent, &pipe, 1)); | 1361 EXPECT_EQ("hi", ReadMessageWithHandles(parent, &pipe, 1)); |
1301 WriteMessage(pipe, "derp"); | 1362 WriteMessage(pipe, "derp"); |
1302 EXPECT_EQ("bye", ReadMessage(parent)); | 1363 EXPECT_EQ("bye", ReadMessage(parent)); |
1303 } | 1364 } |
1304 | 1365 |
1305 void OnProcessError(std::string* out_error, const std::string& error) { | 1366 void OnProcessError(std::string* out_error, const std::string& error) { |
1306 *out_error = error; | 1367 *out_error = error; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1358 EXPECT_NE(std::string::npos, first_process_error.find(kFirstErrorMessage)); | 1419 EXPECT_NE(std::string::npos, first_process_error.find(kFirstErrorMessage)); |
1359 EXPECT_NE(std::string::npos, second_process_error.find(kSecondErrorMessage)); | 1420 EXPECT_NE(std::string::npos, second_process_error.find(kSecondErrorMessage)); |
1360 } | 1421 } |
1361 INSTANTIATE_TEST_CASE_P(, | 1422 INSTANTIATE_TEST_CASE_P(, |
1362 MultiprocessMessagePipeTestWithPeerSupport, | 1423 MultiprocessMessagePipeTestWithPeerSupport, |
1363 testing::Values(test::MojoTestBase::LaunchType::CHILD, | 1424 testing::Values(test::MojoTestBase::LaunchType::CHILD, |
1364 test::MojoTestBase::LaunchType::PEER)); | 1425 test::MojoTestBase::LaunchType::PEER)); |
1365 } // namespace | 1426 } // namespace |
1366 } // namespace edk | 1427 } // namespace edk |
1367 } // namespace mojo | 1428 } // namespace mojo |
OLD | NEW |