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" | |
17 #include "mojo/public/c/system/data_pipe.h" | 16 #include "mojo/public/c/system/data_pipe.h" |
18 #include "mojo/public/c/system/functions.h" | 17 #include "mojo/public/c/system/functions.h" |
| 18 #include "mojo/public/c/system/message_pipe.h" |
19 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace mojo { | 22 namespace mojo { |
23 namespace edk { | 23 namespace edk { |
24 namespace { | 24 namespace { |
25 | 25 |
26 const uint32_t kSizeOfOptions = | 26 const uint32_t kSizeOfOptions = |
27 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 27 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
28 | 28 |
(...skipping 1387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1416 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); | 1416 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); |
1417 ASSERT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); | 1417 ASSERT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); |
1418 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, EndReadData(1u)); | 1418 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, EndReadData(1u)); |
1419 | 1419 |
1420 // Still one element available. | 1420 // Still one element available. |
1421 num_bytes = 0u; | 1421 num_bytes = 0u; |
1422 ASSERT_EQ(MOJO_RESULT_OK, QueryData(&num_bytes)); | 1422 ASSERT_EQ(MOJO_RESULT_OK, QueryData(&num_bytes)); |
1423 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); | 1423 ASSERT_EQ(1u * sizeof(int32_t), num_bytes); |
1424 } | 1424 } |
1425 | 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 |
1426 // Ensures that if a data pipe consumer whose producer has closed is passed over | 1506 // Ensures that if a data pipe consumer whose producer has closed is passed over |
1427 // a message pipe, the deserialized dispatcher is also marked as having a closed | 1507 // a message pipe, the deserialized dispatcher is also marked as having a closed |
1428 // peer. | 1508 // peer. |
1429 TEST_F(DataPipeTest, ConsumerWithClosedProducerSent) { | 1509 TEST_F(DataPipeTest, ConsumerWithClosedProducerSent) { |
1430 const MojoCreateDataPipeOptions options = { | 1510 const MojoCreateDataPipeOptions options = { |
1431 kSizeOfOptions, // |struct_size|. | 1511 kSizeOfOptions, // |struct_size|. |
1432 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1512 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1433 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1513 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1434 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 1514 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
1435 }; | 1515 }; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1482 ASSERT_EQ(sizeof(read_data), num_bytes); | 1562 ASSERT_EQ(sizeof(read_data), num_bytes); |
1483 ASSERT_EQ(data, read_data); | 1563 ASSERT_EQ(data, read_data); |
1484 | 1564 |
1485 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(pipe0)); | 1565 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(pipe0)); |
1486 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(pipe1)); | 1566 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(pipe1)); |
1487 } | 1567 } |
1488 | 1568 |
1489 } // namespace | 1569 } // namespace |
1490 } // namespace edk | 1570 } // namespace edk |
1491 } // namespace mojo | 1571 } // namespace mojo |
OLD | NEW |