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