Add ICU message format support
[chromium-blink-merge.git] / sandbox / win / src / ipc_unittest.cc
blob0dc957184b0beb68d3f9dc0acd88021c5e8cd8e9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "sandbox/win/src/crosscall_client.h"
7 #include "sandbox/win/src/crosscall_server.h"
8 #include "sandbox/win/src/sharedmem_ipc_client.h"
9 #include "sandbox/win/src/sharedmem_ipc_server.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace sandbox {
14 // Helper function to make the fake shared memory with some
15 // basic elements initialized.
16 IPCControl* MakeChannels(size_t channel_size, size_t total_shared_size,
17 size_t* base_start) {
18 // Allocate memory
19 char* mem = new char[total_shared_size];
20 memset(mem, 0, total_shared_size);
21 // Calculate how many channels we can fit in the shared memory.
22 total_shared_size -= offsetof(IPCControl, channels);
23 size_t channel_count =
24 total_shared_size / (sizeof(ChannelControl) + channel_size);
25 // Calculate the start of the first channel.
26 *base_start = (sizeof(ChannelControl)* channel_count) +
27 offsetof(IPCControl, channels);
28 // Setup client structure.
29 IPCControl* client_control = reinterpret_cast<IPCControl*>(mem);
30 client_control->channels_count = channel_count;
31 return client_control;
34 enum TestFixMode {
35 FIX_NO_EVENTS,
36 FIX_PONG_READY,
37 FIX_PONG_NOT_READY
40 void FixChannels(IPCControl* client_control, size_t base_start,
41 size_t channel_size, TestFixMode mode) {
42 for (size_t ix = 0; ix != client_control->channels_count; ++ix) {
43 ChannelControl& channel = client_control->channels[ix];
44 channel.channel_base = base_start;
45 channel.state = kFreeChannel;
46 if (mode != FIX_NO_EVENTS) {
47 BOOL signaled = (FIX_PONG_READY == mode)? TRUE : FALSE;
48 channel.ping_event = ::CreateEventW(NULL, FALSE, FALSE, NULL);
49 channel.pong_event = ::CreateEventW(NULL, FALSE, signaled, NULL);
51 base_start += channel_size;
55 void CloseChannelEvents(IPCControl* client_control) {
56 for (size_t ix = 0; ix != client_control->channels_count; ++ix) {
57 ChannelControl& channel = client_control->channels[ix];
58 ::CloseHandle(channel.ping_event);
59 ::CloseHandle(channel.pong_event);
63 TEST(IPCTest, ChannelMaker) {
64 // Test that our testing rig is computing offsets properly. We should have
65 // 5 channnels and the offset to the first channel is 108 bytes in 32 bits
66 // and 216 in 64 bits.
67 size_t channel_start = 0;
68 IPCControl* client_control = MakeChannels(12 * 64, 4096, &channel_start);
69 ASSERT_TRUE(NULL != client_control);
70 EXPECT_EQ(5, client_control->channels_count);
71 #if defined(_WIN64)
72 EXPECT_EQ(216, channel_start);
73 #else
74 EXPECT_EQ(108, channel_start);
75 #endif
76 delete[] reinterpret_cast<char*>(client_control);
79 TEST(IPCTest, ClientLockUnlock) {
80 // Make 7 channels of kIPCChannelSize (1kb) each. Test that we lock and
81 // unlock channels properly.
82 size_t base_start = 0;
83 IPCControl* client_control =
84 MakeChannels(kIPCChannelSize, 4096 * 2, &base_start);
85 FixChannels(client_control, base_start, kIPCChannelSize, FIX_NO_EVENTS);
87 char* mem = reinterpret_cast<char*>(client_control);
88 SharedMemIPCClient client(mem);
90 // Test that we lock the first 3 channels in sequence.
91 void* buff0 = client.GetBuffer();
92 EXPECT_TRUE(mem + client_control->channels[0].channel_base == buff0);
93 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
94 EXPECT_EQ(kFreeChannel, client_control->channels[1].state);
95 EXPECT_EQ(kFreeChannel, client_control->channels[2].state);
96 EXPECT_EQ(kFreeChannel, client_control->channels[3].state);
97 EXPECT_EQ(kFreeChannel, client_control->channels[4].state);
98 EXPECT_EQ(kFreeChannel, client_control->channels[5].state);
100 void* buff1 = client.GetBuffer();
101 EXPECT_TRUE(mem + client_control->channels[1].channel_base == buff1);
102 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
103 EXPECT_EQ(kBusyChannel, client_control->channels[1].state);
104 EXPECT_EQ(kFreeChannel, client_control->channels[2].state);
105 EXPECT_EQ(kFreeChannel, client_control->channels[3].state);
106 EXPECT_EQ(kFreeChannel, client_control->channels[4].state);
107 EXPECT_EQ(kFreeChannel, client_control->channels[5].state);
109 void* buff2 = client.GetBuffer();
110 EXPECT_TRUE(mem + client_control->channels[2].channel_base == buff2);
111 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
112 EXPECT_EQ(kBusyChannel, client_control->channels[1].state);
113 EXPECT_EQ(kBusyChannel, client_control->channels[2].state);
114 EXPECT_EQ(kFreeChannel, client_control->channels[3].state);
115 EXPECT_EQ(kFreeChannel, client_control->channels[4].state);
116 EXPECT_EQ(kFreeChannel, client_control->channels[5].state);
118 // Test that we unlock and re-lock the right channel.
119 client.FreeBuffer(buff1);
120 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
121 EXPECT_EQ(kFreeChannel, client_control->channels[1].state);
122 EXPECT_EQ(kBusyChannel, client_control->channels[2].state);
123 EXPECT_EQ(kFreeChannel, client_control->channels[3].state);
124 EXPECT_EQ(kFreeChannel, client_control->channels[4].state);
125 EXPECT_EQ(kFreeChannel, client_control->channels[5].state);
127 void* buff2b = client.GetBuffer();
128 EXPECT_TRUE(mem + client_control->channels[1].channel_base == buff2b);
129 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
130 EXPECT_EQ(kBusyChannel, client_control->channels[1].state);
131 EXPECT_EQ(kBusyChannel, client_control->channels[2].state);
132 EXPECT_EQ(kFreeChannel, client_control->channels[3].state);
133 EXPECT_EQ(kFreeChannel, client_control->channels[4].state);
134 EXPECT_EQ(kFreeChannel, client_control->channels[5].state);
136 client.FreeBuffer(buff0);
137 EXPECT_EQ(kFreeChannel, client_control->channels[0].state);
138 EXPECT_EQ(kBusyChannel, client_control->channels[1].state);
139 EXPECT_EQ(kBusyChannel, client_control->channels[2].state);
140 EXPECT_EQ(kFreeChannel, client_control->channels[3].state);
141 EXPECT_EQ(kFreeChannel, client_control->channels[4].state);
142 EXPECT_EQ(kFreeChannel, client_control->channels[5].state);
144 delete[] reinterpret_cast<char*>(client_control);
147 TEST(IPCTest, CrossCallStrPacking) {
148 // This test tries the CrossCall object with null and non-null string
149 // combination of parameters, integer types and verifies that the unpacker
150 // can read them properly.
151 size_t base_start = 0;
152 IPCControl* client_control =
153 MakeChannels(kIPCChannelSize, 4096 * 4, &base_start);
154 client_control->server_alive = HANDLE(1);
155 FixChannels(client_control, base_start, kIPCChannelSize, FIX_PONG_READY);
157 char* mem = reinterpret_cast<char*>(client_control);
158 SharedMemIPCClient client(mem);
160 CrossCallReturn answer;
161 uint32 tag1 = 666;
162 const wchar_t *text = L"98765 - 43210";
163 base::string16 copied_text;
164 CrossCallParamsEx* actual_params;
166 CrossCall(client, tag1, text, &answer);
167 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
168 EXPECT_EQ(1, actual_params->GetParamsCount());
169 EXPECT_EQ(tag1, actual_params->GetTag());
170 EXPECT_TRUE(actual_params->GetParameterStr(0, &copied_text));
171 EXPECT_STREQ(text, copied_text.c_str());
173 // Check with an empty string.
174 uint32 tag2 = 777;
175 const wchar_t* null_text = NULL;
176 CrossCall(client, tag2, null_text, &answer);
177 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
178 EXPECT_EQ(1, actual_params->GetParamsCount());
179 EXPECT_EQ(tag2, actual_params->GetTag());
180 uint32 param_size = 1;
181 ArgType type = INVALID_TYPE;
182 void* param_addr = actual_params->GetRawParameter(0, &param_size, &type);
183 EXPECT_TRUE(NULL != param_addr);
184 EXPECT_EQ(0, param_size);
185 EXPECT_EQ(WCHAR_TYPE, type);
186 EXPECT_TRUE(actual_params->GetParameterStr(0, &copied_text));
188 uint32 tag3 = 888;
189 param_size = 1;
190 copied_text.clear();
192 // Check with an empty string and a non-empty string.
193 CrossCall(client, tag3, null_text, text, &answer);
194 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
195 EXPECT_EQ(2, actual_params->GetParamsCount());
196 EXPECT_EQ(tag3, actual_params->GetTag());
197 type = INVALID_TYPE;
198 param_addr = actual_params->GetRawParameter(0, &param_size, &type);
199 EXPECT_TRUE(NULL != param_addr);
200 EXPECT_EQ(0, param_size);
201 EXPECT_EQ(WCHAR_TYPE, type);
202 EXPECT_TRUE(actual_params->GetParameterStr(0, &copied_text));
203 EXPECT_TRUE(actual_params->GetParameterStr(1, &copied_text));
204 EXPECT_STREQ(text, copied_text.c_str());
206 param_size = 1;
207 base::string16 copied_text_p0, copied_text_p2;
209 const wchar_t *text2 = L"AeFG";
210 CrossCall(client, tag1, text2, null_text, text, &answer);
211 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
212 EXPECT_EQ(3, actual_params->GetParamsCount());
213 EXPECT_EQ(tag1, actual_params->GetTag());
214 EXPECT_TRUE(actual_params->GetParameterStr(0, &copied_text_p0));
215 EXPECT_STREQ(text2, copied_text_p0.c_str());
216 EXPECT_TRUE(actual_params->GetParameterStr(2, &copied_text_p2));
217 EXPECT_STREQ(text, copied_text_p2.c_str());
218 type = INVALID_TYPE;
219 param_addr = actual_params->GetRawParameter(1, &param_size, &type);
220 EXPECT_TRUE(NULL != param_addr);
221 EXPECT_EQ(0, param_size);
222 EXPECT_EQ(WCHAR_TYPE, type);
224 CloseChannelEvents(client_control);
225 delete[] reinterpret_cast<char*>(client_control);
228 TEST(IPCTest, CrossCallIntPacking) {
229 // Check handling for regular 32 bit integers used in Windows.
230 size_t base_start = 0;
231 IPCControl* client_control =
232 MakeChannels(kIPCChannelSize, 4096 * 4, &base_start);
233 client_control->server_alive = HANDLE(1);
234 FixChannels(client_control, base_start, kIPCChannelSize, FIX_PONG_READY);
236 uint32 tag1 = 999;
237 uint32 tag2 = 111;
238 const wchar_t *text = L"godzilla";
239 CrossCallParamsEx* actual_params;
241 char* mem = reinterpret_cast<char*>(client_control);
242 SharedMemIPCClient client(mem);
244 CrossCallReturn answer;
245 DWORD dw = 0xE6578;
246 CrossCall(client, tag2, dw, &answer);
247 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
248 EXPECT_EQ(1, actual_params->GetParamsCount());
249 EXPECT_EQ(tag2, actual_params->GetTag());
250 ArgType type = INVALID_TYPE;
251 uint32 param_size = 1;
252 void* param_addr = actual_params->GetRawParameter(0, &param_size, &type);
253 ASSERT_EQ(sizeof(dw), param_size);
254 EXPECT_EQ(UINT32_TYPE, type);
255 ASSERT_TRUE(NULL != param_addr);
256 EXPECT_EQ(0, memcmp(&dw, param_addr, param_size));
258 // Check handling for windows HANDLES.
259 HANDLE h = HANDLE(0x70000500);
260 CrossCall(client, tag1, text, h, &answer);
261 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
262 EXPECT_EQ(2, actual_params->GetParamsCount());
263 EXPECT_EQ(tag1, actual_params->GetTag());
264 type = INVALID_TYPE;
265 param_addr = actual_params->GetRawParameter(1, &param_size, &type);
266 ASSERT_EQ(sizeof(h), param_size);
267 EXPECT_EQ(VOIDPTR_TYPE, type);
268 ASSERT_TRUE(NULL != param_addr);
269 EXPECT_EQ(0, memcmp(&h, param_addr, param_size));
271 // Check combination of 32 and 64 bits.
272 CrossCall(client, tag2, h, dw, h, &answer);
273 actual_params = reinterpret_cast<CrossCallParamsEx*>(client.GetBuffer());
274 EXPECT_EQ(3, actual_params->GetParamsCount());
275 EXPECT_EQ(tag2, actual_params->GetTag());
276 type = INVALID_TYPE;
277 param_addr = actual_params->GetRawParameter(0, &param_size, &type);
278 ASSERT_EQ(sizeof(h), param_size);
279 EXPECT_EQ(VOIDPTR_TYPE, type);
280 ASSERT_TRUE(NULL != param_addr);
281 EXPECT_EQ(0, memcmp(&h, param_addr, param_size));
282 type = INVALID_TYPE;
283 param_addr = actual_params->GetRawParameter(1, &param_size, &type);
284 ASSERT_EQ(sizeof(dw), param_size);
285 EXPECT_EQ(UINT32_TYPE, type);
286 ASSERT_TRUE(NULL != param_addr);
287 EXPECT_EQ(0, memcmp(&dw, param_addr, param_size));
288 type = INVALID_TYPE;
289 param_addr = actual_params->GetRawParameter(2, &param_size, &type);
290 ASSERT_EQ(sizeof(h), param_size);
291 EXPECT_EQ(VOIDPTR_TYPE, type);
292 ASSERT_TRUE(NULL != param_addr);
293 EXPECT_EQ(0, memcmp(&h, param_addr, param_size));
295 CloseChannelEvents(client_control);
296 delete[] reinterpret_cast<char*>(client_control);
299 TEST(IPCTest, CrossCallValidation) {
300 // First a sanity test with a well formed parameter object.
301 unsigned long value = 124816;
302 const uint32 kTag = 33;
303 const uint32 kBufferSize = 256;
304 ActualCallParams<1, kBufferSize> params_1(kTag);
305 params_1.CopyParamIn(0, &value, sizeof(value), false, UINT32_TYPE);
306 void* buffer = const_cast<void*>(params_1.GetBuffer());
308 uint32 out_size = 0;
309 CrossCallParamsEx* ccp = 0;
310 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, params_1.GetSize(),
311 &out_size);
312 ASSERT_TRUE(NULL != ccp);
313 EXPECT_TRUE(ccp->GetBuffer() != buffer);
314 EXPECT_EQ(kTag, ccp->GetTag());
315 EXPECT_EQ(1, ccp->GetParamsCount());
316 delete[] (reinterpret_cast<char*>(ccp));
318 // Test that we handle integer overflow on the number of params
319 // correctly. We use a test-only ctor for ActualCallParams that
320 // allows to create malformed cross-call buffers.
321 const int32 kPtrDiffSz = sizeof(ptrdiff_t);
322 for (int32 ix = -1; ix != 3; ++ix) {
323 uint32 fake_num_params = (kuint32max / kPtrDiffSz) + ix;
324 ActualCallParams<1, kBufferSize> params_2(kTag, fake_num_params);
325 params_2.CopyParamIn(0, &value, sizeof(value), false, UINT32_TYPE);
326 buffer = const_cast<void*>(params_2.GetBuffer());
328 EXPECT_TRUE(NULL != buffer);
329 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, params_1.GetSize(),
330 &out_size);
331 // If the buffer is malformed the return is NULL.
332 EXPECT_TRUE(NULL == ccp);
335 ActualCallParams<1, kBufferSize> params_3(kTag, 1);
336 params_3.CopyParamIn(0, &value, sizeof(value), false, UINT32_TYPE);
337 buffer = const_cast<void*>(params_3.GetBuffer());
338 EXPECT_TRUE(NULL != buffer);
340 uint32 correct_size = params_3.OverrideSize(1);
341 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, kBufferSize, &out_size);
342 EXPECT_TRUE(NULL == ccp);
344 // The correct_size is 8 bytes aligned.
345 params_3.OverrideSize(correct_size - 7);
346 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, kBufferSize, &out_size);
347 EXPECT_TRUE(NULL == ccp);
349 params_3.OverrideSize(correct_size);
350 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, kBufferSize, &out_size);
351 EXPECT_TRUE(NULL != ccp);
353 // Make sure that two parameters work as expected.
354 ActualCallParams<2, kBufferSize> params_4(kTag, 2);
355 params_4.CopyParamIn(0, &value, sizeof(value), false, UINT32_TYPE);
356 params_4.CopyParamIn(1, buffer, sizeof(buffer), false, VOIDPTR_TYPE);
357 buffer = const_cast<void*>(params_4.GetBuffer());
358 EXPECT_TRUE(NULL != buffer);
360 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, kBufferSize, &out_size);
361 EXPECT_TRUE(NULL != ccp);
363 #if defined(_WIN64)
364 correct_size = params_4.OverrideSize(1);
365 params_4.OverrideSize(correct_size - 1);
366 ccp = CrossCallParamsEx::CreateFromBuffer(buffer, kBufferSize, &out_size);
367 EXPECT_TRUE(NULL == ccp);
368 #endif
371 // This structure is passed to the mock server threads to simulate
372 // the server side IPC so it has the required kernel objects.
373 struct ServerEvents {
374 HANDLE ping;
375 HANDLE pong;
376 volatile LONG* state;
377 HANDLE mutex;
380 // This is the server thread that quicky answers an IPC and exits.
381 DWORD WINAPI QuickResponseServer(PVOID param) {
382 ServerEvents* events = reinterpret_cast<ServerEvents*>(param);
383 DWORD wait_result = 0;
384 wait_result = ::WaitForSingleObject(events->ping, INFINITE);
385 ::InterlockedExchange(events->state, kAckChannel);
386 ::SetEvent(events->pong);
387 return wait_result;
390 class CrossCallParamsMock : public CrossCallParams {
391 public:
392 CrossCallParamsMock(uint32 tag, uint32 params_count)
393 : CrossCallParams(tag, params_count) {
397 void FakeOkAnswerInChannel(void* channel) {
398 CrossCallReturn* answer = reinterpret_cast<CrossCallReturn*>(channel);
399 answer->call_outcome = SBOX_ALL_OK;
402 // Create two threads that will quickly answer IPCs; the first one
403 // using channel 1 (channel 0 is busy) and one using channel 0. No time-out
404 // should occur.
405 TEST(IPCTest, ClientFastServer) {
406 const size_t channel_size = kIPCChannelSize;
407 size_t base_start = 0;
408 IPCControl* client_control =
409 MakeChannels(channel_size, 4096 * 2, &base_start);
410 FixChannels(client_control, base_start, kIPCChannelSize, FIX_PONG_NOT_READY);
411 client_control->server_alive = ::CreateMutex(NULL, FALSE, NULL);
413 char* mem = reinterpret_cast<char*>(client_control);
414 SharedMemIPCClient client(mem);
416 ServerEvents events = {0};
417 events.ping = client_control->channels[1].ping_event;
418 events.pong = client_control->channels[1].pong_event;
419 events.state = &client_control->channels[1].state;
421 HANDLE t1 = ::CreateThread(NULL, 0, QuickResponseServer, &events, 0, NULL);
422 ASSERT_TRUE(NULL != t1);
423 ::CloseHandle(t1);
425 void* buff0 = client.GetBuffer();
426 EXPECT_TRUE(mem + client_control->channels[0].channel_base == buff0);
427 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
428 EXPECT_EQ(kFreeChannel, client_control->channels[1].state);
429 EXPECT_EQ(kFreeChannel, client_control->channels[2].state);
431 void* buff1 = client.GetBuffer();
432 EXPECT_TRUE(mem + client_control->channels[1].channel_base == buff1);
433 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
434 EXPECT_EQ(kBusyChannel, client_control->channels[1].state);
435 EXPECT_EQ(kFreeChannel, client_control->channels[2].state);
437 EXPECT_EQ(0, client_control->channels[1].ipc_tag);
439 uint32 tag = 7654;
440 CrossCallReturn answer;
441 CrossCallParamsMock* params1 = new(buff1) CrossCallParamsMock(tag, 1);
442 FakeOkAnswerInChannel(buff1);
444 ResultCode result = client.DoCall(params1, &answer);
445 if (SBOX_ERROR_CHANNEL_ERROR != result)
446 client.FreeBuffer(buff1);
448 EXPECT_TRUE(SBOX_ALL_OK == result);
449 EXPECT_EQ(tag, client_control->channels[1].ipc_tag);
450 EXPECT_EQ(kBusyChannel, client_control->channels[0].state);
451 EXPECT_EQ(kFreeChannel, client_control->channels[1].state);
452 EXPECT_EQ(kFreeChannel, client_control->channels[2].state);
454 HANDLE t2 = ::CreateThread(NULL, 0, QuickResponseServer, &events, 0, NULL);
455 ASSERT_TRUE(NULL != t2);
456 ::CloseHandle(t2);
458 client.FreeBuffer(buff0);
459 events.ping = client_control->channels[0].ping_event;
460 events.pong = client_control->channels[0].pong_event;
461 events.state = &client_control->channels[0].state;
463 tag = 4567;
464 CrossCallParamsMock* params2 = new(buff0) CrossCallParamsMock(tag, 1);
465 FakeOkAnswerInChannel(buff0);
467 result = client.DoCall(params2, &answer);
468 if (SBOX_ERROR_CHANNEL_ERROR != result)
469 client.FreeBuffer(buff0);
471 EXPECT_TRUE(SBOX_ALL_OK == result);
472 EXPECT_EQ(tag, client_control->channels[0].ipc_tag);
473 EXPECT_EQ(kFreeChannel, client_control->channels[0].state);
474 EXPECT_EQ(kFreeChannel, client_control->channels[1].state);
475 EXPECT_EQ(kFreeChannel, client_control->channels[2].state);
477 CloseChannelEvents(client_control);
478 ::CloseHandle(client_control->server_alive);
480 delete[] reinterpret_cast<char*>(client_control);
483 // This is the server thread that very slowly answers an IPC and exits. Note
484 // that the pong event needs to be signaled twice.
485 DWORD WINAPI SlowResponseServer(PVOID param) {
486 ServerEvents* events = reinterpret_cast<ServerEvents*>(param);
487 DWORD wait_result = 0;
488 wait_result = ::WaitForSingleObject(events->ping, INFINITE);
489 ::Sleep(kIPCWaitTimeOut1 + kIPCWaitTimeOut2 + 200);
490 ::InterlockedExchange(events->state, kAckChannel);
491 ::SetEvent(events->pong);
492 return wait_result;
495 // This thread's job is to keep the mutex locked.
496 DWORD WINAPI MainServerThread(PVOID param) {
497 ServerEvents* events = reinterpret_cast<ServerEvents*>(param);
498 DWORD wait_result = 0;
499 wait_result = ::WaitForSingleObject(events->mutex, INFINITE);
500 Sleep(kIPCWaitTimeOut1 * 20);
501 return wait_result;
504 // Creates a server thread that answers the IPC so slow that is guaranteed to
505 // trigger the time-out code path in the client. A second thread is created
506 // to hold locked the server_alive mutex: this signals the client that the
507 // server is not dead and it retries the wait.
508 TEST(IPCTest, ClientSlowServer) {
509 size_t base_start = 0;
510 IPCControl* client_control =
511 MakeChannels(kIPCChannelSize, 4096*2, &base_start);
512 FixChannels(client_control, base_start, kIPCChannelSize, FIX_PONG_NOT_READY);
513 client_control->server_alive = ::CreateMutex(NULL, FALSE, NULL);
515 char* mem = reinterpret_cast<char*>(client_control);
516 SharedMemIPCClient client(mem);
518 ServerEvents events = {0};
519 events.ping = client_control->channels[0].ping_event;
520 events.pong = client_control->channels[0].pong_event;
521 events.state = &client_control->channels[0].state;
523 HANDLE t1 = ::CreateThread(NULL, 0, SlowResponseServer, &events, 0, NULL);
524 ASSERT_TRUE(NULL != t1);
525 ::CloseHandle(t1);
527 ServerEvents events2 = {0};
528 events2.pong = events.pong;
529 events2.mutex = client_control->server_alive;
531 HANDLE t2 = ::CreateThread(NULL, 0, MainServerThread, &events2, 0, NULL);
532 ASSERT_TRUE(NULL != t2);
533 ::CloseHandle(t2);
535 ::Sleep(1);
537 void* buff0 = client.GetBuffer();
538 uint32 tag = 4321;
539 CrossCallReturn answer;
540 CrossCallParamsMock* params1 = new(buff0) CrossCallParamsMock(tag, 1);
541 FakeOkAnswerInChannel(buff0);
543 ResultCode result = client.DoCall(params1, &answer);
544 if (SBOX_ERROR_CHANNEL_ERROR != result)
545 client.FreeBuffer(buff0);
547 EXPECT_TRUE(SBOX_ALL_OK == result);
548 EXPECT_EQ(tag, client_control->channels[0].ipc_tag);
549 EXPECT_EQ(kFreeChannel, client_control->channels[0].state);
551 CloseChannelEvents(client_control);
552 ::CloseHandle(client_control->server_alive);
553 delete[] reinterpret_cast<char*>(client_control);
556 // This test-only IPC dispatcher has two handlers with the same signature
557 // but only CallOneHandler should be used.
558 class UnitTestIPCDispatcher : public Dispatcher {
559 public:
560 enum {
561 CALL_ONE_TAG = 78,
562 CALL_TWO_TAG = 87
565 UnitTestIPCDispatcher();
566 ~UnitTestIPCDispatcher() override{};
568 bool SetupService(InterceptionManager* manager, int service) override {
569 return true;
572 private:
573 bool CallOneHandler(IPCInfo* ipc, HANDLE p1, uint32 p2) {
574 ipc->return_info.extended[0].handle = p1;
575 ipc->return_info.extended[1].unsigned_int = p2;
576 return true;
579 bool CallTwoHandler(IPCInfo* ipc, HANDLE p1, uint32 p2) {
580 return true;
584 UnitTestIPCDispatcher::UnitTestIPCDispatcher() {
585 static const IPCCall call_one = {{CALL_ONE_TAG, {VOIDPTR_TYPE, UINT32_TYPE}},
586 reinterpret_cast<CallbackGeneric>(
587 &UnitTestIPCDispatcher::CallOneHandler)};
588 static const IPCCall call_two = {{CALL_TWO_TAG, {VOIDPTR_TYPE, UINT32_TYPE}},
589 reinterpret_cast<CallbackGeneric>(
590 &UnitTestIPCDispatcher::CallTwoHandler)};
591 ipc_calls_.push_back(call_one);
592 ipc_calls_.push_back(call_two);
595 // This test does most of the shared memory IPC client-server roundtrip
596 // and tests the packing, unpacking and call dispatching.
597 TEST(IPCTest, SharedMemServerTests) {
598 size_t base_start = 0;
599 IPCControl* client_control =
600 MakeChannels(kIPCChannelSize, 4096, &base_start);
601 client_control->server_alive = HANDLE(1);
602 FixChannels(client_control, base_start, kIPCChannelSize, FIX_PONG_READY);
604 char* mem = reinterpret_cast<char*>(client_control);
605 SharedMemIPCClient client(mem);
607 CrossCallReturn answer;
608 HANDLE bar = HANDLE(191919);
609 DWORD foo = 6767676;
610 CrossCall(client, UnitTestIPCDispatcher::CALL_ONE_TAG, bar, foo, &answer);
611 void* buff = client.GetBuffer();
612 ASSERT_TRUE(NULL != buff);
614 UnitTestIPCDispatcher dispatcher;
615 // Since we are directly calling InvokeCallback, most of this structure
616 // can be set to NULL.
617 sandbox::SharedMemIPCServer::ServerControl srv_control = {};
618 srv_control.channel_size = kIPCChannelSize;
619 srv_control.shared_base = reinterpret_cast<char*>(client_control);
620 srv_control.dispatcher = &dispatcher;
622 sandbox::CrossCallReturn call_return = {0};
623 EXPECT_TRUE(SharedMemIPCServer::InvokeCallback(&srv_control, buff,
624 &call_return));
625 EXPECT_EQ(SBOX_ALL_OK, call_return.call_outcome);
626 EXPECT_TRUE(bar == call_return.extended[0].handle);
627 EXPECT_EQ(foo, call_return.extended[1].unsigned_int);
629 CloseChannelEvents(client_control);
630 delete[] reinterpret_cast<char*>(client_control);
633 } // namespace sandbox