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. 4 5 #include "mojo/system/core.h" 6 7 #include <limits> 8 9 #include "base/basictypes.h" 10 #include "base/threading/platform_thread.h" 11 #include "base/time/time.h" 12 #include "mojo/system/core_test_base.h" 13 14 namespace mojo { 15 namespace system { 16 namespace { 17 18 typedef test::CoreTestBase CoreTest; 19 20 TEST_F(CoreTest, GetTimeTicksNow) { 21 const MojoTimeTicks start = core()->GetTimeTicksNow(); 22 EXPECT_NE(static_cast<MojoTimeTicks>(0), start) 23 << "GetTimeTicksNow should return nonzero value"; 24 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15)); 25 const MojoTimeTicks finish = core()->GetTimeTicksNow(); 26 // Allow for some fuzz in sleep. 27 EXPECT_GE((finish - start), static_cast<MojoTimeTicks>(8000)) 28 << "Sleeping should result in increasing time ticks"; 29 } 30 31 TEST_F(CoreTest, Basic) { 32 MockHandleInfo info; 33 34 EXPECT_EQ(0u, info.GetCtorCallCount()); 35 MojoHandle h = CreateMockHandle(&info); 36 EXPECT_EQ(1u, info.GetCtorCallCount()); 37 EXPECT_NE(h, MOJO_HANDLE_INVALID); 38 39 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 40 EXPECT_EQ(MOJO_RESULT_OK, 41 core()->WriteMessage(h, NULL, 0, NULL, 0, 42 MOJO_WRITE_MESSAGE_FLAG_NONE)); 43 EXPECT_EQ(1u, info.GetWriteMessageCallCount()); 44 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 45 core()->WriteMessage(h, NULL, 1, NULL, 0, 46 MOJO_WRITE_MESSAGE_FLAG_NONE)); 47 EXPECT_EQ(2u, info.GetWriteMessageCallCount()); 48 49 EXPECT_EQ(0u, info.GetReadMessageCallCount()); 50 uint32_t num_bytes = 0; 51 EXPECT_EQ(MOJO_RESULT_OK, 52 core()->ReadMessage(h, NULL, &num_bytes, NULL, NULL, 53 MOJO_READ_MESSAGE_FLAG_NONE)); 54 EXPECT_EQ(1u, info.GetReadMessageCallCount()); 55 num_bytes = 1; 56 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 57 core()->ReadMessage(h, NULL, &num_bytes, NULL, NULL, 58 MOJO_READ_MESSAGE_FLAG_NONE)); 59 EXPECT_EQ(2u, info.GetReadMessageCallCount()); 60 EXPECT_EQ(MOJO_RESULT_OK, 61 core()->ReadMessage(h, NULL, NULL, NULL, NULL, 62 MOJO_READ_MESSAGE_FLAG_NONE)); 63 EXPECT_EQ(3u, info.GetReadMessageCallCount()); 64 65 EXPECT_EQ(0u, info.GetWriteDataCallCount()); 66 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 67 core()->WriteData(h, NULL, NULL, MOJO_WRITE_DATA_FLAG_NONE)); 68 EXPECT_EQ(1u, info.GetWriteDataCallCount()); 69 70 EXPECT_EQ(0u, info.GetBeginWriteDataCallCount()); 71 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 72 core()->BeginWriteData(h, NULL, NULL, MOJO_WRITE_DATA_FLAG_NONE)); 73 EXPECT_EQ(1u, info.GetBeginWriteDataCallCount()); 74 75 EXPECT_EQ(0u, info.GetEndWriteDataCallCount()); 76 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 77 core()->EndWriteData(h, 0)); 78 EXPECT_EQ(1u, info.GetEndWriteDataCallCount()); 79 80 EXPECT_EQ(0u, info.GetReadDataCallCount()); 81 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 82 core()->ReadData(h, NULL, NULL, MOJO_READ_DATA_FLAG_NONE)); 83 EXPECT_EQ(1u, info.GetReadDataCallCount()); 84 85 EXPECT_EQ(0u, info.GetBeginReadDataCallCount()); 86 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 87 core()->BeginReadData(h, NULL, NULL, MOJO_READ_DATA_FLAG_NONE)); 88 EXPECT_EQ(1u, info.GetBeginReadDataCallCount()); 89 90 EXPECT_EQ(0u, info.GetEndReadDataCallCount()); 91 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 92 core()->EndReadData(h, 0)); 93 EXPECT_EQ(1u, info.GetEndReadDataCallCount()); 94 95 EXPECT_EQ(0u, info.GetAddWaiterCallCount()); 96 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 97 core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, 98 MOJO_DEADLINE_INDEFINITE)); 99 EXPECT_EQ(1u, info.GetAddWaiterCallCount()); 100 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 101 core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, 0)); 102 EXPECT_EQ(2u, info.GetAddWaiterCallCount()); 103 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 104 core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, 10 * 1000)); 105 EXPECT_EQ(3u, info.GetAddWaiterCallCount()); 106 MojoHandleSignals handle_signals = ~MOJO_HANDLE_SIGNAL_NONE; 107 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 108 core()->WaitMany(&h, &handle_signals, 1, MOJO_DEADLINE_INDEFINITE)); 109 EXPECT_EQ(4u, info.GetAddWaiterCallCount()); 110 111 EXPECT_EQ(0u, info.GetDtorCallCount()); 112 EXPECT_EQ(0u, info.GetCloseCallCount()); 113 EXPECT_EQ(0u, info.GetCancelAllWaitersCallCount()); 114 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); 115 EXPECT_EQ(1u, info.GetCancelAllWaitersCallCount()); 116 EXPECT_EQ(1u, info.GetCloseCallCount()); 117 EXPECT_EQ(1u, info.GetDtorCallCount()); 118 119 // No waiters should ever have ever been added. 120 EXPECT_EQ(0u, info.GetRemoveWaiterCallCount()); 121 } 122 123 TEST_F(CoreTest, InvalidArguments) { 124 // |Close()|: 125 { 126 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(MOJO_HANDLE_INVALID)); 127 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(10)); 128 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(1000000000)); 129 130 // Test a double-close. 131 MockHandleInfo info; 132 MojoHandle h = CreateMockHandle(&info); 133 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); 134 EXPECT_EQ(1u, info.GetCloseCallCount()); 135 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(h)); 136 EXPECT_EQ(1u, info.GetCloseCallCount()); 137 } 138 139 // |Wait()|: 140 { 141 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 142 core()->Wait(MOJO_HANDLE_INVALID, ~MOJO_HANDLE_SIGNAL_NONE, 143 MOJO_DEADLINE_INDEFINITE)); 144 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 145 core()->Wait(10, ~MOJO_HANDLE_SIGNAL_NONE, 146 MOJO_DEADLINE_INDEFINITE)); 147 } 148 149 // |WaitMany()|: 150 { 151 MojoHandle handles[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID}; 152 MojoHandleSignals signals[2] = {~MOJO_HANDLE_SIGNAL_NONE, 153 ~MOJO_HANDLE_SIGNAL_NONE}; 154 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 155 core()->WaitMany(handles, signals, 0, MOJO_DEADLINE_INDEFINITE)); 156 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 157 core()->WaitMany(NULL, signals, 0, MOJO_DEADLINE_INDEFINITE)); 158 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 159 core()->WaitMany(handles, NULL, 0, MOJO_DEADLINE_INDEFINITE)); 160 161 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 162 core()->WaitMany(NULL, signals, 1, MOJO_DEADLINE_INDEFINITE)); 163 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 164 core()->WaitMany(handles, NULL, 1, MOJO_DEADLINE_INDEFINITE)); 165 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 166 core()->WaitMany(handles, signals, 1, MOJO_DEADLINE_INDEFINITE)); 167 168 MockHandleInfo info[2]; 169 handles[0] = CreateMockHandle(&info[0]); 170 171 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 172 core()->WaitMany(handles, signals, 1, MOJO_DEADLINE_INDEFINITE)); 173 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 174 core()->WaitMany(handles, signals, 2, MOJO_DEADLINE_INDEFINITE)); 175 handles[1] = handles[0] + 1; // Invalid handle. 176 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 177 core()->WaitMany(handles, signals, 2, MOJO_DEADLINE_INDEFINITE)); 178 handles[1] = CreateMockHandle(&info[1]); 179 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 180 core()->WaitMany(handles, signals, 2, MOJO_DEADLINE_INDEFINITE)); 181 182 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(handles[0])); 183 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(handles[1])); 184 } 185 186 // |CreateMessagePipe()|: 187 { 188 MojoHandle h; 189 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 190 core()->CreateMessagePipe(NULL, NULL, NULL)); 191 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 192 core()->CreateMessagePipe(NULL, &h, NULL)); 193 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 194 core()->CreateMessagePipe(NULL, NULL, &h)); 195 } 196 197 // |WriteMessage()|: 198 // Only check arguments checked by |Core|, namely |handle|, |handles|, and 199 // |num_handles|. 200 { 201 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 202 core()->WriteMessage(MOJO_HANDLE_INVALID, NULL, 0, NULL, 0, 203 MOJO_WRITE_MESSAGE_FLAG_NONE)); 204 205 MockHandleInfo info; 206 MojoHandle h = CreateMockHandle(&info); 207 MojoHandle handles[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID}; 208 209 // Null |handles| with nonzero |num_handles|. 210 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 211 core()->WriteMessage(h, NULL, 0, NULL, 1, 212 MOJO_WRITE_MESSAGE_FLAG_NONE)); 213 // Checked by |Core|, shouldn't go through to the dispatcher. 214 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 215 216 // Huge handle count (implausibly big on some systems -- more than can be 217 // stored in a 32-bit address space). 218 // Note: This may return either |MOJO_RESULT_INVALID_ARGUMENT| or 219 // |MOJO_RESULT_RESOURCE_EXHAUSTED|, depending on whether it's plausible or 220 // not. 221 EXPECT_NE(MOJO_RESULT_OK, 222 core()->WriteMessage(h, NULL, 0, handles, 223 std::numeric_limits<uint32_t>::max(), 224 MOJO_WRITE_MESSAGE_FLAG_NONE)); 225 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 226 227 // Huge handle count (plausibly big). 228 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, 229 core()->WriteMessage(h, NULL, 0, handles, 230 std::numeric_limits<uint32_t>::max() / 231 sizeof(handles[0]), 232 MOJO_WRITE_MESSAGE_FLAG_NONE)); 233 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 234 235 // Invalid handle in |handles|. 236 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 237 core()->WriteMessage(h, NULL, 0, handles, 1, 238 MOJO_WRITE_MESSAGE_FLAG_NONE)); 239 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 240 241 // Two invalid handles in |handles|. 242 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 243 core()->WriteMessage(h, NULL, 0, handles, 2, 244 MOJO_WRITE_MESSAGE_FLAG_NONE)); 245 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 246 247 // Can't send a handle over itself. 248 handles[0] = h; 249 EXPECT_EQ(MOJO_RESULT_BUSY, 250 core()->WriteMessage(h, NULL, 0, handles, 1, 251 MOJO_WRITE_MESSAGE_FLAG_NONE)); 252 EXPECT_EQ(0u, info.GetWriteMessageCallCount()); 253 254 MockHandleInfo info2; 255 MojoHandle h2 = CreateMockHandle(&info2); 256 257 // This is "okay", but |MockDispatcher| doesn't implement it. 258 handles[0] = h2; 259 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 260 core()->WriteMessage(h, NULL, 0, handles, 1, 261 MOJO_WRITE_MESSAGE_FLAG_NONE)); 262 EXPECT_EQ(1u, info.GetWriteMessageCallCount()); 263 264 // One of the |handles| is still invalid. 265 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 266 core()->WriteMessage(h, NULL, 0, handles, 2, 267 MOJO_WRITE_MESSAGE_FLAG_NONE)); 268 EXPECT_EQ(1u, info.GetWriteMessageCallCount()); 269 270 // One of the |handles| is the same as |handle|. 271 handles[1] = h; 272 EXPECT_EQ(MOJO_RESULT_BUSY, 273 core()->WriteMessage(h, NULL, 0, handles, 2, 274 MOJO_WRITE_MESSAGE_FLAG_NONE)); 275 EXPECT_EQ(1u, info.GetWriteMessageCallCount()); 276 277 // Can't send a handle twice in the same message. 278 handles[1] = h2; 279 EXPECT_EQ(MOJO_RESULT_BUSY, 280 core()->WriteMessage(h, NULL, 0, handles, 2, 281 MOJO_WRITE_MESSAGE_FLAG_NONE)); 282 EXPECT_EQ(1u, info.GetWriteMessageCallCount()); 283 284 // Note: Since we never successfully sent anything with it, |h2| should 285 // still be valid. 286 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h2)); 287 288 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); 289 } 290 291 // |ReadMessage()|: 292 // Only check arguments checked by |Core|, namely |handle|, |handles|, and 293 // |num_handles|. 294 { 295 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 296 core()->ReadMessage(MOJO_HANDLE_INVALID, NULL, NULL, NULL, NULL, 297 MOJO_READ_MESSAGE_FLAG_NONE)); 298 299 MockHandleInfo info; 300 MojoHandle h = CreateMockHandle(&info); 301 302 uint32_t handle_count = 1; 303 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 304 core()->ReadMessage(h, NULL, NULL, NULL, &handle_count, 305 MOJO_READ_MESSAGE_FLAG_NONE)); 306 // Checked by |Core|, shouldn't go through to the dispatcher. 307 EXPECT_EQ(0u, info.GetReadMessageCallCount()); 308 309 // Okay. 310 handle_count = 0; 311 EXPECT_EQ(MOJO_RESULT_OK, 312 core()->ReadMessage(h, NULL, NULL, NULL, &handle_count, 313 MOJO_READ_MESSAGE_FLAG_NONE)); 314 // Checked by |Core|, shouldn't go through to the dispatcher. 315 EXPECT_EQ(1u, info.GetReadMessageCallCount()); 316 317 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); 318 } 319 } 320 321 // TODO(vtl): test |Wait()| and |WaitMany()| properly 322 // - including |WaitMany()| with the same handle more than once (with 323 // same/different signals) 324 325 TEST_F(CoreTest, MessagePipe) { 326 MojoHandle h[2]; 327 328 EXPECT_EQ(MOJO_RESULT_OK, core()->CreateMessagePipe(NULL, &h[0], &h[1])); 329 // Should get two distinct, valid handles. 330 EXPECT_NE(h[0], MOJO_HANDLE_INVALID); 331 EXPECT_NE(h[1], MOJO_HANDLE_INVALID); 332 EXPECT_NE(h[0], h[1]); 333 334 // Neither should be readable. 335 MojoHandleSignals signals[2] = {MOJO_HANDLE_SIGNAL_READABLE, 336 MOJO_HANDLE_SIGNAL_READABLE}; 337 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, 338 core()->WaitMany(h, signals, 2, 0)); 339 340 // Try to read anyway. 341 char buffer[1] = {'a'}; 342 uint32_t buffer_size = 1; 343 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 344 core()->ReadMessage(h[0], buffer, &buffer_size, NULL, NULL, 345 MOJO_READ_MESSAGE_FLAG_NONE)); 346 // Check that it left its inputs alone. 347 EXPECT_EQ('a', buffer[0]); 348 EXPECT_EQ(1u, buffer_size); 349 350 // Both should be writable. 351 EXPECT_EQ(MOJO_RESULT_OK, 352 core()->Wait(h[0], MOJO_HANDLE_SIGNAL_WRITABLE, 1000000000)); 353 EXPECT_EQ(MOJO_RESULT_OK, 354 core()->Wait(h[1], MOJO_HANDLE_SIGNAL_WRITABLE, 1000000000)); 355 356 // Also check that |h[1]| is writable using |WaitMany()|. 357 signals[0] = MOJO_HANDLE_SIGNAL_READABLE; 358 signals[1] = MOJO_HANDLE_SIGNAL_WRITABLE; 359 EXPECT_EQ(1, core()->WaitMany(h, signals, 2, MOJO_DEADLINE_INDEFINITE)); 360 361 // Write to |h[1]|. 362 buffer[0] = 'b'; 363 EXPECT_EQ(MOJO_RESULT_OK, 364 core()->WriteMessage(h[1], buffer, 1, NULL, 0, 365 MOJO_WRITE_MESSAGE_FLAG_NONE)); 366 367 // Check that |h[0]| is now readable. 368 signals[0] = MOJO_HANDLE_SIGNAL_READABLE; 369 signals[1] = MOJO_HANDLE_SIGNAL_READABLE; 370 EXPECT_EQ(0, core()->WaitMany(h, signals, 2, MOJO_DEADLINE_INDEFINITE)); 371 372 // Read from |h[0]|. 373 // First, get only the size. 374 buffer_size = 0; 375 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, 376 core()->ReadMessage(h[0], NULL, &buffer_size, NULL, NULL, 377 MOJO_READ_MESSAGE_FLAG_NONE)); 378 EXPECT_EQ(1u, buffer_size); 379 // Then actually read it. 380 buffer[0] = 'c'; 381 buffer_size = 1; 382 EXPECT_EQ(MOJO_RESULT_OK, 383 core()->ReadMessage(h[0], buffer, &buffer_size, NULL, NULL, 384 MOJO_READ_MESSAGE_FLAG_NONE)); 385 EXPECT_EQ('b', buffer[0]); 386 EXPECT_EQ(1u, buffer_size); 387 388 // |h[0]| should no longer be readable. 389 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, 390 core()->Wait(h[0], MOJO_HANDLE_SIGNAL_READABLE, 0)); 391 392 // Write to |h[0]|. 393 buffer[0] = 'd'; 394 EXPECT_EQ(MOJO_RESULT_OK, 395 core()->WriteMessage(h[0], buffer, 1, NULL, 0, 396 MOJO_WRITE_MESSAGE_FLAG_NONE)); 397 398 // Close |h[0]|. 399 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h[0])); 400 401 // Check that |h[1]| is no longer writable (and will never be). 402 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 403 core()->Wait(h[1], MOJO_HANDLE_SIGNAL_WRITABLE, 1000000000)); 404 405 // Check that |h[1]| is still readable (for the moment). 406 EXPECT_EQ(MOJO_RESULT_OK, 407 core()->Wait(h[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 408 409 // Discard a message from |h[1]|. 410 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, 411 core()->ReadMessage(h[1], NULL, NULL, NULL, NULL, 412 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)); 413 414 // |h[1]| is no longer readable (and will never be). 415 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 416 core()->Wait(h[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 417 418 // Try writing to |h[1]|. 419 buffer[0] = 'e'; 420 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 421 core()->WriteMessage(h[1], buffer, 1, NULL, 0, 422 MOJO_WRITE_MESSAGE_FLAG_NONE)); 423 424 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h[1])); 425 } 426 427 // Tests passing a message pipe handle. 428 TEST_F(CoreTest, MessagePipeBasicLocalHandlePassing1) { 429 const char kHello[] = "hello"; 430 const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); 431 const char kWorld[] = "world!!!"; 432 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); 433 char buffer[100]; 434 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); 435 uint32_t num_bytes; 436 MojoHandle handles[10]; 437 uint32_t num_handles; 438 MojoHandle h_received; 439 440 MojoHandle h_passing[2]; 441 EXPECT_EQ(MOJO_RESULT_OK, 442 core()->CreateMessagePipe(NULL, &h_passing[0], &h_passing[1])); 443 444 // Make sure that |h_passing[]| work properly. 445 EXPECT_EQ(MOJO_RESULT_OK, 446 core()->WriteMessage(h_passing[0], 447 kHello, kHelloSize, 448 NULL, 0, 449 MOJO_WRITE_MESSAGE_FLAG_NONE)); 450 EXPECT_EQ(MOJO_RESULT_OK, 451 core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 452 1000000000)); 453 num_bytes = kBufferSize; 454 num_handles = arraysize(handles); 455 EXPECT_EQ(MOJO_RESULT_OK, 456 core()->ReadMessage(h_passing[1], 457 buffer, &num_bytes, 458 handles, &num_handles, 459 MOJO_READ_MESSAGE_FLAG_NONE)); 460 EXPECT_EQ(kHelloSize, num_bytes); 461 EXPECT_STREQ(kHello, buffer); 462 EXPECT_EQ(0u, num_handles); 463 464 // Make sure that you can't pass either of the message pipe's handles over 465 // itself. 466 EXPECT_EQ(MOJO_RESULT_BUSY, 467 core()->WriteMessage(h_passing[0], 468 kHello, kHelloSize, 469 &h_passing[0], 1, 470 MOJO_WRITE_MESSAGE_FLAG_NONE)); 471 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 472 core()->WriteMessage(h_passing[0], 473 kHello, kHelloSize, 474 &h_passing[1], 1, 475 MOJO_WRITE_MESSAGE_FLAG_NONE)); 476 477 MojoHandle h_passed[2]; 478 EXPECT_EQ(MOJO_RESULT_OK, 479 core()->CreateMessagePipe(NULL, &h_passed[0], &h_passed[1])); 480 481 // Make sure that |h_passed[]| work properly. 482 EXPECT_EQ(MOJO_RESULT_OK, 483 core()->WriteMessage(h_passed[0], 484 kHello, kHelloSize, 485 NULL, 0, 486 MOJO_WRITE_MESSAGE_FLAG_NONE)); 487 EXPECT_EQ(MOJO_RESULT_OK, 488 core()->Wait(h_passed[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 489 num_bytes = kBufferSize; 490 num_handles = arraysize(handles); 491 EXPECT_EQ(MOJO_RESULT_OK, 492 core()->ReadMessage(h_passed[1], 493 buffer, &num_bytes, 494 handles, &num_handles, 495 MOJO_READ_MESSAGE_FLAG_NONE)); 496 EXPECT_EQ(kHelloSize, num_bytes); 497 EXPECT_STREQ(kHello, buffer); 498 EXPECT_EQ(0u, num_handles); 499 500 // Send |h_passed[1]| from |h_passing[0]| to |h_passing[1]|. 501 EXPECT_EQ(MOJO_RESULT_OK, 502 core()->WriteMessage(h_passing[0], 503 kWorld, kWorldSize, 504 &h_passed[1], 1, 505 MOJO_WRITE_MESSAGE_FLAG_NONE)); 506 EXPECT_EQ(MOJO_RESULT_OK, 507 core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 508 1000000000)); 509 num_bytes = kBufferSize; 510 num_handles = arraysize(handles); 511 EXPECT_EQ(MOJO_RESULT_OK, 512 core()->ReadMessage(h_passing[1], 513 buffer, &num_bytes, 514 handles, &num_handles, 515 MOJO_READ_MESSAGE_FLAG_NONE)); 516 EXPECT_EQ(kWorldSize, num_bytes); 517 EXPECT_STREQ(kWorld, buffer); 518 EXPECT_EQ(1u, num_handles); 519 h_received = handles[0]; 520 EXPECT_NE(h_received, MOJO_HANDLE_INVALID); 521 EXPECT_NE(h_received, h_passing[0]); 522 EXPECT_NE(h_received, h_passing[1]); 523 EXPECT_NE(h_received, h_passed[0]); 524 525 // Note: We rely on the Mojo system not re-using handle values very often. 526 EXPECT_NE(h_received, h_passed[1]); 527 528 // |h_passed[1]| should no longer be valid; check that trying to close it 529 // fails. See above note. 530 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(h_passed[1])); 531 532 // Write to |h_passed[0]|. Should receive on |h_received|. 533 EXPECT_EQ(MOJO_RESULT_OK, 534 core()->WriteMessage(h_passed[0], 535 kHello, kHelloSize, 536 NULL, 0, 537 MOJO_WRITE_MESSAGE_FLAG_NONE)); 538 EXPECT_EQ(MOJO_RESULT_OK, 539 core()->Wait(h_received, MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 540 num_bytes = kBufferSize; 541 num_handles = arraysize(handles); 542 EXPECT_EQ(MOJO_RESULT_OK, 543 core()->ReadMessage(h_received, 544 buffer, &num_bytes, 545 handles, &num_handles, 546 MOJO_READ_MESSAGE_FLAG_NONE)); 547 EXPECT_EQ(kHelloSize, num_bytes); 548 EXPECT_STREQ(kHello, buffer); 549 EXPECT_EQ(0u, num_handles); 550 551 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[0])); 552 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[1])); 553 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_passed[0])); 554 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_received)); 555 } 556 557 TEST_F(CoreTest, DataPipe) { 558 MojoHandle ph, ch; // p is for producer and c is for consumer. 559 560 EXPECT_EQ(MOJO_RESULT_OK, core()->CreateDataPipe(NULL, &ph, &ch)); 561 // Should get two distinct, valid handles. 562 EXPECT_NE(ph, MOJO_HANDLE_INVALID); 563 EXPECT_NE(ch, MOJO_HANDLE_INVALID); 564 EXPECT_NE(ph, ch); 565 566 // Producer should be never-readable, but already writable. 567 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 568 core()->Wait(ph, MOJO_HANDLE_SIGNAL_READABLE, 0)); 569 EXPECT_EQ(MOJO_RESULT_OK, 570 core()->Wait(ph, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 571 572 // Consumer should be never-writable, and not yet readable. 573 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 574 core()->Wait(ch, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 575 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, 576 core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0)); 577 578 // Write. 579 char elements[2] = {'A', 'B'}; 580 uint32_t num_bytes = 2u; 581 EXPECT_EQ(MOJO_RESULT_OK, 582 core()->WriteData(ph, elements, &num_bytes, 583 MOJO_WRITE_DATA_FLAG_NONE)); 584 EXPECT_EQ(2u, num_bytes); 585 586 // Consumer should now be readable. 587 EXPECT_EQ(MOJO_RESULT_OK, 588 core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0)); 589 590 // Read one character. 591 elements[0] = -1; 592 elements[1] = -1; 593 num_bytes = 1u; 594 EXPECT_EQ(MOJO_RESULT_OK, 595 core()->ReadData(ch, elements, &num_bytes, 596 MOJO_READ_DATA_FLAG_NONE)); 597 EXPECT_EQ('A', elements[0]); 598 EXPECT_EQ(-1, elements[1]); 599 600 // Two-phase write. 601 void* write_ptr = NULL; 602 num_bytes = 0u; 603 ASSERT_EQ(MOJO_RESULT_OK, 604 core()->BeginWriteData(ph, &write_ptr, &num_bytes, 605 MOJO_WRITE_DATA_FLAG_NONE)); 606 // We count on the default options providing a decent buffer size. 607 ASSERT_GE(num_bytes, 3u); 608 609 // Trying to do a normal write during a two-phase write should fail. 610 elements[0] = 'X'; 611 num_bytes = 1u; 612 EXPECT_EQ(MOJO_RESULT_BUSY, 613 core()->WriteData(ph, elements, &num_bytes, 614 MOJO_WRITE_DATA_FLAG_NONE)); 615 616 // Actually write the data, and complete it now. 617 static_cast<char*>(write_ptr)[0] = 'C'; 618 static_cast<char*>(write_ptr)[1] = 'D'; 619 static_cast<char*>(write_ptr)[2] = 'E'; 620 EXPECT_EQ(MOJO_RESULT_OK, core()->EndWriteData(ph, 3u)); 621 622 // Query how much data we have. 623 num_bytes = 0; 624 EXPECT_EQ(MOJO_RESULT_OK, 625 core()->ReadData(ch, NULL, &num_bytes, MOJO_READ_DATA_FLAG_QUERY)); 626 EXPECT_EQ(4u, num_bytes); 627 628 // Try to discard ten characters, in all-or-none mode. Should fail. 629 num_bytes = 10; 630 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 631 core()->ReadData(ch, NULL, &num_bytes, 632 MOJO_READ_DATA_FLAG_DISCARD | 633 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); 634 635 // Discard two characters. 636 num_bytes = 2; 637 EXPECT_EQ(MOJO_RESULT_OK, 638 core()->ReadData(ch, NULL, &num_bytes, 639 MOJO_READ_DATA_FLAG_DISCARD | 640 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); 641 642 // Read the remaining two characters, in two-phase mode (all-or-none). 643 const void* read_ptr = NULL; 644 num_bytes = 2; 645 ASSERT_EQ(MOJO_RESULT_OK, 646 core()->BeginReadData(ch, &read_ptr, &num_bytes, 647 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); 648 // Note: Count on still being able to do the contiguous read here. 649 ASSERT_EQ(2u, num_bytes); 650 651 // Discarding right now should fail. 652 num_bytes = 1; 653 EXPECT_EQ(MOJO_RESULT_BUSY, 654 core()->ReadData(ch, NULL, &num_bytes, 655 MOJO_READ_DATA_FLAG_DISCARD)); 656 657 // Actually check our data and end the two-phase read. 658 EXPECT_EQ('D', static_cast<const char*>(read_ptr)[0]); 659 EXPECT_EQ('E', static_cast<const char*>(read_ptr)[1]); 660 EXPECT_EQ(MOJO_RESULT_OK, core()->EndReadData(ch, 2u)); 661 662 // Consumer should now be no longer readable. 663 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, 664 core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0)); 665 666 // TODO(vtl): More. 667 668 // Close the producer. 669 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(ph)); 670 671 // The consumer should now be never-readable. 672 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 673 core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0)); 674 675 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(ch)); 676 } 677 678 // Tests passing data pipe producer and consumer handles. 679 TEST_F(CoreTest, MessagePipeBasicLocalHandlePassing2) { 680 const char kHello[] = "hello"; 681 const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); 682 const char kWorld[] = "world!!!"; 683 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); 684 char buffer[100]; 685 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); 686 uint32_t num_bytes; 687 MojoHandle handles[10]; 688 uint32_t num_handles; 689 690 MojoHandle h_passing[2]; 691 EXPECT_EQ(MOJO_RESULT_OK, 692 core()->CreateMessagePipe(NULL, &h_passing[0], &h_passing[1])); 693 694 MojoHandle ph, ch; 695 EXPECT_EQ(MOJO_RESULT_OK, 696 core()->CreateDataPipe(NULL, &ph, &ch)); 697 698 // Send |ch| from |h_passing[0]| to |h_passing[1]|. 699 EXPECT_EQ(MOJO_RESULT_OK, 700 core()->WriteMessage(h_passing[0], 701 kHello, kHelloSize, 702 &ch, 1, 703 MOJO_WRITE_MESSAGE_FLAG_NONE)); 704 EXPECT_EQ(MOJO_RESULT_OK, 705 core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 706 1000000000)); 707 num_bytes = kBufferSize; 708 num_handles = arraysize(handles); 709 EXPECT_EQ(MOJO_RESULT_OK, 710 core()->ReadMessage(h_passing[1], 711 buffer, &num_bytes, 712 handles, &num_handles, 713 MOJO_READ_MESSAGE_FLAG_NONE)); 714 EXPECT_EQ(kHelloSize, num_bytes); 715 EXPECT_STREQ(kHello, buffer); 716 EXPECT_EQ(1u, num_handles); 717 MojoHandle ch_received = handles[0]; 718 EXPECT_NE(ch_received, MOJO_HANDLE_INVALID); 719 EXPECT_NE(ch_received, h_passing[0]); 720 EXPECT_NE(ch_received, h_passing[1]); 721 EXPECT_NE(ch_received, ph); 722 723 // Note: We rely on the Mojo system not re-using handle values very often. 724 EXPECT_NE(ch_received, ch); 725 726 // |ch| should no longer be valid; check that trying to close it fails. See 727 // above note. 728 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(ch)); 729 730 // Write to |ph|. Should receive on |ch_received|. 731 num_bytes = kWorldSize; 732 EXPECT_EQ(MOJO_RESULT_OK, 733 core()->WriteData(ph, kWorld, &num_bytes, 734 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 735 EXPECT_EQ(MOJO_RESULT_OK, 736 core()->Wait(ch_received, MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 737 num_bytes = kBufferSize; 738 EXPECT_EQ(MOJO_RESULT_OK, 739 core()->ReadData(ch_received, buffer, &num_bytes, 740 MOJO_READ_MESSAGE_FLAG_NONE)); 741 EXPECT_EQ(kWorldSize, num_bytes); 742 EXPECT_STREQ(kWorld, buffer); 743 744 // Now pass |ph| in the same direction. 745 EXPECT_EQ(MOJO_RESULT_OK, 746 core()->WriteMessage(h_passing[0], 747 kWorld, kWorldSize, 748 &ph, 1, 749 MOJO_WRITE_MESSAGE_FLAG_NONE)); 750 EXPECT_EQ(MOJO_RESULT_OK, 751 core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 752 1000000000)); 753 num_bytes = kBufferSize; 754 num_handles = arraysize(handles); 755 EXPECT_EQ(MOJO_RESULT_OK, 756 core()->ReadMessage(h_passing[1], 757 buffer, &num_bytes, 758 handles, &num_handles, 759 MOJO_READ_MESSAGE_FLAG_NONE)); 760 EXPECT_EQ(kWorldSize, num_bytes); 761 EXPECT_STREQ(kWorld, buffer); 762 EXPECT_EQ(1u, num_handles); 763 MojoHandle ph_received = handles[0]; 764 EXPECT_NE(ph_received, MOJO_HANDLE_INVALID); 765 EXPECT_NE(ph_received, h_passing[0]); 766 EXPECT_NE(ph_received, h_passing[1]); 767 EXPECT_NE(ph_received, ch_received); 768 769 // Again, rely on the Mojo system not re-using handle values very often. 770 EXPECT_NE(ph_received, ph); 771 772 // |ph| should no longer be valid; check that trying to close it fails. See 773 // above note. 774 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(ph)); 775 776 // Write to |ph_received|. Should receive on |ch_received|. 777 num_bytes = kHelloSize; 778 EXPECT_EQ(MOJO_RESULT_OK, 779 core()->WriteData(ph_received, kHello, &num_bytes, 780 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 781 EXPECT_EQ(MOJO_RESULT_OK, 782 core()->Wait(ch_received, MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 783 num_bytes = kBufferSize; 784 EXPECT_EQ(MOJO_RESULT_OK, 785 core()->ReadData(ch_received, buffer, &num_bytes, 786 MOJO_READ_MESSAGE_FLAG_NONE)); 787 EXPECT_EQ(kHelloSize, num_bytes); 788 EXPECT_STREQ(kHello, buffer); 789 790 ph = ph_received; 791 ph_received = MOJO_HANDLE_INVALID; 792 ch = ch_received; 793 ch_received = MOJO_HANDLE_INVALID; 794 795 // Make sure that |ph| can't be sent if it's in a two-phase write. 796 void* write_ptr = NULL; 797 num_bytes = 0; 798 ASSERT_EQ(MOJO_RESULT_OK, 799 core()->BeginWriteData(ph, &write_ptr, &num_bytes, 800 MOJO_WRITE_DATA_FLAG_NONE)); 801 ASSERT_GE(num_bytes, 1u); 802 EXPECT_EQ(MOJO_RESULT_BUSY, 803 core()->WriteMessage(h_passing[0], 804 kHello, kHelloSize, 805 &ph, 1, 806 MOJO_WRITE_MESSAGE_FLAG_NONE)); 807 808 // But |ch| can, even if |ph| is in a two-phase write. 809 EXPECT_EQ(MOJO_RESULT_OK, 810 core()->WriteMessage(h_passing[0], 811 kHello, kHelloSize, 812 &ch, 1, 813 MOJO_WRITE_MESSAGE_FLAG_NONE)); 814 ch = MOJO_HANDLE_INVALID; 815 EXPECT_EQ(MOJO_RESULT_OK, 816 core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 817 1000000000)); 818 num_bytes = kBufferSize; 819 num_handles = arraysize(handles); 820 EXPECT_EQ(MOJO_RESULT_OK, 821 core()->ReadMessage(h_passing[1], 822 buffer, &num_bytes, 823 handles, &num_handles, 824 MOJO_READ_MESSAGE_FLAG_NONE)); 825 EXPECT_EQ(kHelloSize, num_bytes); 826 EXPECT_STREQ(kHello, buffer); 827 EXPECT_EQ(1u, num_handles); 828 ch = handles[0]; 829 EXPECT_NE(ch, MOJO_HANDLE_INVALID); 830 831 // Complete the two-phase write. 832 static_cast<char*>(write_ptr)[0] = 'x'; 833 EXPECT_EQ(MOJO_RESULT_OK, core()->EndWriteData(ph, 1)); 834 835 // Wait for |ch| to be readable. 836 EXPECT_EQ(MOJO_RESULT_OK, 837 core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 1000000000)); 838 839 // Make sure that |ch| can't be sent if it's in a two-phase read. 840 const void* read_ptr = NULL; 841 num_bytes = 1; 842 ASSERT_EQ(MOJO_RESULT_OK, 843 core()->BeginReadData(ch, &read_ptr, &num_bytes, 844 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); 845 EXPECT_EQ(MOJO_RESULT_BUSY, 846 core()->WriteMessage(h_passing[0], 847 kHello, kHelloSize, 848 &ch, 1, 849 MOJO_WRITE_MESSAGE_FLAG_NONE)); 850 851 // But |ph| can, even if |ch| is in a two-phase read. 852 EXPECT_EQ(MOJO_RESULT_OK, 853 core()->WriteMessage(h_passing[0], 854 kWorld, kWorldSize, 855 &ph, 1, 856 MOJO_WRITE_MESSAGE_FLAG_NONE)); 857 ph = MOJO_HANDLE_INVALID; 858 EXPECT_EQ(MOJO_RESULT_OK, 859 core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 860 1000000000)); 861 num_bytes = kBufferSize; 862 num_handles = arraysize(handles); 863 EXPECT_EQ(MOJO_RESULT_OK, 864 core()->ReadMessage(h_passing[1], 865 buffer, &num_bytes, 866 handles, &num_handles, 867 MOJO_READ_MESSAGE_FLAG_NONE)); 868 EXPECT_EQ(kWorldSize, num_bytes); 869 EXPECT_STREQ(kWorld, buffer); 870 EXPECT_EQ(1u, num_handles); 871 ph = handles[0]; 872 EXPECT_NE(ph, MOJO_HANDLE_INVALID); 873 874 // Complete the two-phase read. 875 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]); 876 EXPECT_EQ(MOJO_RESULT_OK, core()->EndReadData(ch, 1)); 877 878 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[0])); 879 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[1])); 880 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(ph)); 881 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(ch)); 882 } 883 884 // TODO(vtl): Test |DuplicateBufferHandle()| and |MapBuffer()|. 885 886 } // namespace 887 } // namespace system 888 } // namespace mojo 889