From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- ipc/ipdl/test/cxx/TestShmem.cpp | 115 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 ipc/ipdl/test/cxx/TestShmem.cpp (limited to 'ipc/ipdl/test/cxx/TestShmem.cpp') diff --git a/ipc/ipdl/test/cxx/TestShmem.cpp b/ipc/ipdl/test/cxx/TestShmem.cpp new file mode 100644 index 000000000..1ff62bc3a --- /dev/null +++ b/ipc/ipdl/test/cxx/TestShmem.cpp @@ -0,0 +1,115 @@ +#include "TestShmem.h" + +#include "IPDLUnitTests.h" // fail etc. + + +namespace mozilla { +namespace _ipdltest { + +//----------------------------------------------------------------------------- +// Parent + +void +TestShmemParent::Main() +{ + Shmem mem; + Shmem unsafe; + + size_t size = 12345; + if (!AllocShmem(size, SharedMemory::TYPE_BASIC, &mem)) + fail("can't alloc shmem"); + if (!AllocUnsafeShmem(size, SharedMemory::TYPE_BASIC, &unsafe)) + fail("can't alloc shmem"); + + if (mem.Size() != size) + fail("shmem is wrong size: expected %lu, got %lu", + size, mem.Size()); + if (unsafe.Size() != size) + fail("shmem is wrong size: expected %lu, got %lu", + size, unsafe.Size()); + + char* ptr = mem.get(); + memcpy(ptr, "Hello!", sizeof("Hello!")); + + char* unsafeptr = unsafe.get(); + memcpy(unsafeptr, "Hello!", sizeof("Hello!")); + + Shmem unsafecopy = unsafe; + if (!SendGive(mem, unsafe, size)) + fail("can't send Give()"); + + // uncomment the following line for a (nondeterministic) surprise! + //char c1 = *ptr; (void)c1; + + // uncomment the following line for a deterministic surprise! + //char c2 = *mem.get(); (void)c2; + + // unsafe shmem gets rid of those checks + char uc1 = *unsafeptr; (void)uc1; + char uc2 = *unsafecopy.get(); (void)uc2; +} + + +bool +TestShmemParent::RecvTake(Shmem&& mem, Shmem&& unsafe, + const size_t& expectedSize) +{ + if (mem.Size() != expectedSize) + fail("expected shmem size %lu, but it has size %lu", + expectedSize, mem.Size()); + if (unsafe.Size() != expectedSize) + fail("expected shmem size %lu, but it has size %lu", + expectedSize, unsafe.Size()); + + if (strcmp(mem.get(), "And yourself!")) + fail("expected message was not written"); + if (strcmp(unsafe.get(), "And yourself!")) + fail("expected message was not written"); + + if (!DeallocShmem(mem)) + fail("DeallocShmem"); + if (!DeallocShmem(unsafe)) + fail("DeallocShmem"); + + Close(); + + return true; +} + +//----------------------------------------------------------------------------- +// Child + +bool +TestShmemChild::RecvGive(Shmem&& mem, Shmem&& unsafe, const size_t& expectedSize) +{ + if (mem.Size() != expectedSize) + fail("expected shmem size %lu, but it has size %lu", + expectedSize, mem.Size()); + if (unsafe.Size() != expectedSize) + fail("expected shmem size %lu, but it has size %lu", + expectedSize, unsafe.Size()); + + if (strcmp(mem.get(), "Hello!")) + fail("expected message was not written"); + if (strcmp(unsafe.get(), "Hello!")) + fail("expected message was not written"); + + char* unsafeptr = unsafe.get(); + + memcpy(mem.get(), "And yourself!", sizeof("And yourself!")); + memcpy(unsafeptr, "And yourself!", sizeof("And yourself!")); + + Shmem unsafecopy = unsafe; + if (!SendTake(mem, unsafe, expectedSize)) + fail("can't send Take()"); + + // these checks also shouldn't fail in the child + char uc1 = *unsafeptr; (void)uc1; + char uc2 = *unsafecopy.get(); (void)uc2; + + return true; +} + + +} // namespace _ipdltest +} // namespace mozilla -- cgit v1.2.3