gecko/ipc/ipdl/test/cxx/TestSysVShmem.cpp

121 lines
3.4 KiB
C++
Raw Normal View History

2010-04-15 22:29:17 -07:00
#include "TestSysVShmem.h"
#include "IPDLUnitTests.h" // fail etc.
namespace mozilla {
namespace _ipdltest {
//-----------------------------------------------------------------------------
// Parent
void
TestSysVShmemParent::Main()
{
Shmem mem;
2010-11-05 00:17:07 -07:00
Shmem unsafe;
2010-04-15 22:29:17 -07:00
size_t size = 12345;
if (!AllocShmem(size, SharedMemory::TYPE_SYSV, &mem))
fail("can't alloc shmem");
2010-11-05 00:17:07 -07:00
if (!AllocUnsafeShmem(size, SharedMemory::TYPE_SYSV, &unsafe))
fail("can't alloc shmem");
2010-04-15 22:29:17 -07:00
if (0 > mem.GetSysVID())
fail("invalid shmem ID");
2010-11-05 00:17:07 -07:00
if (0 > unsafe.GetSysVID())
fail("invalid shmem ID");
2010-04-15 22:29:17 -07:00
if (mem.Size<char>() != size)
fail("shmem is wrong size: expected %lu, got %lu",
size, mem.Size<char>());
2010-11-05 00:17:07 -07:00
if (unsafe.Size<char>() != size)
fail("shmem is wrong size: expected %lu, got %lu",
size, unsafe.Size<char>());
2010-04-15 22:29:17 -07:00
char* ptr = mem.get<char>();
memcpy(ptr, "Hello!", sizeof("Hello!"));
2010-11-05 00:17:07 -07:00
char* unsafeptr = unsafe.get<char>();
memcpy(unsafeptr, "Hello!", sizeof("Hello!"));
Shmem unsafecopy = unsafe;
if (!SendGive(mem, unsafe, size))
2010-04-15 22:29:17 -07:00
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<char>(); (void)c2;
2010-11-05 00:17:07 -07:00
// unsafe shmem gets rid of those checks
char uc1 = *unsafeptr; (void)uc1;
char uc2 = *unsafecopy.get<char>(); (void)uc2;
2010-04-15 22:29:17 -07:00
}
bool
2010-11-05 00:17:07 -07:00
TestSysVShmemParent::RecvTake(Shmem& mem, Shmem& unsafe,
const size_t& expectedSize)
2010-04-15 22:29:17 -07:00
{
if (mem.Size<char>() != expectedSize)
fail("expected shmem size %lu, but it has size %lu",
expectedSize, mem.Size<char>());
2010-11-05 00:17:07 -07:00
if (unsafe.Size<char>() != expectedSize)
fail("expected shmem size %lu, but it has size %lu",
expectedSize, unsafe.Size<char>());
2010-04-15 22:29:17 -07:00
if (strcmp(mem.get<char>(), "And yourself!"))
fail("expected message was not written");
2010-11-05 00:17:07 -07:00
if (strcmp(unsafe.get<char>(), "And yourself!"))
fail("expected message was not written");
2010-04-15 22:29:17 -07:00
if (!DeallocShmem(mem))
fail("DeallocShmem");
2010-11-05 00:17:07 -07:00
if (!DeallocShmem(unsafe))
fail("DeallocShmem");
2010-04-15 22:29:17 -07:00
Close();
return true;
}
//-----------------------------------------------------------------------------
// Child
bool
2010-11-05 00:17:07 -07:00
TestSysVShmemChild::RecvGive(Shmem& mem, Shmem& unsafe, const size_t& expectedSize)
2010-04-15 22:29:17 -07:00
{
if (mem.Size<char>() != expectedSize)
fail("expected shmem size %lu, but it has size %lu",
expectedSize, mem.Size<char>());
2010-11-05 00:17:07 -07:00
if (unsafe.Size<char>() != expectedSize)
fail("expected shmem size %lu, but it has size %lu",
expectedSize, unsafe.Size<char>());
2010-04-15 22:29:17 -07:00
if (strcmp(mem.get<char>(), "Hello!"))
fail("expected message was not written");
2010-11-05 00:17:07 -07:00
if (strcmp(unsafe.get<char>(), "Hello!"))
fail("expected message was not written");
char* unsafeptr = unsafe.get<char>();
2010-04-15 22:29:17 -07:00
memcpy(mem.get<char>(), "And yourself!", sizeof("And yourself!"));
2010-11-05 00:17:07 -07:00
memcpy(unsafeptr, "And yourself!", sizeof("And yourself!"));
2010-04-15 22:29:17 -07:00
2010-11-05 00:17:07 -07:00
Shmem unsafecopy = unsafe;
if (!SendTake(mem, unsafe, expectedSize))
2010-04-15 22:29:17 -07:00
fail("can't send Take()");
2010-11-05 00:17:07 -07:00
// these checks also shouldn't fail in the child
char uc1 = *unsafeptr; (void)uc1;
char uc2 = *unsafecopy.get<char>(); (void)uc2;
2010-04-15 22:29:17 -07:00
return true;
}
} // namespace _ipdltest
} // namespace mozilla