| 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" | 18 #include "base/files/scoped_file.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "build/build_config.h" |
| 20 #include "mojo/edk/embedder/platform_channel_utils_posix.h" | 21 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
| 21 #include "mojo/edk/embedder/platform_handle.h" | 22 #include "mojo/edk/embedder/platform_handle.h" |
| 22 #include "mojo/edk/embedder/platform_handle_vector.h" | 23 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 23 #include "mojo/edk/embedder/scoped_platform_handle.h" | 24 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 24 #include "mojo/edk/test/test_utils.h" | 25 #include "mojo/edk/test/test_utils.h" |
| 25 #include "mojo/public/cpp/system/macros.h" | 26 #include "mojo/public/cpp/system/macros.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 29 #if defined(OS_ANDROID) |
| 30 #include "base/android/path_utils.h" |
| 31 #include "base/files/file_path.h" |
| 32 #endif |
| 33 |
| 28 namespace mojo { | 34 namespace mojo { |
| 29 namespace embedder { | 35 namespace embedder { |
| 30 namespace { | 36 namespace { |
| 31 | 37 |
| 32 void WaitReadable(PlatformHandle h) { | 38 void WaitReadable(PlatformHandle h) { |
| 33 struct pollfd pfds = {}; | 39 struct pollfd pfds = {}; |
| 34 pfds.fd = h.fd; | 40 pfds.fd = h.fd; |
| 35 pfds.events = POLLIN; | 41 pfds.events = POLLIN; |
| 36 CHECK_EQ(poll(&pfds, 1, -1), 1); | 42 CHECK_EQ(poll(&pfds, 1, -1), 1); |
| 37 } | 43 } |
| 38 | 44 |
| 45 FILE* NewTmpFile() { |
| 46 #if defined(OS_ANDROID) |
| 47 base::FilePath tmpdir; |
| 48 if (!base::android::GetCacheDirectory(&tmpdir)) |
| 49 return nullptr; |
| 50 std::string templ = tmpdir.Append("XXXXXXXX").value(); |
| 51 int fd = mkstemp(const_cast<char*>(templ.c_str())); |
| 52 if (fd == -1) |
| 53 return nullptr; |
| 54 CHECK(unlink(templ.c_str()) == 0); |
| 55 return fdopen(fd, "w+"); |
| 56 #else |
| 57 return tmpfile(); |
| 58 #endif |
| 59 } |
| 60 |
| 39 class PlatformChannelPairPosixTest : public testing::Test { | 61 class PlatformChannelPairPosixTest : public testing::Test { |
| 40 public: | 62 public: |
| 41 PlatformChannelPairPosixTest() {} | 63 PlatformChannelPairPosixTest() {} |
| 42 ~PlatformChannelPairPosixTest() override {} | 64 ~PlatformChannelPairPosixTest() override {} |
| 43 | 65 |
| 44 void SetUp() override { | 66 void SetUp() override { |
| 45 // Make sure |SIGPIPE| isn't being ignored. | 67 // Make sure |SIGPIPE| isn't being ignored. |
| 46 struct sigaction action = {}; | 68 struct sigaction action = {}; |
| 47 action.sa_handler = SIG_DFL; | 69 action.sa_handler = SIG_DFL; |
| 48 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); | 70 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 #else | 159 #else |
| 138 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; | 160 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; |
| 139 #endif | 161 #endif |
| 140 | 162 |
| 141 for (size_t i = 1; i < kNumHandlesToSend; i++) { | 163 for (size_t i = 1; i < kNumHandlesToSend; i++) { |
| 142 // 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 |
| 143 // |c|. | 165 // |c|. |
| 144 const char c = '0' + (i % 10); | 166 const char c = '0' + (i % 10); |
| 145 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); | 167 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
| 146 for (size_t j = 1; j <= i; j++) { | 168 for (size_t j = 1; j <= i; j++) { |
| 147 base::ScopedFILE fp(tmpfile()); | 169 base::ScopedFILE fp(NewTmpFile()); |
| 148 ASSERT_TRUE(fp); | 170 ASSERT_TRUE(fp); |
| 149 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())); |
| 150 platform_handles->push_back( | 172 platform_handles->push_back( |
| 151 test::PlatformHandleFromFILE(fp.Pass()).release()); | 173 test::PlatformHandleFromFILE(fp.Pass()).release()); |
| 152 ASSERT_TRUE(platform_handles->back().is_valid()); | 174 ASSERT_TRUE(platform_handles->back().is_valid()); |
| 153 } | 175 } |
| 154 | 176 |
| 155 // Send the FDs (+ "hello"). | 177 // Send the FDs (+ "hello"). |
| 156 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 178 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
| 157 // We assume that the |sendmsg()| actually sends all the data. | 179 // We assume that the |sendmsg()| actually sends all the data. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 188 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { | 210 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { |
| 189 static const char kHello[] = "hello"; | 211 static const char kHello[] = "hello"; |
| 190 | 212 |
| 191 PlatformChannelPair channel_pair; | 213 PlatformChannelPair channel_pair; |
| 192 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 214 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); |
| 193 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 215 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
| 194 | 216 |
| 195 const std::string file_contents("hello world"); | 217 const std::string file_contents("hello world"); |
| 196 | 218 |
| 197 { | 219 { |
| 198 base::ScopedFILE fp(tmpfile()); | 220 base::ScopedFILE fp(NewTmpFile()); |
| 199 ASSERT_TRUE(fp); | 221 ASSERT_TRUE(fp); |
| 200 ASSERT_EQ(file_contents.size(), | 222 ASSERT_EQ(file_contents.size(), |
| 201 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); | 223 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); |
| 202 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); | 224 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
| 203 platform_handles->push_back( | 225 platform_handles->push_back( |
| 204 test::PlatformHandleFromFILE(fp.Pass()).release()); | 226 test::PlatformHandleFromFILE(fp.Pass()).release()); |
| 205 ASSERT_TRUE(platform_handles->back().is_valid()); | 227 ASSERT_TRUE(platform_handles->back().is_valid()); |
| 206 | 228 |
| 207 // Send the FD (+ "hello"). | 229 // Send the FD (+ "hello"). |
| 208 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 230 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 238 char read_buf[100]; | 260 char read_buf[100]; |
| 239 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()); |
| 240 EXPECT_EQ(file_contents.size(), bytes_read); | 262 EXPECT_EQ(file_contents.size(), bytes_read); |
| 241 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 263 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
| 242 } | 264 } |
| 243 } | 265 } |
| 244 | 266 |
| 245 } // namespace | 267 } // namespace |
| 246 } // namespace embedder | 268 } // namespace embedder |
| 247 } // namespace mojo | 269 } // namespace mojo |
| OLD | NEW |