diff --git a/ipc/ipdl/test/cxx/Makefile.in b/ipc/ipdl/test/cxx/Makefile.in index fc74ff325a88..30c588475647 100644 --- a/ipc/ipdl/test/cxx/Makefile.in +++ b/ipc/ipdl/test/cxx/Makefile.in @@ -78,6 +78,10 @@ IPDLTESTS = \ TestSyncWakeup \ $(NULL) +ifeq ($(OS_ARCH),Linux) +IPDLTESTS += TestSysVShmem +endif + IPDLTESTSRCS = $(addsuffix .cpp,$(IPDLTESTS)) IPDLTESTHDRS = $(addprefix $(srcdir)/,$(addsuffix .h,$(IPDLTESTS))) diff --git a/ipc/ipdl/test/cxx/PTestSysVShmem.ipdl b/ipc/ipdl/test/cxx/PTestSysVShmem.ipdl new file mode 100644 index 000000000000..e51acf42849b --- /dev/null +++ b/ipc/ipdl/test/cxx/PTestSysVShmem.ipdl @@ -0,0 +1,22 @@ +namespace mozilla { +namespace _ipdltest { + +protocol PTestSysVShmem { +child: + Give(Shmem mem, size_t expectedSize); + +parent: + Take(Shmem mem, size_t expectedSize); + __delete__(); + + +state GIVING: + send Give goto TAKING; + +state TAKING: + recv Take goto TAKING; + recv __delete__; +}; + +} +} diff --git a/ipc/ipdl/test/cxx/TestSysVShmem.cpp b/ipc/ipdl/test/cxx/TestSysVShmem.cpp new file mode 100644 index 000000000000..90d95eef8237 --- /dev/null +++ b/ipc/ipdl/test/cxx/TestSysVShmem.cpp @@ -0,0 +1,75 @@ +#include "TestSysVShmem.h" + +#include "IPDLUnitTests.h" // fail etc. + + +namespace mozilla { +namespace _ipdltest { + +//----------------------------------------------------------------------------- +// Parent + +void +TestSysVShmemParent::Main() +{ + Shmem mem; + size_t size = 12345; + if (!AllocShmem(size, SharedMemory::TYPE_SYSV, &mem)) + fail("can't alloc shmem"); + + if (mem.Size() != size) + fail("shmem is wrong size: expected %lu, got %lu", + size, mem.Size()); + + char* ptr = mem.get(); + memcpy(ptr, "Hello!", sizeof("Hello!")); + if (!SendGive(mem, 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; +} + + +bool +TestSysVShmemParent::RecvTake(Shmem& mem, const size_t& expectedSize) +{ + if (mem.Size() != expectedSize) + fail("expected shmem size %lu, but it has size %lu", + expectedSize, mem.Size()); + + if (strcmp(mem.get(), "And yourself!")) + fail("expected message was not written"); + + Close(); + + return true; +} + +//----------------------------------------------------------------------------- +// Child + +bool +TestSysVShmemChild::RecvGive(Shmem& mem, const size_t& expectedSize) +{ + if (mem.Size() != expectedSize) + fail("expected shmem size %lu, but it has size %lu", + expectedSize, mem.Size()); + + if (strcmp(mem.get(), "Hello!")) + fail("expected message was not written"); + + memcpy(mem.get(), "And yourself!", sizeof("And yourself!")); + + if (!SendTake(mem, expectedSize)) + fail("can't send Take()"); + + return true; +} + + +} // namespace _ipdltest +} // namespace mozilla diff --git a/ipc/ipdl/test/cxx/TestSysVShmem.h b/ipc/ipdl/test/cxx/TestSysVShmem.h new file mode 100644 index 000000000000..ce98d6d0a06f --- /dev/null +++ b/ipc/ipdl/test/cxx/TestSysVShmem.h @@ -0,0 +1,65 @@ +#ifndef mozilla__ipdltest_TestSysVShmem_h +#define mozilla__ipdltest_TestSysVShmem_h + +#include "mozilla/_ipdltest/IPDLUnitTests.h" + +#include "mozilla/_ipdltest/PTestSysVShmemParent.h" +#include "mozilla/_ipdltest/PTestSysVShmemChild.h" + +namespace mozilla { +namespace _ipdltest { + + +class TestSysVShmemParent : + public PTestSysVShmemParent +{ +public: + TestSysVShmemParent() { } + virtual ~TestSysVShmemParent() { } + + void Main(); + +protected: + NS_OVERRIDE + virtual bool RecvTake( + Shmem& mem, + const size_t& expectedSize); + + NS_OVERRIDE + virtual void ActorDestroy(ActorDestroyReason why) + { + if (NormalShutdown != why) + fail("unexpected destruction!"); + passed("ok"); + QuitParent(); + } +}; + + +class TestSysVShmemChild : + public PTestSysVShmemChild +{ +public: + TestSysVShmemChild() { } + virtual ~TestSysVShmemChild() { } + +protected: + NS_OVERRIDE + virtual bool RecvGive( + Shmem& mem, + const size_t& expectedSize); + + NS_OVERRIDE + virtual void ActorDestroy(ActorDestroyReason why) + { + if (NormalShutdown != why) + fail("unexpected destruction!"); + QuitChild(); + } +}; + + +} // namespace _ipdltest +} // namespace mozilla + +#endif // ifndef mozilla__ipdltest_TestSysVShmem_h diff --git a/ipc/ipdl/test/cxx/ipdl.mk b/ipc/ipdl/test/cxx/ipdl.mk index 1ab038fb440c..aaf3118e3b13 100644 --- a/ipc/ipdl/test/cxx/ipdl.mk +++ b/ipc/ipdl/test/cxx/ipdl.mk @@ -26,4 +26,5 @@ IPDLSRCS = \ PTestShutdownSubsub.ipdl \ PTestStackHooks.ipdl \ PTestSyncWakeup.ipdl \ + PTestSysVShmem.ipdl \ $(NULL)