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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <string.h> |
6 | 7 |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
8 #include "mojo/edk/system/test_utils.h" | 9 #include "mojo/edk/system/test_utils.h" |
9 #include "mojo/edk/test/mojo_test_base.h" | 10 #include "mojo/edk/test/mojo_test_base.h" |
10 #include "mojo/public/c/system/core.h" | 11 #include "mojo/public/c/system/core.h" |
11 #include "mojo/public/c/system/types.h" | 12 #include "mojo/public/c/system/types.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 namespace edk { | 15 namespace edk { |
15 namespace { | 16 namespace { |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 403 |
403 // Now port 1 should no longer be readable. | 404 // Now port 1 should no longer be readable. |
404 hss = MojoHandleSignalsState(); | 405 hss = MojoHandleSignalsState(); |
405 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 406 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
406 MojoWait(pipe1_, MOJO_HANDLE_SIGNAL_READABLE, | 407 MojoWait(pipe1_, MOJO_HANDLE_SIGNAL_READABLE, |
407 MOJO_DEADLINE_INDEFINITE, &hss)); | 408 MOJO_DEADLINE_INDEFINITE, &hss)); |
408 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 409 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
409 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 410 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
410 } | 411 } |
411 | 412 |
| 413 TEST_F(MessagePipeTest, InvalidMessageObjects) { |
| 414 // null message |
| 415 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 416 MojoDestroyMessage(nullptr)); |
| 417 |
| 418 // null message |
| 419 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 420 MojoGetMessageBuffer(nullptr, nullptr)); |
| 421 |
| 422 // Non-zero num_handles with null handles array. |
| 423 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 424 MojoCreateMessage(0, nullptr, 1, MOJO_CREATE_MESSAGE_FLAG_NONE, |
| 425 nullptr)); |
| 426 } |
| 427 |
| 428 TEST_F(MessagePipeTest, CreateAndDestroyMessage) { |
| 429 const std::string kMessage = "Hello, world."; |
| 430 void* message = nullptr; |
| 431 ASSERT_EQ(MOJO_RESULT_OK, |
| 432 MojoCreateMessage(static_cast<uint32_t>(kMessage.size()), |
| 433 nullptr, 0, MOJO_CREATE_MESSAGE_FLAG_NONE, |
| 434 &message)); |
| 435 ASSERT_TRUE(message); |
| 436 |
| 437 ASSERT_EQ(MOJO_RESULT_OK, MojoDestroyMessage(message)); |
| 438 } |
| 439 |
| 440 TEST_F(MessagePipeTest, WriteAndReadMessageObject) { |
| 441 const std::string kMessage = "Hello, world."; |
| 442 void* message = nullptr; |
| 443 EXPECT_EQ(MOJO_RESULT_OK, |
| 444 MojoCreateMessage(static_cast<uint32_t>(kMessage.size()), |
| 445 nullptr, 0, MOJO_CREATE_MESSAGE_FLAG_NONE, |
| 446 &message)); |
| 447 ASSERT_TRUE(message); |
| 448 |
| 449 void* buffer = nullptr; |
| 450 EXPECT_EQ(MOJO_RESULT_OK, MojoGetMessageBuffer(message, &buffer)); |
| 451 ASSERT_TRUE(buffer); |
| 452 memcpy(buffer, kMessage.data(), kMessage.size()); |
| 453 |
| 454 MojoHandle a, b; |
| 455 CreateMessagePipe(&a, &b); |
| 456 EXPECT_EQ(MOJO_RESULT_OK, |
| 457 MojoWriteMessageNew(a, message, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 458 |
| 459 EXPECT_EQ(MOJO_RESULT_OK, |
| 460 MojoWait(b, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, |
| 461 nullptr)); |
| 462 uint32_t num_bytes = 0; |
| 463 uint32_t num_handles = 0; |
| 464 EXPECT_EQ(MOJO_RESULT_OK, |
| 465 MojoReadMessageNew(b, &message, &num_bytes, nullptr, &num_handles, |
| 466 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 467 ASSERT_TRUE(message); |
| 468 EXPECT_EQ(static_cast<uint32_t>(kMessage.size()), num_bytes); |
| 469 EXPECT_EQ(0u, num_handles); |
| 470 |
| 471 EXPECT_EQ(MOJO_RESULT_OK, MojoGetMessageBuffer(message, &buffer)); |
| 472 ASSERT_TRUE(buffer); |
| 473 |
| 474 EXPECT_EQ(0, strncmp(static_cast<const char*>(buffer), kMessage.data(), |
| 475 num_bytes)); |
| 476 |
| 477 EXPECT_EQ(MOJO_RESULT_OK, MojoDestroyMessage(message)); |
| 478 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 479 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b)); |
| 480 } |
| 481 |
412 #if !defined(OS_IOS) | 482 #if !defined(OS_IOS) |
413 | 483 |
414 const size_t kPingPongHandlesPerIteration = 50; | 484 const size_t kPingPongHandlesPerIteration = 50; |
415 const size_t kPingPongIterations = 500; | 485 const size_t kPingPongIterations = 500; |
416 | 486 |
417 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(HandlePingPong, MessagePipeTest, h) { | 487 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(HandlePingPong, MessagePipeTest, h) { |
418 // Waits for a handle to become readable and writes it back to the sender. | 488 // Waits for a handle to become readable and writes it back to the sender. |
419 for (size_t i = 0; i < kPingPongIterations; i++) { | 489 for (size_t i = 0; i < kPingPongIterations; i++) { |
420 MojoHandle handles[kPingPongHandlesPerIteration]; | 490 MojoHandle handles[kPingPongHandlesPerIteration]; |
421 ReadMessageWithHandles(h, handles, kPingPongHandlesPerIteration); | 491 ReadMessageWithHandles(h, handles, kPingPongHandlesPerIteration); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 EXPECT_EQ(kTestMessage, ReadMessage(d)); | 711 EXPECT_EQ(kTestMessage, ReadMessage(d)); |
642 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 712 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
643 MOJO_DEADLINE_INDEFINITE, nullptr)); | 713 MOJO_DEADLINE_INDEFINITE, nullptr)); |
644 | 714 |
645 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); | 715 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); |
646 } | 716 } |
647 | 717 |
648 } // namespace | 718 } // namespace |
649 } // namespace edk | 719 } // namespace edk |
650 } // namespace mojo | 720 } // namespace mojo |
OLD | NEW |