| 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 | 15 |
| 16 #include <deque> | 16 #include <deque> |
| 17 | 17 |
| 18 #include "base/files/scoped_file.h" | |
| 19 #include "base/logging.h" | 18 #include "base/logging.h" |
| 20 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 21 #include "mojo/edk/embedder/platform_channel_utils_posix.h" | 20 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
| 22 #include "mojo/edk/embedder/platform_handle.h" | 21 #include "mojo/edk/embedder/platform_handle.h" |
| 23 #include "mojo/edk/embedder/platform_handle_vector.h" | 22 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 24 #include "mojo/edk/embedder/scoped_platform_handle.h" | 23 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 25 #include "mojo/edk/test/test_utils.h" | 24 #include "mojo/edk/test/test_utils.h" |
| 25 #include "mojo/edk/util/scoped_file.h" |
| 26 #include "mojo/public/cpp/system/macros.h" | 26 #include "mojo/public/cpp/system/macros.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
| 30 #include "base/android/path_utils.h" | 30 #include "base/android/path_utils.h" |
| 31 #include "base/files/file_path.h" | 31 #include "base/files/file_path.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 namespace mojo { | 34 namespace mojo { |
| 35 namespace embedder { | 35 namespace embedder { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 #else | 159 #else |
| 160 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; | 160 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; |
| 161 #endif | 161 #endif |
| 162 | 162 |
| 163 for (size_t i = 1; i < kNumHandlesToSend; i++) { | 163 for (size_t i = 1; i < kNumHandlesToSend; i++) { |
| 164 // Make |i| files, with the j-th file consisting of j copies of the digit | 164 // Make |i| files, with the j-th file consisting of j copies of the digit |
| 165 // |c|. | 165 // |c|. |
| 166 const char c = '0' + (i % 10); | 166 const char c = '0' + (i % 10); |
| 167 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); | 167 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
| 168 for (size_t j = 1; j <= i; j++) { | 168 for (size_t j = 1; j <= i; j++) { |
| 169 base::ScopedFILE fp(NewTmpFile()); | 169 util::ScopedFILE fp(NewTmpFile()); |
| 170 ASSERT_TRUE(fp); | 170 ASSERT_TRUE(fp); |
| 171 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); | 171 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); |
| 172 platform_handles->push_back( | 172 platform_handles->push_back( |
| 173 test::PlatformHandleFromFILE(fp.Pass()).release()); | 173 test::PlatformHandleFromFILE(fp.Pass()).release()); |
| 174 ASSERT_TRUE(platform_handles->back().is_valid()); | 174 ASSERT_TRUE(platform_handles->back().is_valid()); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Send the FDs (+ "hello"). | 177 // Send the FDs (+ "hello"). |
| 178 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 178 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
| 179 // We assume that the |sendmsg()| actually sends all the data. | 179 // We assume that the |sendmsg()| actually sends all the data. |
| 180 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 180 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 181 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 181 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
| 182 &platform_handles->at(0), | 182 &platform_handles->at(0), |
| 183 platform_handles->size())); | 183 platform_handles->size())); |
| 184 | 184 |
| 185 WaitReadable(client_handle.get()); | 185 WaitReadable(client_handle.get()); |
| 186 | 186 |
| 187 char buf[10000] = {}; | 187 char buf[10000] = {}; |
| 188 std::deque<PlatformHandle> received_handles; | 188 std::deque<PlatformHandle> received_handles; |
| 189 // We assume that the |recvmsg()| actually reads all the data. | 189 // We assume that the |recvmsg()| actually reads all the data. |
| 190 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 190 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 191 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 191 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), |
| 192 &received_handles)); | 192 &received_handles)); |
| 193 EXPECT_STREQ(kHello, buf); | 193 EXPECT_STREQ(kHello, buf); |
| 194 EXPECT_EQ(i, received_handles.size()); | 194 EXPECT_EQ(i, received_handles.size()); |
| 195 | 195 |
| 196 for (size_t j = 0; !received_handles.empty(); j++) { | 196 for (size_t j = 0; !received_handles.empty(); j++) { |
| 197 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 197 util::ScopedFILE fp(test::FILEFromPlatformHandle( |
| 198 ScopedPlatformHandle(received_handles.front()), "rb")); | 198 ScopedPlatformHandle(received_handles.front()), "rb")); |
| 199 received_handles.pop_front(); | 199 received_handles.pop_front(); |
| 200 ASSERT_TRUE(fp); | 200 ASSERT_TRUE(fp); |
| 201 rewind(fp.get()); | 201 rewind(fp.get()); |
| 202 char read_buf[kNumHandlesToSend]; | 202 char read_buf[kNumHandlesToSend]; |
| 203 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 203 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
| 204 EXPECT_EQ(j + 1, bytes_read); | 204 EXPECT_EQ(j + 1, bytes_read); |
| 205 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); | 205 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 | 209 |
| 210 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { | 210 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { |
| 211 static const char kHello[] = "hello"; | 211 static const char kHello[] = "hello"; |
| 212 | 212 |
| 213 PlatformChannelPair channel_pair; | 213 PlatformChannelPair channel_pair; |
| 214 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 214 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); |
| 215 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 215 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
| 216 | 216 |
| 217 const std::string file_contents("hello world"); | 217 const std::string file_contents("hello world"); |
| 218 | 218 |
| 219 { | 219 { |
| 220 base::ScopedFILE fp(NewTmpFile()); | 220 util::ScopedFILE fp(NewTmpFile()); |
| 221 ASSERT_TRUE(fp); | 221 ASSERT_TRUE(fp); |
| 222 ASSERT_EQ(file_contents.size(), | 222 ASSERT_EQ(file_contents.size(), |
| 223 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); | 223 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); |
| 224 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); | 224 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
| 225 platform_handles->push_back( | 225 platform_handles->push_back( |
| 226 test::PlatformHandleFromFILE(fp.Pass()).release()); | 226 test::PlatformHandleFromFILE(fp.Pass()).release()); |
| 227 ASSERT_TRUE(platform_handles->back().is_valid()); | 227 ASSERT_TRUE(platform_handles->back().is_valid()); |
| 228 | 228 |
| 229 // Send the FD (+ "hello"). | 229 // Send the FD (+ "hello"). |
| 230 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 230 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 245 // We assume that the |recvmsg()| actually reads all the data. | 245 // We assume that the |recvmsg()| actually reads all the data. |
| 246 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 246 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 247 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 247 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), |
| 248 &received_handles)); | 248 &received_handles)); |
| 249 EXPECT_STREQ(kHello, buf); | 249 EXPECT_STREQ(kHello, buf); |
| 250 ASSERT_EQ(2u, received_handles.size()); | 250 ASSERT_EQ(2u, received_handles.size()); |
| 251 EXPECT_FALSE(received_handles[0].is_valid()); | 251 EXPECT_FALSE(received_handles[0].is_valid()); |
| 252 EXPECT_TRUE(received_handles[1].is_valid()); | 252 EXPECT_TRUE(received_handles[1].is_valid()); |
| 253 | 253 |
| 254 { | 254 { |
| 255 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 255 util::ScopedFILE fp(test::FILEFromPlatformHandle( |
| 256 ScopedPlatformHandle(received_handles[1]), "rb")); | 256 ScopedPlatformHandle(received_handles[1]), "rb")); |
| 257 received_handles[1] = PlatformHandle(); | 257 received_handles[1] = PlatformHandle(); |
| 258 ASSERT_TRUE(fp); | 258 ASSERT_TRUE(fp); |
| 259 rewind(fp.get()); | 259 rewind(fp.get()); |
| 260 char read_buf[100]; | 260 char read_buf[100]; |
| 261 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 261 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
| 262 EXPECT_EQ(file_contents.size(), bytes_read); | 262 EXPECT_EQ(file_contents.size(), bytes_read); |
| 263 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 263 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
| 264 } | 264 } |
| 265 } | 265 } |
| 266 | 266 |
| 267 } // namespace | 267 } // namespace |
| 268 } // namespace embedder | 268 } // namespace embedder |
| 269 } // namespace mojo | 269 } // namespace mojo |
| OLD | NEW |