OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/common/test/multiprocess_test_base.h" | 5 #include "mojo/common/test/multiprocess_test_base.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "mojo/system/platform_channel.h" | 9 #include "mojo/system/scoped_platform_handle.h" |
10 #include "mojo/system/platform_channel_handle.h" | |
11 | 10 |
12 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
13 #include <fcntl.h> | 12 #include <fcntl.h> |
14 #include <unistd.h> | 13 #include <unistd.h> |
15 | 14 |
16 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
17 #endif | 16 #endif |
18 | 17 |
19 namespace mojo { | 18 namespace mojo { |
20 namespace { | 19 namespace { |
21 | 20 |
22 class MultiprocessTestBaseTest : public test::MultiprocessTestBase { | 21 class MultiprocessTestBaseTest : public test::MultiprocessTestBase { |
23 }; | 22 }; |
24 | 23 |
25 TEST_F(MultiprocessTestBaseTest, RunChild) { | 24 TEST_F(MultiprocessTestBaseTest, RunChild) { |
26 // TODO(vtl): Not implemented on Windows yet. | 25 // TODO(vtl): Not implemented on Windows yet. |
27 #if defined(OS_POSIX) | 26 #if defined(OS_POSIX) |
28 EXPECT_TRUE(server_platform_channel.get()); | 27 EXPECT_TRUE(server_platform_handle.is_valid()); |
29 EXPECT_TRUE(server_platform_channel->is_valid()); | |
30 #endif | 28 #endif |
31 StartChild("RunChild"); | 29 StartChild("RunChild"); |
32 EXPECT_EQ(123, WaitForChildShutdown()); | 30 EXPECT_EQ(123, WaitForChildShutdown()); |
33 } | 31 } |
34 | 32 |
35 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { | 33 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { |
36 // TODO(vtl): Not implemented on Windows yet. | 34 // TODO(vtl): Not implemented on Windows yet. |
37 #if defined(OS_POSIX) | 35 #if defined(OS_POSIX) |
38 CHECK(MultiprocessTestBaseTest::client_platform_channel.get()); | 36 CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid()); |
39 CHECK(MultiprocessTestBaseTest::client_platform_channel->is_valid()); | |
40 #endif | 37 #endif |
41 return 123; | 38 return 123; |
42 } | 39 } |
43 | 40 |
44 TEST_F(MultiprocessTestBaseTest, TestChildMainNotFound) { | 41 TEST_F(MultiprocessTestBaseTest, TestChildMainNotFound) { |
45 StartChild("NoSuchTestChildMain"); | 42 StartChild("NoSuchTestChildMain"); |
46 int result = WaitForChildShutdown(); | 43 int result = WaitForChildShutdown(); |
47 EXPECT_FALSE(result >= 0 && result <= 127); | 44 EXPECT_FALSE(result >= 0 && result <= 127); |
48 } | 45 } |
49 | 46 |
50 // POSIX-specific test of passed channel --------------------------------------- | 47 // POSIX-specific test of passed handle ---------------------------------------- |
51 | 48 |
52 #if defined(OS_POSIX) | 49 #if defined(OS_POSIX) |
53 TEST_F(MultiprocessTestBaseTest, PassedChannelPosix) { | 50 TEST_F(MultiprocessTestBaseTest, PassedChannelPosix) { |
54 EXPECT_TRUE(server_platform_channel.get()); | 51 EXPECT_TRUE(server_platform_handle.is_valid()); |
55 EXPECT_TRUE(server_platform_channel->is_valid()); | |
56 StartChild("PassedChannelPosix"); | 52 StartChild("PassedChannelPosix"); |
57 | 53 |
58 // Take ownership of the FD. | 54 // Take ownership of the FD. |
59 mojo::system::PlatformChannelHandle channel = | 55 system::ScopedPlatformHandle handle = server_platform_handle.Pass(); |
60 server_platform_channel->PassHandle(); | 56 int fd = handle.get().fd; |
61 server_platform_channel.reset(); | |
62 int fd = channel.fd; | |
63 | 57 |
64 // The FD should be non-blocking. Check this. | 58 // The FD should be non-blocking. Check this. |
65 CHECK((fcntl(fd, F_GETFL) & O_NONBLOCK)); | 59 CHECK((fcntl(fd, F_GETFL) & O_NONBLOCK)); |
66 // We're lazy. Set it to block. | 60 // We're lazy. Set it to block. |
67 PCHECK(fcntl(fd, F_SETFL, 0) == 0); | 61 PCHECK(fcntl(fd, F_SETFL, 0) == 0); |
68 | 62 |
69 // Write a byte. | 63 // Write a byte. |
70 const char c = 'X'; | 64 const char c = 'X'; |
71 ssize_t write_size = HANDLE_EINTR(write(fd, &c, 1)); | 65 ssize_t write_size = HANDLE_EINTR(write(fd, &c, 1)); |
72 EXPECT_EQ(1, write_size); | 66 EXPECT_EQ(1, write_size); |
73 | 67 |
74 // It'll echo it back to us, incremented. | 68 // It'll echo it back to us, incremented. |
75 char d = 0; | 69 char d = 0; |
76 ssize_t read_size = HANDLE_EINTR(read(fd, &d, 1)); | 70 ssize_t read_size = HANDLE_EINTR(read(fd, &d, 1)); |
77 EXPECT_EQ(1, read_size); | 71 EXPECT_EQ(1, read_size); |
78 EXPECT_EQ(c + 1, d); | 72 EXPECT_EQ(c + 1, d); |
79 | 73 |
80 // And return it, incremented again. | 74 // And return it, incremented again. |
81 EXPECT_EQ(c + 2, WaitForChildShutdown()); | 75 EXPECT_EQ(c + 2, WaitForChildShutdown()); |
82 } | 76 } |
83 | 77 |
84 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannelPosix) { | 78 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannelPosix) { |
85 CHECK(MultiprocessTestBaseTest::client_platform_channel.get()); | 79 CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid()); |
86 CHECK(MultiprocessTestBaseTest::client_platform_channel->is_valid()); | |
87 | 80 |
88 // Take ownership of the FD. | 81 // Take ownership of the FD. |
89 mojo::system::PlatformChannelHandle channel = | 82 system::ScopedPlatformHandle handle = |
90 MultiprocessTestBaseTest::client_platform_channel->PassHandle(); | 83 MultiprocessTestBaseTest::client_platform_handle.Pass(); |
91 MultiprocessTestBaseTest::client_platform_channel.reset(); | 84 int fd = handle.get().fd; |
92 int fd = channel.fd; | |
93 | 85 |
94 // The FD should still be non-blocking. Check this. | 86 // The FD should still be non-blocking. Check this. |
95 CHECK((fcntl(fd, F_GETFL) & O_NONBLOCK)); | 87 CHECK((fcntl(fd, F_GETFL) & O_NONBLOCK)); |
96 // We're lazy. Set it to block. | 88 // We're lazy. Set it to block. |
97 PCHECK(fcntl(fd, F_SETFL, 0) == 0); | 89 PCHECK(fcntl(fd, F_SETFL, 0) == 0); |
98 | 90 |
99 // Read a byte. | 91 // Read a byte. |
100 char c = 0; | 92 char c = 0; |
101 ssize_t read_size = HANDLE_EINTR(read(fd, &c, 1)); | 93 ssize_t read_size = HANDLE_EINTR(read(fd, &c, 1)); |
102 CHECK_EQ(read_size, 1); | 94 CHECK_EQ(read_size, 1); |
103 | 95 |
104 // Write it back, incremented. | 96 // Write it back, incremented. |
105 c++; | 97 c++; |
106 ssize_t write_size = HANDLE_EINTR(write(fd, &c, 1)); | 98 ssize_t write_size = HANDLE_EINTR(write(fd, &c, 1)); |
107 CHECK_EQ(write_size, 1); | 99 CHECK_EQ(write_size, 1); |
108 | 100 |
109 PCHECK(close(fd) == 0); | |
110 | |
111 // And return it, incremented again. | 101 // And return it, incremented again. |
112 c++; | 102 c++; |
113 return static_cast<int>(c); | 103 return static_cast<int>(c); |
114 } | 104 } |
115 #endif // defined(OS_POSIX) | 105 #endif // defined(OS_POSIX) |
116 | 106 |
117 } // namespace | 107 } // namespace |
118 } // namespace mojo | 108 } // namespace mojo |
OLD | NEW |