michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: #include "mozilla/VolatileBuffer.h" michael@0: #include "mozilla/NullPtr.h" michael@0: #include michael@0: michael@0: #if defined(ANDROID) michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #elif defined(XP_DARWIN) michael@0: #include michael@0: #endif michael@0: michael@0: using namespace mozilla; michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: ScopedXPCOM xpcom("VolatileBufferTests"); michael@0: if (xpcom.failed()) { michael@0: return 1; michael@0: } michael@0: michael@0: RefPtr heapbuf = new VolatileBuffer(); michael@0: if (!heapbuf || !heapbuf->Init(512)) { michael@0: fail("Failed to initialize VolatileBuffer"); michael@0: return 1; michael@0: } michael@0: michael@0: { michael@0: VolatileBufferPtr ptr(heapbuf); michael@0: if (ptr.WasBufferPurged()) { michael@0: fail("Buffer was immediately purged after initialization"); michael@0: return 1; michael@0: } michael@0: michael@0: if (!ptr) { michael@0: fail("Didn't get a pointer"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: RefPtr buf = new VolatileBuffer(); michael@0: if (!buf || !buf->Init(16384)) { michael@0: fail("Failed to initialize VolatileBuffer"); michael@0: return 1; michael@0: } michael@0: michael@0: const char teststr[] = "foobar"; michael@0: michael@0: { michael@0: VolatileBufferPtr ptr(buf); michael@0: if (ptr.WasBufferPurged()) { michael@0: fail("Buffer should not be purged immediately after initialization"); michael@0: return 1; michael@0: } michael@0: michael@0: if (!ptr) { michael@0: fail("Didn't get a pointer"); michael@0: return 1; michael@0: } michael@0: michael@0: { michael@0: VolatileBufferPtr ptr2(buf); michael@0: if (ptr2.WasBufferPurged()) { michael@0: fail("Failed to Lock buffer again while currently locked"); michael@0: return 1; michael@0: } michael@0: michael@0: if (!ptr2) { michael@0: fail("Didn't get a pointer on the second lock"); michael@0: return 1; michael@0: } michael@0: michael@0: strcpy(ptr2, teststr); michael@0: } michael@0: } michael@0: michael@0: { michael@0: VolatileBufferPtr ptr(buf); michael@0: if (ptr.WasBufferPurged()) { michael@0: fail("Buffer was immediately purged after unlock"); michael@0: return 1; michael@0: } michael@0: michael@0: if (strcmp(ptr, teststr)) { michael@0: fail("Buffer failed to retain data after unlock"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: // Test purging if we know how to michael@0: #if defined(MOZ_WIDGET_GONK) michael@0: // This also works on Android, but we need root. michael@0: int fd = open("/" ASHMEM_NAME_DEF, O_RDWR); michael@0: if (fd < 0) { michael@0: fail("Failed to open ashmem device"); michael@0: return 1; michael@0: } michael@0: if (ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL) < 0) { michael@0: fail("Failed to purge ashmem caches"); michael@0: return 1; michael@0: } michael@0: #elif defined(XP_DARWIN) michael@0: int state; michael@0: vm_purgable_control(mach_task_self(), (vm_address_t)NULL, michael@0: VM_PURGABLE_PURGE_ALL, &state); michael@0: #else michael@0: return 0; michael@0: #endif michael@0: michael@0: if (!buf->NonHeapSizeOfExcludingThis()) { michael@0: fail("Buffer should not be allocated on heap"); michael@0: return 1; michael@0: } michael@0: michael@0: { michael@0: VolatileBufferPtr ptr(buf); michael@0: if (!ptr.WasBufferPurged()) { michael@0: fail("Buffer should not be unpurged after forced purge"); michael@0: return 1; michael@0: } michael@0: michael@0: if (!strcmp(ptr, teststr)) { michael@0: fail("Purge did not actually purge data"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: { michael@0: VolatileBufferPtr ptr(buf); michael@0: if (ptr.WasBufferPurged()) { michael@0: fail("Buffer still purged after lock"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: }