blob: 7d21236a452e5e8aedb281457d11e290c928d516 (
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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/VolatileBuffer.h"
#include <string.h>
#ifdef XP_DARWIN
#include <mach/mach.h>
#endif
using namespace mozilla;
TEST(VolatileBufferTest, HeapVolatileBuffersWork)
{
RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer();
ASSERT_TRUE(heapbuf) << "Failed to create VolatileBuffer";
ASSERT_TRUE(heapbuf->Init(512)) << "Failed to initialize VolatileBuffer";
VolatileBufferPtr<char> ptr(heapbuf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Buffer should not be purged immediately after initialization";
EXPECT_TRUE(ptr) << "Couldn't get pointer from VolatileBufferPtr";
}
TEST(VolatileBufferTest, RealVolatileBuffersWork)
{
RefPtr<VolatileBuffer> buf = new VolatileBuffer();
ASSERT_TRUE(buf) << "Failed to create VolatileBuffer";
ASSERT_TRUE(buf->Init(16384)) << "Failed to initialize VolatileBuffer";
const char teststr[] = "foobar";
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Buffer should not be purged immediately after initialization";
EXPECT_TRUE(ptr) << "Couldn't get pointer from VolatileBufferPtr";
{
VolatileBufferPtr<char> ptr2(buf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Failed to lock buffer again while currently locked";
ASSERT_TRUE(ptr2) << "Didn't get a pointer on the second lock";
strcpy(ptr2, teststr);
}
}
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Buffer was immediately purged after unlock";
EXPECT_STREQ(ptr, teststr) << "Buffer failed to retain data after unlock";
}
// Test purging if we know how to
#if defined(XP_DARWIN)
int state;
vm_purgable_control(mach_task_self(), (vm_address_t)NULL,
VM_PURGABLE_PURGE_ALL, &state);
#else
return;
#endif
EXPECT_GT(buf->NonHeapSizeOfExcludingThis(), 0ul)
<< "Buffer should not be allocated on heap";
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_TRUE(ptr.WasBufferPurged())
<< "Buffer should not be unpurged after forced purge";
EXPECT_STRNE(ptr, teststr) << "Purge did not actually purge data";
}
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_FALSE(ptr.WasBufferPurged()) << "Buffer still purged after lock";
}
}
|