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

Unified Diff: mojo/edk/system/platform_wrapper_unittest.cc

Issue 1995753002: [mojo-edk] Expose portable API for platform handle wrapping (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: mojo/edk/system/platform_wrapper_unittest.cc
diff --git a/mojo/edk/system/platform_wrapper_unittest.cc b/mojo/edk/system/platform_wrapper_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..513b51ca7d48cf732627ea26a77a203a0633811b
--- /dev/null
+++ b/mojo/edk/system/platform_wrapper_unittest.cc
@@ -0,0 +1,130 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string.h>
+
+#include <algorithm>
+#include <string>
+#include <vector>
+
+#include "base/files/file.h"
+#include "base/files/file_util.h"
+#include "base/memory/shared_memory.h"
+#include "mojo/edk/embedder/platform_shared_buffer.h"
+#include "mojo/edk/test/mojo_test_base.h"
+#include "mojo/public/c/system/functions.h"
+#include "mojo/public/c/system/types.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+namespace edk {
+namespace {
+
+using PlatformWrapperTest = test::MojoTestBase;
+
+TEST_F(PlatformWrapperTest, WrapPlatformHandle) {
+ // Create a temporary file and write a message to it.
+ base::FilePath temp_file_path;
+ ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path));
+ const std::string kMessage = "Hello, world!";
+ EXPECT_EQ(base::WriteFile(temp_file_path, kMessage.data(),
+ static_cast<int>(kMessage.size())),
+ static_cast<int>(kMessage.size()));
+
+ RUN_CHILD_ON_PIPE(ReadPlatformFile, h)
+ // Open the temporary file for reading, wrap its handle, and send it to
+ // the child along with the expected message to be read.
+ base::File file(temp_file_path,
+ base::File::FLAG_OPEN | base::File::FLAG_READ);
+ EXPECT_TRUE(file.IsValid());
+
+ MojoHandle wrapped_handle;
+ MojoPlatformHandle os_file =
+ static_cast<MojoPlatformHandle>(file.TakePlatformFile());
+ EXPECT_EQ(MOJO_RESULT_OK, MojoWrapPlatformHandle(os_file, &wrapped_handle));
+
+ WriteMessageWithHandles(h, kMessage, &wrapped_handle, 1);
+ END_CHILD()
+
+ base::DeleteFile(temp_file_path, false);
+}
+
+DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadPlatformFile, PlatformWrapperTest, h) {
+ // Read a message and a wrapped file handle; unwrap the handle.
+ MojoHandle wrapped_handle;
+ std::string message = ReadMessageWithHandles(h, &wrapped_handle, 1);
+
+ MojoPlatformHandle platform_handle;
+ ASSERT_EQ(MOJO_RESULT_OK,
+ MojoUnwrapPlatformHandle(wrapped_handle, &platform_handle));
+ base::File file(static_cast<base::PlatformFile>(platform_handle));
+
+ // Expect to read the same message from the file.
+ std::vector<char> data(message.size());
+ EXPECT_EQ(file.ReadAtCurrentPos(data.data(), static_cast<int>(data.size())),
+ static_cast<int>(data.size()));
+ EXPECT_TRUE(std::equal(message.begin(), message.end(), data.begin()));
+}
+
+TEST_F(PlatformWrapperTest, WrapPlatformSharedBufferHandle) {
+ // Allocate a new platform shared buffer and write a message into it.
+ const std::string kMessage = "Hello, world!";
+ base::SharedMemory buffer;
+ buffer.CreateAndMapAnonymous(kMessage.size());
+ CHECK(buffer.memory());
+ memcpy(buffer.memory(), kMessage.data(), kMessage.size());
+
+ RUN_CHILD_ON_PIPE(ReadPlatformSharedBuffer, h)
+ // Wrap the shared memory handle and send it to the child along with the
+ // expected message.
+ base::SharedMemoryHandle memory_handle =
+ base::SharedMemory::DuplicateHandle(buffer.handle());
+ MojoPlatformHandle os_buffer = static_cast<MojoPlatformHandle>(
+ PlatformSharedBuffer::GetPlatformHandleFromSharedMemoryHandle(
+ memory_handle).handle);
+ MojoHandle wrapped_handle;
+ ASSERT_EQ(MOJO_RESULT_OK,
+ MojoWrapPlatformSharedBufferHandle(
+ os_buffer, kMessage.size(),
+ MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE,
+ &wrapped_handle));
+ WriteMessageWithHandles(h, kMessage, &wrapped_handle, 1);
+ END_CHILD()
+}
+
+DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadPlatformSharedBuffer, PlatformWrapperTest,
+ h) {
+ // Read a message and a wrapped shared buffer handle.
+ MojoHandle wrapped_handle;
+ std::string message = ReadMessageWithHandles(h, &wrapped_handle, 1);
+
+ // Check the message in the buffer
+ ExpectBufferContents(wrapped_handle, 0, message);
+
+ // Now unwrap the buffer and verify that the base::SharedMemoryHandle also
+ // works as expected.
+ MojoPlatformHandle os_buffer;
+ size_t size;
+ MojoPlatformSharedBufferHandleFlags flags;
+ ASSERT_EQ(MOJO_RESULT_OK,
+ MojoUnwrapPlatformSharedBufferHandle(wrapped_handle, &os_buffer,
+ &size, &flags));
+ bool read_only = flags & MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE;
+ EXPECT_FALSE(read_only);
+
+ base::SharedMemoryHandle memory_handle =
+ PlatformSharedBuffer::GetSharedMemoryHandleFromPlatformHandle(
+ os_buffer, size, read_only);
+
+ base::SharedMemory memory(memory_handle, read_only);
+ memory.Map(message.size());
+ ASSERT_TRUE(memory.memory());
+
+ EXPECT_TRUE(std::equal(message.begin(), message.end(),
+ static_cast<const char*>(memory.memory())));
+}
+
+} // namespace
+} // namespace edk
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698