ipc/ipdl/test/cxx/TestShmem.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

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

mercurial