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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string.h>
6
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10
11 #include "base/files/file.h"
12 #include "base/files/file_util.h"
13 #include "base/memory/shared_memory.h"
14 #include "mojo/edk/embedder/platform_shared_buffer.h"
15 #include "mojo/edk/test/mojo_test_base.h"
16 #include "mojo/public/c/system/functions.h"
17 #include "mojo/public/c/system/types.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace mojo {
21 namespace edk {
22 namespace {
23
24 using PlatformWrapperTest = test::MojoTestBase;
25
26 TEST_F(PlatformWrapperTest, WrapPlatformHandle) {
27 // Create a temporary file and write a message to it.
28 base::FilePath temp_file_path;
29 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path));
30 const std::string kMessage = "Hello, world!";
31 EXPECT_EQ(base::WriteFile(temp_file_path, kMessage.data(),
32 static_cast<int>(kMessage.size())),
33 static_cast<int>(kMessage.size()));
34
35 RUN_CHILD_ON_PIPE(ReadPlatformFile, h)
36 // Open the temporary file for reading, wrap its handle, and send it to
37 // the child along with the expected message to be read.
38 base::File file(temp_file_path,
39 base::File::FLAG_OPEN | base::File::FLAG_READ);
40 EXPECT_TRUE(file.IsValid());
41
42 MojoHandle wrapped_handle;
43 MojoPlatformHandle os_file =
44 static_cast<MojoPlatformHandle>(file.TakePlatformFile());
45 EXPECT_EQ(MOJO_RESULT_OK, MojoWrapPlatformHandle(os_file, &wrapped_handle));
46
47 WriteMessageWithHandles(h, kMessage, &wrapped_handle, 1);
48 END_CHILD()
49
50 base::DeleteFile(temp_file_path, false);
51 }
52
53 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadPlatformFile, PlatformWrapperTest, h) {
54 // Read a message and a wrapped file handle; unwrap the handle.
55 MojoHandle wrapped_handle;
56 std::string message = ReadMessageWithHandles(h, &wrapped_handle, 1);
57
58 MojoPlatformHandle platform_handle;
59 ASSERT_EQ(MOJO_RESULT_OK,
60 MojoUnwrapPlatformHandle(wrapped_handle, &platform_handle));
61 base::File file(static_cast<base::PlatformFile>(platform_handle));
62
63 // Expect to read the same message from the file.
64 std::vector<char> data(message.size());
65 EXPECT_EQ(file.ReadAtCurrentPos(data.data(), static_cast<int>(data.size())),
66 static_cast<int>(data.size()));
67 EXPECT_TRUE(std::equal(message.begin(), message.end(), data.begin()));
68 }
69
70 TEST_F(PlatformWrapperTest, WrapPlatformSharedBufferHandle) {
71 // Allocate a new platform shared buffer and write a message into it.
72 const std::string kMessage = "Hello, world!";
73 base::SharedMemory buffer;
74 buffer.CreateAndMapAnonymous(kMessage.size());
75 CHECK(buffer.memory());
76 memcpy(buffer.memory(), kMessage.data(), kMessage.size());
77
78 RUN_CHILD_ON_PIPE(ReadPlatformSharedBuffer, h)
79 // Wrap the shared memory handle and send it to the child along with the
80 // expected message.
81 base::SharedMemoryHandle memory_handle =
82 base::SharedMemory::DuplicateHandle(buffer.handle());
83 MojoPlatformHandle os_buffer = static_cast<MojoPlatformHandle>(
84 PlatformSharedBuffer::GetPlatformHandleFromSharedMemoryHandle(
85 memory_handle).handle);
86 MojoHandle wrapped_handle;
87 ASSERT_EQ(MOJO_RESULT_OK,
88 MojoWrapPlatformSharedBufferHandle(
89 os_buffer, kMessage.size(),
90 MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE,
91 &wrapped_handle));
92 WriteMessageWithHandles(h, kMessage, &wrapped_handle, 1);
93 END_CHILD()
94 }
95
96 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadPlatformSharedBuffer, PlatformWrapperTest,
97 h) {
98 // Read a message and a wrapped shared buffer handle.
99 MojoHandle wrapped_handle;
100 std::string message = ReadMessageWithHandles(h, &wrapped_handle, 1);
101
102 // Check the message in the buffer
103 ExpectBufferContents(wrapped_handle, 0, message);
104
105 // Now unwrap the buffer and verify that the base::SharedMemoryHandle also
106 // works as expected.
107 MojoPlatformHandle os_buffer;
108 size_t size;
109 MojoPlatformSharedBufferHandleFlags flags;
110 ASSERT_EQ(MOJO_RESULT_OK,
111 MojoUnwrapPlatformSharedBufferHandle(wrapped_handle, &os_buffer,
112 &size, &flags));
113 bool read_only = flags & MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE;
114 EXPECT_FALSE(read_only);
115
116 base::SharedMemoryHandle memory_handle =
117 PlatformSharedBuffer::GetSharedMemoryHandleFromPlatformHandle(
118 os_buffer, size, read_only);
119
120 base::SharedMemory memory(memory_handle, read_only);
121 memory.Map(message.size());
122 ASSERT_TRUE(memory.memory());
123
124 EXPECT_TRUE(std::equal(message.begin(), message.end(),
125 static_cast<const char*>(memory.memory())));
126 }
127
128 } // namespace
129 } // namespace edk
130 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698