Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "mojo/edk/embedder/platform_channel_pair.h" | 12 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 13 #include "mojo/edk/embedder/simple_platform_support.h" | 13 #include "mojo/edk/embedder/simple_platform_support.h" |
| 14 #include "mojo/edk/system/test_utils.h" | 14 #include "mojo/edk/system/test_utils.h" |
| 15 #include "mojo/edk/system/waiter.h" | 15 #include "mojo/edk/system/waiter.h" |
| 16 #include "mojo/public/c/system/core.h" | |
|
yzshen1
2015/10/13 16:17:41
nit: I think if we include core.h we probably don'
jam
2015/10/13 16:34:05
switched to just including message_pipe.h
| |
| 16 #include "mojo/public/c/system/data_pipe.h" | 17 #include "mojo/public/c/system/data_pipe.h" |
| 17 #include "mojo/public/c/system/functions.h" | 18 #include "mojo/public/c/system/functions.h" |
| 18 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 namespace edk { | 23 namespace edk { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const uint32_t kSizeOfOptions = | 26 const uint32_t kSizeOfOptions = |
| (...skipping 1389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1415 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); | 1416 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); |
| 1416 ASSERT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); | 1417 ASSERT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); |
| 1417 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, EndReadData(1u)); | 1418 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, EndReadData(1u)); |
| 1418 | 1419 |
| 1419 // Still one element available. | 1420 // Still one element available. |
| 1420 num_bytes = 0u; | 1421 num_bytes = 0u; |
| 1421 ASSERT_EQ(MOJO_RESULT_OK, QueryData(&num_bytes)); | 1422 ASSERT_EQ(MOJO_RESULT_OK, QueryData(&num_bytes)); |
| 1422 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); | 1423 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); |
| 1423 } | 1424 } |
| 1424 | 1425 |
| 1426 // Test that a producer can be sent over a MP. | |
| 1427 TEST_F(DataPipeTest, SendProducer) { | |
| 1428 const char kTestData[] = "hello world"; | |
| 1429 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData)); | |
| 1430 | |
| 1431 const MojoCreateDataPipeOptions options = { | |
| 1432 kSizeOfOptions, // |struct_size|. | |
| 1433 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | |
| 1434 1u, // |element_num_bytes|. | |
| 1435 1000u // |capacity_num_bytes|. | |
| 1436 }; | |
| 1437 ASSERT_EQ(MOJO_RESULT_OK, Create(&options)); | |
| 1438 MojoHandleSignalsState hss; | |
| 1439 | |
| 1440 // Write some data. | |
| 1441 uint32_t num_bytes = kTestDataSize; | |
| 1442 ASSERT_EQ(MOJO_RESULT_OK, WriteData(kTestData, &num_bytes)); | |
| 1443 ASSERT_EQ(kTestDataSize, num_bytes); | |
| 1444 | |
| 1445 // Wait for the data. | |
| 1446 hss = MojoHandleSignalsState(); | |
| 1447 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1448 MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, | |
| 1449 MOJO_DEADLINE_INDEFINITE, &hss)); | |
| 1450 ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | |
| 1451 ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 1452 hss.satisfiable_signals); | |
| 1453 | |
| 1454 // Check the data. | |
| 1455 const void* read_buffer = nullptr; | |
| 1456 num_bytes = 0u; | |
| 1457 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1458 BeginReadData(&read_buffer, &num_bytes, false)); | |
| 1459 ASSERT_EQ(0, memcmp(read_buffer, kTestData, kTestDataSize)); | |
| 1460 EndReadData(num_bytes); | |
| 1461 | |
| 1462 // Now send the producer over a MP so that it's serialized. | |
| 1463 MojoHandle pipe0, pipe1; | |
| 1464 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1465 MojoCreateMessagePipe(nullptr, &pipe0, &pipe1)); | |
| 1466 | |
| 1467 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1468 MojoWriteMessage(pipe0, nullptr, 0, &producer_, 1, | |
| 1469 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 1470 producer_ = MOJO_HANDLE_INVALID; | |
| 1471 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(pipe1, MOJO_HANDLE_SIGNAL_READABLE, | |
| 1472 MOJO_DEADLINE_INDEFINITE, &hss)); | |
| 1473 uint32_t num_handles = 1; | |
| 1474 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1475 MojoReadMessage(pipe1, nullptr, 0, &producer_, &num_handles, | |
| 1476 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 1477 ASSERT_EQ(num_handles, 1u); | |
| 1478 | |
| 1479 // Write more data. | |
| 1480 const char kExtraData[] = "bye world"; | |
| 1481 const uint32_t kExtraDataSize = static_cast<uint32_t>(sizeof(kExtraData)); | |
| 1482 num_bytes = kExtraDataSize; | |
| 1483 ASSERT_EQ(MOJO_RESULT_OK, WriteData(kExtraData, &num_bytes)); | |
| 1484 ASSERT_EQ(kExtraDataSize, num_bytes); | |
| 1485 | |
| 1486 // Wait for it. | |
| 1487 hss = MojoHandleSignalsState(); | |
| 1488 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1489 MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, | |
| 1490 MOJO_DEADLINE_INDEFINITE, &hss)); | |
| 1491 ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | |
| 1492 ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 1493 hss.satisfiable_signals); | |
| 1494 | |
| 1495 // Check the second write. | |
| 1496 num_bytes = 0u; | |
| 1497 ASSERT_EQ(MOJO_RESULT_OK, | |
| 1498 BeginReadData(&read_buffer, &num_bytes, false)); | |
| 1499 ASSERT_EQ(0, memcmp(read_buffer, kExtraData, kExtraDataSize)); | |
| 1500 EndReadData(num_bytes); | |
| 1501 | |
| 1502 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(pipe0)); | |
| 1503 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(pipe1)); | |
| 1504 } | |
| 1505 | |
| 1425 } // namespace | 1506 } // namespace |
| 1426 } // namespace edk | 1507 } // namespace edk |
| 1427 } // namespace mojo | 1508 } // namespace mojo |
| OLD | NEW |