1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/core/ipcz_driver/shared_buffer.h"
#include <string.h>
#include <array>
#include <string>
#include <utility>
#include "base/memory/platform_shared_memory_region.h"
#include "base/notreached.h"
#include "build/blink_buildflags.h"
#include "build/build_config.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/test/mojo_test_base.h"
#include "mojo/public/c/system/types.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)
#include "mojo/core/core.h"
#include "mojo/core/shared_buffer_dispatcher.h"
#endif
namespace mojo::core {
namespace {
using SharedBufferTest = test::MojoTestBase;
TEST_F(SharedBufferTest, CreateSharedBuffer) {
const std::string message = "hello";
MojoHandle h = CreateBuffer(message.size());
WriteToBuffer(h, 0, message);
ExpectBufferContents(h, 0, message);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
}
TEST_F(SharedBufferTest, DuplicateSharedBuffer) {
const std::string message = "hello";
MojoHandle h = CreateBuffer(message.size());
WriteToBuffer(h, 0, message);
MojoHandle dupe = DuplicateBuffer(h, false);
ExpectBufferContents(dupe, 0, message);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(dupe));
}
TEST_F(SharedBufferTest, PassSharedBufferLocal) {
const std::string message = "hello";
MojoHandle h = CreateBuffer(message.size());
WriteToBuffer(h, 0, message);
MojoHandle dupe = DuplicateBuffer(h, false);
MojoHandle p0, p1;
CreateMessagePipe(&p0, &p1);
WriteMessageWithHandles(p0, "...", &dupe, 1);
EXPECT_EQ("...", ReadMessageWithHandles(p1, &dupe, 1));
ExpectBufferContents(dupe, 0, message);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(dupe));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(p0));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(p1));
}
#if BUILDFLAG(USE_BLINK)
// Reads a single message with a shared buffer handle, maps the buffer, copies
// the message contents into it, then exits.
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CopyToBufferClient, SharedBufferTest, h) {
MojoHandle b;
std::string message = ReadMessageWithHandles(h, &b, 1);
WriteToBuffer(b, 0, message);
EXPECT_EQ("quit", ReadMessage(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_PassSharedBufferCrossProcess DISABLED_PassSharedBufferCrossProcess
#else
#define MAYBE_PassSharedBufferCrossProcess PassSharedBufferCrossProcess
#endif // BUILDFLAG(IS_IOS)
TEST_F(SharedBufferTest, MAYBE_PassSharedBufferCrossProcess) {
const std::string message = "hello";
MojoHandle b = CreateBuffer(message.size());
RunTestClient("CopyToBufferClient", [&](MojoHandle h) {
MojoHandle dupe = DuplicateBuffer(b, false);
WriteMessageWithHandles(h, message, &dupe, 1);
WriteMessage(h, "quit");
});
ExpectBufferContents(b, 0, message);
MojoClose(b);
}
// Creates a new buffer, maps it, writes a message contents to it, unmaps it,
// and finally passes it back to the parent.
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateBufferClient, SharedBufferTest, h) {
std::string message = ReadMessage(h);
MojoHandle b = CreateBuffer(message.size());
WriteToBuffer(b, 0, message);
WriteMessageWithHandles(h, "have a buffer", &b, 1);
EXPECT_EQ("quit", ReadMessage(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
}
#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_PassSharedBufferFromChild DISABLED_PassSharedBufferFromChild
#else
#define MAYBE_PassSharedBufferFromChild PassSharedBufferFromChild
#endif // BUILDFLAG(IS_IOS)
TEST_F(SharedBufferTest, MAYBE_PassSharedBufferFromChild) {
const std::string message = "hello";
MojoHandle b;
RunTestClient("CreateBufferClient", [&](MojoHandle h) {
WriteMessage(h, message);
ReadMessageWithHandles(h, &b, 1);
WriteMessage(h, "quit");
});
ExpectBufferContents(b, 0, message);
MojoClose(b);
}
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassBuffer, SharedBufferTest, h) {
// Receive a pipe handle over the primordial pipe. This will be connected to
// another child process.
MojoHandle other_child;
std::string message = ReadMessageWithHandles(h, &other_child, 1);
// Create a new shared buffer.
MojoHandle b = CreateBuffer(message.size());
// Send a copy of the buffer to the parent and the other child.
MojoHandle dupe = DuplicateBuffer(b, false);
WriteMessageWithHandles(h, "", &b, 1);
WriteMessageWithHandles(other_child, "", &dupe, 1);
EXPECT_EQ("quit", ReadMessage(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(other_child));
}
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveAndEditBuffer, SharedBufferTest, h) {
// Receive a pipe handle over the primordial pipe. This will be connected to
// another child process (running CreateAndPassBuffer).
MojoHandle other_child;
std::string message = ReadMessageWithHandles(h, &other_child, 1);
// Receive a shared buffer from the other child.
MojoHandle b;
ReadMessageWithHandles(other_child, &b, 1);
// Write the message from the parent into the buffer and exit.
WriteToBuffer(b, 0, message);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ("quit", ReadMessage(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(other_child));
}
#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_PassSharedBufferFromChildToChild \
DISABLED_PassSharedBufferFromChildToChild
#else
#define MAYBE_PassSharedBufferFromChildToChild PassSharedBufferFromChildToChild
#endif // BUILDFLAG(IS_IOS)
TEST_F(SharedBufferTest, MAYBE_PassSharedBufferFromChildToChild) {
const std::string message = "hello";
MojoHandle p0, p1;
CreateMessagePipe(&p0, &p1);
MojoHandle b;
RunTestClient("CreateAndPassBuffer", [&](MojoHandle h0) {
RunTestClient("ReceiveAndEditBuffer", [&](MojoHandle h1) {
// Send one end of the pipe to each child. The first child will create
// and pass a buffer to the second child and back to us. The second child
// will write our message into the buffer.
WriteMessageWithHandles(h0, message, &p0, 1);
WriteMessageWithHandles(h1, message, &p1, 1);
// Receive the buffer back from the first child.
ReadMessageWithHandles(h0, &b, 1);
WriteMessage(h1, "quit");
});
WriteMessage(h0, "quit");
});
// The second child should have written this message.
ExpectBufferContents(b, 0, message);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassBufferParent,
SharedBufferTest,
parent) {
RunTestClient("CreateAndPassBuffer", [&](MojoHandle child) {
// Read a pipe from the parent and forward it to our child.
MojoHandle pipe;
std::string message = ReadMessageWithHandles(parent, &pipe, 1);
WriteMessageWithHandles(child, message, &pipe, 1);
// Read a buffer handle from the child and pass it back to the parent.
MojoHandle buffer;
EXPECT_EQ("", ReadMessageWithHandles(child, &buffer, 1));
WriteMessageWithHandles(parent, "", &buffer, 1);
EXPECT_EQ("quit", ReadMessage(parent));
WriteMessage(child, "quit");
});
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(parent));
}
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveAndEditBufferParent,
SharedBufferTest,
parent) {
RunTestClient("ReceiveAndEditBuffer", [&](MojoHandle child) {
// Read a pipe from the parent and forward it to our child.
MojoHandle pipe;
std::string message = ReadMessageWithHandles(parent, &pipe, 1);
WriteMessageWithHandles(child, message, &pipe, 1);
EXPECT_EQ("quit", ReadMessage(parent));
WriteMessage(child, "quit");
});
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(parent));
}
#if BUILDFLAG(IS_ANDROID)
// Android multi-process tests are not executing the new process. This is flaky.
#define MAYBE_PassHandleBetweenCousins DISABLED_PassHandleBetweenCousins
#else
#define MAYBE_PassHandleBetweenCousins PassHandleBetweenCousins
#endif
TEST_F(SharedBufferTest, MAYBE_PassHandleBetweenCousins) {
if (IsMojoIpczEnabled()) {
// TODO(crbug.com/40058840): This test relies on Mojo invitations
// between non-broker nodes, which is not currently supported by MojoIpcz.
GTEST_SKIP() << "Invitations between non-brokers are not yet supported "
<< "by MojoIpcz.";
}
const std::string message = "hello";
// Spawn two children who will each spawn their own child. Make sure the
// grandchildren (cousins to each other) can pass platform handles.
MojoHandle b;
RunTestClient("CreateAndPassBufferParent", [&](MojoHandle child1) {
RunTestClient("ReceiveAndEditBufferParent", [&](MojoHandle child2) {
std::array<MojoHandle, 2> pipe;
CreateMessagePipe(&pipe[0], &pipe[1]);
WriteMessageWithHandles(child1, message, &pipe[0], 1);
WriteMessageWithHandles(child2, message, &pipe[1], 1);
// Receive the buffer back from the first child.
ReadMessageWithHandles(child1, &b, 1);
WriteMessage(child2, "quit");
});
WriteMessage(child1, "quit");
});
// The second grandchild should have written this message.
ExpectBufferContents(b, 0, message);
MojoClose(b);
}
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadAndMapWriteSharedBuffer,
SharedBufferTest,
h) {
// Receive the shared buffer.
MojoHandle b;
EXPECT_EQ("hello", ReadMessageWithHandles(h, &b, 1));
// Read from the bufer.
ExpectBufferContents(b, 0, "hello");
// Extract the shared memory handle and verify that it is read-only.
if (IsMojoIpczEnabled()) {
auto buffer = ipcz_driver::SharedBuffer::Unbox(b);
EXPECT_EQ(buffer->region().GetMode(),
base::subtle::PlatformSharedMemoryRegion::Mode::kReadOnly);
} else {
#if BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)
auto* dispatcher = static_cast<SharedBufferDispatcher*>(
Core::Get()->GetDispatcher(b).get());
base::subtle::PlatformSharedMemoryRegion& region =
dispatcher->GetRegionForTesting();
EXPECT_EQ(region.GetMode(),
base::subtle::PlatformSharedMemoryRegion::Mode::kReadOnly);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
#else
NOTREACHED();
#endif
}
WriteMessage(h, "ok");
EXPECT_EQ("quit", ReadMessage(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
}
#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_CreateAndPassReadOnlyBuffer DISABLED_CreateAndPassReadOnlyBuffer
#else
#define MAYBE_CreateAndPassReadOnlyBuffer CreateAndPassReadOnlyBuffer
#endif // BUILDFLAG(IS_IOS)
TEST_F(SharedBufferTest, MAYBE_CreateAndPassReadOnlyBuffer) {
RunTestClient("ReadAndMapWriteSharedBuffer", [&](MojoHandle h) {
// Create a new shared buffer.
MojoHandle b = CreateBuffer(1234);
WriteToBuffer(b, 0, "hello");
// Send a read-only copy of the buffer to the child.
MojoHandle dupe = DuplicateBuffer(b, true /* read_only */);
WriteMessageWithHandles(h, "hello", &dupe, 1);
EXPECT_EQ("ok", ReadMessage(h));
WriteMessage(h, "quit");
MojoClose(b);
});
}
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassReadOnlyBuffer,
SharedBufferTest,
h) {
// Create a new shared buffer.
MojoHandle b = CreateBuffer(1234);
WriteToBuffer(b, 0, "hello");
// Send a read-only copy of the buffer to the parent.
MojoHandle dupe = DuplicateBuffer(b, true /* read_only */);
WriteMessageWithHandles(h, "", &dupe, 1);
WriteMessage(h, "ok");
EXPECT_EQ("quit", ReadMessage(h));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h));
MojoClose(b);
}
#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_CreateAndPassFromChildReadOnlyBuffer \
DISABLED_CreateAndPassFromChildReadOnlyBuffer
#else
#define MAYBE_CreateAndPassFromChildReadOnlyBuffer \
CreateAndPassFromChildReadOnlyBuffer
#endif // BUILDFLAG(IS_IOS)
TEST_F(SharedBufferTest, MAYBE_CreateAndPassFromChildReadOnlyBuffer) {
RunTestClient("CreateAndPassReadOnlyBuffer", [&](MojoHandle h) {
MojoHandle b;
EXPECT_EQ("", ReadMessageWithHandles(h, &b, 1));
ExpectBufferContents(b, 0, "hello");
// Extract the shared memory handle and verify that it is read-only.
if (IsMojoIpczEnabled()) {
auto buffer = ipcz_driver::SharedBuffer::Unbox(b);
EXPECT_EQ(buffer->region().GetMode(),
base::subtle::PlatformSharedMemoryRegion::Mode::kReadOnly);
} else {
#if BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)
auto* dispatcher = static_cast<SharedBufferDispatcher*>(
Core::Get()->GetDispatcher(b).get());
base::subtle::PlatformSharedMemoryRegion& region =
dispatcher->GetRegionForTesting();
EXPECT_EQ(region.GetMode(),
base::subtle::PlatformSharedMemoryRegion::Mode::kReadOnly);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
#else
NOTREACHED();
#endif
}
EXPECT_EQ("ok", ReadMessage(h));
WriteMessage(h, "quit");
});
}
#endif // BUILDFLAG(USE_BLINK)
} // namespace
} // namespace mojo::core
|