• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 <algorithm>
6 #include <vector>
7 
8 #include "base/rand_util.h"
9 #include "mojo/public/cpp/base/big_buffer.h"
10 #include "mojo/public/cpp/base/big_buffer_mojom_traits.h"
11 #include "mojo/public/cpp/test_support/test_utils.h"
12 #include "mojo/public/mojom/base/big_buffer.mojom.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace mojo_base {
16 namespace big_buffer_unittest {
17 
18 namespace {
19 
BufferEquals(const BigBuffer & a,const BigBuffer & b)20 bool BufferEquals(const BigBuffer& a, const BigBuffer& b) {
21   return a.size() == b.size() && std::equal(a.data(), a.data() + a.size(),
22                                             b.data(), b.data() + b.size());
23 }
24 
25 }  // namespace
26 
TEST(BigBufferTest,EmptyBuffer)27 TEST(BigBufferTest, EmptyBuffer) {
28   BigBuffer in;
29   BigBuffer out;
30   EXPECT_EQ(BigBuffer::StorageType::kBytes, in.storage_type());
31   EXPECT_EQ(0u, in.size());
32 
33   ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::BigBuffer>(&in, &out));
34 
35   EXPECT_EQ(BigBuffer::StorageType::kBytes, out.storage_type());
36   EXPECT_TRUE(BufferEquals(in, out));
37 }
38 
TEST(BigBufferTest,SmallDataSize)39 TEST(BigBufferTest, SmallDataSize) {
40   BigBuffer in(std::vector<uint8_t>{1, 2, 3});
41   EXPECT_EQ(BigBuffer::StorageType::kBytes, in.storage_type());
42 
43   BigBuffer out;
44   ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::BigBuffer>(&in, &out));
45 
46   EXPECT_EQ(BigBuffer::StorageType::kBytes, out.storage_type());
47   EXPECT_TRUE(BufferEquals(in, out));
48 }
49 
TEST(BigBufferTest,LargeDataSize)50 TEST(BigBufferTest, LargeDataSize) {
51   constexpr size_t kLargeDataSize = BigBuffer::kMaxInlineBytes * 2;
52   std::vector<uint8_t> data(kLargeDataSize);
53   base::RandBytes(data.data(), kLargeDataSize);
54 
55   BigBuffer in(data);
56   EXPECT_EQ(BigBuffer::StorageType::kSharedMemory, in.storage_type());
57 
58   BigBuffer out;
59   ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::BigBuffer>(&in, &out));
60 
61   EXPECT_EQ(BigBuffer::StorageType::kSharedMemory, out.storage_type());
62 
63   // NOTE: It's not safe to compare to |in| here since serialization will have
64   // taken ownership of its internal shared buffer handle.
65   EXPECT_TRUE(BufferEquals(data, out));
66 }
67 
68 }  // namespace big_buffer_unittest
69 }  // namespace mojo_base
70