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 MojoHandle b = handles[0]; | |
yzshen1
2016/08/19 22:20:05
optional, nit: it seems a little easier to read if
Ken Rockot(use gerrit already)
2016/08/19 22:28:00
Done
| |
1307 MojoHandle d = handles[1]; | |
1308 MojoHandle f = handles[2]; | |
1309 MojoHandle h = handles[3]; | |
1310 | |
1311 // Wait on handle |b| using MojoWait. | |
1312 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(b, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
1313 MOJO_DEADLINE_INDEFINITE, nullptr)); | |
1314 | |
1315 base::MessageLoop message_loop; | |
1316 | |
1317 // Wait on handle |d| using a Watcher. | |
1318 { | |
1319 base::RunLoop run_loop; | |
1320 Watcher watcher; | |
1321 watcher.Start(Handle(d), MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
1322 base::Bind([] (base::RunLoop* loop, MojoResult result) { | |
1323 EXPECT_EQ(MOJO_RESULT_OK, result); | |
1324 loop->Quit(); | |
1325 }, &run_loop)); | |
1326 run_loop.Run(); | |
1327 } | |
1328 | |
1329 // Wait on handle |f| by polling with MojoReadMessage. | |
1330 MojoResult result; | |
1331 do { | |
1332 result = MojoReadMessage(f, nullptr, nullptr, nullptr, nullptr, | |
1333 MOJO_READ_MESSAGE_FLAG_NONE); | |
1334 } while (result == MOJO_RESULT_SHOULD_WAIT); | |
1335 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | |
1336 | |
1337 // Wait on handle |h| by polling with MojoWriteMessage. | |
1338 do { | |
1339 result = MojoWriteMessage(h, nullptr, 0, nullptr, 0, | |
1340 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
1341 } while (result == MOJO_RESULT_OK); | |
1342 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | |
1343 | |
1344 CloseHandle(b); | |
1345 CloseHandle(d); | |
1346 CloseHandle(f); | |
1347 CloseHandle(h); | |
1348 } | |
1349 | |
1350 TEST_F(MultiprocessMessagePipeTest, MessagePipeStatusChangeInTransit) { | |
1351 MojoHandle a, b, c, d, e, f, g, h; | |
1352 CreateMessagePipe(&a, &b); | |
1353 CreateMessagePipe(&c, &d); | |
1354 CreateMessagePipe(&e, &f); | |
1355 CreateMessagePipe(&g, &h); | |
1356 | |
1357 RUN_CHILD_ON_PIPE(MessagePipeStatusChangeInTransitClient, child) | |
1358 MojoHandle handles[] = { b, d, f, h }; | |
1359 | |
1360 // Send 4 handles and let their transfer race with their peers' closure. | |
1361 WriteMessageWithHandles(child, "o_O", handles, 4); | |
1362 CloseHandle(a); | |
1363 CloseHandle(c); | |
1364 CloseHandle(e); | |
1365 CloseHandle(g); | |
1366 END_CHILD() | |
1367 } | |
1368 | |
1297 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(BadMessageClient, MultiprocessMessagePipeTest, | 1369 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(BadMessageClient, MultiprocessMessagePipeTest, |
1298 parent) { | 1370 parent) { |
1299 MojoHandle pipe; | 1371 MojoHandle pipe; |
1300 EXPECT_EQ("hi", ReadMessageWithHandles(parent, &pipe, 1)); | 1372 EXPECT_EQ("hi", ReadMessageWithHandles(parent, &pipe, 1)); |
1301 WriteMessage(pipe, "derp"); | 1373 WriteMessage(pipe, "derp"); |
1302 EXPECT_EQ("bye", ReadMessage(parent)); | 1374 EXPECT_EQ("bye", ReadMessage(parent)); |
1303 } | 1375 } |
1304 | 1376 |
1305 void OnProcessError(std::string* out_error, const std::string& error) { | 1377 void OnProcessError(std::string* out_error, const std::string& error) { |
1306 *out_error = error; | 1378 *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)); | 1430 EXPECT_NE(std::string::npos, first_process_error.find(kFirstErrorMessage)); |
1359 EXPECT_NE(std::string::npos, second_process_error.find(kSecondErrorMessage)); | 1431 EXPECT_NE(std::string::npos, second_process_error.find(kSecondErrorMessage)); |
1360 } | 1432 } |
1361 INSTANTIATE_TEST_CASE_P(, | 1433 INSTANTIATE_TEST_CASE_P(, |
1362 MultiprocessMessagePipeTestWithPeerSupport, | 1434 MultiprocessMessagePipeTestWithPeerSupport, |
1363 testing::Values(test::MojoTestBase::LaunchType::CHILD, | 1435 testing::Values(test::MojoTestBase::LaunchType::CHILD, |
1364 test::MojoTestBase::LaunchType::PEER)); | 1436 test::MojoTestBase::LaunchType::PEER)); |
1365 } // namespace | 1437 } // namespace |
1366 } // namespace edk | 1438 } // namespace edk |
1367 } // namespace mojo | 1439 } // namespace mojo |
OLD | NEW |