| 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/embedder/platform_channel_pair.h" | 5 #include "mojo/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> |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 std::deque<PlatformHandle> received_handles; | 122 std::deque<PlatformHandle> received_handles; |
| 123 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, | 123 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, |
| 124 sizeof(buf), &received_handles); | 124 sizeof(buf), &received_handles); |
| 125 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); | 125 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); |
| 126 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); | 126 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); |
| 127 EXPECT_TRUE(received_handles.empty()); | 127 EXPECT_TRUE(received_handles.empty()); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { | 131 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { |
| 132 static const char kHello[] = "hello"; |
| 133 |
| 132 PlatformChannelPair channel_pair; | 134 PlatformChannelPair channel_pair; |
| 133 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 135 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); |
| 134 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 136 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
| 135 | 137 |
| 136 for (size_t i = 1; i < kPlatformChannelMaxNumHandles; i++) { | 138 for (size_t i = 1; i < kPlatformChannelMaxNumHandles; i++) { |
| 137 // Make |i| files, with the j-th file consisting of j copies of the digit i. | 139 // Make |i| files, with the j-th file consisting of j copies of the digit i. |
| 138 PlatformHandleVector platform_handles; | 140 PlatformHandleVector platform_handles; |
| 139 for (size_t j = 1; j <= i; j++) { | 141 for (size_t j = 1; j <= i; j++) { |
| 140 base::FilePath ignored; | 142 base::FilePath ignored; |
| 141 base::ScopedFILE fp(base::CreateAndOpenTemporaryFile(&ignored)); | 143 base::ScopedFILE fp(base::CreateAndOpenTemporaryFile(&ignored)); |
| 142 ASSERT_TRUE(fp); | 144 ASSERT_TRUE(fp); |
| 143 fwrite(std::string(j, '0' + i).data(), 1, j, fp.get()); | 145 fwrite(std::string(j, '0' + i).data(), 1, j, fp.get()); |
| 144 platform_handles.push_back( | 146 platform_handles.push_back( |
| 145 test::PlatformHandleFromFILE(fp.Pass()).release()); | 147 test::PlatformHandleFromFILE(fp.Pass()).release()); |
| 146 ASSERT_TRUE(platform_handles.back().is_valid()); | 148 ASSERT_TRUE(platform_handles.back().is_valid()); |
| 147 } | 149 } |
| 148 | 150 |
| 149 // Send the FDs. | 151 // Send the FDs (+ "hello"). |
| 150 EXPECT_TRUE(PlatformChannelSendHandles(server_handle.get(), | 152 struct iovec iov = { const_cast<char*>(kHello), sizeof(kHello) }; |
| 151 &platform_handles[0], | 153 // We assume that the |sendmsg()| actually sends all the data. |
| 152 platform_handles.size())); | 154 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 155 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
| 156 &platform_handles[0], |
| 157 platform_handles.size())); |
| 153 | 158 |
| 154 WaitReadable(client_handle.get()); | 159 WaitReadable(client_handle.get()); |
| 155 | 160 |
| 156 char buf[100] = { 'a' }; | 161 char buf[100] = {}; |
| 157 std::deque<PlatformHandle> received_handles; | 162 std::deque<PlatformHandle> received_handles; |
| 158 EXPECT_EQ(1, PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 163 // We assume that the |recvmsg()| actually reads all the data. |
| 159 &received_handles)); | 164 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 160 EXPECT_EQ('\0', buf[0]); | 165 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), |
| 161 ASSERT_FALSE(received_handles.empty()); | 166 &received_handles)); |
| 167 EXPECT_STREQ(kHello, buf); |
| 162 EXPECT_EQ(i, received_handles.size()); | 168 EXPECT_EQ(i, received_handles.size()); |
| 163 | 169 |
| 164 for (size_t j = 0; !received_handles.empty(); j++) { | 170 for (size_t j = 0; !received_handles.empty(); j++) { |
| 165 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 171 base::ScopedFILE fp(test::FILEFromPlatformHandle( |
| 166 ScopedPlatformHandle(received_handles.front()), "rb")); | 172 ScopedPlatformHandle(received_handles.front()), "rb")); |
| 167 received_handles.pop_front(); | 173 received_handles.pop_front(); |
| 168 ASSERT_TRUE(fp); | 174 ASSERT_TRUE(fp); |
| 169 rewind(fp.get()); | 175 rewind(fp.get()); |
| 170 char read_buf[100]; | 176 char read_buf[100]; |
| 171 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 177 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
| 172 EXPECT_EQ(j + 1, bytes_read); | 178 EXPECT_EQ(j + 1, bytes_read); |
| 173 EXPECT_EQ(std::string(j + 1, '0' + i), std::string(read_buf, bytes_read)); | 179 EXPECT_EQ(std::string(j + 1, '0' + i), std::string(read_buf, bytes_read)); |
| 174 } | 180 } |
| 175 } | 181 } |
| 176 } | 182 } |
| 177 | 183 |
| 178 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { | 184 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { |
| 185 static const char kHello[] = "hello"; |
| 186 |
| 179 PlatformChannelPair channel_pair; | 187 PlatformChannelPair channel_pair; |
| 180 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 188 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); |
| 181 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 189 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
| 182 | 190 |
| 183 const std::string file_contents("hello world"); | 191 const std::string file_contents("hello world"); |
| 184 | 192 |
| 185 { | 193 { |
| 186 base::FilePath ignored; | 194 base::FilePath ignored; |
| 187 base::ScopedFILE fp(base::CreateAndOpenTemporaryFile(&ignored)); | 195 base::ScopedFILE fp(base::CreateAndOpenTemporaryFile(&ignored)); |
| 188 ASSERT_TRUE(fp); | 196 ASSERT_TRUE(fp); |
| 189 fwrite(file_contents.data(), 1, file_contents.size(), fp.get()); | 197 fwrite(file_contents.data(), 1, file_contents.size(), fp.get()); |
| 190 PlatformHandleVector platform_handles; | 198 PlatformHandleVector platform_handles; |
| 191 platform_handles.push_back( | 199 platform_handles.push_back( |
| 192 test::PlatformHandleFromFILE(fp.Pass()).release()); | 200 test::PlatformHandleFromFILE(fp.Pass()).release()); |
| 193 ASSERT_TRUE(platform_handles.back().is_valid()); | 201 ASSERT_TRUE(platform_handles.back().is_valid()); |
| 194 | 202 |
| 195 // Send the FD. | 203 // Send the FD (+ "hello"). |
| 196 EXPECT_TRUE(PlatformChannelSendHandles(server_handle.get(), | 204 struct iovec iov = { const_cast<char*>(kHello), sizeof(kHello) }; |
| 197 &platform_handles[0], | 205 // We assume that the |sendmsg()| actually sends all the data. |
| 198 platform_handles.size())); | 206 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 207 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
| 208 &platform_handles[0], |
| 209 platform_handles.size())); |
| 199 } | 210 } |
| 200 | 211 |
| 201 WaitReadable(client_handle.get()); | 212 WaitReadable(client_handle.get()); |
| 202 | 213 |
| 203 // Start with an invalid handle in the deque. | 214 // Start with an invalid handle in the deque. |
| 204 std::deque<PlatformHandle> received_handles; | 215 std::deque<PlatformHandle> received_handles; |
| 205 received_handles.push_back(PlatformHandle()); | 216 received_handles.push_back(PlatformHandle()); |
| 206 | 217 |
| 207 char buf[100] = { 'a' }; | 218 char buf[100] = {}; |
| 208 EXPECT_EQ(1, PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 219 // We assume that the |recvmsg()| actually reads all the data. |
| 209 &received_handles)); | 220 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
| 210 EXPECT_EQ('\0', buf[0]); | 221 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), |
| 222 &received_handles)); |
| 223 EXPECT_STREQ(kHello, buf); |
| 211 ASSERT_EQ(2u, received_handles.size()); | 224 ASSERT_EQ(2u, received_handles.size()); |
| 212 EXPECT_FALSE(received_handles[0].is_valid()); | 225 EXPECT_FALSE(received_handles[0].is_valid()); |
| 213 EXPECT_TRUE(received_handles[1].is_valid()); | 226 EXPECT_TRUE(received_handles[1].is_valid()); |
| 214 | 227 |
| 215 { | 228 { |
| 216 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 229 base::ScopedFILE fp(test::FILEFromPlatformHandle( |
| 217 ScopedPlatformHandle(received_handles[1]), "rb")); | 230 ScopedPlatformHandle(received_handles[1]), "rb")); |
| 218 received_handles[1] = PlatformHandle(); | 231 received_handles[1] = PlatformHandle(); |
| 219 ASSERT_TRUE(fp); | 232 ASSERT_TRUE(fp); |
| 220 rewind(fp.get()); | 233 rewind(fp.get()); |
| 221 char read_buf[100]; | 234 char read_buf[100]; |
| 222 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 235 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
| 223 EXPECT_EQ(file_contents.size(), bytes_read); | 236 EXPECT_EQ(file_contents.size(), bytes_read); |
| 224 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 237 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
| 225 } | 238 } |
| 226 } | 239 } |
| 227 | 240 |
| 228 } // namespace | 241 } // namespace |
| 229 } // namespace embedder | 242 } // namespace embedder |
| 230 } // namespace mojo | 243 } // namespace mojo |
| OLD | NEW |