Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / ipc / gtest / TestBigBuffer.cpp
blob88c353d53d061397b220165a6f258bc754c4db50
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/BigBuffer.h"
14 namespace mozilla::ipc {
16 static bool SerializeAndDeserialize(BigBuffer&& aIn, BigBuffer* aOut) {
17 IPC::Message msg(MSG_ROUTING_NONE, 0);
19 IPC::MessageWriter writer(msg);
20 IPC::WriteParam(&writer, std::move(aIn));
22 EXPECT_EQ(aIn.Size(), 0u);
24 IPC::MessageReader reader(msg);
25 return IPC::ReadParam(&reader, aOut);
28 TEST(BigBuffer, Empty)
30 BigBuffer in(0);
31 EXPECT_EQ(in.GetSharedMemory(), nullptr);
32 EXPECT_EQ(in.Size(), 0u);
34 BigBuffer out;
35 ASSERT_TRUE(SerializeAndDeserialize(std::move(in), &out));
37 EXPECT_EQ(in.GetSharedMemory(), nullptr);
38 EXPECT_EQ(in.Size(), 0u);
39 EXPECT_EQ(out.GetSharedMemory(), nullptr);
40 EXPECT_EQ(out.Size(), 0u);
43 TEST(BigBuffer, SmallSize)
45 uint8_t data[]{1, 2, 3};
46 BigBuffer in{Span(data)};
47 EXPECT_EQ(in.GetSharedMemory(), nullptr);
48 EXPECT_EQ(in.Size(), 3u);
50 BigBuffer out;
51 ASSERT_TRUE(SerializeAndDeserialize(std::move(in), &out));
53 EXPECT_EQ(in.GetSharedMemory(), nullptr);
54 EXPECT_EQ(in.Size(), 0u);
55 EXPECT_EQ(out.GetSharedMemory(), nullptr);
56 EXPECT_EQ(out.Size(), 3u);
58 EXPECT_TRUE(out.AsSpan() == Span(data));
61 TEST(BigBuffer, BigSize)
63 size_t size = BigBuffer::kShmemThreshold * 2;
64 // Generate a large block of random data which will be serialized in a shmem.
65 nsTArray<uint8_t> data(size);
66 for (size_t i = 0; i < size; ++i) {
67 data.AppendElement(mozilla::RandomUint64OrDie());
69 BigBuffer in{Span(data)};
70 EXPECT_NE(in.GetSharedMemory(), nullptr);
71 EXPECT_EQ(in.Size(), size);
72 EXPECT_EQ(in.GetSharedMemory()->memory(), in.Data());
74 BigBuffer out;
75 ASSERT_TRUE(SerializeAndDeserialize(std::move(in), &out));
77 EXPECT_EQ(in.GetSharedMemory(), nullptr);
78 EXPECT_EQ(in.Size(), 0u);
79 EXPECT_NE(out.GetSharedMemory(), nullptr);
80 EXPECT_EQ(out.Size(), size);
81 EXPECT_EQ(out.GetSharedMemory()->memory(), out.Data());
83 EXPECT_TRUE(out.AsSpan() == Span(data));
86 } // namespace mozilla::ipc