1 // Copyright 2013 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 "mojo/system/core.h"
11 #include "base/threading/platform_thread.h"
12 #include "base/time/time.h"
13 #include "mojo/system/core_test_base.h"
19 const MojoHandleSignalsState kEmptyMojoHandleSignalsState
= {0u, 0u};
20 const MojoHandleSignalsState kFullMojoHandleSignalsState
= {~0u, ~0u};
22 typedef test::CoreTestBase CoreTest
;
24 TEST_F(CoreTest
, GetTimeTicksNow
) {
25 const MojoTimeTicks start
= core()->GetTimeTicksNow();
26 EXPECT_NE(static_cast<MojoTimeTicks
>(0), start
)
27 << "GetTimeTicksNow should return nonzero value";
28 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15));
29 const MojoTimeTicks finish
= core()->GetTimeTicksNow();
30 // Allow for some fuzz in sleep.
31 EXPECT_GE((finish
- start
), static_cast<MojoTimeTicks
>(8000))
32 << "Sleeping should result in increasing time ticks";
35 TEST_F(CoreTest
, Basic
) {
38 EXPECT_EQ(0u, info
.GetCtorCallCount());
39 MojoHandle h
= CreateMockHandle(&info
);
40 EXPECT_EQ(1u, info
.GetCtorCallCount());
41 EXPECT_NE(h
, MOJO_HANDLE_INVALID
);
43 EXPECT_EQ(0u, info
.GetWriteMessageCallCount());
44 EXPECT_EQ(MOJO_RESULT_OK
,
45 core()->WriteMessage(h
,
50 MOJO_WRITE_MESSAGE_FLAG_NONE
));
51 EXPECT_EQ(1u, info
.GetWriteMessageCallCount());
53 EXPECT_EQ(0u, info
.GetReadMessageCallCount());
54 uint32_t num_bytes
= 0;
55 EXPECT_EQ(MOJO_RESULT_OK
,
56 core()->ReadMessage(h
,
58 MakeUserPointer(&num_bytes
),
61 MOJO_READ_MESSAGE_FLAG_NONE
));
62 EXPECT_EQ(1u, info
.GetReadMessageCallCount());
63 EXPECT_EQ(MOJO_RESULT_OK
,
64 core()->ReadMessage(h
,
69 MOJO_READ_MESSAGE_FLAG_NONE
));
70 EXPECT_EQ(2u, info
.GetReadMessageCallCount());
72 EXPECT_EQ(0u, info
.GetWriteDataCallCount());
74 MOJO_RESULT_UNIMPLEMENTED
,
76 h
, NullUserPointer(), NullUserPointer(), MOJO_WRITE_DATA_FLAG_NONE
));
77 EXPECT_EQ(1u, info
.GetWriteDataCallCount());
79 EXPECT_EQ(0u, info
.GetBeginWriteDataCallCount());
81 MOJO_RESULT_UNIMPLEMENTED
,
82 core()->BeginWriteData(
83 h
, NullUserPointer(), NullUserPointer(), MOJO_WRITE_DATA_FLAG_NONE
));
84 EXPECT_EQ(1u, info
.GetBeginWriteDataCallCount());
86 EXPECT_EQ(0u, info
.GetEndWriteDataCallCount());
87 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED
, core()->EndWriteData(h
, 0));
88 EXPECT_EQ(1u, info
.GetEndWriteDataCallCount());
90 EXPECT_EQ(0u, info
.GetReadDataCallCount());
92 MOJO_RESULT_UNIMPLEMENTED
,
94 h
, NullUserPointer(), NullUserPointer(), MOJO_READ_DATA_FLAG_NONE
));
95 EXPECT_EQ(1u, info
.GetReadDataCallCount());
97 EXPECT_EQ(0u, info
.GetBeginReadDataCallCount());
99 MOJO_RESULT_UNIMPLEMENTED
,
100 core()->BeginReadData(
101 h
, NullUserPointer(), NullUserPointer(), MOJO_READ_DATA_FLAG_NONE
));
102 EXPECT_EQ(1u, info
.GetBeginReadDataCallCount());
104 EXPECT_EQ(0u, info
.GetEndReadDataCallCount());
105 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED
, core()->EndReadData(h
, 0));
106 EXPECT_EQ(1u, info
.GetEndReadDataCallCount());
108 EXPECT_EQ(0u, info
.GetAddWaiterCallCount());
109 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
111 ~MOJO_HANDLE_SIGNAL_NONE
,
112 MOJO_DEADLINE_INDEFINITE
,
114 EXPECT_EQ(1u, info
.GetAddWaiterCallCount());
115 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
116 core()->Wait(h
, ~MOJO_HANDLE_SIGNAL_NONE
, 0, NullUserPointer()));
117 EXPECT_EQ(2u, info
.GetAddWaiterCallCount());
118 MojoHandleSignalsState hss
= kFullMojoHandleSignalsState
;
119 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
121 ~MOJO_HANDLE_SIGNAL_NONE
,
122 MOJO_DEADLINE_INDEFINITE
,
123 MakeUserPointer(&hss
)));
124 EXPECT_EQ(3u, info
.GetAddWaiterCallCount());
125 EXPECT_EQ(0u, hss
.satisfied_signals
);
126 EXPECT_EQ(0u, hss
.satisfiable_signals
);
128 MOJO_RESULT_FAILED_PRECONDITION
,
129 core()->Wait(h
, ~MOJO_HANDLE_SIGNAL_NONE
, 10 * 1000, NullUserPointer()));
130 EXPECT_EQ(4u, info
.GetAddWaiterCallCount());
131 hss
= kFullMojoHandleSignalsState
;
132 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
134 h
, ~MOJO_HANDLE_SIGNAL_NONE
, 10 * 1000, MakeUserPointer(&hss
)));
135 EXPECT_EQ(5u, info
.GetAddWaiterCallCount());
136 EXPECT_EQ(0u, hss
.satisfied_signals
);
137 EXPECT_EQ(0u, hss
.satisfiable_signals
);
139 MojoHandleSignals handle_signals
= ~MOJO_HANDLE_SIGNAL_NONE
;
140 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
141 core()->WaitMany(MakeUserPointer(&h
),
142 MakeUserPointer(&handle_signals
),
144 MOJO_DEADLINE_INDEFINITE
,
147 EXPECT_EQ(6u, info
.GetAddWaiterCallCount());
148 uint32_t result_index
= static_cast<uint32_t>(-1);
149 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
150 core()->WaitMany(MakeUserPointer(&h
),
151 MakeUserPointer(&handle_signals
),
153 MOJO_DEADLINE_INDEFINITE
,
154 MakeUserPointer(&result_index
),
156 EXPECT_EQ(7u, info
.GetAddWaiterCallCount());
157 EXPECT_EQ(0u, result_index
);
158 hss
= kFullMojoHandleSignalsState
;
159 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
160 core()->WaitMany(MakeUserPointer(&h
),
161 MakeUserPointer(&handle_signals
),
163 MOJO_DEADLINE_INDEFINITE
,
165 MakeUserPointer(&hss
)));
166 EXPECT_EQ(8u, info
.GetAddWaiterCallCount());
167 EXPECT_EQ(0u, hss
.satisfied_signals
);
168 EXPECT_EQ(0u, hss
.satisfiable_signals
);
169 result_index
= static_cast<uint32_t>(-1);
170 hss
= kFullMojoHandleSignalsState
;
171 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
172 core()->WaitMany(MakeUserPointer(&h
),
173 MakeUserPointer(&handle_signals
),
175 MOJO_DEADLINE_INDEFINITE
,
176 MakeUserPointer(&result_index
),
177 MakeUserPointer(&hss
)));
178 EXPECT_EQ(9u, info
.GetAddWaiterCallCount());
179 EXPECT_EQ(0u, result_index
);
180 EXPECT_EQ(0u, hss
.satisfied_signals
);
181 EXPECT_EQ(0u, hss
.satisfiable_signals
);
183 EXPECT_EQ(0u, info
.GetDtorCallCount());
184 EXPECT_EQ(0u, info
.GetCloseCallCount());
185 EXPECT_EQ(0u, info
.GetCancelAllWaitersCallCount());
186 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
));
187 EXPECT_EQ(1u, info
.GetCancelAllWaitersCallCount());
188 EXPECT_EQ(1u, info
.GetCloseCallCount());
189 EXPECT_EQ(1u, info
.GetDtorCallCount());
191 // No waiters should ever have ever been added.
192 EXPECT_EQ(0u, info
.GetRemoveWaiterCallCount());
195 TEST_F(CoreTest
, InvalidArguments
) {
198 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(MOJO_HANDLE_INVALID
));
199 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(10));
200 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(1000000000));
202 // Test a double-close.
204 MojoHandle h
= CreateMockHandle(&info
);
205 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
));
206 EXPECT_EQ(1u, info
.GetCloseCallCount());
207 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(h
));
208 EXPECT_EQ(1u, info
.GetCloseCallCount());
213 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
214 core()->Wait(MOJO_HANDLE_INVALID
,
215 ~MOJO_HANDLE_SIGNAL_NONE
,
216 MOJO_DEADLINE_INDEFINITE
,
218 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
220 ~MOJO_HANDLE_SIGNAL_NONE
,
221 MOJO_DEADLINE_INDEFINITE
,
224 MojoHandleSignalsState hss
= kFullMojoHandleSignalsState
;
225 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
226 core()->Wait(MOJO_HANDLE_INVALID
,
227 ~MOJO_HANDLE_SIGNAL_NONE
,
228 MOJO_DEADLINE_INDEFINITE
,
229 MakeUserPointer(&hss
)));
230 // On invalid argument, it shouldn't modify the handle signals state.
231 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfied_signals
,
232 hss
.satisfied_signals
);
233 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfiable_signals
,
234 hss
.satisfiable_signals
);
235 hss
= kFullMojoHandleSignalsState
;
236 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
238 ~MOJO_HANDLE_SIGNAL_NONE
,
239 MOJO_DEADLINE_INDEFINITE
,
240 MakeUserPointer(&hss
)));
241 // On invalid argument, it shouldn't modify the handle signals state.
242 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfied_signals
,
243 hss
.satisfied_signals
);
244 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfiable_signals
,
245 hss
.satisfiable_signals
);
250 MojoHandle handles
[2] = {MOJO_HANDLE_INVALID
, MOJO_HANDLE_INVALID
};
251 MojoHandleSignals signals
[2] = {~MOJO_HANDLE_SIGNAL_NONE
,
252 ~MOJO_HANDLE_SIGNAL_NONE
};
253 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
254 core()->WaitMany(MakeUserPointer(handles
),
255 MakeUserPointer(signals
),
257 MOJO_DEADLINE_INDEFINITE
,
260 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
261 core()->WaitMany(NullUserPointer(),
262 MakeUserPointer(signals
),
264 MOJO_DEADLINE_INDEFINITE
,
267 // If |num_handles| is invalid, it should leave |result_index| and
268 // |signals_states| alone.
269 // (We use -1 internally; make sure that doesn't leak.)
270 uint32_t result_index
= 123;
271 MojoHandleSignalsState hss
= kFullMojoHandleSignalsState
;
272 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
273 core()->WaitMany(NullUserPointer(),
274 MakeUserPointer(signals
),
276 MOJO_DEADLINE_INDEFINITE
,
277 MakeUserPointer(&result_index
),
278 MakeUserPointer(&hss
)));
279 EXPECT_EQ(123u, result_index
);
280 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfied_signals
,
281 hss
.satisfied_signals
);
282 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfiable_signals
,
283 hss
.satisfiable_signals
);
285 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
286 core()->WaitMany(MakeUserPointer(handles
),
289 MOJO_DEADLINE_INDEFINITE
,
292 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
293 core()->WaitMany(MakeUserPointer(handles
),
294 MakeUserPointer(signals
),
296 MOJO_DEADLINE_INDEFINITE
,
299 // But if a handle is bad, then it should set |result_index| but still leave
300 // |signals_states| alone.
301 result_index
= static_cast<uint32_t>(-1);
302 hss
= kFullMojoHandleSignalsState
;
303 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
304 core()->WaitMany(MakeUserPointer(handles
),
305 MakeUserPointer(signals
),
307 MOJO_DEADLINE_INDEFINITE
,
308 MakeUserPointer(&result_index
),
309 MakeUserPointer(&hss
)));
310 EXPECT_EQ(0u, result_index
);
311 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfied_signals
,
312 hss
.satisfied_signals
);
313 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfiable_signals
,
314 hss
.satisfiable_signals
);
316 MockHandleInfo info
[2];
317 handles
[0] = CreateMockHandle(&info
[0]);
319 result_index
= static_cast<uint32_t>(-1);
320 hss
= kFullMojoHandleSignalsState
;
321 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
322 core()->WaitMany(MakeUserPointer(handles
),
323 MakeUserPointer(signals
),
325 MOJO_DEADLINE_INDEFINITE
,
326 MakeUserPointer(&result_index
),
327 MakeUserPointer(&hss
)));
328 EXPECT_EQ(0u, result_index
);
329 EXPECT_EQ(0u, hss
.satisfied_signals
);
330 EXPECT_EQ(0u, hss
.satisfiable_signals
);
332 // On invalid argument, it'll leave |signals_states| alone.
333 result_index
= static_cast<uint32_t>(-1);
334 hss
= kFullMojoHandleSignalsState
;
335 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
336 core()->WaitMany(MakeUserPointer(handles
),
337 MakeUserPointer(signals
),
339 MOJO_DEADLINE_INDEFINITE
,
340 MakeUserPointer(&result_index
),
341 MakeUserPointer(&hss
)));
342 EXPECT_EQ(1u, result_index
);
343 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfied_signals
,
344 hss
.satisfied_signals
);
345 EXPECT_EQ(kFullMojoHandleSignalsState
.satisfiable_signals
,
346 hss
.satisfiable_signals
);
347 handles
[1] = handles
[0] + 1; // Invalid handle.
348 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
349 core()->WaitMany(MakeUserPointer(handles
),
350 MakeUserPointer(signals
),
352 MOJO_DEADLINE_INDEFINITE
,
355 handles
[1] = CreateMockHandle(&info
[1]);
356 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
357 core()->WaitMany(MakeUserPointer(handles
),
358 MakeUserPointer(signals
),
360 MOJO_DEADLINE_INDEFINITE
,
364 // TODO(vtl): Test one where we get "failed precondition" only for the
365 // second handle (and the first one is valid to wait on).
367 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(handles
[0]));
368 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(handles
[1]));
371 // |CreateMessagePipe()|: Nothing to check (apart from things that cause
375 // Only check arguments checked by |Core|, namely |handle|, |handles|, and
378 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
379 core()->WriteMessage(MOJO_HANDLE_INVALID
,
384 MOJO_WRITE_MESSAGE_FLAG_NONE
));
387 MojoHandle h
= CreateMockHandle(&info
);
388 MojoHandle handles
[2] = {MOJO_HANDLE_INVALID
, MOJO_HANDLE_INVALID
};
390 // Huge handle count (implausibly big on some systems -- more than can be
391 // stored in a 32-bit address space).
392 // Note: This may return either |MOJO_RESULT_INVALID_ARGUMENT| or
393 // |MOJO_RESULT_RESOURCE_EXHAUSTED|, depending on whether it's plausible or
395 EXPECT_NE(MOJO_RESULT_OK
,
396 core()->WriteMessage(h
,
399 MakeUserPointer(handles
),
400 std::numeric_limits
<uint32_t>::max(),
401 MOJO_WRITE_MESSAGE_FLAG_NONE
));
402 EXPECT_EQ(0u, info
.GetWriteMessageCallCount());
404 // Huge handle count (plausibly big).
405 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED
,
406 core()->WriteMessage(
410 MakeUserPointer(handles
),
411 std::numeric_limits
<uint32_t>::max() / sizeof(handles
[0]),
412 MOJO_WRITE_MESSAGE_FLAG_NONE
));
413 EXPECT_EQ(0u, info
.GetWriteMessageCallCount());
415 // Invalid handle in |handles|.
416 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
417 core()->WriteMessage(h
,
420 MakeUserPointer(handles
),
422 MOJO_WRITE_MESSAGE_FLAG_NONE
));
423 EXPECT_EQ(0u, info
.GetWriteMessageCallCount());
425 // Two invalid handles in |handles|.
426 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
427 core()->WriteMessage(h
,
430 MakeUserPointer(handles
),
432 MOJO_WRITE_MESSAGE_FLAG_NONE
));
433 EXPECT_EQ(0u, info
.GetWriteMessageCallCount());
435 // Can't send a handle over itself.
437 EXPECT_EQ(MOJO_RESULT_BUSY
,
438 core()->WriteMessage(h
,
441 MakeUserPointer(handles
),
443 MOJO_WRITE_MESSAGE_FLAG_NONE
));
444 EXPECT_EQ(0u, info
.GetWriteMessageCallCount());
446 MockHandleInfo info2
;
447 MojoHandle h2
= CreateMockHandle(&info2
);
449 // This is "okay", but |MockDispatcher| doesn't implement it.
451 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED
,
452 core()->WriteMessage(h
,
455 MakeUserPointer(handles
),
457 MOJO_WRITE_MESSAGE_FLAG_NONE
));
458 EXPECT_EQ(1u, info
.GetWriteMessageCallCount());
460 // One of the |handles| is still invalid.
461 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
462 core()->WriteMessage(h
,
465 MakeUserPointer(handles
),
467 MOJO_WRITE_MESSAGE_FLAG_NONE
));
468 EXPECT_EQ(1u, info
.GetWriteMessageCallCount());
470 // One of the |handles| is the same as |handle|.
472 EXPECT_EQ(MOJO_RESULT_BUSY
,
473 core()->WriteMessage(h
,
476 MakeUserPointer(handles
),
478 MOJO_WRITE_MESSAGE_FLAG_NONE
));
479 EXPECT_EQ(1u, info
.GetWriteMessageCallCount());
481 // Can't send a handle twice in the same message.
483 EXPECT_EQ(MOJO_RESULT_BUSY
,
484 core()->WriteMessage(h
,
487 MakeUserPointer(handles
),
489 MOJO_WRITE_MESSAGE_FLAG_NONE
));
490 EXPECT_EQ(1u, info
.GetWriteMessageCallCount());
492 // Note: Since we never successfully sent anything with it, |h2| should
494 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h2
));
496 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
));
500 // Only check arguments checked by |Core|, namely |handle|, |handles|, and
503 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
504 core()->ReadMessage(MOJO_HANDLE_INVALID
,
509 MOJO_READ_MESSAGE_FLAG_NONE
));
512 MojoHandle h
= CreateMockHandle(&info
);
515 uint32_t handle_count
= 0;
516 EXPECT_EQ(MOJO_RESULT_OK
,
517 core()->ReadMessage(h
,
521 MakeUserPointer(&handle_count
),
522 MOJO_READ_MESSAGE_FLAG_NONE
));
523 // Checked by |Core|, shouldn't go through to the dispatcher.
524 EXPECT_EQ(1u, info
.GetReadMessageCallCount());
526 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
));
530 // These test invalid arguments that should cause death if we're being paranoid
531 // about checking arguments (which we would want to do if, e.g., we were in a
532 // true "kernel" situation, but we might not want to do otherwise for
533 // performance reasons). Probably blatant errors like passing in null pointers
534 // (for required pointer arguments) will still cause death, but perhaps not
536 TEST_F(CoreTest
, InvalidArgumentsDeath
) {
537 const char kMemoryCheckFailedRegex
[] = "Check failed";
541 MojoHandle handle
= MOJO_HANDLE_INVALID
;
542 MojoHandleSignals signals
= ~MOJO_HANDLE_SIGNAL_NONE
;
543 EXPECT_DEATH_IF_SUPPORTED(core()->WaitMany(NullUserPointer(),
544 MakeUserPointer(&signals
),
546 MOJO_DEADLINE_INDEFINITE
,
549 kMemoryCheckFailedRegex
);
550 EXPECT_DEATH_IF_SUPPORTED(core()->WaitMany(MakeUserPointer(&handle
),
553 MOJO_DEADLINE_INDEFINITE
,
556 kMemoryCheckFailedRegex
);
557 // TODO(vtl): |result_index| and |signals_states| are optional. Test them
558 // with non-null invalid pointers?
561 // |CreateMessagePipe()|:
564 EXPECT_DEATH_IF_SUPPORTED(
565 core()->CreateMessagePipe(
566 NullUserPointer(), NullUserPointer(), NullUserPointer()),
567 kMemoryCheckFailedRegex
);
568 EXPECT_DEATH_IF_SUPPORTED(
569 core()->CreateMessagePipe(
570 NullUserPointer(), MakeUserPointer(&h
), NullUserPointer()),
571 kMemoryCheckFailedRegex
);
572 EXPECT_DEATH_IF_SUPPORTED(
573 core()->CreateMessagePipe(
574 NullUserPointer(), NullUserPointer(), MakeUserPointer(&h
)),
575 kMemoryCheckFailedRegex
);
579 // Only check arguments checked by |Core|, namely |handle|, |handles|, and
583 MojoHandle h
= CreateMockHandle(&info
);
585 // Null |handles| with nonzero |num_handles|.
586 EXPECT_DEATH_IF_SUPPORTED(
587 core()->WriteMessage(h
,
592 MOJO_WRITE_MESSAGE_FLAG_NONE
),
593 kMemoryCheckFailedRegex
);
595 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
));
599 // Only check arguments checked by |Core|, namely |handle|, |handles|, and
603 MojoHandle h
= CreateMockHandle(&info
);
605 uint32_t handle_count
= 1;
606 EXPECT_DEATH_IF_SUPPORTED(
607 core()->ReadMessage(h
,
611 MakeUserPointer(&handle_count
),
612 MOJO_READ_MESSAGE_FLAG_NONE
),
613 kMemoryCheckFailedRegex
);
615 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
));
619 // TODO(vtl): test |Wait()| and |WaitMany()| properly
620 // - including |WaitMany()| with the same handle more than once (with
621 // same/different signals)
623 TEST_F(CoreTest
, MessagePipe
) {
625 MojoHandleSignalsState hss
[2];
626 uint32_t result_index
;
630 core()->CreateMessagePipe(
631 NullUserPointer(), MakeUserPointer(&h
[0]), MakeUserPointer(&h
[1])));
632 // Should get two distinct, valid handles.
633 EXPECT_NE(h
[0], MOJO_HANDLE_INVALID
);
634 EXPECT_NE(h
[1], MOJO_HANDLE_INVALID
);
635 EXPECT_NE(h
[0], h
[1]);
637 // Neither should be readable.
638 MojoHandleSignals signals
[2] = {MOJO_HANDLE_SIGNAL_READABLE
,
639 MOJO_HANDLE_SIGNAL_READABLE
};
640 result_index
= static_cast<uint32_t>(-1);
641 hss
[0] = kEmptyMojoHandleSignalsState
;
642 hss
[1] = kEmptyMojoHandleSignalsState
;
643 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED
,
644 core()->WaitMany(MakeUserPointer(h
),
645 MakeUserPointer(signals
),
648 MakeUserPointer(&result_index
),
649 MakeUserPointer(hss
)));
650 EXPECT_EQ(static_cast<uint32_t>(-1), result_index
);
651 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[0].satisfied_signals
);
652 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
653 hss
[0].satisfiable_signals
);
654 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[1].satisfied_signals
);
655 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
656 hss
[1].satisfiable_signals
);
658 // Try to read anyway.
659 char buffer
[1] = {'a'};
660 uint32_t buffer_size
= 1;
661 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT
,
662 core()->ReadMessage(h
[0],
663 UserPointer
<void>(buffer
),
664 MakeUserPointer(&buffer_size
),
667 MOJO_READ_MESSAGE_FLAG_NONE
));
668 // Check that it left its inputs alone.
669 EXPECT_EQ('a', buffer
[0]);
670 EXPECT_EQ(1u, buffer_size
);
672 // Both should be writable.
673 hss
[0] = kEmptyMojoHandleSignalsState
;
674 EXPECT_EQ(MOJO_RESULT_OK
,
676 MOJO_HANDLE_SIGNAL_WRITABLE
,
678 MakeUserPointer(&hss
[0])));
679 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[0].satisfied_signals
);
680 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
681 hss
[0].satisfiable_signals
);
682 hss
[0] = kEmptyMojoHandleSignalsState
;
683 EXPECT_EQ(MOJO_RESULT_OK
,
685 MOJO_HANDLE_SIGNAL_WRITABLE
,
687 MakeUserPointer(&hss
[0])));
688 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[0].satisfied_signals
);
689 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
690 hss
[0].satisfiable_signals
);
692 // Also check that |h[1]| is writable using |WaitMany()|.
693 signals
[0] = MOJO_HANDLE_SIGNAL_READABLE
;
694 signals
[1] = MOJO_HANDLE_SIGNAL_WRITABLE
;
695 result_index
= static_cast<uint32_t>(-1);
696 hss
[0] = kEmptyMojoHandleSignalsState
;
697 hss
[1] = kEmptyMojoHandleSignalsState
;
698 EXPECT_EQ(MOJO_RESULT_OK
,
699 core()->WaitMany(MakeUserPointer(h
),
700 MakeUserPointer(signals
),
702 MOJO_DEADLINE_INDEFINITE
,
703 MakeUserPointer(&result_index
),
704 MakeUserPointer(hss
)));
705 EXPECT_EQ(1u, result_index
);
706 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[0].satisfied_signals
);
707 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
708 hss
[0].satisfiable_signals
);
709 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[1].satisfied_signals
);
710 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
711 hss
[1].satisfiable_signals
);
715 EXPECT_EQ(MOJO_RESULT_OK
,
716 core()->WriteMessage(h
[1],
717 UserPointer
<const void>(buffer
),
721 MOJO_WRITE_MESSAGE_FLAG_NONE
));
723 // Check that |h[0]| is now readable.
724 signals
[0] = MOJO_HANDLE_SIGNAL_READABLE
;
725 signals
[1] = MOJO_HANDLE_SIGNAL_READABLE
;
726 result_index
= static_cast<uint32_t>(-1);
727 hss
[0] = kEmptyMojoHandleSignalsState
;
728 hss
[1] = kEmptyMojoHandleSignalsState
;
729 EXPECT_EQ(MOJO_RESULT_OK
,
730 core()->WaitMany(MakeUserPointer(h
),
731 MakeUserPointer(signals
),
733 MOJO_DEADLINE_INDEFINITE
,
734 MakeUserPointer(&result_index
),
735 MakeUserPointer(hss
)));
736 EXPECT_EQ(0u, result_index
);
737 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
738 hss
[0].satisfied_signals
);
739 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
740 hss
[0].satisfiable_signals
);
741 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[1].satisfied_signals
);
742 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
743 hss
[1].satisfiable_signals
);
746 // First, get only the size.
748 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED
,
749 core()->ReadMessage(h
[0],
751 MakeUserPointer(&buffer_size
),
754 MOJO_READ_MESSAGE_FLAG_NONE
));
755 EXPECT_EQ(1u, buffer_size
);
756 // Then actually read it.
759 EXPECT_EQ(MOJO_RESULT_OK
,
760 core()->ReadMessage(h
[0],
761 UserPointer
<void>(buffer
),
762 MakeUserPointer(&buffer_size
),
765 MOJO_READ_MESSAGE_FLAG_NONE
));
766 EXPECT_EQ('b', buffer
[0]);
767 EXPECT_EQ(1u, buffer_size
);
769 // |h[0]| should no longer be readable.
770 hss
[0] = kEmptyMojoHandleSignalsState
;
772 MOJO_RESULT_DEADLINE_EXCEEDED
,
774 h
[0], MOJO_HANDLE_SIGNAL_READABLE
, 0, MakeUserPointer(&hss
[0])));
775 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
[0].satisfied_signals
);
776 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
777 hss
[0].satisfiable_signals
);
781 EXPECT_EQ(MOJO_RESULT_OK
,
782 core()->WriteMessage(h
[0],
783 UserPointer
<const void>(buffer
),
787 MOJO_WRITE_MESSAGE_FLAG_NONE
));
790 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
[0]));
792 // Check that |h[1]| is no longer writable (and will never be).
793 hss
[0] = kEmptyMojoHandleSignalsState
;
794 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
796 MOJO_HANDLE_SIGNAL_WRITABLE
,
798 MakeUserPointer(&hss
[0])));
799 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
[0].satisfied_signals
);
800 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
[0].satisfiable_signals
);
802 // Check that |h[1]| is still readable (for the moment).
803 hss
[0] = kEmptyMojoHandleSignalsState
;
804 EXPECT_EQ(MOJO_RESULT_OK
,
806 MOJO_HANDLE_SIGNAL_READABLE
,
808 MakeUserPointer(&hss
[0])));
809 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
[0].satisfied_signals
);
810 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
[0].satisfiable_signals
);
812 // Discard a message from |h[1]|.
813 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED
,
814 core()->ReadMessage(h
[1],
819 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD
));
821 // |h[1]| is no longer readable (and will never be).
822 hss
[0] = kFullMojoHandleSignalsState
;
823 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
825 MOJO_HANDLE_SIGNAL_READABLE
,
827 MakeUserPointer(&hss
[0])));
828 EXPECT_EQ(0u, hss
[0].satisfied_signals
);
829 EXPECT_EQ(0u, hss
[0].satisfiable_signals
);
831 // Try writing to |h[1]|.
833 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
834 core()->WriteMessage(h
[1],
835 UserPointer
<const void>(buffer
),
839 MOJO_WRITE_MESSAGE_FLAG_NONE
));
841 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h
[1]));
844 // Tests passing a message pipe handle.
845 TEST_F(CoreTest
, MessagePipeBasicLocalHandlePassing1
) {
846 const char kHello
[] = "hello";
847 const uint32_t kHelloSize
= static_cast<uint32_t>(sizeof(kHello
));
848 const char kWorld
[] = "world!!!";
849 const uint32_t kWorldSize
= static_cast<uint32_t>(sizeof(kWorld
));
851 const uint32_t kBufferSize
= static_cast<uint32_t>(sizeof(buffer
));
853 MojoHandle handles
[10];
854 uint32_t num_handles
;
855 MojoHandleSignalsState hss
;
856 MojoHandle h_received
;
858 MojoHandle h_passing
[2];
859 EXPECT_EQ(MOJO_RESULT_OK
,
860 core()->CreateMessagePipe(NullUserPointer(),
861 MakeUserPointer(&h_passing
[0]),
862 MakeUserPointer(&h_passing
[1])));
864 // Make sure that |h_passing[]| work properly.
865 EXPECT_EQ(MOJO_RESULT_OK
,
866 core()->WriteMessage(h_passing
[0],
867 UserPointer
<const void>(kHello
),
871 MOJO_WRITE_MESSAGE_FLAG_NONE
));
872 hss
= kEmptyMojoHandleSignalsState
;
873 EXPECT_EQ(MOJO_RESULT_OK
,
874 core()->Wait(h_passing
[1],
875 MOJO_HANDLE_SIGNAL_READABLE
,
877 MakeUserPointer(&hss
)));
878 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
879 hss
.satisfied_signals
);
880 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
881 hss
.satisfiable_signals
);
882 num_bytes
= kBufferSize
;
883 num_handles
= arraysize(handles
);
884 EXPECT_EQ(MOJO_RESULT_OK
,
885 core()->ReadMessage(h_passing
[1],
886 UserPointer
<void>(buffer
),
887 MakeUserPointer(&num_bytes
),
888 MakeUserPointer(handles
),
889 MakeUserPointer(&num_handles
),
890 MOJO_READ_MESSAGE_FLAG_NONE
));
891 EXPECT_EQ(kHelloSize
, num_bytes
);
892 EXPECT_STREQ(kHello
, buffer
);
893 EXPECT_EQ(0u, num_handles
);
895 // Make sure that you can't pass either of the message pipe's handles over
897 EXPECT_EQ(MOJO_RESULT_BUSY
,
898 core()->WriteMessage(h_passing
[0],
899 UserPointer
<const void>(kHello
),
901 MakeUserPointer(&h_passing
[0]),
903 MOJO_WRITE_MESSAGE_FLAG_NONE
));
904 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
,
905 core()->WriteMessage(h_passing
[0],
906 UserPointer
<const void>(kHello
),
908 MakeUserPointer(&h_passing
[1]),
910 MOJO_WRITE_MESSAGE_FLAG_NONE
));
912 MojoHandle h_passed
[2];
913 EXPECT_EQ(MOJO_RESULT_OK
,
914 core()->CreateMessagePipe(NullUserPointer(),
915 MakeUserPointer(&h_passed
[0]),
916 MakeUserPointer(&h_passed
[1])));
918 // Make sure that |h_passed[]| work properly.
919 EXPECT_EQ(MOJO_RESULT_OK
,
920 core()->WriteMessage(h_passed
[0],
921 UserPointer
<const void>(kHello
),
925 MOJO_WRITE_MESSAGE_FLAG_NONE
));
926 hss
= kEmptyMojoHandleSignalsState
;
927 EXPECT_EQ(MOJO_RESULT_OK
,
928 core()->Wait(h_passed
[1],
929 MOJO_HANDLE_SIGNAL_READABLE
,
931 MakeUserPointer(&hss
)));
932 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
933 hss
.satisfied_signals
);
934 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
935 hss
.satisfiable_signals
);
936 num_bytes
= kBufferSize
;
937 num_handles
= arraysize(handles
);
938 EXPECT_EQ(MOJO_RESULT_OK
,
939 core()->ReadMessage(h_passed
[1],
940 UserPointer
<void>(buffer
),
941 MakeUserPointer(&num_bytes
),
942 MakeUserPointer(handles
),
943 MakeUserPointer(&num_handles
),
944 MOJO_READ_MESSAGE_FLAG_NONE
));
945 EXPECT_EQ(kHelloSize
, num_bytes
);
946 EXPECT_STREQ(kHello
, buffer
);
947 EXPECT_EQ(0u, num_handles
);
949 // Send |h_passed[1]| from |h_passing[0]| to |h_passing[1]|.
950 EXPECT_EQ(MOJO_RESULT_OK
,
951 core()->WriteMessage(h_passing
[0],
952 UserPointer
<const void>(kWorld
),
954 MakeUserPointer(&h_passed
[1]),
956 MOJO_WRITE_MESSAGE_FLAG_NONE
));
957 hss
= kEmptyMojoHandleSignalsState
;
958 EXPECT_EQ(MOJO_RESULT_OK
,
959 core()->Wait(h_passing
[1],
960 MOJO_HANDLE_SIGNAL_READABLE
,
962 MakeUserPointer(&hss
)));
963 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
964 hss
.satisfied_signals
);
965 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
966 hss
.satisfiable_signals
);
967 num_bytes
= kBufferSize
;
968 num_handles
= arraysize(handles
);
969 EXPECT_EQ(MOJO_RESULT_OK
,
970 core()->ReadMessage(h_passing
[1],
971 UserPointer
<void>(buffer
),
972 MakeUserPointer(&num_bytes
),
973 MakeUserPointer(handles
),
974 MakeUserPointer(&num_handles
),
975 MOJO_READ_MESSAGE_FLAG_NONE
));
976 EXPECT_EQ(kWorldSize
, num_bytes
);
977 EXPECT_STREQ(kWorld
, buffer
);
978 EXPECT_EQ(1u, num_handles
);
979 h_received
= handles
[0];
980 EXPECT_NE(h_received
, MOJO_HANDLE_INVALID
);
981 EXPECT_NE(h_received
, h_passing
[0]);
982 EXPECT_NE(h_received
, h_passing
[1]);
983 EXPECT_NE(h_received
, h_passed
[0]);
985 // Note: We rely on the Mojo system not re-using handle values very often.
986 EXPECT_NE(h_received
, h_passed
[1]);
988 // |h_passed[1]| should no longer be valid; check that trying to close it
989 // fails. See above note.
990 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(h_passed
[1]));
992 // Write to |h_passed[0]|. Should receive on |h_received|.
993 EXPECT_EQ(MOJO_RESULT_OK
,
994 core()->WriteMessage(h_passed
[0],
995 UserPointer
<const void>(kHello
),
999 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1000 hss
= kEmptyMojoHandleSignalsState
;
1001 EXPECT_EQ(MOJO_RESULT_OK
,
1002 core()->Wait(h_received
,
1003 MOJO_HANDLE_SIGNAL_READABLE
,
1005 MakeUserPointer(&hss
)));
1006 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1007 hss
.satisfied_signals
);
1008 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1009 hss
.satisfiable_signals
);
1010 num_bytes
= kBufferSize
;
1011 num_handles
= arraysize(handles
);
1012 EXPECT_EQ(MOJO_RESULT_OK
,
1013 core()->ReadMessage(h_received
,
1014 UserPointer
<void>(buffer
),
1015 MakeUserPointer(&num_bytes
),
1016 MakeUserPointer(handles
),
1017 MakeUserPointer(&num_handles
),
1018 MOJO_READ_MESSAGE_FLAG_NONE
));
1019 EXPECT_EQ(kHelloSize
, num_bytes
);
1020 EXPECT_STREQ(kHello
, buffer
);
1021 EXPECT_EQ(0u, num_handles
);
1023 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h_passing
[0]));
1024 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h_passing
[1]));
1025 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h_passed
[0]));
1026 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h_received
));
1029 TEST_F(CoreTest
, DataPipe
) {
1030 MojoHandle ph
, ch
; // p is for producer and c is for consumer.
1031 MojoHandleSignalsState hss
;
1033 EXPECT_EQ(MOJO_RESULT_OK
,
1034 core()->CreateDataPipe(
1035 NullUserPointer(), MakeUserPointer(&ph
), MakeUserPointer(&ch
)));
1036 // Should get two distinct, valid handles.
1037 EXPECT_NE(ph
, MOJO_HANDLE_INVALID
);
1038 EXPECT_NE(ch
, MOJO_HANDLE_INVALID
);
1041 // Producer should be never-readable, but already writable.
1042 hss
= kEmptyMojoHandleSignalsState
;
1044 MOJO_RESULT_FAILED_PRECONDITION
,
1045 core()->Wait(ph
, MOJO_HANDLE_SIGNAL_READABLE
, 0, MakeUserPointer(&hss
)));
1046 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
.satisfied_signals
);
1047 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
.satisfiable_signals
);
1048 hss
= kEmptyMojoHandleSignalsState
;
1051 core()->Wait(ph
, MOJO_HANDLE_SIGNAL_WRITABLE
, 0, MakeUserPointer(&hss
)));
1052 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
.satisfied_signals
);
1053 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE
, hss
.satisfiable_signals
);
1055 // Consumer should be never-writable, and not yet readable.
1056 hss
= kFullMojoHandleSignalsState
;
1058 MOJO_RESULT_FAILED_PRECONDITION
,
1059 core()->Wait(ch
, MOJO_HANDLE_SIGNAL_WRITABLE
, 0, MakeUserPointer(&hss
)));
1060 EXPECT_EQ(0u, hss
.satisfied_signals
);
1061 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1062 hss
= kFullMojoHandleSignalsState
;
1064 MOJO_RESULT_DEADLINE_EXCEEDED
,
1065 core()->Wait(ch
, MOJO_HANDLE_SIGNAL_READABLE
, 0, MakeUserPointer(&hss
)));
1066 EXPECT_EQ(0u, hss
.satisfied_signals
);
1067 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1070 char elements
[2] = {'A', 'B'};
1071 uint32_t num_bytes
= 2u;
1072 EXPECT_EQ(MOJO_RESULT_OK
,
1073 core()->WriteData(ph
,
1074 UserPointer
<const void>(elements
),
1075 MakeUserPointer(&num_bytes
),
1076 MOJO_WRITE_DATA_FLAG_NONE
));
1077 EXPECT_EQ(2u, num_bytes
);
1079 // Consumer should now be readable.
1080 hss
= kEmptyMojoHandleSignalsState
;
1083 core()->Wait(ch
, MOJO_HANDLE_SIGNAL_READABLE
, 0, MakeUserPointer(&hss
)));
1084 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfied_signals
);
1085 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1087 // Read one character.
1091 EXPECT_EQ(MOJO_RESULT_OK
,
1092 core()->ReadData(ch
,
1093 UserPointer
<void>(elements
),
1094 MakeUserPointer(&num_bytes
),
1095 MOJO_READ_DATA_FLAG_NONE
));
1096 EXPECT_EQ('A', elements
[0]);
1097 EXPECT_EQ(-1, elements
[1]);
1100 void* write_ptr
= NULL
;
1102 ASSERT_EQ(MOJO_RESULT_OK
,
1103 core()->BeginWriteData(ph
,
1104 MakeUserPointer(&write_ptr
),
1105 MakeUserPointer(&num_bytes
),
1106 MOJO_WRITE_DATA_FLAG_NONE
));
1107 // We count on the default options providing a decent buffer size.
1108 ASSERT_GE(num_bytes
, 3u);
1110 // Trying to do a normal write during a two-phase write should fail.
1113 EXPECT_EQ(MOJO_RESULT_BUSY
,
1114 core()->WriteData(ph
,
1115 UserPointer
<const void>(elements
),
1116 MakeUserPointer(&num_bytes
),
1117 MOJO_WRITE_DATA_FLAG_NONE
));
1119 // Actually write the data, and complete it now.
1120 static_cast<char*>(write_ptr
)[0] = 'C';
1121 static_cast<char*>(write_ptr
)[1] = 'D';
1122 static_cast<char*>(write_ptr
)[2] = 'E';
1123 EXPECT_EQ(MOJO_RESULT_OK
, core()->EndWriteData(ph
, 3u));
1125 // Query how much data we have.
1127 EXPECT_EQ(MOJO_RESULT_OK
,
1128 core()->ReadData(ch
,
1130 MakeUserPointer(&num_bytes
),
1131 MOJO_READ_DATA_FLAG_QUERY
));
1132 EXPECT_EQ(4u, num_bytes
);
1134 // Try to discard ten characters, in all-or-none mode. Should fail.
1136 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE
,
1140 MakeUserPointer(&num_bytes
),
1141 MOJO_READ_DATA_FLAG_DISCARD
| MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
1143 // Discard two characters.
1145 EXPECT_EQ(MOJO_RESULT_OK
,
1149 MakeUserPointer(&num_bytes
),
1150 MOJO_READ_DATA_FLAG_DISCARD
| MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
1152 // Read the remaining two characters, in two-phase mode (all-or-none).
1153 const void* read_ptr
= NULL
;
1155 ASSERT_EQ(MOJO_RESULT_OK
,
1156 core()->BeginReadData(ch
,
1157 MakeUserPointer(&read_ptr
),
1158 MakeUserPointer(&num_bytes
),
1159 MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
1160 // Note: Count on still being able to do the contiguous read here.
1161 ASSERT_EQ(2u, num_bytes
);
1163 // Discarding right now should fail.
1165 EXPECT_EQ(MOJO_RESULT_BUSY
,
1166 core()->ReadData(ch
,
1168 MakeUserPointer(&num_bytes
),
1169 MOJO_READ_DATA_FLAG_DISCARD
));
1171 // Actually check our data and end the two-phase read.
1172 EXPECT_EQ('D', static_cast<const char*>(read_ptr
)[0]);
1173 EXPECT_EQ('E', static_cast<const char*>(read_ptr
)[1]);
1174 EXPECT_EQ(MOJO_RESULT_OK
, core()->EndReadData(ch
, 2u));
1176 // Consumer should now be no longer readable.
1177 hss
= kFullMojoHandleSignalsState
;
1179 MOJO_RESULT_DEADLINE_EXCEEDED
,
1180 core()->Wait(ch
, MOJO_HANDLE_SIGNAL_READABLE
, 0, MakeUserPointer(&hss
)));
1181 EXPECT_EQ(0u, hss
.satisfied_signals
);
1182 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1186 // Close the producer.
1187 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(ph
));
1189 // The consumer should now be never-readable.
1190 hss
= kFullMojoHandleSignalsState
;
1192 MOJO_RESULT_FAILED_PRECONDITION
,
1193 core()->Wait(ch
, MOJO_HANDLE_SIGNAL_READABLE
, 0, MakeUserPointer(&hss
)));
1194 EXPECT_EQ(0u, hss
.satisfied_signals
);
1195 EXPECT_EQ(0u, hss
.satisfiable_signals
);
1197 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(ch
));
1200 // Tests passing data pipe producer and consumer handles.
1201 TEST_F(CoreTest
, MessagePipeBasicLocalHandlePassing2
) {
1202 const char kHello
[] = "hello";
1203 const uint32_t kHelloSize
= static_cast<uint32_t>(sizeof(kHello
));
1204 const char kWorld
[] = "world!!!";
1205 const uint32_t kWorldSize
= static_cast<uint32_t>(sizeof(kWorld
));
1207 const uint32_t kBufferSize
= static_cast<uint32_t>(sizeof(buffer
));
1209 MojoHandle handles
[10];
1210 uint32_t num_handles
;
1211 MojoHandleSignalsState hss
;
1213 MojoHandle h_passing
[2];
1214 EXPECT_EQ(MOJO_RESULT_OK
,
1215 core()->CreateMessagePipe(NullUserPointer(),
1216 MakeUserPointer(&h_passing
[0]),
1217 MakeUserPointer(&h_passing
[1])));
1220 EXPECT_EQ(MOJO_RESULT_OK
,
1221 core()->CreateDataPipe(
1222 NullUserPointer(), MakeUserPointer(&ph
), MakeUserPointer(&ch
)));
1224 // Send |ch| from |h_passing[0]| to |h_passing[1]|.
1225 EXPECT_EQ(MOJO_RESULT_OK
,
1226 core()->WriteMessage(h_passing
[0],
1227 UserPointer
<const void>(kHello
),
1229 MakeUserPointer(&ch
),
1231 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1232 hss
= kEmptyMojoHandleSignalsState
;
1233 EXPECT_EQ(MOJO_RESULT_OK
,
1234 core()->Wait(h_passing
[1],
1235 MOJO_HANDLE_SIGNAL_READABLE
,
1237 MakeUserPointer(&hss
)));
1238 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1239 hss
.satisfied_signals
);
1240 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1241 hss
.satisfiable_signals
);
1242 num_bytes
= kBufferSize
;
1243 num_handles
= arraysize(handles
);
1244 EXPECT_EQ(MOJO_RESULT_OK
,
1245 core()->ReadMessage(h_passing
[1],
1246 UserPointer
<void>(buffer
),
1247 MakeUserPointer(&num_bytes
),
1248 MakeUserPointer(handles
),
1249 MakeUserPointer(&num_handles
),
1250 MOJO_READ_MESSAGE_FLAG_NONE
));
1251 EXPECT_EQ(kHelloSize
, num_bytes
);
1252 EXPECT_STREQ(kHello
, buffer
);
1253 EXPECT_EQ(1u, num_handles
);
1254 MojoHandle ch_received
= handles
[0];
1255 EXPECT_NE(ch_received
, MOJO_HANDLE_INVALID
);
1256 EXPECT_NE(ch_received
, h_passing
[0]);
1257 EXPECT_NE(ch_received
, h_passing
[1]);
1258 EXPECT_NE(ch_received
, ph
);
1260 // Note: We rely on the Mojo system not re-using handle values very often.
1261 EXPECT_NE(ch_received
, ch
);
1263 // |ch| should no longer be valid; check that trying to close it fails. See
1265 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(ch
));
1267 // Write to |ph|. Should receive on |ch_received|.
1268 num_bytes
= kWorldSize
;
1269 EXPECT_EQ(MOJO_RESULT_OK
,
1270 core()->WriteData(ph
,
1271 UserPointer
<const void>(kWorld
),
1272 MakeUserPointer(&num_bytes
),
1273 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE
));
1274 hss
= kEmptyMojoHandleSignalsState
;
1275 EXPECT_EQ(MOJO_RESULT_OK
,
1276 core()->Wait(ch_received
,
1277 MOJO_HANDLE_SIGNAL_READABLE
,
1279 MakeUserPointer(&hss
)));
1280 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfied_signals
);
1281 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1282 num_bytes
= kBufferSize
;
1283 EXPECT_EQ(MOJO_RESULT_OK
,
1284 core()->ReadData(ch_received
,
1285 UserPointer
<void>(buffer
),
1286 MakeUserPointer(&num_bytes
),
1287 MOJO_READ_MESSAGE_FLAG_NONE
));
1288 EXPECT_EQ(kWorldSize
, num_bytes
);
1289 EXPECT_STREQ(kWorld
, buffer
);
1291 // Now pass |ph| in the same direction.
1292 EXPECT_EQ(MOJO_RESULT_OK
,
1293 core()->WriteMessage(h_passing
[0],
1294 UserPointer
<const void>(kWorld
),
1296 MakeUserPointer(&ph
),
1298 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1299 hss
= kEmptyMojoHandleSignalsState
;
1300 EXPECT_EQ(MOJO_RESULT_OK
,
1301 core()->Wait(h_passing
[1],
1302 MOJO_HANDLE_SIGNAL_READABLE
,
1304 MakeUserPointer(&hss
)));
1305 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1306 hss
.satisfied_signals
);
1307 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1308 hss
.satisfiable_signals
);
1309 num_bytes
= kBufferSize
;
1310 num_handles
= arraysize(handles
);
1311 EXPECT_EQ(MOJO_RESULT_OK
,
1312 core()->ReadMessage(h_passing
[1],
1313 UserPointer
<void>(buffer
),
1314 MakeUserPointer(&num_bytes
),
1315 MakeUserPointer(handles
),
1316 MakeUserPointer(&num_handles
),
1317 MOJO_READ_MESSAGE_FLAG_NONE
));
1318 EXPECT_EQ(kWorldSize
, num_bytes
);
1319 EXPECT_STREQ(kWorld
, buffer
);
1320 EXPECT_EQ(1u, num_handles
);
1321 MojoHandle ph_received
= handles
[0];
1322 EXPECT_NE(ph_received
, MOJO_HANDLE_INVALID
);
1323 EXPECT_NE(ph_received
, h_passing
[0]);
1324 EXPECT_NE(ph_received
, h_passing
[1]);
1325 EXPECT_NE(ph_received
, ch_received
);
1327 // Again, rely on the Mojo system not re-using handle values very often.
1328 EXPECT_NE(ph_received
, ph
);
1330 // |ph| should no longer be valid; check that trying to close it fails. See
1332 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT
, core()->Close(ph
));
1334 // Write to |ph_received|. Should receive on |ch_received|.
1335 num_bytes
= kHelloSize
;
1336 EXPECT_EQ(MOJO_RESULT_OK
,
1337 core()->WriteData(ph_received
,
1338 UserPointer
<const void>(kHello
),
1339 MakeUserPointer(&num_bytes
),
1340 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE
));
1341 hss
= kEmptyMojoHandleSignalsState
;
1342 EXPECT_EQ(MOJO_RESULT_OK
,
1343 core()->Wait(ch_received
,
1344 MOJO_HANDLE_SIGNAL_READABLE
,
1346 MakeUserPointer(&hss
)));
1347 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfied_signals
);
1348 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1349 num_bytes
= kBufferSize
;
1350 EXPECT_EQ(MOJO_RESULT_OK
,
1351 core()->ReadData(ch_received
,
1352 UserPointer
<void>(buffer
),
1353 MakeUserPointer(&num_bytes
),
1354 MOJO_READ_MESSAGE_FLAG_NONE
));
1355 EXPECT_EQ(kHelloSize
, num_bytes
);
1356 EXPECT_STREQ(kHello
, buffer
);
1359 ph_received
= MOJO_HANDLE_INVALID
;
1361 ch_received
= MOJO_HANDLE_INVALID
;
1363 // Make sure that |ph| can't be sent if it's in a two-phase write.
1364 void* write_ptr
= NULL
;
1366 ASSERT_EQ(MOJO_RESULT_OK
,
1367 core()->BeginWriteData(ph
,
1368 MakeUserPointer(&write_ptr
),
1369 MakeUserPointer(&num_bytes
),
1370 MOJO_WRITE_DATA_FLAG_NONE
));
1371 ASSERT_GE(num_bytes
, 1u);
1372 EXPECT_EQ(MOJO_RESULT_BUSY
,
1373 core()->WriteMessage(h_passing
[0],
1374 UserPointer
<const void>(kHello
),
1376 MakeUserPointer(&ph
),
1378 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1380 // But |ch| can, even if |ph| is in a two-phase write.
1381 EXPECT_EQ(MOJO_RESULT_OK
,
1382 core()->WriteMessage(h_passing
[0],
1383 UserPointer
<const void>(kHello
),
1385 MakeUserPointer(&ch
),
1387 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1388 ch
= MOJO_HANDLE_INVALID
;
1389 EXPECT_EQ(MOJO_RESULT_OK
,
1390 core()->Wait(h_passing
[1],
1391 MOJO_HANDLE_SIGNAL_READABLE
,
1393 NullUserPointer()));
1394 num_bytes
= kBufferSize
;
1395 num_handles
= arraysize(handles
);
1396 EXPECT_EQ(MOJO_RESULT_OK
,
1397 core()->ReadMessage(h_passing
[1],
1398 UserPointer
<void>(buffer
),
1399 MakeUserPointer(&num_bytes
),
1400 MakeUserPointer(handles
),
1401 MakeUserPointer(&num_handles
),
1402 MOJO_READ_MESSAGE_FLAG_NONE
));
1403 EXPECT_EQ(kHelloSize
, num_bytes
);
1404 EXPECT_STREQ(kHello
, buffer
);
1405 EXPECT_EQ(1u, num_handles
);
1407 EXPECT_NE(ch
, MOJO_HANDLE_INVALID
);
1409 // Complete the two-phase write.
1410 static_cast<char*>(write_ptr
)[0] = 'x';
1411 EXPECT_EQ(MOJO_RESULT_OK
, core()->EndWriteData(ph
, 1));
1413 // Wait for |ch| to be readable.
1414 hss
= kEmptyMojoHandleSignalsState
;
1418 ch
, MOJO_HANDLE_SIGNAL_READABLE
, 1000000000, MakeUserPointer(&hss
)));
1419 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfied_signals
);
1420 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
, hss
.satisfiable_signals
);
1422 // Make sure that |ch| can't be sent if it's in a two-phase read.
1423 const void* read_ptr
= NULL
;
1425 ASSERT_EQ(MOJO_RESULT_OK
,
1426 core()->BeginReadData(ch
,
1427 MakeUserPointer(&read_ptr
),
1428 MakeUserPointer(&num_bytes
),
1429 MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
1430 EXPECT_EQ(MOJO_RESULT_BUSY
,
1431 core()->WriteMessage(h_passing
[0],
1432 UserPointer
<const void>(kHello
),
1434 MakeUserPointer(&ch
),
1436 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1438 // But |ph| can, even if |ch| is in a two-phase read.
1439 EXPECT_EQ(MOJO_RESULT_OK
,
1440 core()->WriteMessage(h_passing
[0],
1441 UserPointer
<const void>(kWorld
),
1443 MakeUserPointer(&ph
),
1445 MOJO_WRITE_MESSAGE_FLAG_NONE
));
1446 ph
= MOJO_HANDLE_INVALID
;
1447 hss
= kEmptyMojoHandleSignalsState
;
1448 EXPECT_EQ(MOJO_RESULT_OK
,
1449 core()->Wait(h_passing
[1],
1450 MOJO_HANDLE_SIGNAL_READABLE
,
1452 MakeUserPointer(&hss
)));
1453 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1454 hss
.satisfied_signals
);
1455 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE
| MOJO_HANDLE_SIGNAL_WRITABLE
,
1456 hss
.satisfiable_signals
);
1457 num_bytes
= kBufferSize
;
1458 num_handles
= arraysize(handles
);
1459 EXPECT_EQ(MOJO_RESULT_OK
,
1460 core()->ReadMessage(h_passing
[1],
1461 UserPointer
<void>(buffer
),
1462 MakeUserPointer(&num_bytes
),
1463 MakeUserPointer(handles
),
1464 MakeUserPointer(&num_handles
),
1465 MOJO_READ_MESSAGE_FLAG_NONE
));
1466 EXPECT_EQ(kWorldSize
, num_bytes
);
1467 EXPECT_STREQ(kWorld
, buffer
);
1468 EXPECT_EQ(1u, num_handles
);
1470 EXPECT_NE(ph
, MOJO_HANDLE_INVALID
);
1472 // Complete the two-phase read.
1473 EXPECT_EQ('x', static_cast<const char*>(read_ptr
)[0]);
1474 EXPECT_EQ(MOJO_RESULT_OK
, core()->EndReadData(ch
, 1));
1476 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h_passing
[0]));
1477 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(h_passing
[1]));
1478 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(ph
));
1479 EXPECT_EQ(MOJO_RESULT_OK
, core()->Close(ch
));
1482 // TODO(vtl): Test |DuplicateBufferHandle()| and |MapBuffer()|.
1485 } // namespace system