tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestBigBuffer.cpp (2595B)


      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/. */
      6 
      7 #include "chrome/common/ipc_message.h"
      8 #include "chrome/common/ipc_message_utils.h"
      9 #include "gtest/gtest.h"
     10 
     11 #include "mozilla/RandomNum.h"
     12 #include "mozilla/ipc/BigBuffer.h"
     13 
     14 namespace mozilla::ipc {
     15 
     16 static bool SerializeAndDeserialize(BigBuffer&& aIn, BigBuffer* aOut) {
     17  IPC::Message msg(MSG_ROUTING_NONE, 0);
     18  {
     19    IPC::MessageWriter writer(msg);
     20    IPC::WriteParam(&writer, std::move(aIn));
     21  }
     22  EXPECT_EQ(aIn.Size(), 0u);
     23 
     24  IPC::MessageReader reader(msg);
     25  return IPC::ReadParam(&reader, aOut);
     26 }
     27 
     28 TEST(BigBuffer, Empty)
     29 {
     30  BigBuffer in(0);
     31  EXPECT_EQ(in.GetSharedMemory(), nullptr);
     32  EXPECT_EQ(in.Size(), 0u);
     33 
     34  BigBuffer out;
     35  ASSERT_TRUE(SerializeAndDeserialize(std::move(in), &out));
     36 
     37  EXPECT_EQ(in.GetSharedMemory(), nullptr);
     38  EXPECT_EQ(in.Size(), 0u);
     39  EXPECT_EQ(out.GetSharedMemory(), nullptr);
     40  EXPECT_EQ(out.Size(), 0u);
     41 }
     42 
     43 TEST(BigBuffer, SmallSize)
     44 {
     45  uint8_t data[]{1, 2, 3};
     46  BigBuffer in{Span(data)};
     47  EXPECT_EQ(in.GetSharedMemory(), nullptr);
     48  EXPECT_EQ(in.Size(), 3u);
     49 
     50  BigBuffer out;
     51  ASSERT_TRUE(SerializeAndDeserialize(std::move(in), &out));
     52 
     53  EXPECT_EQ(in.GetSharedMemory(), nullptr);
     54  EXPECT_EQ(in.Size(), 0u);
     55  EXPECT_EQ(out.GetSharedMemory(), nullptr);
     56  EXPECT_EQ(out.Size(), 3u);
     57 
     58  EXPECT_TRUE(out.AsSpan() == Span(data));
     59 }
     60 
     61 TEST(BigBuffer, BigSize)
     62 {
     63  // Use something that's unlikely to be a multiple of the page size to ensure
     64  // that the requested size is retained (versus the allocated shmem size).
     65  size_t size = BigBuffer::kShmemThreshold * 2 + 41;
     66  // Generate a large block of random data which will be serialized in a shmem.
     67  nsTArray<uint8_t> data(size);
     68  for (size_t i = 0; i < size; ++i) {
     69    data.AppendElement(mozilla::RandomUint64OrDie());
     70  }
     71  BigBuffer in{Span(data)};
     72  EXPECT_NE(in.GetSharedMemory(), nullptr);
     73  EXPECT_EQ(in.Size(), size);
     74  EXPECT_EQ(in.GetSharedMemory()->DataAs<uint8_t>(), in.Data());
     75 
     76  BigBuffer out;
     77  ASSERT_TRUE(SerializeAndDeserialize(std::move(in), &out));
     78 
     79  EXPECT_EQ(in.GetSharedMemory(), nullptr);
     80  EXPECT_EQ(in.Size(), 0u);
     81  EXPECT_NE(out.GetSharedMemory(), nullptr);
     82  EXPECT_EQ(out.Size(), size);
     83  EXPECT_EQ(out.GetSharedMemory()->DataAs<uint8_t>(), out.Data());
     84 
     85  EXPECT_TRUE(out.AsSpan() == Span(data));
     86 }
     87 
     88 }  // namespace mozilla::ipc