/src/mozilla-central/memory/volatile/tests/TestVolatileBuffer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #include "gtest/gtest.h" |
6 | | #include "mozilla/VolatileBuffer.h" |
7 | | #include <string.h> |
8 | | |
9 | | #if defined(ANDROID) |
10 | | #include <fcntl.h> |
11 | | #include <linux/ashmem.h> |
12 | | #include <sys/ioctl.h> |
13 | | #include <sys/stat.h> |
14 | | #include <sys/types.h> |
15 | | #elif defined(XP_DARWIN) |
16 | | #include <mach/mach.h> |
17 | | #endif |
18 | | |
19 | | using namespace mozilla; |
20 | | |
21 | | TEST(VolatileBufferTest, HeapVolatileBuffersWork) |
22 | 0 | { |
23 | 0 | RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer(); |
24 | 0 |
|
25 | 0 | ASSERT_TRUE(heapbuf) << "Failed to create VolatileBuffer"; |
26 | 0 | ASSERT_TRUE(heapbuf->Init(512)) << "Failed to initialize VolatileBuffer"; |
27 | 0 | |
28 | 0 | VolatileBufferPtr<char> ptr(heapbuf); |
29 | 0 |
|
30 | 0 | EXPECT_FALSE(ptr.WasBufferPurged()) |
31 | 0 | << "Buffer should not be purged immediately after initialization"; |
32 | 0 | EXPECT_TRUE(ptr) << "Couldn't get pointer from VolatileBufferPtr"; |
33 | 0 | } |
34 | | |
35 | | TEST(VolatileBufferTest, RealVolatileBuffersWork) |
36 | 0 | { |
37 | 0 | RefPtr<VolatileBuffer> buf = new VolatileBuffer(); |
38 | 0 |
|
39 | 0 | ASSERT_TRUE(buf) << "Failed to create VolatileBuffer"; |
40 | 0 | ASSERT_TRUE(buf->Init(16384)) << "Failed to initialize VolatileBuffer"; |
41 | 0 | |
42 | 0 | const char teststr[] = "foobar"; |
43 | 0 |
|
44 | 0 | { |
45 | 0 | VolatileBufferPtr<char> ptr(buf); |
46 | 0 |
|
47 | 0 | EXPECT_FALSE(ptr.WasBufferPurged()) |
48 | 0 | << "Buffer should not be purged immediately after initialization"; |
49 | 0 | EXPECT_TRUE(ptr) << "Couldn't get pointer from VolatileBufferPtr"; |
50 | 0 |
|
51 | 0 | { |
52 | 0 | VolatileBufferPtr<char> ptr2(buf); |
53 | 0 |
|
54 | 0 | EXPECT_FALSE(ptr.WasBufferPurged()) |
55 | 0 | << "Failed to lock buffer again while currently locked"; |
56 | 0 | ASSERT_TRUE(ptr2) << "Didn't get a pointer on the second lock"; |
57 | 0 | |
58 | 0 | strcpy(ptr2, teststr); |
59 | 0 | } |
60 | 0 | } |
61 | 0 |
|
62 | 0 | { |
63 | 0 | VolatileBufferPtr<char> ptr(buf); |
64 | 0 |
|
65 | 0 | EXPECT_FALSE(ptr.WasBufferPurged()) |
66 | 0 | << "Buffer was immediately purged after unlock"; |
67 | 0 | EXPECT_STREQ(ptr, teststr) << "Buffer failed to retain data after unlock"; |
68 | 0 | } |
69 | 0 |
|
70 | 0 | // Test purging if we know how to |
71 | | #if defined(XP_DARWIN) |
72 | | int state; |
73 | | vm_purgable_control(mach_task_self(), (vm_address_t)NULL, |
74 | | VM_PURGABLE_PURGE_ALL, &state); |
75 | | #else |
76 | 0 | return; |
77 | 0 | #endif |
78 | 0 |
|
79 | 0 | EXPECT_GT(buf->NonHeapSizeOfExcludingThis(), 0ul) |
80 | 0 | << "Buffer should not be allocated on heap"; |
81 | 0 |
|
82 | 0 | { |
83 | 0 | VolatileBufferPtr<char> ptr(buf); |
84 | 0 |
|
85 | 0 | EXPECT_TRUE(ptr.WasBufferPurged()) |
86 | 0 | << "Buffer should not be unpurged after forced purge"; |
87 | 0 | EXPECT_STRNE(ptr, teststr) << "Purge did not actually purge data"; |
88 | 0 | } |
89 | 0 |
|
90 | 0 | { |
91 | 0 | VolatileBufferPtr<char> ptr(buf); |
92 | 0 |
|
93 | 0 | EXPECT_FALSE(ptr.WasBufferPurged()) << "Buffer still purged after lock"; |
94 | 0 | } |
95 | 0 | } |