michael@0: #include "TestShmem.h" michael@0: michael@0: #include "IPDLUnitTests.h" // fail etc. michael@0: michael@0: michael@0: namespace mozilla { michael@0: namespace _ipdltest { michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // Parent michael@0: michael@0: void michael@0: TestShmemParent::Main() michael@0: { michael@0: Shmem mem; michael@0: Shmem unsafe; michael@0: michael@0: size_t size = 12345; michael@0: if (!AllocShmem(size, SharedMemory::TYPE_BASIC, &mem)) michael@0: fail("can't alloc shmem"); michael@0: if (!AllocUnsafeShmem(size, SharedMemory::TYPE_BASIC, &unsafe)) michael@0: fail("can't alloc shmem"); michael@0: michael@0: if (mem.Size() != size) michael@0: fail("shmem is wrong size: expected %lu, got %lu", michael@0: size, mem.Size()); michael@0: if (unsafe.Size() != size) michael@0: fail("shmem is wrong size: expected %lu, got %lu", michael@0: size, unsafe.Size()); michael@0: michael@0: char* ptr = mem.get(); michael@0: memcpy(ptr, "Hello!", sizeof("Hello!")); michael@0: michael@0: char* unsafeptr = unsafe.get(); michael@0: memcpy(unsafeptr, "Hello!", sizeof("Hello!")); michael@0: michael@0: Shmem unsafecopy = unsafe; michael@0: if (!SendGive(mem, unsafe, size)) michael@0: fail("can't send Give()"); michael@0: michael@0: // uncomment the following line for a (nondeterministic) surprise! michael@0: //char c1 = *ptr; (void)c1; michael@0: michael@0: // uncomment the following line for a deterministic surprise! michael@0: //char c2 = *mem.get(); (void)c2; michael@0: michael@0: // unsafe shmem gets rid of those checks michael@0: char uc1 = *unsafeptr; (void)uc1; michael@0: char uc2 = *unsafecopy.get(); (void)uc2; michael@0: } michael@0: michael@0: michael@0: bool michael@0: TestShmemParent::RecvTake(Shmem& mem, Shmem& unsafe, michael@0: const size_t& expectedSize) michael@0: { michael@0: if (mem.Size() != expectedSize) michael@0: fail("expected shmem size %lu, but it has size %lu", michael@0: expectedSize, mem.Size()); michael@0: if (unsafe.Size() != expectedSize) michael@0: fail("expected shmem size %lu, but it has size %lu", michael@0: expectedSize, unsafe.Size()); michael@0: michael@0: if (strcmp(mem.get(), "And yourself!")) michael@0: fail("expected message was not written"); michael@0: if (strcmp(unsafe.get(), "And yourself!")) michael@0: fail("expected message was not written"); michael@0: michael@0: if (!DeallocShmem(mem)) michael@0: fail("DeallocShmem"); michael@0: if (!DeallocShmem(unsafe)) michael@0: fail("DeallocShmem"); michael@0: michael@0: Close(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // Child michael@0: michael@0: bool michael@0: TestShmemChild::RecvGive(Shmem& mem, Shmem& unsafe, const size_t& expectedSize) michael@0: { michael@0: if (mem.Size() != expectedSize) michael@0: fail("expected shmem size %lu, but it has size %lu", michael@0: expectedSize, mem.Size()); michael@0: if (unsafe.Size() != expectedSize) michael@0: fail("expected shmem size %lu, but it has size %lu", michael@0: expectedSize, unsafe.Size()); michael@0: michael@0: if (strcmp(mem.get(), "Hello!")) michael@0: fail("expected message was not written"); michael@0: if (strcmp(unsafe.get(), "Hello!")) michael@0: fail("expected message was not written"); michael@0: michael@0: char* unsafeptr = unsafe.get(); michael@0: michael@0: memcpy(mem.get(), "And yourself!", sizeof("And yourself!")); michael@0: memcpy(unsafeptr, "And yourself!", sizeof("And yourself!")); michael@0: michael@0: Shmem unsafecopy = unsafe; michael@0: if (!SendTake(mem, unsafe, expectedSize)) michael@0: fail("can't send Take()"); michael@0: michael@0: // these checks also shouldn't fail in the child michael@0: char uc1 = *unsafeptr; (void)uc1; michael@0: char uc2 = *unsafecopy.get(); (void)uc2; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: } // namespace _ipdltest michael@0: } // namespace mozilla