Bug 1883861 - Part 1: Move visitMemoryBarrier into the common CodeGenerator file...
[gecko.git] / ipc / gtest / TestUnsafeSharedMemoryHandle.cpp
blob6934ae0d4c28db07578b591db69bf109eb54c433
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "chrome/common/ipc_message.h"
8 #include "chrome/common/ipc_message_utils.h"
9 #include "gtest/gtest.h"
11 #include "mozilla/RandomNum.h"
12 #include "mozilla/ipc/RawShmem.h"
14 namespace mozilla::ipc {
16 static bool SerializeAndDeserialize(UnsafeSharedMemoryHandle&& aIn,
17 UnsafeSharedMemoryHandle* aOut) {
18 IPC::Message msg(MSG_ROUTING_NONE, 0);
20 IPC::MessageWriter writer(msg);
21 IPC::WriteParam(&writer, std::move(aIn));
24 IPC::MessageReader reader(msg);
25 return IPC::ReadParam(&reader, aOut);
28 void TestUnsafeSharedMemoryHandle(size_t aSize) {
29 auto maybeHandle = UnsafeSharedMemoryHandle::CreateAndMap(aSize);
30 ASSERT_TRUE(maybeHandle);
31 auto [handle, mapping] = maybeHandle.extract();
33 EXPECT_EQ(mapping.Size(), aSize);
35 auto inBytes = mapping.Bytes();
36 for (size_t i = 0; i < aSize; ++i) {
37 inBytes[i] = static_cast<uint8_t>(i % 255);
40 UnsafeSharedMemoryHandle out;
41 ASSERT_TRUE(SerializeAndDeserialize(std::move(handle), &out));
43 auto mapping2 = WritableSharedMemoryMapping::Open(std::move(out)).value();
45 EXPECT_EQ(mapping2.Size(), aSize);
46 auto outBytes = mapping2.Bytes();
47 for (size_t i = 0; i < aSize; ++i) {
48 EXPECT_EQ(outBytes[i], static_cast<uint8_t>(i % 255));
52 TEST(UnsafeSharedMemoryHandle, Empty)
53 { TestUnsafeSharedMemoryHandle(0); }
55 TEST(UnsafeSharedMemoryHandle, SmallSize)
56 { TestUnsafeSharedMemoryHandle(2048); }
58 } // namespace mozilla::ipc