OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/edk/embedder/platform_channel_pair.h" | 5 // TODO(vtl): Move this to //mojo/platform (and rename it) once |
| 6 // platform_channel_utils.* has been moved/renamed. |
| 7 |
| 8 #include "mojo/edk/platform/platform_pipe.h" |
6 | 9 |
7 #include <errno.h> | 10 #include <errno.h> |
8 #include <poll.h> | 11 #include <poll.h> |
9 #include <signal.h> | 12 #include <signal.h> |
10 #include <stdio.h> | 13 #include <stdio.h> |
11 #include <sys/socket.h> | 14 #include <sys/socket.h> |
12 #include <sys/types.h> | 15 #include <sys/types.h> |
13 #include <sys/uio.h> | 16 #include <sys/uio.h> |
14 #include <unistd.h> | 17 #include <unistd.h> |
15 | 18 |
16 #include <deque> | 19 #include <deque> |
17 #include <utility> | 20 #include <utility> |
18 #include <vector> | 21 #include <vector> |
19 | 22 |
20 #include "build/build_config.h" | 23 #include "build/build_config.h" |
21 #include "mojo/edk/embedder/platform_channel_utils.h" | 24 #include "mojo/edk/embedder/platform_channel_utils.h" |
22 #include "mojo/edk/platform/platform_handle.h" | 25 #include "mojo/edk/platform/platform_handle.h" |
23 #include "mojo/edk/platform/scoped_platform_handle.h" | 26 #include "mojo/edk/platform/scoped_platform_handle.h" |
24 #include "mojo/edk/system/test/scoped_test_dir.h" | 27 #include "mojo/edk/system/test/scoped_test_dir.h" |
25 #include "mojo/edk/test/test_utils.h" | 28 #include "mojo/edk/test/test_utils.h" |
26 #include "mojo/edk/util/scoped_file.h" | 29 #include "mojo/edk/util/scoped_file.h" |
27 #include "mojo/public/cpp/system/macros.h" | 30 #include "mojo/public/cpp/system/macros.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
29 | 32 |
| 33 using mojo::platform::PlatformPipe; |
30 using mojo::platform::PlatformHandle; | 34 using mojo::platform::PlatformHandle; |
31 using mojo::platform::ScopedPlatformHandle; | 35 using mojo::platform::ScopedPlatformHandle; |
32 | 36 |
33 namespace mojo { | 37 namespace mojo { |
34 namespace embedder { | 38 namespace embedder { |
35 namespace { | 39 namespace { |
36 | 40 |
37 void WaitReadable(PlatformHandle h) { | 41 void WaitReadable(PlatformHandle h) { |
38 struct pollfd pfds = {}; | 42 struct pollfd pfds = {}; |
39 pfds.fd = h.fd; | 43 pfds.fd = h.fd; |
40 pfds.events = POLLIN; | 44 pfds.events = POLLIN; |
41 ASSERT_EQ(1, poll(&pfds, 1, -1)); | 45 ASSERT_EQ(1, poll(&pfds, 1, -1)); |
42 } | 46 } |
43 | 47 |
44 class PlatformChannelPairTest : public testing::Test { | 48 class PlatformPipeTest : public testing::Test { |
45 public: | 49 public: |
46 PlatformChannelPairTest() {} | 50 PlatformPipeTest() {} |
47 ~PlatformChannelPairTest() override {} | 51 ~PlatformPipeTest() override {} |
48 | 52 |
49 void SetUp() override { | 53 void SetUp() override { |
50 // Make sure |SIGPIPE| isn't being ignored. | 54 // Make sure |SIGPIPE| isn't being ignored. |
51 struct sigaction action = {}; | 55 struct sigaction action = {}; |
52 action.sa_handler = SIG_DFL; | 56 action.sa_handler = SIG_DFL; |
53 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); | 57 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); |
54 } | 58 } |
55 | 59 |
56 void TearDown() override { | 60 void TearDown() override { |
57 // Restore the |SIGPIPE| handler. | 61 // Restore the |SIGPIPE| handler. |
58 ASSERT_EQ(0, sigaction(SIGPIPE, &old_action_, nullptr)); | 62 ASSERT_EQ(0, sigaction(SIGPIPE, &old_action_, nullptr)); |
59 } | 63 } |
60 | 64 |
61 private: | 65 private: |
62 struct sigaction old_action_; | 66 struct sigaction old_action_; |
63 | 67 |
64 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformChannelPairTest); | 68 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformPipeTest); |
65 }; | 69 }; |
66 | 70 |
67 TEST_F(PlatformChannelPairTest, NoSigPipe) { | 71 TEST_F(PlatformPipeTest, NoSigPipe) { |
68 PlatformChannelPair channel_pair; | 72 PlatformPipe channel_pair; |
69 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); | 73 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); |
70 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); | 74 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); |
71 | 75 |
72 // Write to the client. | 76 // Write to the client. |
73 static const char kHello[] = "hello"; | 77 static const char kHello[] = "hello"; |
74 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 78 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
75 write(client_handle.get().fd, kHello, sizeof(kHello))); | 79 write(client_handle.get().fd, kHello, sizeof(kHello))); |
76 | 80 |
77 // Close the client. | 81 // Close the client. |
78 client_handle.reset(); | 82 client_handle.reset(); |
(...skipping 15 matching lines...) Expand all Loading... |
94 EXPECT_EQ(EPIPE, errno); | 98 EXPECT_EQ(EPIPE, errno); |
95 | 99 |
96 // Test our replacement for |writev()|/|sendv()|. | 100 // Test our replacement for |writev()|/|sendv()|. |
97 struct iovec iov[2] = {{const_cast<char*>(kHello), sizeof(kHello)}, | 101 struct iovec iov[2] = {{const_cast<char*>(kHello), sizeof(kHello)}, |
98 {const_cast<char*>(kHello), sizeof(kHello)}}; | 102 {const_cast<char*>(kHello), sizeof(kHello)}}; |
99 result = PlatformChannelWritev(server_handle.get(), iov, 2); | 103 result = PlatformChannelWritev(server_handle.get(), iov, 2); |
100 EXPECT_EQ(-1, result); | 104 EXPECT_EQ(-1, result); |
101 EXPECT_EQ(EPIPE, errno); | 105 EXPECT_EQ(EPIPE, errno); |
102 } | 106 } |
103 | 107 |
104 TEST_F(PlatformChannelPairTest, SendReceiveData) { | 108 TEST_F(PlatformPipeTest, SendReceiveData) { |
105 PlatformChannelPair channel_pair; | 109 PlatformPipe channel_pair; |
106 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); | 110 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); |
107 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); | 111 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); |
108 | 112 |
109 for (size_t i = 0; i < 10; i++) { | 113 for (size_t i = 0; i < 10; i++) { |
110 std::string send_string(1 << i, 'A' + i); | 114 std::string send_string(1 << i, 'A' + i); |
111 | 115 |
112 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), | 116 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), |
113 PlatformChannelWrite(server_handle.get(), send_string.data(), | 117 PlatformChannelWrite(server_handle.get(), send_string.data(), |
114 send_string.size())); | 118 send_string.size())); |
115 | 119 |
116 WaitReadable(client_handle.get()); | 120 WaitReadable(client_handle.get()); |
117 | 121 |
118 char buf[10000] = {}; | 122 char buf[10000] = {}; |
119 std::deque<ScopedPlatformHandle> received_handles; | 123 std::deque<ScopedPlatformHandle> received_handles; |
120 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, | 124 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, |
121 sizeof(buf), &received_handles); | 125 sizeof(buf), &received_handles); |
122 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); | 126 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); |
123 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); | 127 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); |
124 EXPECT_TRUE(received_handles.empty()); | 128 EXPECT_TRUE(received_handles.empty()); |
125 } | 129 } |
126 } | 130 } |
127 | 131 |
128 TEST_F(PlatformChannelPairTest, SendReceiveFDs) { | 132 TEST_F(PlatformPipeTest, SendReceiveFDs) { |
129 mojo::system::test::ScopedTestDir test_dir; | 133 mojo::system::test::ScopedTestDir test_dir; |
130 | 134 |
131 static const char kHello[] = "hello"; | 135 static const char kHello[] = "hello"; |
132 | 136 |
133 PlatformChannelPair channel_pair; | 137 PlatformPipe channel_pair; |
134 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); | 138 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); |
135 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); | 139 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); |
136 | 140 |
137 // Reduce the number of FDs opened on OS X to avoid test flake. | 141 // Reduce the number of FDs opened on OS X to avoid test flake. |
138 #if defined(OS_MACOSX) | 142 #if defined(OS_MACOSX) |
139 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2; | 143 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2; |
140 #else | 144 #else |
141 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; | 145 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; |
142 #endif | 146 #endif |
143 | 147 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 ASSERT_TRUE(fp); | 185 ASSERT_TRUE(fp); |
182 rewind(fp.get()); | 186 rewind(fp.get()); |
183 char read_buf[kNumHandlesToSend]; | 187 char read_buf[kNumHandlesToSend]; |
184 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 188 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
185 EXPECT_EQ(j + 1, bytes_read); | 189 EXPECT_EQ(j + 1, bytes_read); |
186 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); | 190 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); |
187 } | 191 } |
188 } | 192 } |
189 } | 193 } |
190 | 194 |
191 TEST_F(PlatformChannelPairTest, AppendReceivedFDs) { | 195 TEST_F(PlatformPipeTest, AppendReceivedFDs) { |
192 mojo::system::test::ScopedTestDir test_dir; | 196 mojo::system::test::ScopedTestDir test_dir; |
193 | 197 |
194 static const char kHello[] = "hello"; | 198 static const char kHello[] = "hello"; |
195 | 199 |
196 PlatformChannelPair channel_pair; | 200 PlatformPipe channel_pair; |
197 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); | 201 ScopedPlatformHandle server_handle = channel_pair.handle0.Pass(); |
198 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); | 202 ScopedPlatformHandle client_handle = channel_pair.handle1.Pass(); |
199 | 203 |
200 const std::string file_contents("hello world"); | 204 const std::string file_contents("hello world"); |
201 | 205 |
202 { | 206 { |
203 util::ScopedFILE fp(test_dir.CreateFile()); | 207 util::ScopedFILE fp(test_dir.CreateFile()); |
204 ASSERT_TRUE(fp); | 208 ASSERT_TRUE(fp); |
205 ASSERT_EQ(file_contents.size(), | 209 ASSERT_EQ(file_contents.size(), |
206 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); | 210 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 char read_buf[100]; | 246 char read_buf[100]; |
243 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 247 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
244 EXPECT_EQ(file_contents.size(), bytes_read); | 248 EXPECT_EQ(file_contents.size(), bytes_read); |
245 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 249 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
246 } | 250 } |
247 } | 251 } |
248 | 252 |
249 } // namespace | 253 } // namespace |
250 } // namespace embedder | 254 } // namespace embedder |
251 } // namespace mojo | 255 } // namespace mojo |
OLD | NEW |