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

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

Powered by Google App Engine
This is Rietveld 408576698