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

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

Issue 1354433003: Add a mojo::test::ScopedTestDir and convert tests to use it. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 3 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
« no previous file with comments | « no previous file | mojo/edk/system/multiprocess_message_pipe_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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/logging.h" 18 #include "base/logging.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "mojo/edk/embedder/platform_channel_utils_posix.h" 20 #include "mojo/edk/embedder/platform_channel_utils_posix.h"
21 #include "mojo/edk/embedder/platform_handle.h" 21 #include "mojo/edk/embedder/platform_handle.h"
22 #include "mojo/edk/embedder/platform_handle_vector.h" 22 #include "mojo/edk/embedder/platform_handle_vector.h"
23 #include "mojo/edk/embedder/scoped_platform_handle.h" 23 #include "mojo/edk/embedder/scoped_platform_handle.h"
24 #include "mojo/edk/test/scoped_test_dir.h"
24 #include "mojo/edk/test/test_utils.h" 25 #include "mojo/edk/test/test_utils.h"
25 #include "mojo/edk/util/scoped_file.h" 26 #include "mojo/edk/util/scoped_file.h"
26 #include "mojo/public/cpp/system/macros.h" 27 #include "mojo/public/cpp/system/macros.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 29
29 #if defined(OS_ANDROID)
30 #include "base/android/path_utils.h"
31 #include "base/files/file_path.h"
32 #endif
33
34 namespace mojo { 30 namespace mojo {
35 namespace embedder { 31 namespace embedder {
36 namespace { 32 namespace {
37 33
38 void WaitReadable(PlatformHandle h) { 34 void WaitReadable(PlatformHandle h) {
39 struct pollfd pfds = {}; 35 struct pollfd pfds = {};
40 pfds.fd = h.fd; 36 pfds.fd = h.fd;
41 pfds.events = POLLIN; 37 pfds.events = POLLIN;
42 CHECK_EQ(poll(&pfds, 1, -1), 1); 38 CHECK_EQ(poll(&pfds, 1, -1), 1);
43 } 39 }
44 40
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 { 41 class PlatformChannelPairPosixTest : public testing::Test {
62 public: 42 public:
63 PlatformChannelPairPosixTest() {} 43 PlatformChannelPairPosixTest() {}
64 ~PlatformChannelPairPosixTest() override {} 44 ~PlatformChannelPairPosixTest() override {}
65 45
66 void SetUp() override { 46 void SetUp() override {
67 // Make sure |SIGPIPE| isn't being ignored. 47 // Make sure |SIGPIPE| isn't being ignored.
68 struct sigaction action = {}; 48 struct sigaction action = {};
69 action.sa_handler = SIG_DFL; 49 action.sa_handler = SIG_DFL;
70 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); 50 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; 120 std::deque<PlatformHandle> received_handles;
141 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, 121 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf,
142 sizeof(buf), &received_handles); 122 sizeof(buf), &received_handles);
143 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); 123 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result);
144 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); 124 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result)));
145 EXPECT_TRUE(received_handles.empty()); 125 EXPECT_TRUE(received_handles.empty());
146 } 126 }
147 } 127 }
148 128
149 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { 129 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) {
130 mojo::test::ScopedTestDir test_dir;
131
150 static const char kHello[] = "hello"; 132 static const char kHello[] = "hello";
151 133
152 PlatformChannelPair channel_pair; 134 PlatformChannelPair channel_pair;
153 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); 135 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass();
154 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); 136 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass();
155 137
156 // Reduce the number of FDs opened on OS X to avoid test flake. 138 // Reduce the number of FDs opened on OS X to avoid test flake.
157 #if defined(OS_MACOSX) 139 #if defined(OS_MACOSX)
158 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2; 140 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles / 2;
159 #else 141 #else
160 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; 142 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles;
161 #endif 143 #endif
162 144
163 for (size_t i = 1; i < kNumHandlesToSend; i++) { 145 for (size_t i = 1; i < kNumHandlesToSend; i++) {
164 // Make |i| files, with the j-th file consisting of j copies of the digit 146 // Make |i| files, with the j-th file consisting of j copies of the digit
165 // |c|. 147 // |c|.
166 const char c = '0' + (i % 10); 148 const char c = '0' + (i % 10);
167 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); 149 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
168 for (size_t j = 1; j <= i; j++) { 150 for (size_t j = 1; j <= i; j++) {
169 util::ScopedFILE fp(NewTmpFile()); 151 util::ScopedFILE fp(test_dir.CreateFile());
170 ASSERT_TRUE(fp); 152 ASSERT_TRUE(fp);
171 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); 153 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get()));
172 platform_handles->push_back( 154 platform_handles->push_back(
173 test::PlatformHandleFromFILE(fp.Pass()).release()); 155 test::PlatformHandleFromFILE(fp.Pass()).release());
174 ASSERT_TRUE(platform_handles->back().is_valid()); 156 ASSERT_TRUE(platform_handles->back().is_valid());
175 } 157 }
176 158
177 // Send the FDs (+ "hello"). 159 // Send the FDs (+ "hello").
178 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; 160 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)};
179 // We assume that the |sendmsg()| actually sends all the data. 161 // We assume that the |sendmsg()| actually sends all the data.
(...skipping 21 matching lines...) Expand all
201 rewind(fp.get()); 183 rewind(fp.get());
202 char read_buf[kNumHandlesToSend]; 184 char read_buf[kNumHandlesToSend];
203 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); 185 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get());
204 EXPECT_EQ(j + 1, bytes_read); 186 EXPECT_EQ(j + 1, bytes_read);
205 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read)); 187 EXPECT_EQ(std::string(j + 1, c), std::string(read_buf, bytes_read));
206 } 188 }
207 } 189 }
208 } 190 }
209 191
210 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { 192 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) {
193 mojo::test::ScopedTestDir test_dir;
194
211 static const char kHello[] = "hello"; 195 static const char kHello[] = "hello";
212 196
213 PlatformChannelPair channel_pair; 197 PlatformChannelPair channel_pair;
214 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); 198 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass();
215 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); 199 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass();
216 200
217 const std::string file_contents("hello world"); 201 const std::string file_contents("hello world");
218 202
219 { 203 {
220 util::ScopedFILE fp(NewTmpFile()); 204 util::ScopedFILE fp(test_dir.CreateFile());
221 ASSERT_TRUE(fp); 205 ASSERT_TRUE(fp);
222 ASSERT_EQ(file_contents.size(), 206 ASSERT_EQ(file_contents.size(),
223 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); 207 fwrite(file_contents.data(), 1, file_contents.size(), fp.get()));
224 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); 208 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
225 platform_handles->push_back( 209 platform_handles->push_back(
226 test::PlatformHandleFromFILE(fp.Pass()).release()); 210 test::PlatformHandleFromFILE(fp.Pass()).release());
227 ASSERT_TRUE(platform_handles->back().is_valid()); 211 ASSERT_TRUE(platform_handles->back().is_valid());
228 212
229 // Send the FD (+ "hello"). 213 // Send the FD (+ "hello").
230 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; 214 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)};
(...skipping 29 matching lines...) Expand all
260 char read_buf[100]; 244 char read_buf[100];
261 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());
262 EXPECT_EQ(file_contents.size(), bytes_read); 246 EXPECT_EQ(file_contents.size(), bytes_read);
263 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); 247 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read));
264 } 248 }
265 } 249 }
266 250
267 } // namespace 251 } // namespace
268 } // namespace embedder 252 } // namespace embedder
269 } // namespace mojo 253 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/edk/system/multiprocess_message_pipe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698