Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: mojo/edk/system/message_pipe_unittest.cc

Issue 1880823005: [mojo-edk] Add explicit message object APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 MojoFreeMessage(MOJO_MESSAGE_HANDLE_INVALID));
417
418 // null message
419 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
420 MojoGetMessageBuffer(MOJO_MESSAGE_HANDLE_INVALID, nullptr));
421
422 // Non-zero num_handles with null handles array.
423 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
424 MojoAllocMessage(0, nullptr, 1, MOJO_ALLOC_MESSAGE_FLAG_NONE,
425 nullptr));
426 }
427
428 TEST_F(MessagePipeTest, AllocAndFreeMessage) {
429 const std::string kMessage = "Hello, world.";
430 MojoMessageHandle message = MOJO_MESSAGE_HANDLE_INVALID;
431 ASSERT_EQ(MOJO_RESULT_OK,
432 MojoAllocMessage(static_cast<uint32_t>(kMessage.size()), nullptr, 0,
433 MOJO_ALLOC_MESSAGE_FLAG_NONE, &message));
434 ASSERT_NE(MOJO_MESSAGE_HANDLE_INVALID, message);
435 ASSERT_EQ(MOJO_RESULT_OK, MojoFreeMessage(message));
436 }
437
438 TEST_F(MessagePipeTest, WriteAndReadMessageObject) {
439 const std::string kMessage = "Hello, world.";
440 MojoMessageHandle message = MOJO_MESSAGE_HANDLE_INVALID;
441 EXPECT_EQ(MOJO_RESULT_OK,
442 MojoAllocMessage(static_cast<uint32_t>(kMessage.size()), nullptr, 0,
443 MOJO_ALLOC_MESSAGE_FLAG_NONE, &message));
444 ASSERT_NE(MOJO_MESSAGE_HANDLE_INVALID, message);
445
446 void* buffer = nullptr;
447 EXPECT_EQ(MOJO_RESULT_OK, MojoGetMessageBuffer(message, &buffer));
448 ASSERT_TRUE(buffer);
449 memcpy(buffer, kMessage.data(), kMessage.size());
450
451 MojoHandle a, b;
452 CreateMessagePipe(&a, &b);
453 EXPECT_EQ(MOJO_RESULT_OK,
454 MojoWriteMessageNew(a, message, MOJO_WRITE_MESSAGE_FLAG_NONE));
455
456 EXPECT_EQ(MOJO_RESULT_OK,
457 MojoWait(b, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
458 nullptr));
459 uint32_t num_bytes = 0;
460 uint32_t num_handles = 0;
461 EXPECT_EQ(MOJO_RESULT_OK,
462 MojoReadMessageNew(b, &message, &num_bytes, nullptr, &num_handles,
463 MOJO_READ_MESSAGE_FLAG_NONE));
464 ASSERT_NE(MOJO_MESSAGE_HANDLE_INVALID, message);
465 EXPECT_EQ(static_cast<uint32_t>(kMessage.size()), num_bytes);
466 EXPECT_EQ(0u, num_handles);
467
468 EXPECT_EQ(MOJO_RESULT_OK, MojoGetMessageBuffer(message, &buffer));
469 ASSERT_TRUE(buffer);
470
471 EXPECT_EQ(0, strncmp(static_cast<const char*>(buffer), kMessage.data(),
472 num_bytes));
473
474 EXPECT_EQ(MOJO_RESULT_OK, MojoFreeMessage(message));
475 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
476 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
477 }
478
412 #if !defined(OS_IOS) 479 #if !defined(OS_IOS)
413 480
414 const size_t kPingPongHandlesPerIteration = 50; 481 const size_t kPingPongHandlesPerIteration = 50;
415 const size_t kPingPongIterations = 500; 482 const size_t kPingPongIterations = 500;
416 483
417 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(HandlePingPong, MessagePipeTest, h) { 484 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(HandlePingPong, MessagePipeTest, h) {
418 // Waits for a handle to become readable and writes it back to the sender. 485 // Waits for a handle to become readable and writes it back to the sender.
419 for (size_t i = 0; i < kPingPongIterations; i++) { 486 for (size_t i = 0; i < kPingPongIterations; i++) {
420 MojoHandle handles[kPingPongHandlesPerIteration]; 487 MojoHandle handles[kPingPongHandlesPerIteration];
421 ReadMessageWithHandles(h, handles, kPingPongHandlesPerIteration); 488 ReadMessageWithHandles(h, handles, kPingPongHandlesPerIteration);
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 EXPECT_EQ(kTestMessage, ReadMessage(d)); 708 EXPECT_EQ(kTestMessage, ReadMessage(d));
642 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 709 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
643 MOJO_DEADLINE_INDEFINITE, nullptr)); 710 MOJO_DEADLINE_INDEFINITE, nullptr));
644 711
645 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); 712 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
646 } 713 }
647 714
648 } // namespace 715 } // namespace
649 } // namespace edk 716 } // namespace edk
650 } // namespace mojo 717 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/message_pipe_dispatcher.cc ('k') | mojo/edk/system/wait_set_dispatcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698