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> |
11 | 11 |
| 12 #define pipe nacl_spawn_pipe |
| 13 |
12 static char *argv0; | 14 static char *argv0; |
13 | 15 |
14 // Make sure that the plumbing works. | 16 // Make sure that the plumbing works. |
15 TEST(Plumbing, Identity) { | 17 TEST(Plumbing, Identity) { |
16 EXPECT_EQ(0, 0); | 18 EXPECT_EQ(0, 0); |
17 } | 19 } |
18 | 20 |
19 // Test process functions. | 21 // Test process functions. |
20 TEST(Plumbing, ProcessTests) { | 22 TEST(Plumbing, ProcessTests) { |
21 int pid = getpid(); | 23 int pid = getpid(); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 ASSERT_EQ(1, dup2(pipes[1], 1)); | 272 ASSERT_EQ(1, dup2(pipes[1], 1)); |
271 EXPECT_EQ(0, close(pipes[0])); | 273 EXPECT_EQ(0, close(pipes[0])); |
272 execlp(argv0, argv0, "echo", NULL); | 274 execlp(argv0, argv0, "echo", NULL); |
273 } | 275 } |
274 | 276 |
275 EXPECT_EQ(0, close(pipes[1])); | 277 EXPECT_EQ(0, close(pipes[1])); |
276 char check_msg[] = "test"; | 278 char check_msg[] = "test"; |
277 char buffer[100]; | 279 char buffer[100]; |
278 size_t total = 0; | 280 size_t total = 0; |
279 while (total < strlen(check_msg)) { | 281 while (total < strlen(check_msg)) { |
280 ssize_t len = read(pipes[0], buffer+total, sizeof(buffer)); | 282 ssize_t len = read(pipes[0], buffer + total, sizeof(buffer)); |
281 ASSERT_GE(len, 0); | 283 ASSERT_GE(len, 0); |
282 if (len == 0) break; | 284 if (len == 0) break; |
283 total += len; | 285 total += len; |
284 } | 286 } |
285 ASSERT_EQ(0, memcmp(buffer, check_msg, total)); | 287 ASSERT_EQ(0, memcmp(buffer, check_msg, total)); |
286 EXPECT_EQ(0, close(pipes[0])); | 288 EXPECT_EQ(0, close(pipes[0])); |
287 } | 289 } |
288 | 290 |
289 // Write to an echo process, close immediately, then wait for reply. | 291 // Write to an echo process, close immediately, then wait for reply. |
290 TEST(Pipes, PipeFastClose) { | 292 TEST(Pipes, PipeFastClose) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 if (len == 0) break; | 394 if (len == 0) break; |
393 total += len; | 395 total += len; |
394 } | 396 } |
395 EXPECT_EQ(strlen(test_message), total); | 397 EXPECT_EQ(strlen(test_message), total); |
396 EXPECT_TRUE(memcmp(buffer, test_message, total) == 0); | 398 EXPECT_TRUE(memcmp(buffer, test_message, total) == 0); |
397 | 399 |
398 EXPECT_EQ(0, close(pipe_a[1])); | 400 EXPECT_EQ(0, close(pipe_a[1])); |
399 EXPECT_EQ(0, close(pipe_c[0])); | 401 EXPECT_EQ(0, close(pipe_c[0])); |
400 } | 402 } |
401 | 403 |
| 404 // Read non-block from an echo process. Then write, then read. |
| 405 TEST(Pipes, EchoNonBlock) { |
| 406 int pipe_a[2]; |
| 407 int pipe_b[2]; |
| 408 // Create two pipe pairs pipe_a[1] -> pipe_a[0] |
| 409 // pipe_b[1] -> pipe_b[0] |
| 410 ASSERT_EQ(0, nacl_spawn_pipe_flags(O_NONBLOCK, pipe_a)); |
| 411 ASSERT_EQ(0, nacl_spawn_pipe_flags(O_NONBLOCK, pipe_b)); |
| 412 pid_t pid = vfork(); |
| 413 ASSERT_GE(pid, 0); |
| 414 if (!pid) { |
| 415 // Dup two ends of the pipes into stdin + stdout of the echo process. |
| 416 ASSERT_EQ(0, dup2(pipe_a[0], 0)); |
| 417 EXPECT_EQ(0, close(pipe_a[0])); |
| 418 EXPECT_EQ(0, close(pipe_a[1])); |
| 419 EXPECT_EQ(1, dup2(pipe_b[1], 1)); |
| 420 EXPECT_EQ(0, close(pipe_b[0])); |
| 421 EXPECT_EQ(0, close(pipe_b[1])); |
| 422 execlp(argv0, argv0, "pipes", NULL); |
| 423 // Don't get here. |
| 424 ASSERT_TRUE(false); |
| 425 } |
| 426 |
| 427 EXPECT_EQ(0, close(pipe_a[0])); |
| 428 EXPECT_EQ(0, close(pipe_b[1])); |
| 429 |
| 430 // Attempt to read from non-blocking pipe. |
| 431 char buffer[100]; |
| 432 size_t total = 0; |
| 433 ssize_t len = read(pipe_b[0], buffer + total, sizeof(buffer)); |
| 434 EXPECT_EQ(-1, len); |
| 435 EXPECT_EQ(EAGAIN, errno); |
| 436 |
| 437 const char test_message[] = "test message"; |
| 438 |
| 439 // Write to pipe_a. |
| 440 len = write(pipe_a[1], test_message, strlen(test_message)); |
| 441 EXPECT_EQ(static_cast<ssize_t>(strlen(test_message)), len); |
| 442 // Wait for an echo back on pipe_b. |
| 443 while (total < strlen(test_message)) { |
| 444 len = read(pipe_b[0], buffer + total, sizeof(buffer)); |
| 445 if (len < 0 && errno == EAGAIN) { |
| 446 continue; |
| 447 } |
| 448 ASSERT_GE(len, 0); |
| 449 if (len == 0) { |
| 450 break; |
| 451 } |
| 452 total += len; |
| 453 } |
| 454 EXPECT_EQ(strlen(test_message), total); |
| 455 EXPECT_TRUE(memcmp(buffer, test_message, total) == 0); |
| 456 |
| 457 EXPECT_EQ(0, close(pipe_a[1])); |
| 458 EXPECT_EQ(0, close(pipe_b[0])); |
| 459 } |
| 460 |
402 TEST(Pipes, NullFeof) { | 461 TEST(Pipes, NullFeof) { |
403 int p[2]; | 462 int p[2]; |
404 ASSERT_EQ(0, pipe(p)); | 463 ASSERT_EQ(0, pipe(p)); |
405 ASSERT_EQ(0, close(p[1])); | 464 ASSERT_EQ(0, close(p[1])); |
406 ASSERT_EQ(0, dup2(p[0], 0)); | 465 ASSERT_EQ(0, dup2(p[0], 0)); |
407 while (!feof(stdin)) { | 466 while (!feof(stdin)) { |
408 fgetc(stdin); | 467 fgetc(stdin); |
409 } | 468 } |
410 } | 469 } |
411 | 470 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 } | 563 } |
505 printf("unknown child_command: %s\n", child_command); | 564 printf("unknown child_command: %s\n", child_command); |
506 return 1; | 565 return 1; |
507 } | 566 } |
508 | 567 |
509 // Preserve argv[0] for use in some tests. | 568 // Preserve argv[0] for use in some tests. |
510 argv0 = argv[0]; | 569 argv0 = argv[0]; |
511 testing::InitGoogleTest(&argc, argv); | 570 testing::InitGoogleTest(&argc, argv); |
512 return RUN_ALL_TESTS(); | 571 return RUN_ALL_TESTS(); |
513 } | 572 } |
OLD | NEW |