| 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 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <poll.h> | 8 #include <poll.h> |
| 9 #include <signal.h> | 9 #include <signal.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 #include <sys/socket.h> | 11 #include <sys/socket.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <sys/uio.h> | 13 #include <sys/uio.h> |
| 14 #include <unistd.h> | 14 #include <unistd.h> |
| 15 | |
| 16 #include <deque> | 15 #include <deque> |
| 16 #include <utility> |
| 17 | 17 |
| 18 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 19 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
| 20 #include "base/files/scoped_file.h" | 20 #include "base/files/scoped_file.h" |
| 21 #include "base/files/scoped_temp_dir.h" | 21 #include "base/files/scoped_temp_dir.h" |
| 22 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "mojo/edk/embedder/platform_channel_utils_posix.h" | 23 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
| 24 #include "mojo/edk/embedder/platform_handle.h" | 24 #include "mojo/edk/embedder/platform_handle.h" |
| 25 #include "mojo/edk/embedder/platform_handle_vector.h" | 25 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 26 #include "mojo/edk/embedder/scoped_platform_handle.h" | 26 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 57 } | 57 } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 struct sigaction old_action_; | 60 struct sigaction old_action_; |
| 61 | 61 |
| 62 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformChannelPairPosixTest); | 62 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformChannelPairPosixTest); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 TEST_F(PlatformChannelPairPosixTest, NoSigPipe) { | 65 TEST_F(PlatformChannelPairPosixTest, NoSigPipe) { |
| 66 PlatformChannelPair channel_pair; | 66 PlatformChannelPair channel_pair; |
| 67 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 67 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle(); |
| 68 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 68 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle(); |
| 69 | 69 |
| 70 // Write to the client. | 70 // Write to the client. |
| 71 static const char kHello[] = "hello"; | 71 static const char kHello[] = "hello"; |
| 72 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 72 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 73 write(client_handle.get().handle, kHello, sizeof(kHello))); | 73 write(client_handle.get().handle, kHello, sizeof(kHello))); |
| 74 | 74 |
| 75 // Close the client. | 75 // Close the client. |
| 76 client_handle.reset(); | 76 client_handle.reset(); |
| 77 | 77 |
| 78 // Read from the server; this should be okay. | 78 // Read from the server; this should be okay. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 98 struct iovec iov[2] = {{const_cast<char*>(kHello), sizeof(kHello)}, | 98 struct iovec iov[2] = {{const_cast<char*>(kHello), sizeof(kHello)}, |
| 99 {const_cast<char*>(kHello), sizeof(kHello)}}; | 99 {const_cast<char*>(kHello), sizeof(kHello)}}; |
| 100 result = PlatformChannelWritev(server_handle.get(), iov, 2); | 100 result = PlatformChannelWritev(server_handle.get(), iov, 2); |
| 101 EXPECT_EQ(-1, result); | 101 EXPECT_EQ(-1, result); |
| 102 if (errno != EPIPE) | 102 if (errno != EPIPE) |
| 103 PLOG(WARNING) << "write (expected EPIPE)"; | 103 PLOG(WARNING) << "write (expected EPIPE)"; |
| 104 } | 104 } |
| 105 | 105 |
| 106 TEST_F(PlatformChannelPairPosixTest, SendReceiveData) { | 106 TEST_F(PlatformChannelPairPosixTest, SendReceiveData) { |
| 107 PlatformChannelPair channel_pair; | 107 PlatformChannelPair channel_pair; |
| 108 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 108 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle(); |
| 109 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 109 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle(); |
| 110 | 110 |
| 111 for (size_t i = 0; i < 10; i++) { | 111 for (size_t i = 0; i < 10; i++) { |
| 112 std::string send_string(1 << i, 'A' + i); | 112 std::string send_string(1 << i, 'A' + i); |
| 113 | 113 |
| 114 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), | 114 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), |
| 115 PlatformChannelWrite(server_handle.get(), send_string.data(), | 115 PlatformChannelWrite(server_handle.get(), send_string.data(), |
| 116 send_string.size())); | 116 send_string.size())); |
| 117 | 117 |
| 118 WaitReadable(client_handle.get()); | 118 WaitReadable(client_handle.get()); |
| 119 | 119 |
| 120 char buf[10000] = {}; | 120 char buf[10000] = {}; |
| 121 std::deque<PlatformHandle> received_handles; | 121 std::deque<PlatformHandle> received_handles; |
| 122 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, | 122 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, |
| 123 sizeof(buf), &received_handles); | 123 sizeof(buf), &received_handles); |
| 124 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); | 124 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); |
| 125 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); | 125 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); |
| 126 EXPECT_TRUE(received_handles.empty()); | 126 EXPECT_TRUE(received_handles.empty()); |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { | 130 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { |
| 131 base::ScopedTempDir temp_dir; | 131 base::ScopedTempDir temp_dir; |
| 132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 133 | 133 |
| 134 static const char kHello[] = "hello"; | 134 static const char kHello[] = "hello"; |
| 135 | 135 |
| 136 PlatformChannelPair channel_pair; | 136 PlatformChannelPair channel_pair; |
| 137 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 137 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle(); |
| 138 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 138 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle(); |
| 139 | 139 |
| 140 // Reduce the number of FDs opened on OS X to avoid test flake. | 140 // Reduce the number of FDs opened on OS X to avoid test flake. |
| 141 #if defined(OS_MACOSX) | 141 #if defined(OS_MACOSX) |
| 142 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2; | 142 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2; |
| 143 #else | 143 #else |
| 144 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; | 144 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; |
| 145 #endif | 145 #endif |
| 146 | 146 |
| 147 for (size_t i = 1; i < kNumHandlesToSend; i++) { | 147 for (size_t i = 1; i < kNumHandlesToSend; i++) { |
| 148 // Make |i| files, with the j-th file consisting of j copies of the digit | 148 // Make |i| files, with the j-th file consisting of j copies of the digit |
| 149 // |c|. | 149 // |c|. |
| 150 const char c = '0' + (i % 10); | 150 const char c = '0' + (i % 10); |
| 151 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); | 151 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
| 152 for (size_t j = 1; j <= i; j++) { | 152 for (size_t j = 1; j <= i; j++) { |
| 153 base::FilePath unused; | 153 base::FilePath unused; |
| 154 base::ScopedFILE fp( | 154 base::ScopedFILE fp( |
| 155 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 155 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 156 ASSERT_TRUE(fp); | 156 ASSERT_TRUE(fp); |
| 157 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); | 157 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); |
| 158 platform_handles->push_back( | 158 platform_handles->push_back( |
| 159 test::PlatformHandleFromFILE(fp.Pass()).release()); | 159 test::PlatformHandleFromFILE(std::move(fp)).release()); |
| 160 ASSERT_TRUE(platform_handles->back().is_valid()); | 160 ASSERT_TRUE(platform_handles->back().is_valid()); |
| 161 } | 161 } |
| 162 | 162 |
| 163 // Send the FDs (+ "hello"). | 163 // Send the FDs (+ "hello"). |
| 164 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 164 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
| 165 // We assume that the |sendmsg()| actually sends all the data. | 165 // We assume that the |sendmsg()| actually sends all the data. |
| 166 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 166 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 167 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 167 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
| 168 &platform_handles->at(0), | 168 &platform_handles->at(0), |
| 169 platform_handles->size())); | 169 platform_handles->size())); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 193 } | 193 } |
| 194 } | 194 } |
| 195 | 195 |
| 196 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { | 196 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { |
| 197 base::ScopedTempDir temp_dir; | 197 base::ScopedTempDir temp_dir; |
| 198 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 198 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 199 | 199 |
| 200 static const char kHello[] = "hello"; | 200 static const char kHello[] = "hello"; |
| 201 | 201 |
| 202 PlatformChannelPair channel_pair; | 202 PlatformChannelPair channel_pair; |
| 203 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 203 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle(); |
| 204 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 204 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle(); |
| 205 | 205 |
| 206 const std::string file_contents("hello world"); | 206 const std::string file_contents("hello world"); |
| 207 | 207 |
| 208 { | 208 { |
| 209 base::FilePath unused; | 209 base::FilePath unused; |
| 210 base::ScopedFILE fp( | 210 base::ScopedFILE fp( |
| 211 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 211 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 212 ASSERT_TRUE(fp); | 212 ASSERT_TRUE(fp); |
| 213 ASSERT_EQ(file_contents.size(), | 213 ASSERT_EQ(file_contents.size(), |
| 214 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); | 214 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); |
| 215 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); | 215 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
| 216 platform_handles->push_back( | 216 platform_handles->push_back( |
| 217 test::PlatformHandleFromFILE(fp.Pass()).release()); | 217 test::PlatformHandleFromFILE(std::move(fp)).release()); |
| 218 ASSERT_TRUE(platform_handles->back().is_valid()); | 218 ASSERT_TRUE(platform_handles->back().is_valid()); |
| 219 | 219 |
| 220 // Send the FD (+ "hello"). | 220 // Send the FD (+ "hello"). |
| 221 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 221 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
| 222 // We assume that the |sendmsg()| actually sends all the data. | 222 // We assume that the |sendmsg()| actually sends all the data. |
| 223 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 223 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 224 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 224 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
| 225 &platform_handles->at(0), | 225 &platform_handles->at(0), |
| 226 platform_handles->size())); | 226 platform_handles->size())); |
| 227 } | 227 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 251 char read_buf[100]; | 251 char read_buf[100]; |
| 252 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 252 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
| 253 EXPECT_EQ(file_contents.size(), bytes_read); | 253 EXPECT_EQ(file_contents.size(), bytes_read); |
| 254 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 254 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
| 255 } | 255 } |
| 256 } | 256 } |
| 257 | 257 |
| 258 } // namespace | 258 } // namespace |
| 259 } // namespace edk | 259 } // namespace edk |
| 260 } // namespace mojo | 260 } // namespace mojo |
| OLD | NEW |