1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/ipdl/test/cxx/TestShmem.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +#include "TestShmem.h" 1.5 + 1.6 +#include "IPDLUnitTests.h" // fail etc. 1.7 + 1.8 + 1.9 +namespace mozilla { 1.10 +namespace _ipdltest { 1.11 + 1.12 +//----------------------------------------------------------------------------- 1.13 +// Parent 1.14 + 1.15 +void 1.16 +TestShmemParent::Main() 1.17 +{ 1.18 + Shmem mem; 1.19 + Shmem unsafe; 1.20 + 1.21 + size_t size = 12345; 1.22 + if (!AllocShmem(size, SharedMemory::TYPE_BASIC, &mem)) 1.23 + fail("can't alloc shmem"); 1.24 + if (!AllocUnsafeShmem(size, SharedMemory::TYPE_BASIC, &unsafe)) 1.25 + fail("can't alloc shmem"); 1.26 + 1.27 + if (mem.Size<char>() != size) 1.28 + fail("shmem is wrong size: expected %lu, got %lu", 1.29 + size, mem.Size<char>()); 1.30 + if (unsafe.Size<char>() != size) 1.31 + fail("shmem is wrong size: expected %lu, got %lu", 1.32 + size, unsafe.Size<char>()); 1.33 + 1.34 + char* ptr = mem.get<char>(); 1.35 + memcpy(ptr, "Hello!", sizeof("Hello!")); 1.36 + 1.37 + char* unsafeptr = unsafe.get<char>(); 1.38 + memcpy(unsafeptr, "Hello!", sizeof("Hello!")); 1.39 + 1.40 + Shmem unsafecopy = unsafe; 1.41 + if (!SendGive(mem, unsafe, size)) 1.42 + fail("can't send Give()"); 1.43 + 1.44 + // uncomment the following line for a (nondeterministic) surprise! 1.45 + //char c1 = *ptr; (void)c1; 1.46 + 1.47 + // uncomment the following line for a deterministic surprise! 1.48 + //char c2 = *mem.get<char>(); (void)c2; 1.49 + 1.50 + // unsafe shmem gets rid of those checks 1.51 + char uc1 = *unsafeptr; (void)uc1; 1.52 + char uc2 = *unsafecopy.get<char>(); (void)uc2; 1.53 +} 1.54 + 1.55 + 1.56 +bool 1.57 +TestShmemParent::RecvTake(Shmem& mem, Shmem& unsafe, 1.58 + const size_t& expectedSize) 1.59 +{ 1.60 + if (mem.Size<char>() != expectedSize) 1.61 + fail("expected shmem size %lu, but it has size %lu", 1.62 + expectedSize, mem.Size<char>()); 1.63 + if (unsafe.Size<char>() != expectedSize) 1.64 + fail("expected shmem size %lu, but it has size %lu", 1.65 + expectedSize, unsafe.Size<char>()); 1.66 + 1.67 + if (strcmp(mem.get<char>(), "And yourself!")) 1.68 + fail("expected message was not written"); 1.69 + if (strcmp(unsafe.get<char>(), "And yourself!")) 1.70 + fail("expected message was not written"); 1.71 + 1.72 + if (!DeallocShmem(mem)) 1.73 + fail("DeallocShmem"); 1.74 + if (!DeallocShmem(unsafe)) 1.75 + fail("DeallocShmem"); 1.76 + 1.77 + Close(); 1.78 + 1.79 + return true; 1.80 +} 1.81 + 1.82 +//----------------------------------------------------------------------------- 1.83 +// Child 1.84 + 1.85 +bool 1.86 +TestShmemChild::RecvGive(Shmem& mem, Shmem& unsafe, const size_t& expectedSize) 1.87 +{ 1.88 + if (mem.Size<char>() != expectedSize) 1.89 + fail("expected shmem size %lu, but it has size %lu", 1.90 + expectedSize, mem.Size<char>()); 1.91 + if (unsafe.Size<char>() != expectedSize) 1.92 + fail("expected shmem size %lu, but it has size %lu", 1.93 + expectedSize, unsafe.Size<char>()); 1.94 + 1.95 + if (strcmp(mem.get<char>(), "Hello!")) 1.96 + fail("expected message was not written"); 1.97 + if (strcmp(unsafe.get<char>(), "Hello!")) 1.98 + fail("expected message was not written"); 1.99 + 1.100 + char* unsafeptr = unsafe.get<char>(); 1.101 + 1.102 + memcpy(mem.get<char>(), "And yourself!", sizeof("And yourself!")); 1.103 + memcpy(unsafeptr, "And yourself!", sizeof("And yourself!")); 1.104 + 1.105 + Shmem unsafecopy = unsafe; 1.106 + if (!SendTake(mem, unsafe, expectedSize)) 1.107 + fail("can't send Take()"); 1.108 + 1.109 + // these checks also shouldn't fail in the child 1.110 + char uc1 = *unsafeptr; (void)uc1; 1.111 + char uc2 = *unsafecopy.get<char>(); (void)uc2; 1.112 + 1.113 + return true; 1.114 +} 1.115 + 1.116 + 1.117 +} // namespace _ipdltest 1.118 +} // namespace mozilla