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

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: nits 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
« no previous file with comments | « mojo/edk/system/core.cc ('k') | mojo/gles2/command_buffer_client_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stdint.h>
6 #include <string.h>
7
8 #include <algorithm>
9 #include <string>
10 #include <vector>
11
12 #include "base/files/file.h"
13 #include "base/files/file_util.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/process/process_handle.h"
16 #include "mojo/edk/embedder/platform_shared_buffer.h"
17 #include "mojo/edk/test/mojo_test_base.h"
18 #include "mojo/public/c/system/platform_handle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 #if defined(OS_WIN)
22 #include <windows.h>
23 #endif
24
25 #if defined(OS_POSIX)
26 #define SIMPLE_PLATFORM_HANDLE_TYPE MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR
27 #elif defined(OS_WIN)
28 #define SIMPLE_PLATFORM_HANDLE_TYPE MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE
29 #endif
30
31 #if defined(OS_MACOSX) && !defined(OS_IOS)
32 #define SHARED_BUFFER_PLATFORM_HANDLE_TYPE MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT
33 #else
34 #define SHARED_BUFFER_PLATFORM_HANDLE_TYPE SIMPLE_PLATFORM_HANDLE_TYPE
35 #endif
36
37 uint64_t PlatformHandleValueFromPlatformFile(base::PlatformFile file) {
38 #if defined(OS_WIN)
39 return reinterpret_cast<uint64_t>(file);
40 #else
41 return static_cast<uint64_t>(file);
42 #endif
43 }
44
45 base::PlatformFile PlatformFileFromPlatformHandleValue(uint64_t value) {
46 #if defined(OS_WIN)
47 return reinterpret_cast<base::PlatformFile>(value);
48 #else
49 return static_cast<base::PlatformFile>(value);
50 #endif
51 }
52
53 namespace mojo {
54 namespace edk {
55 namespace {
56
57 using PlatformWrapperTest = test::MojoTestBase;
58
59 TEST_F(PlatformWrapperTest, WrapPlatformHandle) {
60 // Create a temporary file and write a message to it.
61 base::FilePath temp_file_path;
62 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path));
63 const std::string kMessage = "Hello, world!";
64 EXPECT_EQ(base::WriteFile(temp_file_path, kMessage.data(),
65 static_cast<int>(kMessage.size())),
66 static_cast<int>(kMessage.size()));
67
68 RUN_CHILD_ON_PIPE(ReadPlatformFile, h)
69 // Open the temporary file for reading, wrap its handle, and send it to
70 // the child along with the expected message to be read.
71 base::File file(temp_file_path,
72 base::File::FLAG_OPEN | base::File::FLAG_READ);
73 EXPECT_TRUE(file.IsValid());
74
75 MojoHandle wrapped_handle;
76 MojoPlatformHandle os_file;
77 os_file.struct_size = sizeof(MojoPlatformHandle);
78 os_file.type = SIMPLE_PLATFORM_HANDLE_TYPE;
79 os_file.value =
80 PlatformHandleValueFromPlatformFile(file.TakePlatformFile());
81 EXPECT_EQ(MOJO_RESULT_OK,
82 MojoWrapPlatformHandle(&os_file, &wrapped_handle));
83
84 WriteMessageWithHandles(h, kMessage, &wrapped_handle, 1);
85 END_CHILD()
86
87 base::DeleteFile(temp_file_path, false);
88 }
89
90 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadPlatformFile, PlatformWrapperTest, h) {
91 // Read a message and a wrapped file handle; unwrap the handle.
92 MojoHandle wrapped_handle;
93 std::string message = ReadMessageWithHandles(h, &wrapped_handle, 1);
94
95 MojoPlatformHandle platform_handle;
96 platform_handle.struct_size = sizeof(MojoPlatformHandle);
97 ASSERT_EQ(MOJO_RESULT_OK,
98 MojoUnwrapPlatformHandle(wrapped_handle, &platform_handle));
99 EXPECT_EQ(SIMPLE_PLATFORM_HANDLE_TYPE, platform_handle.type);
100 base::File file(PlatformFileFromPlatformHandleValue(platform_handle.value));
101
102 // Expect to read the same message from the file.
103 std::vector<char> data(message.size());
104 EXPECT_EQ(file.ReadAtCurrentPos(data.data(), static_cast<int>(data.size())),
105 static_cast<int>(data.size()));
106 EXPECT_TRUE(std::equal(message.begin(), message.end(), data.begin()));
107 }
108
109 TEST_F(PlatformWrapperTest, WrapPlatformSharedBufferHandle) {
110 // Allocate a new platform shared buffer and write a message into it.
111 const std::string kMessage = "Hello, world!";
112 base::SharedMemory buffer;
113 buffer.CreateAndMapAnonymous(kMessage.size());
114 CHECK(buffer.memory());
115 memcpy(buffer.memory(), kMessage.data(), kMessage.size());
116
117 RUN_CHILD_ON_PIPE(ReadPlatformSharedBuffer, h)
118 // Wrap the shared memory handle and send it to the child along with the
119 // expected message.
120 base::SharedMemoryHandle memory_handle =
121 base::SharedMemory::DuplicateHandle(buffer.handle());
122 MojoPlatformHandle os_buffer;
123 os_buffer.struct_size = sizeof(MojoPlatformHandle);
124 os_buffer.type = SHARED_BUFFER_PLATFORM_HANDLE_TYPE;
125 #if defined(OS_MACOSX) && !defined(OS_IOS)
126 os_buffer.value = static_cast<uint64_t>(memory_handle.GetMemoryObject());
127 #elif defined(OS_POSIX)
128 os_buffer.value = static_cast<uint64_t>(memory_handle.fd);
129 #elif defined(OS_WIN)
130 os_buffer.value = reinterpret_cast<uint64_t>(memory_handle.GetHandle());
131 #endif
132
133 MojoHandle wrapped_handle;
134 ASSERT_EQ(MOJO_RESULT_OK,
135 MojoWrapPlatformSharedBufferHandle(
136 &os_buffer, kMessage.size(),
137 MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE,
138 &wrapped_handle));
139 WriteMessageWithHandles(h, kMessage, &wrapped_handle, 1);
140 END_CHILD()
141 }
142
143 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadPlatformSharedBuffer, PlatformWrapperTest,
144 h) {
145 // Read a message and a wrapped shared buffer handle.
146 MojoHandle wrapped_handle;
147 std::string message = ReadMessageWithHandles(h, &wrapped_handle, 1);
148
149 // Check the message in the buffer
150 ExpectBufferContents(wrapped_handle, 0, message);
151
152 // Now unwrap the buffer and verify that the base::SharedMemoryHandle also
153 // works as expected.
154 MojoPlatformHandle os_buffer;
155 os_buffer.struct_size = sizeof(MojoPlatformHandle);
156 size_t size;
157 MojoPlatformSharedBufferHandleFlags flags;
158 ASSERT_EQ(MOJO_RESULT_OK,
159 MojoUnwrapPlatformSharedBufferHandle(wrapped_handle, &os_buffer,
160 &size, &flags));
161 bool read_only = flags & MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE;
162 EXPECT_FALSE(read_only);
163
164 #if defined(OS_MACOSX) && !defined(OS_IOS)
165 ASSERT_EQ(MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT, os_buffer.type);
166 base::SharedMemoryHandle memory_handle(
167 static_cast<mach_port_t>(os_buffer.value), size,
168 base::GetCurrentProcId());
169 #elif defined(OS_POSIX)
170 ASSERT_EQ(MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR, os_buffer.type);
171 base::SharedMemoryHandle memory_handle(static_cast<int>(os_buffer.value),
172 false);
173 #elif defined(OS_WIN)
174 ASSERT_EQ(MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE, os_buffer.type);
175 base::SharedMemoryHandle memory_handle(
176 reinterpret_cast<HANDLE>(os_buffer.value), base::GetCurrentProcId());
177 #endif
178
179 base::SharedMemory memory(memory_handle, read_only);
180 memory.Map(message.size());
181 ASSERT_TRUE(memory.memory());
182
183 EXPECT_TRUE(std::equal(message.begin(), message.end(),
184 static_cast<const char*>(memory.memory())));
185 }
186
187 TEST_F(PlatformWrapperTest, InvalidHandle) {
188 // Wrap an invalid platform handle and expect to unwrap the same.
189
190 MojoHandle wrapped_handle;
191 MojoPlatformHandle invalid_handle;
192 invalid_handle.struct_size = sizeof(MojoPlatformHandle);
193 invalid_handle.type = MOJO_PLATFORM_HANDLE_TYPE_INVALID;
194 EXPECT_EQ(MOJO_RESULT_OK,
195 MojoWrapPlatformHandle(&invalid_handle, &wrapped_handle));
196 EXPECT_EQ(MOJO_RESULT_OK,
197 MojoUnwrapPlatformHandle(wrapped_handle, &invalid_handle));
198 EXPECT_EQ(MOJO_PLATFORM_HANDLE_TYPE_INVALID, invalid_handle.type);
199 }
200
201 TEST_F(PlatformWrapperTest, InvalidArgument) {
202 // Try to wrap an invalid MojoPlatformHandle struct and expect an error.
203 MojoHandle wrapped_handle;
204 MojoPlatformHandle platform_handle;
205 platform_handle.struct_size = 0;
206 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
207 MojoWrapPlatformHandle(&platform_handle, &wrapped_handle));
208 }
209
210 } // namespace
211 } // namespace edk
212 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/core.cc ('k') | mojo/gles2/command_buffer_client_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698