1 // Copyright (c) 2011 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 "base/memory/ref_counted_memory.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10
11 #include "base/memory/read_only_shared_memory_region.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::Each;
16 using testing::ElementsAre;
17
18 namespace base {
19
TEST(RefCountedMemoryUnitTest,RefCountedStaticMemory)20 TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) {
21 auto mem = MakeRefCounted<RefCountedStaticMemory>("static mem00", 10);
22
23 EXPECT_EQ(10U, mem->size());
24 EXPECT_EQ("static mem", std::string(mem->front_as<char>(), mem->size()));
25 }
26
TEST(RefCountedMemoryUnitTest,RefCountedBytes)27 TEST(RefCountedMemoryUnitTest, RefCountedBytes) {
28 std::vector<uint8_t> data;
29 data.push_back(45);
30 data.push_back(99);
31 scoped_refptr<RefCountedMemory> mem = RefCountedBytes::TakeVector(&data);
32
33 EXPECT_EQ(0U, data.size());
34
35 ASSERT_EQ(2U, mem->size());
36 EXPECT_EQ(45U, mem->front()[0]);
37 EXPECT_EQ(99U, mem->front()[1]);
38
39 scoped_refptr<RefCountedMemory> mem2;
40 {
41 const unsigned char kData[] = {12, 11, 99};
42 mem2 = MakeRefCounted<RefCountedBytes>(kData, arraysize(kData));
43 }
44 ASSERT_EQ(3U, mem2->size());
45 EXPECT_EQ(12U, mem2->front()[0]);
46 EXPECT_EQ(11U, mem2->front()[1]);
47 EXPECT_EQ(99U, mem2->front()[2]);
48 }
49
TEST(RefCountedMemoryUnitTest,RefCountedBytesMutable)50 TEST(RefCountedMemoryUnitTest, RefCountedBytesMutable) {
51 auto mem = base::MakeRefCounted<RefCountedBytes>(10);
52
53 ASSERT_EQ(10U, mem->size());
54 EXPECT_THAT(mem->data(), Each(0U));
55
56 // Test non-const versions of data(), front() and front_as<>().
57 mem->data()[0] = 1;
58 mem->front()[1] = 2;
59 mem->front_as<char>()[2] = 3;
60
61 EXPECT_THAT(mem->data(), ElementsAre(1, 2, 3, 0, 0, 0, 0, 0, 0, 0));
62 }
63
TEST(RefCountedMemoryUnitTest,RefCountedString)64 TEST(RefCountedMemoryUnitTest, RefCountedString) {
65 std::string s("destroy me");
66 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
67
68 EXPECT_EQ(0U, s.size());
69
70 ASSERT_EQ(10U, mem->size());
71 EXPECT_EQ('d', mem->front()[0]);
72 EXPECT_EQ('e', mem->front()[1]);
73 EXPECT_EQ('e', mem->front()[9]);
74 }
75
TEST(RefCountedMemoryUnitTest,RefCountedSharedMemory)76 TEST(RefCountedMemoryUnitTest, RefCountedSharedMemory) {
77 static const char kData[] = "shm_dummy_data";
78 auto shm = std::make_unique<SharedMemory>();
79 ASSERT_TRUE(shm->CreateAndMapAnonymous(sizeof(kData)));
80 memcpy(shm->memory(), kData, sizeof(kData));
81
82 auto mem =
83 MakeRefCounted<RefCountedSharedMemory>(std::move(shm), sizeof(kData));
84 ASSERT_EQ(sizeof(kData), mem->size());
85 EXPECT_EQ('s', mem->front()[0]);
86 EXPECT_EQ('h', mem->front()[1]);
87 EXPECT_EQ('_', mem->front()[9]);
88 }
89
TEST(RefCountedMemoryUnitTest,RefCountedSharedMemoryMapping)90 TEST(RefCountedMemoryUnitTest, RefCountedSharedMemoryMapping) {
91 static const char kData[] = "mem_region_dummy_data";
92 scoped_refptr<RefCountedSharedMemoryMapping> mem;
93 {
94 MappedReadOnlyRegion region =
95 ReadOnlySharedMemoryRegion::Create(sizeof(kData));
96 ReadOnlySharedMemoryMapping ro_mapping = region.region.Map();
97 WritableSharedMemoryMapping rw_mapping = std::move(region.mapping);
98 ASSERT_TRUE(rw_mapping.IsValid());
99 memcpy(rw_mapping.memory(), kData, sizeof(kData));
100 mem = MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(ro_mapping));
101 }
102
103 ASSERT_LE(sizeof(kData), mem->size());
104 EXPECT_EQ('e', mem->front()[1]);
105 EXPECT_EQ('m', mem->front()[2]);
106 EXPECT_EQ('o', mem->front()[8]);
107
108 {
109 MappedReadOnlyRegion region =
110 ReadOnlySharedMemoryRegion::Create(sizeof(kData));
111 WritableSharedMemoryMapping rw_mapping = std::move(region.mapping);
112 ASSERT_TRUE(rw_mapping.IsValid());
113 memcpy(rw_mapping.memory(), kData, sizeof(kData));
114 mem = RefCountedSharedMemoryMapping::CreateFromWholeRegion(region.region);
115 }
116
117 ASSERT_LE(sizeof(kData), mem->size());
118 EXPECT_EQ('_', mem->front()[3]);
119 EXPECT_EQ('r', mem->front()[4]);
120 EXPECT_EQ('i', mem->front()[7]);
121 }
122
TEST(RefCountedMemoryUnitTest,Equals)123 TEST(RefCountedMemoryUnitTest, Equals) {
124 std::string s1("same");
125 scoped_refptr<RefCountedMemory> mem1 = RefCountedString::TakeString(&s1);
126
127 std::vector<unsigned char> d2 = {'s', 'a', 'm', 'e'};
128 scoped_refptr<RefCountedMemory> mem2 = RefCountedBytes::TakeVector(&d2);
129
130 EXPECT_TRUE(mem1->Equals(mem2));
131
132 std::string s3("diff");
133 scoped_refptr<RefCountedMemory> mem3 = RefCountedString::TakeString(&s3);
134
135 EXPECT_FALSE(mem1->Equals(mem3));
136 EXPECT_FALSE(mem2->Equals(mem3));
137 }
138
TEST(RefCountedMemoryUnitTest,EqualsNull)139 TEST(RefCountedMemoryUnitTest, EqualsNull) {
140 std::string s("str");
141 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
142 EXPECT_FALSE(mem->Equals(nullptr));
143 }
144
145 } // namespace base
146