ipc/ipdl/test/cxx/TestSysVShmem.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 #include "TestSysVShmem.h"
     3 #include "IPDLUnitTests.h"      // fail etc.
     6 namespace mozilla {
     7 namespace _ipdltest {
     9 //-----------------------------------------------------------------------------
    10 // Parent
    12 void
    13 TestSysVShmemParent::Main()
    14 {
    15     Shmem mem;
    16     Shmem unsafe;
    18     size_t size = 12345;
    19     if (!AllocShmem(size, SharedMemory::TYPE_SYSV, &mem))
    20         fail("can't alloc shmem");
    21     if (!AllocUnsafeShmem(size, SharedMemory::TYPE_SYSV, &unsafe))
    22         fail("can't alloc shmem");
    24     if (0 > mem.GetSysVID())
    25         fail("invalid shmem ID");
    26     if (0 > unsafe.GetSysVID())
    27         fail("invalid shmem ID");
    29     if (mem.Size<char>() != size)
    30         fail("shmem is wrong size: expected %lu, got %lu",
    31              size, mem.Size<char>());
    32     if (unsafe.Size<char>() != size)
    33         fail("shmem is wrong size: expected %lu, got %lu",
    34              size, unsafe.Size<char>());
    36     char* ptr = mem.get<char>();
    37     memcpy(ptr, "Hello!", sizeof("Hello!"));
    39     char* unsafeptr = unsafe.get<char>();
    40     memcpy(unsafeptr, "Hello!", sizeof("Hello!"));
    42     Shmem unsafecopy = unsafe;
    43     if (!SendGive(mem, unsafe, size))
    44         fail("can't send Give()");
    46     // uncomment the following line for a (nondeterministic) surprise!
    47     //char c1 = *ptr;  (void)c1;
    49     // uncomment the following line for a deterministic surprise!
    50     //char c2 = *mem.get<char>(); (void)c2;
    52     // unsafe shmem gets rid of those checks
    53     char uc1 = *unsafeptr;  (void)uc1;
    54     char uc2 = *unsafecopy.get<char>(); (void)uc2;
    55 }
    58 bool
    59 TestSysVShmemParent::RecvTake(Shmem& mem, Shmem& unsafe,
    60                           const size_t& expectedSize)
    61 {
    62     if (mem.Size<char>() != expectedSize)
    63         fail("expected shmem size %lu, but it has size %lu",
    64              expectedSize, mem.Size<char>());
    65     if (unsafe.Size<char>() != expectedSize)
    66         fail("expected shmem size %lu, but it has size %lu",
    67              expectedSize, unsafe.Size<char>());
    69     if (strcmp(mem.get<char>(), "And yourself!"))
    70         fail("expected message was not written");
    71     if (strcmp(unsafe.get<char>(), "And yourself!"))
    72         fail("expected message was not written");
    74     if (!DeallocShmem(mem))
    75         fail("DeallocShmem");
    76     if (!DeallocShmem(unsafe))
    77         fail("DeallocShmem");
    79     Close();
    81     return true;
    82 }
    84 //-----------------------------------------------------------------------------
    85 // Child
    87 bool
    88 TestSysVShmemChild::RecvGive(Shmem& mem, Shmem& unsafe, const size_t& expectedSize)
    89 {
    90     if (mem.Size<char>() != expectedSize)
    91         fail("expected shmem size %lu, but it has size %lu",
    92              expectedSize, mem.Size<char>());
    93     if (unsafe.Size<char>() != expectedSize)
    94         fail("expected shmem size %lu, but it has size %lu",
    95              expectedSize, unsafe.Size<char>());
    97     if (strcmp(mem.get<char>(), "Hello!"))
    98         fail("expected message was not written");
    99     if (strcmp(unsafe.get<char>(), "Hello!"))
   100         fail("expected message was not written");
   102     char* unsafeptr = unsafe.get<char>();
   104     memcpy(mem.get<char>(), "And yourself!", sizeof("And yourself!"));
   105     memcpy(unsafeptr, "And yourself!", sizeof("And yourself!"));
   107     Shmem unsafecopy = unsafe;
   108     if (!SendTake(mem, unsafe, expectedSize))
   109         fail("can't send Take()");
   111     // these checks also shouldn't fail in the child
   112     char uc1 = *unsafeptr;  (void)uc1;
   113     char uc2 = *unsafecopy.get<char>(); (void)uc2;
   115     return true;
   116 }
   119 } // namespace _ipdltest
   120 } // namespace mozilla

mercurial