1 // Copyright 2021 The Chromium Authors
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/unsafe_shared_memory_pool.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace base {
10
TEST(UnsafeSharedMemoryPoolTest,CreatesRegion)11 TEST(UnsafeSharedMemoryPoolTest, CreatesRegion) {
12 scoped_refptr<UnsafeSharedMemoryPool> pool(
13 base::MakeRefCounted<UnsafeSharedMemoryPool>());
14 auto handle = pool->MaybeAllocateBuffer(1000);
15 ASSERT_TRUE(handle);
16 EXPECT_TRUE(handle->GetRegion().IsValid());
17 EXPECT_TRUE(handle->GetMapping().IsValid());
18 }
19
TEST(UnsafeSharedMemoryPoolTest,ReusesRegions)20 TEST(UnsafeSharedMemoryPoolTest, ReusesRegions) {
21 scoped_refptr<UnsafeSharedMemoryPool> pool(
22 base::MakeRefCounted<UnsafeSharedMemoryPool>());
23 auto handle = pool->MaybeAllocateBuffer(1000u);
24 ASSERT_TRUE(handle);
25 auto id1 = handle->GetRegion().GetGUID();
26
27 // Return memory to the pool.
28 handle.reset();
29
30 handle = pool->MaybeAllocateBuffer(1000u);
31 // Should reuse the freed region.
32 EXPECT_EQ(id1, handle->GetRegion().GetGUID());
33 }
34
TEST(UnsafeSharedMemoryPoolTest,RespectsSize)35 TEST(UnsafeSharedMemoryPoolTest, RespectsSize) {
36 scoped_refptr<UnsafeSharedMemoryPool> pool(
37 base::MakeRefCounted<UnsafeSharedMemoryPool>());
38 auto handle = pool->MaybeAllocateBuffer(1000u);
39 ASSERT_TRUE(handle);
40 EXPECT_GE(handle->GetRegion().GetSize(), 1000u);
41
42 handle = pool->MaybeAllocateBuffer(100u);
43 ASSERT_TRUE(handle);
44 EXPECT_GE(handle->GetRegion().GetSize(), 100u);
45
46 handle = pool->MaybeAllocateBuffer(1100u);
47 ASSERT_TRUE(handle);
48 EXPECT_GE(handle->GetRegion().GetSize(), 1100u);
49 }
50 } // namespace base
51