ipc/ipdl/test/cxx/TestShmem.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #include "TestShmem.h"
     3 #include "IPDLUnitTests.h"      // fail etc.
     6 namespace mozilla {
     7 namespace _ipdltest {
     9 //-----------------------------------------------------------------------------
    10 // Parent
    12 void
    13 TestShmemParent::Main()
    14 {
    15     Shmem mem;
    16     Shmem unsafe;
    18     size_t size = 12345;
    19     if (!AllocShmem(size, SharedMemory::TYPE_BASIC, &mem))
    20         fail("can't alloc shmem");
    21     if (!AllocUnsafeShmem(size, SharedMemory::TYPE_BASIC, &unsafe))
    22         fail("can't alloc shmem");
    24     if (mem.Size<char>() != size)
    25         fail("shmem is wrong size: expected %lu, got %lu",
    26              size, mem.Size<char>());
    27     if (unsafe.Size<char>() != size)
    28         fail("shmem is wrong size: expected %lu, got %lu",
    29              size, unsafe.Size<char>());
    31     char* ptr = mem.get<char>();
    32     memcpy(ptr, "Hello!", sizeof("Hello!"));
    34     char* unsafeptr = unsafe.get<char>();
    35     memcpy(unsafeptr, "Hello!", sizeof("Hello!"));
    37     Shmem unsafecopy = unsafe;
    38     if (!SendGive(mem, unsafe, size))
    39         fail("can't send Give()");
    41     // uncomment the following line for a (nondeterministic) surprise!
    42     //char c1 = *ptr;  (void)c1;
    44     // uncomment the following line for a deterministic surprise!
    45     //char c2 = *mem.get<char>(); (void)c2;
    47     // unsafe shmem gets rid of those checks
    48     char uc1 = *unsafeptr;  (void)uc1;
    49     char uc2 = *unsafecopy.get<char>(); (void)uc2;
    50 }
    53 bool
    54 TestShmemParent::RecvTake(Shmem& mem, Shmem& unsafe,
    55                           const size_t& expectedSize)
    56 {
    57     if (mem.Size<char>() != expectedSize)
    58         fail("expected shmem size %lu, but it has size %lu",
    59              expectedSize, mem.Size<char>());
    60     if (unsafe.Size<char>() != expectedSize)
    61         fail("expected shmem size %lu, but it has size %lu",
    62              expectedSize, unsafe.Size<char>());
    64     if (strcmp(mem.get<char>(), "And yourself!"))
    65         fail("expected message was not written");
    66     if (strcmp(unsafe.get<char>(), "And yourself!"))
    67         fail("expected message was not written");
    69     if (!DeallocShmem(mem))
    70         fail("DeallocShmem");
    71     if (!DeallocShmem(unsafe))
    72         fail("DeallocShmem");
    74     Close();
    76     return true;
    77 }
    79 //-----------------------------------------------------------------------------
    80 // Child
    82 bool
    83 TestShmemChild::RecvGive(Shmem& mem, Shmem& unsafe, const size_t& expectedSize)
    84 {
    85     if (mem.Size<char>() != expectedSize)
    86         fail("expected shmem size %lu, but it has size %lu",
    87              expectedSize, mem.Size<char>());
    88     if (unsafe.Size<char>() != expectedSize)
    89         fail("expected shmem size %lu, but it has size %lu",
    90              expectedSize, unsafe.Size<char>());
    92     if (strcmp(mem.get<char>(), "Hello!"))
    93         fail("expected message was not written");
    94     if (strcmp(unsafe.get<char>(), "Hello!"))
    95         fail("expected message was not written");
    97     char* unsafeptr = unsafe.get<char>();
    99     memcpy(mem.get<char>(), "And yourself!", sizeof("And yourself!"));
   100     memcpy(unsafeptr, "And yourself!", sizeof("And yourself!"));
   102     Shmem unsafecopy = unsafe;
   103     if (!SendTake(mem, unsafe, expectedSize))
   104         fail("can't send Take()");
   106     // these checks also shouldn't fail in the child
   107     char uc1 = *unsafeptr;  (void)uc1;
   108     char uc2 = *unsafecopy.get<char>(); (void)uc2;
   110     return true;
   111 }
   114 } // namespace _ipdltest
   115 } // namespace mozilla

mercurial