summaryrefslogtreecommitdiffstats
path: root/base/memory/discardable_memory_unittest.cc
blob: c9f67b2556f17a24a1e7f65d9533403106e6c94e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/discardable_memory.h"

#include <limits>

#include "testing/gtest/include/gtest/gtest.h"

namespace base {

const size_t kSize = 1024;

#if defined(OS_ANDROID)
TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
  const size_t kPageSize = 4096;
  const size_t max_allowed_allocation_size =
      std::numeric_limits<size_t>::max() - kPageSize + 1;
  scoped_ptr<DiscardableMemory> memory(
      DiscardableMemory::CreateLockedMemory(max_allowed_allocation_size + 1));
  // On certain platforms (e.g. Android), page-alignment would have caused an
  // overflow resulting in a small allocation if the input size wasn't checked
  // correctly.
  ASSERT_FALSE(memory);
}
#endif

TEST(DiscardableMemoryTest, SupportedNatively) {
#if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
  ASSERT_TRUE(DiscardableMemory::SupportedNatively());
#else
  // If we ever have a platform that decides at runtime if it can support
  // discardable memory natively, then we'll have to add a 'never supported
  // natively' define for this case. At present, if it's not always supported
  // natively, it's never supported.
  ASSERT_FALSE(DiscardableMemory::SupportedNatively());
#endif
}

// Test Lock() and Unlock() functionalities.
TEST(DiscardableMemoryTest, LockAndUnLock) {
  const scoped_ptr<DiscardableMemory> memory(
      DiscardableMemory::CreateLockedMemory(kSize));
  ASSERT_TRUE(memory);
  void* addr = memory->Memory();
  ASSERT_NE(static_cast<void*>(NULL), addr);

  memory->Unlock();
  // The system should have no reason to purge discardable blocks in this brief
  // interval, though technically speaking this might flake.
  EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
  addr = memory->Memory();
  ASSERT_NE(static_cast<void*>(NULL), addr);

  memory->Unlock();
}

// Test delete a discardable memory while it is locked.
TEST(DiscardableMemoryTest, DeleteWhileLocked) {
  const scoped_ptr<DiscardableMemory> memory(
      DiscardableMemory::CreateLockedMemory(kSize));
  ASSERT_TRUE(memory);
}

#if !defined(OS_ANDROID)
// Test forced purging.
TEST(DiscardableMemoryTest, Purge) {
  ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());

  const scoped_ptr<DiscardableMemory> memory(
      DiscardableMemory::CreateLockedMemory(kSize));
  ASSERT_TRUE(memory);
  memory->Unlock();

  DiscardableMemory::PurgeForTesting();
  EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
}
#endif  // !OS_ANDROID

#if !defined(NDEBUG) && !defined(OS_ANDROID)
// Death tests are not supported with Android APKs.
TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
  const scoped_ptr<DiscardableMemory> memory(
      DiscardableMemory::CreateLockedMemory(kSize));
  ASSERT_TRUE(memory);
  memory->Unlock();
  ASSERT_DEATH_IF_SUPPORTED(
      { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
}
#endif

}