|
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 "TestHarness.h" |
|
6 #include "mozilla/VolatileBuffer.h" |
|
7 #include "mozilla/NullPtr.h" |
|
8 #include <string.h> |
|
9 |
|
10 #if defined(ANDROID) |
|
11 #include <fcntl.h> |
|
12 #include <linux/ashmem.h> |
|
13 #include <sys/ioctl.h> |
|
14 #include <sys/stat.h> |
|
15 #include <sys/types.h> |
|
16 #elif defined(XP_DARWIN) |
|
17 #include <mach/mach.h> |
|
18 #endif |
|
19 |
|
20 using namespace mozilla; |
|
21 |
|
22 int main(int argc, char **argv) |
|
23 { |
|
24 ScopedXPCOM xpcom("VolatileBufferTests"); |
|
25 if (xpcom.failed()) { |
|
26 return 1; |
|
27 } |
|
28 |
|
29 RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer(); |
|
30 if (!heapbuf || !heapbuf->Init(512)) { |
|
31 fail("Failed to initialize VolatileBuffer"); |
|
32 return 1; |
|
33 } |
|
34 |
|
35 { |
|
36 VolatileBufferPtr<char> ptr(heapbuf); |
|
37 if (ptr.WasBufferPurged()) { |
|
38 fail("Buffer was immediately purged after initialization"); |
|
39 return 1; |
|
40 } |
|
41 |
|
42 if (!ptr) { |
|
43 fail("Didn't get a pointer"); |
|
44 return 1; |
|
45 } |
|
46 } |
|
47 |
|
48 RefPtr<VolatileBuffer> buf = new VolatileBuffer(); |
|
49 if (!buf || !buf->Init(16384)) { |
|
50 fail("Failed to initialize VolatileBuffer"); |
|
51 return 1; |
|
52 } |
|
53 |
|
54 const char teststr[] = "foobar"; |
|
55 |
|
56 { |
|
57 VolatileBufferPtr<char> ptr(buf); |
|
58 if (ptr.WasBufferPurged()) { |
|
59 fail("Buffer should not be purged immediately after initialization"); |
|
60 return 1; |
|
61 } |
|
62 |
|
63 if (!ptr) { |
|
64 fail("Didn't get a pointer"); |
|
65 return 1; |
|
66 } |
|
67 |
|
68 { |
|
69 VolatileBufferPtr<char> ptr2(buf); |
|
70 if (ptr2.WasBufferPurged()) { |
|
71 fail("Failed to Lock buffer again while currently locked"); |
|
72 return 1; |
|
73 } |
|
74 |
|
75 if (!ptr2) { |
|
76 fail("Didn't get a pointer on the second lock"); |
|
77 return 1; |
|
78 } |
|
79 |
|
80 strcpy(ptr2, teststr); |
|
81 } |
|
82 } |
|
83 |
|
84 { |
|
85 VolatileBufferPtr<char> ptr(buf); |
|
86 if (ptr.WasBufferPurged()) { |
|
87 fail("Buffer was immediately purged after unlock"); |
|
88 return 1; |
|
89 } |
|
90 |
|
91 if (strcmp(ptr, teststr)) { |
|
92 fail("Buffer failed to retain data after unlock"); |
|
93 return 1; |
|
94 } |
|
95 } |
|
96 |
|
97 // Test purging if we know how to |
|
98 #if defined(MOZ_WIDGET_GONK) |
|
99 // This also works on Android, but we need root. |
|
100 int fd = open("/" ASHMEM_NAME_DEF, O_RDWR); |
|
101 if (fd < 0) { |
|
102 fail("Failed to open ashmem device"); |
|
103 return 1; |
|
104 } |
|
105 if (ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL) < 0) { |
|
106 fail("Failed to purge ashmem caches"); |
|
107 return 1; |
|
108 } |
|
109 #elif defined(XP_DARWIN) |
|
110 int state; |
|
111 vm_purgable_control(mach_task_self(), (vm_address_t)NULL, |
|
112 VM_PURGABLE_PURGE_ALL, &state); |
|
113 #else |
|
114 return 0; |
|
115 #endif |
|
116 |
|
117 if (!buf->NonHeapSizeOfExcludingThis()) { |
|
118 fail("Buffer should not be allocated on heap"); |
|
119 return 1; |
|
120 } |
|
121 |
|
122 { |
|
123 VolatileBufferPtr<char> ptr(buf); |
|
124 if (!ptr.WasBufferPurged()) { |
|
125 fail("Buffer should not be unpurged after forced purge"); |
|
126 return 1; |
|
127 } |
|
128 |
|
129 if (!strcmp(ptr, teststr)) { |
|
130 fail("Purge did not actually purge data"); |
|
131 return 1; |
|
132 } |
|
133 } |
|
134 |
|
135 { |
|
136 VolatileBufferPtr<char> ptr(buf); |
|
137 if (ptr.WasBufferPurged()) { |
|
138 fail("Buffer still purged after lock"); |
|
139 return 1; |
|
140 } |
|
141 } |
|
142 |
|
143 return 0; |
|
144 } |