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