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/. */
5 #include "gtest/gtest.h"
6 #include "mozilla/VolatileBuffer.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>
19 using namespace mozilla
;
21 TEST(VolatileBufferTest
, HeapVolatileBuffersWork
)
23 RefPtr
<VolatileBuffer
> heapbuf
= new VolatileBuffer();
26 << "Failed to create VolatileBuffer";
27 ASSERT_TRUE(heapbuf
->Init(512))
28 << "Failed to initialize VolatileBuffer";
30 VolatileBufferPtr
<char> ptr(heapbuf
);
32 EXPECT_FALSE(ptr
.WasBufferPurged())
33 << "Buffer should not be purged immediately after initialization";
34 EXPECT_TRUE(ptr
) << "Couldn't get pointer from VolatileBufferPtr";
37 TEST(VolatileBufferTest
, RealVolatileBuffersWork
)
39 RefPtr
<VolatileBuffer
> buf
= new VolatileBuffer();
42 << "Failed to create VolatileBuffer";
43 ASSERT_TRUE(buf
->Init(16384))
44 << "Failed to initialize VolatileBuffer";
46 const char teststr
[] = "foobar";
49 VolatileBufferPtr
<char> ptr(buf
);
51 EXPECT_FALSE(ptr
.WasBufferPurged())
52 << "Buffer should not be purged immediately after initialization";
53 EXPECT_TRUE(ptr
) << "Couldn't get pointer from VolatileBufferPtr";
56 VolatileBufferPtr
<char> ptr2(buf
);
58 EXPECT_FALSE(ptr
.WasBufferPurged())
59 << "Failed to lock buffer again while currently locked";
61 << "Didn't get a pointer on the second lock";
63 strcpy(ptr2
, teststr
);
68 VolatileBufferPtr
<char> ptr(buf
);
70 EXPECT_FALSE(ptr
.WasBufferPurged())
71 << "Buffer was immediately purged after unlock";
72 EXPECT_STREQ(ptr
, teststr
) << "Buffer failed to retain data after unlock";
75 // Test purging if we know how to
76 #if defined(XP_DARWIN)
78 vm_purgable_control(mach_task_self(), (vm_address_t
)NULL
,
79 VM_PURGABLE_PURGE_ALL
, &state
);
84 EXPECT_GT(buf
->NonHeapSizeOfExcludingThis(), 0ul)
85 << "Buffer should not be allocated on heap";
88 VolatileBufferPtr
<char> ptr(buf
);
90 EXPECT_TRUE(ptr
.WasBufferPurged())
91 << "Buffer should not be unpurged after forced purge";
92 EXPECT_STRNE(ptr
, teststr
) << "Purge did not actually purge data";
96 VolatileBufferPtr
<char> ptr(buf
);
98 EXPECT_FALSE(ptr
.WasBufferPurged()) << "Buffer still purged after lock";