summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestShmem.cpp
blob: 0a778e53227e32b62f10bd72dca88962f865889f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "TestShmem.h"

#include "IPDLUnitTests.h"  // fail etc.

namespace mozilla {
namespace _ipdltest {

//-----------------------------------------------------------------------------
// Parent

void TestShmemParent::Main() {
  Shmem mem;
  Shmem unsafe;

  size_t size = 12345;
  if (!AllocShmem(size, &mem)) fail("can't alloc shmem");
  if (!AllocUnsafeShmem(size, &unsafe)) fail("can't alloc shmem");

  if (mem.Size<char>() != size)
    fail("shmem is wrong size: expected %lu, got %lu", size, mem.Size<char>());
  if (unsafe.Size<char>() != size)
    fail("shmem is wrong size: expected %lu, got %lu", size,
         unsafe.Size<char>());

  char* ptr = mem.get<char>();
  memcpy(ptr, "Hello!", sizeof("Hello!"));

  char* unsafeptr = unsafe.get<char>();
  memcpy(unsafeptr, "Hello!", sizeof("Hello!"));

  Shmem unsafecopy = unsafe;
  if (!SendGive(std::move(mem), std::move(unsafe), size))
    fail("can't send Give()");

  // uncomment the following line for a (nondeterministic) surprise!
  // char c1 = *ptr;  (void)c1;

  // uncomment the following line for a deterministic surprise!
  // char c2 = *mem.get<char>(); (void)c2;

  // unsafe shmem gets rid of those checks
  char uc1 = *unsafeptr;
  (void)uc1;
  char uc2 = *unsafecopy.get<char>();
  (void)uc2;
}

mozilla::ipc::IPCResult TestShmemParent::RecvTake(Shmem&& mem, Shmem&& unsafe,
                                                  const size_t& expectedSize) {
  if (mem.Size<char>() != expectedSize)
    fail("expected shmem size %lu, but it has size %lu", expectedSize,
         mem.Size<char>());
  if (unsafe.Size<char>() != expectedSize)
    fail("expected shmem size %lu, but it has size %lu", expectedSize,
         unsafe.Size<char>());

  if (strcmp(mem.get<char>(), "And yourself!"))
    fail("expected message was not written");
  if (strcmp(unsafe.get<char>(), "And yourself!"))
    fail("expected message was not written");

  if (!DeallocShmem(mem)) fail("DeallocShmem");
  if (!DeallocShmem(unsafe)) fail("DeallocShmem");

  Close();

  return IPC_OK();
}

//-----------------------------------------------------------------------------
// Child

mozilla::ipc::IPCResult TestShmemChild::RecvGive(Shmem&& mem, Shmem&& unsafe,
                                                 const size_t& expectedSize) {
  if (mem.Size<char>() != expectedSize)
    fail("expected shmem size %lu, but it has size %lu", expectedSize,
         mem.Size<char>());
  if (unsafe.Size<char>() != expectedSize)
    fail("expected shmem size %lu, but it has size %lu", expectedSize,
         unsafe.Size<char>());

  if (strcmp(mem.get<char>(), "Hello!"))
    fail("expected message was not written");
  if (strcmp(unsafe.get<char>(), "Hello!"))
    fail("expected message was not written");

  char* unsafeptr = unsafe.get<char>();

  memcpy(mem.get<char>(), "And yourself!", sizeof("And yourself!"));
  memcpy(unsafeptr, "And yourself!", sizeof("And yourself!"));

  Shmem unsafecopy = unsafe;
  if (!SendTake(std::move(mem), std::move(unsafe), expectedSize))
    fail("can't send Take()");

  // these checks also shouldn't fail in the child
  char uc1 = *unsafeptr;
  (void)uc1;
  char uc2 = *unsafecopy.get<char>();
  (void)uc2;

  return IPC_OK();
}

}  // namespace _ipdltest
}  // namespace mozilla