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