#include "base/memory/discardable_shared_memory.h"
#include <fcntl.h>
#include <stdint.h>
#include <algorithm>
#include "base/files/scoped_file.h"
#include "base/memory/page_size.h"
#include "base/memory/shared_memory_tracker.h"
#include "base/tracing_buildflags.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(ENABLE_BASE_TRACING)
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/process_memory_dump.h"
#endif
namespace base {
class TestDiscardableSharedMemory : public DiscardableSharedMemory { … };
TEST(DiscardableSharedMemoryTest, CreateAndMap) { … }
TEST(DiscardableSharedMemoryTest, CreateFromHandle) { … }
TEST(DiscardableSharedMemoryTest, LockAndUnlock) { … }
TEST(DiscardableSharedMemoryTest, Purge) { … }
TEST(DiscardableSharedMemoryTest, PurgeAfterClose) { … }
TEST(DiscardableSharedMemoryTest, LastUsed) { … }
TEST(DiscardableSharedMemoryTest, LockShouldAlwaysFailAfterSuccessfulPurge) { … }
#if BUILDFLAG(IS_ANDROID)
TEST(DiscardableSharedMemoryTest, LockShouldFailIfPlatformLockPagesFails) {
const uint32_t kDataSize = 1024;
if (!DiscardableSharedMemory::IsAshmemDeviceSupportedForTesting())
return;
DiscardableSharedMemory memory1;
bool rv1 = memory1.CreateAndMap(kDataSize);
ASSERT_TRUE(rv1);
base::UnsafeSharedMemoryRegion region = memory1.DuplicateRegion();
int fd = region.GetPlatformHandle();
DiscardableSharedMemory memory2(std::move(region));
bool rv2 = memory2.Map(kDataSize);
ASSERT_TRUE(rv2);
memory2.Unlock(0, base::GetPageSize());
base::ScopedFD null(open("/dev/null", O_RDONLY));
ASSERT_EQ(fd, dup2(null.get(), fd));
DiscardableSharedMemory::LockResult lock_rv =
memory2.Lock(0, base::GetPageSize());
EXPECT_EQ(DiscardableSharedMemory::FAILED, lock_rv);
}
#endif
TEST(DiscardableSharedMemoryTest, LockAndUnlockRange) { … }
TEST(DiscardableSharedMemoryTest, MappedSize) { … }
TEST(DiscardableSharedMemoryTest, Close) { … }
TEST(DiscardableSharedMemoryTest, ZeroSize) { … }
#if defined(DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE)
TEST(DiscardableSharedMemoryTest, ZeroFilledPagesAfterPurge) { … }
#endif
#if BUILDFLAG(ENABLE_BASE_TRACING)
TEST(DiscardableSharedMemoryTest, TracingOwnershipEdges) { … }
#endif
}