Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: mojo/edk/embedder/platform_channel_pair_posix_unittest.cc

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more cleanup Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "third_party/mojo/src/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/file_path.h"
19 #include "base/files/file_util.h"
18 #include "base/files/scoped_file.h" 20 #include "base/files/scoped_file.h"
21 #include "base/files/scoped_temp_dir.h"
19 #include "base/logging.h" 22 #include "base/logging.h"
20 #include "build/build_config.h" 23 #include "mojo/edk/embedder/platform_channel_utils_posix.h"
24 #include "mojo/edk/embedder/platform_handle.h"
25 #include "mojo/edk/embedder/platform_handle_vector.h"
26 #include "mojo/edk/embedder/scoped_platform_handle.h"
27 #include "mojo/edk/test/test_utils.h"
21 #include "mojo/public/cpp/system/macros.h" 28 #include "mojo/public/cpp/system/macros.h"
22 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_utils_posix.h"
24 #include "third_party/mojo/src/mojo/edk/embedder/platform_handle.h"
25 #include "third_party/mojo/src/mojo/edk/embedder/platform_handle_vector.h"
26 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h"
27 #include "third_party/mojo/src/mojo/edk/test/test_utils.h"
28
29 #if defined(OS_ANDROID)
30 #include "base/android/path_utils.h"
31 #include "base/files/file_path.h"
32 #endif
33 30
34 namespace mojo { 31 namespace mojo {
35 namespace embedder { 32 namespace edk {
36 namespace { 33 namespace {
37 34
38 void WaitReadable(PlatformHandle h) { 35 void WaitReadable(PlatformHandle h) {
39 struct pollfd pfds = {}; 36 struct pollfd pfds = {};
40 pfds.fd = h.fd; 37 pfds.fd = h.fd;
41 pfds.events = POLLIN; 38 pfds.events = POLLIN;
42 CHECK_EQ(poll(&pfds, 1, -1), 1); 39 CHECK_EQ(poll(&pfds, 1, -1), 1);
43 } 40 }
44 41
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
61 class PlatformChannelPairPosixTest : public testing::Test { 42 class PlatformChannelPairPosixTest : public testing::Test {
62 public: 43 public:
63 PlatformChannelPairPosixTest() {} 44 PlatformChannelPairPosixTest() {}
64 ~PlatformChannelPairPosixTest() override {} 45 ~PlatformChannelPairPosixTest() override {}
65 46
66 void SetUp() override { 47 void SetUp() override {
67 // Make sure |SIGPIPE| isn't being ignored. 48 // Make sure |SIGPIPE| isn't being ignored.
68 struct sigaction action = {}; 49 struct sigaction action = {};
69 action.sa_handler = SIG_DFL; 50 action.sa_handler = SIG_DFL;
70 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); 51 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 std::deque<PlatformHandle> received_handles; 121 std::deque<PlatformHandle> received_handles;
141 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, 122 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf,
142 sizeof(buf), &received_handles); 123 sizeof(buf), &received_handles);
143 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); 124 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result);
144 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)));
145 EXPECT_TRUE(received_handles.empty()); 126 EXPECT_TRUE(received_handles.empty());
146 } 127 }
147 } 128 }
148 129
149 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { 130 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) {
131 base::ScopedTempDir temp_dir;
132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
133
150 static const char kHello[] = "hello"; 134 static const char kHello[] = "hello";
151 135
152 PlatformChannelPair channel_pair; 136 PlatformChannelPair channel_pair;
153 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); 137 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass();
154 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); 138 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass();
155 139
156 // 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.
157 #if defined(OS_MACOSX) 141 #if defined(OS_MACOSX)
158 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2; 142 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2;
159 #else 143 #else
160 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; 144 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles;
161 #endif 145 #endif
162 146
163 for (size_t i = 1; i < kNumHandlesToSend; i++) { 147 for (size_t i = 1; i < kNumHandlesToSend; i++) {
164 // 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
165 // |c|. 149 // |c|.
166 const char c = '0' + (i % 10); 150 const char c = '0' + (i % 10);
167 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); 151 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
168 for (size_t j = 1; j <= i; j++) { 152 for (size_t j = 1; j <= i; j++) {
169 base::ScopedFILE fp(NewTmpFile()); 153 base::FilePath unused;
154 base::ScopedFILE fp(
155 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
170 ASSERT_TRUE(fp); 156 ASSERT_TRUE(fp);
171 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()));
172 platform_handles->push_back( 158 platform_handles->push_back(
173 test::PlatformHandleFromFILE(fp.Pass()).release()); 159 test::PlatformHandleFromFILE(fp.Pass()).release());
174 ASSERT_TRUE(platform_handles->back().is_valid()); 160 ASSERT_TRUE(platform_handles->back().is_valid());
175 } 161 }
176 162
177 // Send the FDs (+ "hello"). 163 // Send the FDs (+ "hello").
178 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; 164 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)};
179 // We assume that the |sendmsg()| actually sends all the data. 165 // We assume that the |sendmsg()| actually sends all the data.
(...skipping 21 matching lines...) Expand all
201 rewind(fp.get()); 187 rewind(fp.get());
202 char read_buf[kNumHandlesToSend]; 188 char read_buf[kNumHandlesToSend];
203 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); 189 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get());
204 EXPECT_EQ(j + 1, bytes_read); 190 EXPECT_EQ(j + 1, bytes_read);
205 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); 191 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read));
206 } 192 }
207 } 193 }
208 } 194 }
209 195
210 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { 196 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) {
197 base::ScopedTempDir temp_dir;
198 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
199
211 static const char kHello[] = "hello"; 200 static const char kHello[] = "hello";
212 201
213 PlatformChannelPair channel_pair; 202 PlatformChannelPair channel_pair;
214 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); 203 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass();
215 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); 204 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass();
216 205
217 const std::string file_contents("hello world"); 206 const std::string file_contents("hello world");
218 207
219 { 208 {
220 base::ScopedFILE fp(NewTmpFile()); 209 base::FilePath unused;
210 base::ScopedFILE fp(
211 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
221 ASSERT_TRUE(fp); 212 ASSERT_TRUE(fp);
222 ASSERT_EQ(file_contents.size(), 213 ASSERT_EQ(file_contents.size(),
223 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); 214 fwrite(file_contents.data(), 1, file_contents.size(), fp.get()));
224 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); 215 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
225 platform_handles->push_back( 216 platform_handles->push_back(
226 test::PlatformHandleFromFILE(fp.Pass()).release()); 217 test::PlatformHandleFromFILE(fp.Pass()).release());
227 ASSERT_TRUE(platform_handles->back().is_valid()); 218 ASSERT_TRUE(platform_handles->back().is_valid());
228 219
229 // Send the FD (+ "hello"). 220 // Send the FD (+ "hello").
230 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; 221 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)};
(...skipping 27 matching lines...) Expand all
258 ASSERT_TRUE(fp); 249 ASSERT_TRUE(fp);
259 rewind(fp.get()); 250 rewind(fp.get());
260 char read_buf[100]; 251 char read_buf[100];
261 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());
262 EXPECT_EQ(file_contents.size(), bytes_read); 253 EXPECT_EQ(file_contents.size(), bytes_read);
263 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); 254 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read));
264 } 255 }
265 } 256 }
266 257
267 } // namespace 258 } // namespace
268 } // namespace embedder 259 } // namespace edk
269 } // namespace mojo 260 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698