Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #include "TestSysVShmem.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 | TestSysVShmemParent::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_SYSV, &mem)) |
michael@0 | 20 | fail("can't alloc shmem"); |
michael@0 | 21 | if (!AllocUnsafeShmem(size, SharedMemory::TYPE_SYSV, &unsafe)) |
michael@0 | 22 | fail("can't alloc shmem"); |
michael@0 | 23 | |
michael@0 | 24 | if (0 > mem.GetSysVID()) |
michael@0 | 25 | fail("invalid shmem ID"); |
michael@0 | 26 | if (0 > unsafe.GetSysVID()) |
michael@0 | 27 | fail("invalid shmem ID"); |
michael@0 | 28 | |
michael@0 | 29 | if (mem.Size<char>() != size) |
michael@0 | 30 | fail("shmem is wrong size: expected %lu, got %lu", |
michael@0 | 31 | size, mem.Size<char>()); |
michael@0 | 32 | if (unsafe.Size<char>() != size) |
michael@0 | 33 | fail("shmem is wrong size: expected %lu, got %lu", |
michael@0 | 34 | size, unsafe.Size<char>()); |
michael@0 | 35 | |
michael@0 | 36 | char* ptr = mem.get<char>(); |
michael@0 | 37 | memcpy(ptr, "Hello!", sizeof("Hello!")); |
michael@0 | 38 | |
michael@0 | 39 | char* unsafeptr = unsafe.get<char>(); |
michael@0 | 40 | memcpy(unsafeptr, "Hello!", sizeof("Hello!")); |
michael@0 | 41 | |
michael@0 | 42 | Shmem unsafecopy = unsafe; |
michael@0 | 43 | if (!SendGive(mem, unsafe, size)) |
michael@0 | 44 | fail("can't send Give()"); |
michael@0 | 45 | |
michael@0 | 46 | // uncomment the following line for a (nondeterministic) surprise! |
michael@0 | 47 | //char c1 = *ptr; (void)c1; |
michael@0 | 48 | |
michael@0 | 49 | // uncomment the following line for a deterministic surprise! |
michael@0 | 50 | //char c2 = *mem.get<char>(); (void)c2; |
michael@0 | 51 | |
michael@0 | 52 | // unsafe shmem gets rid of those checks |
michael@0 | 53 | char uc1 = *unsafeptr; (void)uc1; |
michael@0 | 54 | char uc2 = *unsafecopy.get<char>(); (void)uc2; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | bool |
michael@0 | 59 | TestSysVShmemParent::RecvTake(Shmem& mem, Shmem& unsafe, |
michael@0 | 60 | const size_t& expectedSize) |
michael@0 | 61 | { |
michael@0 | 62 | if (mem.Size<char>() != expectedSize) |
michael@0 | 63 | fail("expected shmem size %lu, but it has size %lu", |
michael@0 | 64 | expectedSize, mem.Size<char>()); |
michael@0 | 65 | if (unsafe.Size<char>() != expectedSize) |
michael@0 | 66 | fail("expected shmem size %lu, but it has size %lu", |
michael@0 | 67 | expectedSize, unsafe.Size<char>()); |
michael@0 | 68 | |
michael@0 | 69 | if (strcmp(mem.get<char>(), "And yourself!")) |
michael@0 | 70 | fail("expected message was not written"); |
michael@0 | 71 | if (strcmp(unsafe.get<char>(), "And yourself!")) |
michael@0 | 72 | fail("expected message was not written"); |
michael@0 | 73 | |
michael@0 | 74 | if (!DeallocShmem(mem)) |
michael@0 | 75 | fail("DeallocShmem"); |
michael@0 | 76 | if (!DeallocShmem(unsafe)) |
michael@0 | 77 | fail("DeallocShmem"); |
michael@0 | 78 | |
michael@0 | 79 | Close(); |
michael@0 | 80 | |
michael@0 | 81 | return true; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | //----------------------------------------------------------------------------- |
michael@0 | 85 | // Child |
michael@0 | 86 | |
michael@0 | 87 | bool |
michael@0 | 88 | TestSysVShmemChild::RecvGive(Shmem& mem, Shmem& unsafe, const size_t& expectedSize) |
michael@0 | 89 | { |
michael@0 | 90 | if (mem.Size<char>() != expectedSize) |
michael@0 | 91 | fail("expected shmem size %lu, but it has size %lu", |
michael@0 | 92 | expectedSize, mem.Size<char>()); |
michael@0 | 93 | if (unsafe.Size<char>() != expectedSize) |
michael@0 | 94 | fail("expected shmem size %lu, but it has size %lu", |
michael@0 | 95 | expectedSize, unsafe.Size<char>()); |
michael@0 | 96 | |
michael@0 | 97 | if (strcmp(mem.get<char>(), "Hello!")) |
michael@0 | 98 | fail("expected message was not written"); |
michael@0 | 99 | if (strcmp(unsafe.get<char>(), "Hello!")) |
michael@0 | 100 | fail("expected message was not written"); |
michael@0 | 101 | |
michael@0 | 102 | char* unsafeptr = unsafe.get<char>(); |
michael@0 | 103 | |
michael@0 | 104 | memcpy(mem.get<char>(), "And yourself!", sizeof("And yourself!")); |
michael@0 | 105 | memcpy(unsafeptr, "And yourself!", sizeof("And yourself!")); |
michael@0 | 106 | |
michael@0 | 107 | Shmem unsafecopy = unsafe; |
michael@0 | 108 | if (!SendTake(mem, unsafe, expectedSize)) |
michael@0 | 109 | fail("can't send Take()"); |
michael@0 | 110 | |
michael@0 | 111 | // these checks also shouldn't fail in the child |
michael@0 | 112 | char uc1 = *unsafeptr; (void)uc1; |
michael@0 | 113 | char uc2 = *unsafecopy.get<char>(); (void)uc2; |
michael@0 | 114 | |
michael@0 | 115 | return true; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | } // namespace _ipdltest |
michael@0 | 120 | } // namespace mozilla |