OLD | NEW |
1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "../../include/fxcrt/fx_memory.h" | 8 #include "../../include/fxcrt/fx_memory.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 const size_t kMaxByteAlloc = std::numeric_limits<size_t>::max(); | 12 const size_t kMaxByteAlloc = std::numeric_limits<size_t>::max(); |
13 const size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int); | 13 const size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int); |
14 const size_t kOverflowIntAlloc = kMaxIntAlloc + 100; | 14 const size_t kOverflowIntAlloc = kMaxIntAlloc + 100; |
| 15 const size_t kWidth = 640; |
| 16 const size_t kOverflowIntAlloc2D = kMaxIntAlloc / kWidth + 10; |
15 | 17 |
16 } // namespace | 18 } // namespace |
17 | 19 |
18 // TODO(tsepez): re-enable OOM tests if we can find a way to | 20 // TODO(tsepez): re-enable OOM tests if we can find a way to |
19 // prevent it from hosing the bots. | 21 // prevent it from hosing the bots. |
20 TEST(fxcrt, DISABLED_FX_AllocOOM) { | 22 TEST(fxcrt, DISABLED_FX_AllocOOM) { |
21 EXPECT_DEATH_IF_SUPPORTED(FX_Alloc(int, kMaxIntAlloc), ""); | 23 EXPECT_DEATH_IF_SUPPORTED(FX_Alloc(int, kMaxIntAlloc), ""); |
22 | 24 |
23 int* ptr = FX_Alloc(int, 1); | 25 int* ptr = FX_Alloc(int, 1); |
24 EXPECT_TRUE(ptr); | 26 EXPECT_TRUE(ptr); |
25 EXPECT_DEATH_IF_SUPPORTED(FX_Realloc(int, ptr, kMaxIntAlloc), ""); | 27 EXPECT_DEATH_IF_SUPPORTED(FX_Realloc(int, ptr, kMaxIntAlloc), ""); |
26 FX_Free(ptr); | 28 FX_Free(ptr); |
27 } | 29 } |
28 | 30 |
29 TEST(fxcrt, FX_AllocOverflow) { | 31 TEST(fxcrt, FX_AllocOverflow) { |
30 EXPECT_DEATH_IF_SUPPORTED(FX_Alloc(int, kOverflowIntAlloc), ""); | 32 EXPECT_DEATH_IF_SUPPORTED(FX_Alloc(int, kOverflowIntAlloc), ""); |
31 | 33 |
32 int* ptr = FX_Alloc(int, 1); | 34 int* ptr = FX_Alloc(int, 1); |
33 EXPECT_TRUE(ptr); | 35 EXPECT_TRUE(ptr); |
34 EXPECT_DEATH_IF_SUPPORTED(FX_Realloc(int, ptr, kOverflowIntAlloc), ""); | 36 EXPECT_DEATH_IF_SUPPORTED(FX_Realloc(int, ptr, kOverflowIntAlloc), ""); |
35 FX_Free(ptr); | 37 FX_Free(ptr); |
36 } | 38 } |
37 | 39 |
| 40 TEST(fxcrt, FX_AllocOverflow2D) { |
| 41 EXPECT_DEATH_IF_SUPPORTED( |
| 42 FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D), ""); |
| 43 } |
| 44 |
38 TEST(fxcrt, DISABLED_FX_TryAllocOOM) { | 45 TEST(fxcrt, DISABLED_FX_TryAllocOOM) { |
39 EXPECT_FALSE(FX_TryAlloc(int, kMaxIntAlloc)); | 46 EXPECT_FALSE(FX_TryAlloc(int, kMaxIntAlloc)); |
40 | 47 |
41 int* ptr = FX_Alloc(int, 1); | 48 int* ptr = FX_Alloc(int, 1); |
42 EXPECT_TRUE(ptr); | 49 EXPECT_TRUE(ptr); |
43 EXPECT_FALSE(FX_TryRealloc(int, ptr, kMaxIntAlloc)); | 50 EXPECT_FALSE(FX_TryRealloc(int, ptr, kMaxIntAlloc)); |
44 FX_Free(ptr); | 51 FX_Free(ptr); |
45 } | 52 } |
46 | 53 |
47 TEST(fxcrt, FX_TryAllocOverflow) { | 54 TEST(fxcrt, FX_TryAllocOverflow) { |
48 EXPECT_FALSE(FX_TryAlloc(int, kOverflowIntAlloc)); | 55 EXPECT_FALSE(FX_TryAlloc(int, kOverflowIntAlloc)); |
49 | 56 |
50 int* ptr = FX_Alloc(int, 1); | 57 int* ptr = FX_Alloc(int, 1); |
51 EXPECT_TRUE(ptr); | 58 EXPECT_TRUE(ptr); |
52 EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc)); | 59 EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc)); |
53 FX_Free(ptr); | 60 FX_Free(ptr); |
54 } | 61 } |
55 | 62 |
56 TEST(fxcrt, DISABLED_FXMEM_DefaultOOM) { | 63 TEST(fxcrt, DISABLED_FXMEM_DefaultOOM) { |
57 EXPECT_FALSE(FXMEM_DefaultAlloc(kMaxByteAlloc, 0)); | 64 EXPECT_FALSE(FXMEM_DefaultAlloc(kMaxByteAlloc, 0)); |
58 | 65 |
59 void* ptr = FXMEM_DefaultAlloc(1, 0); | 66 void* ptr = FXMEM_DefaultAlloc(1, 0); |
60 EXPECT_TRUE(ptr); | 67 EXPECT_TRUE(ptr); |
61 EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kMaxByteAlloc, 0)); | 68 EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kMaxByteAlloc, 0)); |
62 FXMEM_DefaultFree(ptr, 0); | 69 FXMEM_DefaultFree(ptr, 0); |
63 } | 70 } |
OLD | NEW |