| 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> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "mojo/edk/embedder/platform_channel_pair.h" | 25 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 26 #include "mojo/edk/embedder/scoped_platform_handle.h" | 26 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 27 #include "mojo/edk/system/handle_signals_state.h" | 27 #include "mojo/edk/system/handle_signals_state.h" |
| 28 #include "mojo/edk/system/test_utils.h" | 28 #include "mojo/edk/system/test_utils.h" |
| 29 #include "mojo/edk/test/mojo_test_base.h" | 29 #include "mojo/edk/test/mojo_test_base.h" |
| 30 #include "mojo/edk/test/test_utils.h" | 30 #include "mojo/edk/test/test_utils.h" |
| 31 #include "mojo/public/c/system/buffer.h" | 31 #include "mojo/public/c/system/buffer.h" |
| 32 #include "mojo/public/c/system/functions.h" | 32 #include "mojo/public/c/system/functions.h" |
| 33 #include "mojo/public/c/system/types.h" | 33 #include "mojo/public/c/system/types.h" |
| 34 #include "mojo/public/cpp/system/platform_handle.h" |
| 34 #include "mojo/public/cpp/system/watcher.h" | 35 #include "mojo/public/cpp/system/watcher.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 37 |
| 38 #if defined(OS_ANDROID) |
| 39 #include "base/test/android/jni_test_util.h" |
| 40 #endif |
| 37 | 41 |
| 38 namespace mojo { | 42 namespace mojo { |
| 39 namespace edk { | 43 namespace edk { |
| 40 namespace { | 44 namespace { |
| 41 | 45 |
| 42 class MultiprocessMessagePipeTest : public test::MojoTestBase { | 46 class MultiprocessMessagePipeTest : public test::MojoTestBase { |
| 43 protected: | 47 protected: |
| 44 // Convenience class for tests which will control command-driven children. | 48 // Convenience class for tests which will control command-driven children. |
| 45 // See the CommandDrivenClient definition below. | 49 // See the CommandDrivenClient definition below. |
| 46 class CommandDrivenClientController { | 50 class CommandDrivenClientController { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 CHECK_EQ(hss.satisfied_signals, MOJO_HANDLE_SIGNAL_PEER_CLOSED); | 104 CHECK_EQ(hss.satisfied_signals, MOJO_HANDLE_SIGNAL_PEER_CLOSED); |
| 101 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_PEER_CLOSED); | 105 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_PEER_CLOSED); |
| 102 break; | 106 break; |
| 103 } else { | 107 } else { |
| 104 CHECK((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 108 CHECK((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
| 105 CHECK((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 109 CHECK((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
| 106 } | 110 } |
| 107 | 111 |
| 108 std::string read_buffer(1000, '\0'); | 112 std::string read_buffer(1000, '\0'); |
| 109 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | 113 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
| 110 CHECK_EQ(MojoReadMessage(h, &read_buffer[0], | 114 MojoHandle handles[10]; |
| 111 &read_buffer_size, nullptr, | 115 uint32_t num_handles = 10; |
| 112 0, MOJO_READ_MESSAGE_FLAG_NONE), | 116 CHECK_EQ(MojoReadMessage(h, &read_buffer[0], &read_buffer_size, handles, |
| 117 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), |
| 113 MOJO_RESULT_OK); | 118 MOJO_RESULT_OK); |
| 114 read_buffer.resize(read_buffer_size); | 119 read_buffer.resize(read_buffer_size); |
| 120 |
| 115 VLOG(2) << "Child got: " << read_buffer; | 121 VLOG(2) << "Child got: " << read_buffer; |
| 116 | 122 |
| 117 if (read_buffer == quitquitquit) { | 123 if (read_buffer == quitquitquit) { |
| 118 VLOG(2) << "Child quitting."; | 124 VLOG(2) << "Child quitting."; |
| 119 break; | 125 break; |
| 120 } | 126 } |
| 121 | 127 |
| 122 std::string write_buffer = read_buffer + read_buffer; | 128 std::string write_buffer = read_buffer + read_buffer; |
| 123 CHECK_EQ(MojoWriteMessage(h, write_buffer.data(), | 129 CHECK_EQ( |
| 124 static_cast<uint32_t>(write_buffer.size()), | 130 MojoWriteMessage(h, write_buffer.data(), |
| 125 nullptr, 0u, MOJO_WRITE_MESSAGE_FLAG_NONE), | 131 static_cast<uint32_t>(write_buffer.size()), handles, |
| 126 MOJO_RESULT_OK); | 132 num_handles, MOJO_WRITE_MESSAGE_FLAG_NONE), |
| 133 MOJO_RESULT_OK); |
| 127 } | 134 } |
| 128 | 135 |
| 129 return rv; | 136 return rv; |
| 130 } | 137 } |
| 131 | 138 |
| 132 TEST_P(MultiprocessMessagePipeTestWithPeerSupport, Basic) { | 139 TEST_P(MultiprocessMessagePipeTestWithPeerSupport, Basic) { |
| 133 RUN_CHILD_ON_PIPE(EchoEcho, h) | 140 RUN_CHILD_ON_PIPE(EchoEcho, h) |
| 134 std::string hello("hello"); | 141 std::string hello("hello"); |
| 135 ASSERT_EQ(MOJO_RESULT_OK, | 142 ASSERT_EQ(MOJO_RESULT_OK, |
| 136 MojoWriteMessage(h, hello.data(), | 143 MojoWriteMessage(h, hello.data(), |
| (...skipping 1184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1321 MojoHandle pipe; | 1328 MojoHandle pipe; |
| 1322 EXPECT_EQ("hi", ReadMessageWithHandles(parent, &pipe, 1)); | 1329 EXPECT_EQ("hi", ReadMessageWithHandles(parent, &pipe, 1)); |
| 1323 WriteMessage(pipe, "derp"); | 1330 WriteMessage(pipe, "derp"); |
| 1324 EXPECT_EQ("bye", ReadMessage(parent)); | 1331 EXPECT_EQ("bye", ReadMessage(parent)); |
| 1325 } | 1332 } |
| 1326 | 1333 |
| 1327 void OnProcessError(std::string* out_error, const std::string& error) { | 1334 void OnProcessError(std::string* out_error, const std::string& error) { |
| 1328 *out_error = error; | 1335 *out_error = error; |
| 1329 } | 1336 } |
| 1330 | 1337 |
| 1338 #if defined(OS_ANDROID) |
| 1339 TEST_F(MultiprocessMessagePipeTest, BasicParcelable) { |
| 1340 RUN_CHILD_ON_PIPE(EchoEcho, h) |
| 1341 MojoHandle handles[2]; |
| 1342 base::android::ScopedJavaLocalRef<jobject> parcelable1 = |
| 1343 base::android::test::CreateJavaPoint(11, 22); |
| 1344 base::android::ScopedJavaLocalRef<jobject> parcelable2 = |
| 1345 base::android::test::CreateJavaPoint(88, 99); |
| 1346 ScopedHandle parcelable1_handle = WrapParcelable(parcelable1); |
| 1347 handles[0] = parcelable1_handle.release().value(); |
| 1348 ScopedHandle parcelable2_handle = WrapParcelable(parcelable2); |
| 1349 handles[1] = parcelable2_handle.release().value(); |
| 1350 |
| 1351 std::string hello("hello"); |
| 1352 WriteMessageWithHandles(h, hello, handles, 2u); |
| 1353 |
| 1354 HandleSignalsState hss; |
| 1355 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(h, MOJO_HANDLE_SIGNAL_READABLE, |
| 1356 MOJO_DEADLINE_INDEFINITE, &hss)); |
| 1357 // The child may or may not have closed its end of the message pipe and died |
| 1358 // (and we may or may not know it yet), so our end may or may not appear as |
| 1359 // writable. |
| 1360 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
| 1361 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
| 1362 |
| 1363 std::string read_buffer(1000, '\0'); |
| 1364 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
| 1365 MojoHandle received_handles[10]; |
| 1366 uint32_t num_received_handles = 10; |
| 1367 CHECK_EQ( |
| 1368 MojoReadMessage(h, &read_buffer[0], &read_buffer_size, received_handles, |
| 1369 &num_received_handles, MOJO_READ_MESSAGE_FLAG_NONE), |
| 1370 MOJO_RESULT_OK); |
| 1371 read_buffer.resize(read_buffer_size); |
| 1372 VLOG(2) << "Parent got: " << read_buffer; |
| 1373 ASSERT_EQ(hello + hello, read_buffer); |
| 1374 |
| 1375 ASSERT_EQ(2U, num_received_handles); |
| 1376 base::android::ScopedJavaLocalRef<jobject> received_parcelable1; |
| 1377 ASSERT_EQ(MOJO_RESULT_OK, |
| 1378 UnwrapParcelable(MakeScopedHandle(Handle(received_handles[0])), |
| 1379 &received_parcelable1)); |
| 1380 EXPECT_TRUE(base::android::test::AreJavaObjectsEqual( |
| 1381 parcelable1.obj(), received_parcelable1.obj())); |
| 1382 |
| 1383 base::android::ScopedJavaLocalRef<jobject> received_parcelable2; |
| 1384 ASSERT_EQ(MOJO_RESULT_OK, |
| 1385 UnwrapParcelable(MakeScopedHandle(Handle(received_handles[1])), |
| 1386 &received_parcelable2)); |
| 1387 EXPECT_TRUE(base::android::test::AreJavaObjectsEqual( |
| 1388 parcelable2.obj(), received_parcelable2.obj())); |
| 1389 |
| 1390 std::string quitquitquit("quitquitquit"); |
| 1391 CHECK_EQ(MojoWriteMessage(h, quitquitquit.data(), |
| 1392 static_cast<uint32_t>(quitquitquit.size()), nullptr, |
| 1393 0u, MOJO_WRITE_MESSAGE_FLAG_NONE), |
| 1394 MOJO_RESULT_OK); |
| 1395 END_CHILD_AND_EXPECT_EXIT_CODE(1 % 100); |
| 1396 } |
| 1397 |
| 1398 #endif // defined(OS_ANDROID) |
| 1399 |
| 1331 TEST_F(MultiprocessMessagePipeTest, NotifyBadMessage) { | 1400 TEST_F(MultiprocessMessagePipeTest, NotifyBadMessage) { |
| 1332 const std::string kFirstErrorMessage = "everything is terrible!"; | 1401 const std::string kFirstErrorMessage = "everything is terrible!"; |
| 1333 const std::string kSecondErrorMessage = "not the bits you're looking for"; | 1402 const std::string kSecondErrorMessage = "not the bits you're looking for"; |
| 1334 | 1403 |
| 1335 std::string first_process_error; | 1404 std::string first_process_error; |
| 1336 std::string second_process_error; | 1405 std::string second_process_error; |
| 1337 | 1406 |
| 1338 set_process_error_callback(base::Bind(&OnProcessError, &first_process_error)); | 1407 set_process_error_callback(base::Bind(&OnProcessError, &first_process_error)); |
| 1339 RUN_CHILD_ON_PIPE(BadMessageClient, child1) | 1408 RUN_CHILD_ON_PIPE(BadMessageClient, child1) |
| 1340 set_process_error_callback(base::Bind(&OnProcessError, | 1409 set_process_error_callback(base::Bind(&OnProcessError, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1383 INSTANTIATE_TEST_CASE_P( | 1452 INSTANTIATE_TEST_CASE_P( |
| 1384 , | 1453 , |
| 1385 MultiprocessMessagePipeTestWithPeerSupport, | 1454 MultiprocessMessagePipeTestWithPeerSupport, |
| 1386 testing::Values(test::MojoTestBase::LaunchType::CHILD, | 1455 testing::Values(test::MojoTestBase::LaunchType::CHILD, |
| 1387 test::MojoTestBase::LaunchType::PEER, | 1456 test::MojoTestBase::LaunchType::PEER, |
| 1388 test::MojoTestBase::LaunchType::NAMED_CHILD, | 1457 test::MojoTestBase::LaunchType::NAMED_CHILD, |
| 1389 test::MojoTestBase::LaunchType::NAMED_PEER)); | 1458 test::MojoTestBase::LaunchType::NAMED_PEER)); |
| 1390 } // namespace | 1459 } // namespace |
| 1391 } // namespace edk | 1460 } // namespace edk |
| 1392 } // namespace mojo | 1461 } // namespace mojo |
| OLD | NEW |