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> |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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().Pass(); |
138 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 138 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
139 | 139 |
140 for (size_t i = 1; i < kPlatformChannelMaxNumHandles; i++) { | 140 for (size_t i = 1; i < kPlatformChannelMaxNumHandles; i++) { |
141 // Make |i| files, with the j-th file consisting of j copies of the digit i. | 141 // Make |i| files, with the j-th file consisting of j copies of the digit |
142 PlatformHandleVector platform_handles; | 142 // |c|. |
| 143 const char c = '0' + (i % 10); |
| 144 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
143 for (size_t j = 1; j <= i; j++) { | 145 for (size_t j = 1; j <= i; j++) { |
144 base::FilePath unused; | 146 base::FilePath unused; |
145 base::ScopedFILE fp( | 147 base::ScopedFILE fp( |
146 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 148 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
147 ASSERT_TRUE(fp); | 149 ASSERT_TRUE(fp); |
148 ASSERT_EQ(j, fwrite(std::string(j, '0' + i).data(), 1, j, fp.get())); | 150 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); |
149 platform_handles.push_back( | 151 platform_handles->push_back( |
150 test::PlatformHandleFromFILE(fp.Pass()).release()); | 152 test::PlatformHandleFromFILE(fp.Pass()).release()); |
151 ASSERT_TRUE(platform_handles.back().is_valid()); | 153 ASSERT_TRUE(platform_handles->back().is_valid()); |
152 } | 154 } |
153 | 155 |
154 // Send the FDs (+ "hello"). | 156 // Send the FDs (+ "hello"). |
155 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 157 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
156 // We assume that the |sendmsg()| actually sends all the data. | 158 // We assume that the |sendmsg()| actually sends all the data. |
157 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 159 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
158 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 160 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
159 &platform_handles[0], | 161 &platform_handles->at(0), |
160 platform_handles.size())); | 162 platform_handles->size())); |
161 | 163 |
162 WaitReadable(client_handle.get()); | 164 WaitReadable(client_handle.get()); |
163 | 165 |
164 char buf[100] = {}; | 166 char buf[10000] = {}; |
165 std::deque<PlatformHandle> received_handles; | 167 std::deque<PlatformHandle> received_handles; |
166 // We assume that the |recvmsg()| actually reads all the data. | 168 // We assume that the |recvmsg()| actually reads all the data. |
167 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 169 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
168 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 170 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), |
169 &received_handles)); | 171 &received_handles)); |
170 EXPECT_STREQ(kHello, buf); | 172 EXPECT_STREQ(kHello, buf); |
171 EXPECT_EQ(i, received_handles.size()); | 173 EXPECT_EQ(i, received_handles.size()); |
172 | 174 |
173 for (size_t j = 0; !received_handles.empty(); j++) { | 175 for (size_t j = 0; !received_handles.empty(); j++) { |
174 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 176 base::ScopedFILE fp(test::FILEFromPlatformHandle( |
175 ScopedPlatformHandle(received_handles.front()), "rb")); | 177 ScopedPlatformHandle(received_handles.front()), "rb")); |
176 received_handles.pop_front(); | 178 received_handles.pop_front(); |
177 ASSERT_TRUE(fp); | 179 ASSERT_TRUE(fp); |
178 rewind(fp.get()); | 180 rewind(fp.get()); |
179 char read_buf[100]; | 181 char read_buf[kPlatformChannelMaxNumHandles]; |
180 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 182 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
181 EXPECT_EQ(j + 1, bytes_read); | 183 EXPECT_EQ(j + 1, bytes_read); |
182 EXPECT_EQ(std::string(j + 1, '0' + i), std::string(read_buf, bytes_read)); | 184 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); |
183 } | 185 } |
184 } | 186 } |
185 } | 187 } |
186 | 188 |
187 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { | 189 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { |
188 base::ScopedTempDir temp_dir; | 190 base::ScopedTempDir temp_dir; |
189 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
190 | 192 |
191 static const char kHello[] = "hello"; | 193 static const char kHello[] = "hello"; |
192 | 194 |
193 PlatformChannelPair channel_pair; | 195 PlatformChannelPair channel_pair; |
194 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 196 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); |
195 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 197 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
196 | 198 |
197 const std::string file_contents("hello world"); | 199 const std::string file_contents("hello world"); |
198 | 200 |
199 { | 201 { |
200 base::FilePath unused; | 202 base::FilePath unused; |
201 base::ScopedFILE fp( | 203 base::ScopedFILE fp( |
202 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 204 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
203 ASSERT_TRUE(fp); | 205 ASSERT_TRUE(fp); |
204 ASSERT_EQ(file_contents.size(), | 206 ASSERT_EQ(file_contents.size(), |
205 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); | 207 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); |
206 PlatformHandleVector platform_handles; | 208 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); |
207 platform_handles.push_back( | 209 platform_handles->push_back( |
208 test::PlatformHandleFromFILE(fp.Pass()).release()); | 210 test::PlatformHandleFromFILE(fp.Pass()).release()); |
209 ASSERT_TRUE(platform_handles.back().is_valid()); | 211 ASSERT_TRUE(platform_handles->back().is_valid()); |
210 | 212 |
211 // Send the FD (+ "hello"). | 213 // Send the FD (+ "hello"). |
212 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 214 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
213 // We assume that the |sendmsg()| actually sends all the data. | 215 // We assume that the |sendmsg()| actually sends all the data. |
214 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 216 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
215 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 217 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, |
216 &platform_handles[0], | 218 &platform_handles->at(0), |
217 platform_handles.size())); | 219 platform_handles->size())); |
218 } | 220 } |
219 | 221 |
220 WaitReadable(client_handle.get()); | 222 WaitReadable(client_handle.get()); |
221 | 223 |
222 // Start with an invalid handle in the deque. | 224 // Start with an invalid handle in the deque. |
223 std::deque<PlatformHandle> received_handles; | 225 std::deque<PlatformHandle> received_handles; |
224 received_handles.push_back(PlatformHandle()); | 226 received_handles.push_back(PlatformHandle()); |
225 | 227 |
226 char buf[100] = {}; | 228 char buf[100] = {}; |
227 // We assume that the |recvmsg()| actually reads all the data. | 229 // We assume that the |recvmsg()| actually reads all the data. |
(...skipping 14 matching lines...) Expand all Loading... |
242 char read_buf[100]; | 244 char read_buf[100]; |
243 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 245 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
244 EXPECT_EQ(file_contents.size(), bytes_read); | 246 EXPECT_EQ(file_contents.size(), bytes_read); |
245 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 247 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
246 } | 248 } |
247 } | 249 } |
248 | 250 |
249 } // namespace | 251 } // namespace |
250 } // namespace embedder | 252 } // namespace embedder |
251 } // namespace mojo | 253 } // namespace mojo |
OLD | NEW |