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