OLD | NEW |
1 // Copyright (c) 2014 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2014 The Native Client Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gtest/gtest.h" | 5 #include "gtest/gtest.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <spawn.h> | 9 #include <spawn.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 ASSERT_EQ(1, dup2(pipes[1], 1)); | 270 ASSERT_EQ(1, dup2(pipes[1], 1)); |
271 EXPECT_EQ(0, close(pipes[0])); | 271 EXPECT_EQ(0, close(pipes[0])); |
272 execlp(argv0, argv0, "echo", NULL); | 272 execlp(argv0, argv0, "echo", NULL); |
273 } | 273 } |
274 | 274 |
275 EXPECT_EQ(0, close(pipes[1])); | 275 EXPECT_EQ(0, close(pipes[1])); |
276 char check_msg[] = "test"; | 276 char check_msg[] = "test"; |
277 char buffer[100]; | 277 char buffer[100]; |
278 size_t total = 0; | 278 size_t total = 0; |
279 while (total < strlen(check_msg)) { | 279 while (total < strlen(check_msg)) { |
280 ssize_t len = read(pipes[0], buffer+total, sizeof(buffer)); | 280 ssize_t len = read(pipes[0], buffer + total, sizeof(buffer)); |
281 ASSERT_GE(len, 0); | 281 ASSERT_GE(len, 0); |
282 if (len == 0) break; | 282 if (len == 0) break; |
283 total += len; | 283 total += len; |
284 } | 284 } |
285 ASSERT_EQ(0, memcmp(buffer, check_msg, total)); | 285 ASSERT_EQ(0, memcmp(buffer, check_msg, total)); |
286 EXPECT_EQ(0, close(pipes[0])); | 286 EXPECT_EQ(0, close(pipes[0])); |
287 } | 287 } |
288 | 288 |
289 // Write to an echo process, close immediately, then wait for reply. | 289 // Write to an echo process, close immediately, then wait for reply. |
290 TEST(Pipes, PipeFastClose) { | 290 TEST(Pipes, PipeFastClose) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 if (len == 0) break; | 392 if (len == 0) break; |
393 total += len; | 393 total += len; |
394 } | 394 } |
395 EXPECT_EQ(strlen(test_message), total); | 395 EXPECT_EQ(strlen(test_message), total); |
396 EXPECT_TRUE(memcmp(buffer, test_message, total) == 0); | 396 EXPECT_TRUE(memcmp(buffer, test_message, total) == 0); |
397 | 397 |
398 EXPECT_EQ(0, close(pipe_a[1])); | 398 EXPECT_EQ(0, close(pipe_a[1])); |
399 EXPECT_EQ(0, close(pipe_c[0])); | 399 EXPECT_EQ(0, close(pipe_c[0])); |
400 } | 400 } |
401 | 401 |
| 402 // Read non-block from an echo process. Then write, then read. |
| 403 TEST(Pipes, EchoNonBlock) { |
| 404 int pipe_a[2]; |
| 405 int pipe_b[2]; |
| 406 // Create two pipe pairs pipe_a[1] -> pipe_a[0] |
| 407 // pipe_b[1] -> pipe_b[0] |
| 408 ASSERT_EQ(0, nacl_spawn_pipe2(pipe_a, O_NONBLOCK)); |
| 409 ASSERT_EQ(0, nacl_spawn_pipe2(pipe_b, O_NONBLOCK)); |
| 410 pid_t pid = vfork(); |
| 411 ASSERT_GE(pid, 0); |
| 412 if (!pid) { |
| 413 // Dup two ends of the pipes into stdin + stdout of the echo process. |
| 414 ASSERT_EQ(0, dup2(pipe_a[0], 0)); |
| 415 EXPECT_EQ(0, close(pipe_a[0])); |
| 416 EXPECT_EQ(0, close(pipe_a[1])); |
| 417 EXPECT_EQ(1, dup2(pipe_b[1], 1)); |
| 418 EXPECT_EQ(0, close(pipe_b[0])); |
| 419 EXPECT_EQ(0, close(pipe_b[1])); |
| 420 execlp(argv0, argv0, "pipes", NULL); |
| 421 // Don't get here. |
| 422 ASSERT_TRUE(false); |
| 423 } |
| 424 |
| 425 EXPECT_EQ(0, close(pipe_a[0])); |
| 426 EXPECT_EQ(0, close(pipe_b[1])); |
| 427 |
| 428 // Attempt to read from non-blocking pipe. |
| 429 char buffer[100]; |
| 430 size_t total = 0; |
| 431 ssize_t len = read(pipe_b[0], buffer + total, sizeof(buffer)); |
| 432 EXPECT_EQ(-1, len); |
| 433 EXPECT_EQ(EAGAIN, errno); |
| 434 |
| 435 const char test_message[] = "test message"; |
| 436 |
| 437 // Write to pipe_a. |
| 438 len = write(pipe_a[1], test_message, strlen(test_message)); |
| 439 EXPECT_EQ(static_cast<ssize_t>(strlen(test_message)), len); |
| 440 // Wait for an echo back on pipe_b. |
| 441 while (total < strlen(test_message)) { |
| 442 len = read(pipe_b[0], buffer + total, sizeof(buffer)); |
| 443 if (len < 0 && errno == EAGAIN) { |
| 444 continue; |
| 445 } |
| 446 ASSERT_GE(len, 0); |
| 447 if (len == 0) { |
| 448 break; |
| 449 } |
| 450 total += len; |
| 451 } |
| 452 EXPECT_EQ(strlen(test_message), total); |
| 453 EXPECT_TRUE(memcmp(buffer, test_message, total) == 0); |
| 454 |
| 455 EXPECT_EQ(0, close(pipe_a[1])); |
| 456 EXPECT_EQ(0, close(pipe_b[0])); |
| 457 } |
| 458 |
402 TEST(Pipes, NullFeof) { | 459 TEST(Pipes, NullFeof) { |
403 int p[2]; | 460 int p[2]; |
404 ASSERT_EQ(0, pipe(p)); | 461 ASSERT_EQ(0, pipe(p)); |
405 ASSERT_EQ(0, close(p[1])); | 462 ASSERT_EQ(0, close(p[1])); |
406 ASSERT_EQ(0, dup2(p[0], 0)); | 463 ASSERT_EQ(0, dup2(p[0], 0)); |
407 while (!feof(stdin)) { | 464 while (!feof(stdin)) { |
408 fgetc(stdin); | 465 fgetc(stdin); |
409 } | 466 } |
410 } | 467 } |
411 | 468 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 } | 561 } |
505 printf("unknown child_command: %s\n", child_command); | 562 printf("unknown child_command: %s\n", child_command); |
506 return 1; | 563 return 1; |
507 } | 564 } |
508 | 565 |
509 // Preserve argv[0] for use in some tests. | 566 // Preserve argv[0] for use in some tests. |
510 argv0 = argv[0]; | 567 argv0 = argv[0]; |
511 testing::InitGoogleTest(&argc, argv); | 568 testing::InitGoogleTest(&argc, argv); |
512 return RUN_ALL_TESTS(); | 569 return RUN_ALL_TESTS(); |
513 } | 570 } |
OLD | NEW |