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

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

Issue 1261903002: Fix system unit tests on Android. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 4 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 | no next file » | 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/files/file_path.h"
viettrungluu 2015/07/28 18:17:09 Could you please put this under the ifdef also?
qsr 2015/07/29 08:37:31 Done.
18 #include "base/files/scoped_file.h" 19 #include "base/files/scoped_file.h"
19 #include "base/logging.h" 20 #include "base/logging.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)
viettrungluu 2015/07/28 18:17:10 You should explicitly include build/build_config.h
qsr 2015/07/29 08:37:31 Done.
30 #include "base/android/path_utils.h"
31 #endif
32
28 namespace mojo { 33 namespace mojo {
29 namespace embedder { 34 namespace embedder {
30 namespace { 35 namespace {
31 36
32 void WaitReadable(PlatformHandle h) { 37 void WaitReadable(PlatformHandle h) {
33 struct pollfd pfds = {}; 38 struct pollfd pfds = {};
34 pfds.fd = h.fd; 39 pfds.fd = h.fd;
35 pfds.events = POLLIN; 40 pfds.events = POLLIN;
36 CHECK_EQ(poll(&pfds, 1, -1), 1); 41 CHECK_EQ(poll(&pfds, 1, -1), 1);
37 } 42 }
38 43
44 FILE* NewTmpFile() {
45 #if defined(OS_ANDROID)
46 base::FilePath tmpdir;
47 if (!base::android::GetCacheDirectory(&tmpdir))
48 return nullptr;
49 std::string templ = tmpdir.Append("XXXXXXXX").value();
50 int fd = mkstemp(const_cast<char*>(templ.c_str()));
51 if (fd == -1)
52 return nullptr;
53 return fdopen(fd, "w");
viettrungluu 2015/07/28 18:17:10 You should unlink() the file too.
viettrungluu 2015/07/28 18:17:10 This should be "wb+" according to the C standard (
qsr 2015/07/29 08:37:31 Done.
qsr 2015/07/29 08:37:31 Done.
54 #else
55 return tmpfile();
56 #endif
57 }
58
39 class PlatformChannelPairPosixTest : public testing::Test { 59 class PlatformChannelPairPosixTest : public testing::Test {
40 public: 60 public:
41 PlatformChannelPairPosixTest() {} 61 PlatformChannelPairPosixTest() {}
42 ~PlatformChannelPairPosixTest() override {} 62 ~PlatformChannelPairPosixTest() override {}
43 63
44 void SetUp() override { 64 void SetUp() override {
45 // Make sure |SIGPIPE| isn't being ignored. 65 // Make sure |SIGPIPE| isn't being ignored.
46 struct sigaction action = {}; 66 struct sigaction action = {};
47 action.sa_handler = SIG_DFL; 67 action.sa_handler = SIG_DFL;
48 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_)); 68 ASSERT_EQ(0, sigaction(SIGPIPE, &action, &old_action_));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 #else 157 #else
138 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles; 158 const size_t kNumHandlesToSend = kPlatformChannelMaxNumHandles;
139 #endif 159 #endif
140 160
141 for (size_t i = 1; i < kNumHandlesToSend; i++) { 161 for (size_t i = 1; i < kNumHandlesToSend; i++) {
142 // Make |i| files, with the j-th file consisting of j copies of the digit 162 // Make |i| files, with the j-th file consisting of j copies of the digit
143 // |c|. 163 // |c|.
144 const char c = '0' + (i % 10); 164 const char c = '0' + (i % 10);
145 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); 165 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
146 for (size_t j = 1; j <= i; j++) { 166 for (size_t j = 1; j <= i; j++) {
147 base::ScopedFILE fp(tmpfile()); 167 base::ScopedFILE fp(NewTmpFile());
148 ASSERT_TRUE(fp); 168 ASSERT_TRUE(fp);
149 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); 169 ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get()));
150 platform_handles->push_back( 170 platform_handles->push_back(
151 test::PlatformHandleFromFILE(fp.Pass()).release()); 171 test::PlatformHandleFromFILE(fp.Pass()).release());
152 ASSERT_TRUE(platform_handles->back().is_valid()); 172 ASSERT_TRUE(platform_handles->back().is_valid());
153 } 173 }
154 174
155 // Send the FDs (+ "hello"). 175 // Send the FDs (+ "hello").
156 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; 176 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)};
157 // We assume that the |sendmsg()| actually sends all the data. 177 // We assume that the |sendmsg()| actually sends all the data.
(...skipping 30 matching lines...) Expand all
188 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) { 208 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) {
189 static const char kHello[] = "hello"; 209 static const char kHello[] = "hello";
190 210
191 PlatformChannelPair channel_pair; 211 PlatformChannelPair channel_pair;
192 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); 212 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass();
193 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); 213 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass();
194 214
195 const std::string file_contents("hello world"); 215 const std::string file_contents("hello world");
196 216
197 { 217 {
198 base::ScopedFILE fp(tmpfile()); 218 base::ScopedFILE fp(NewTmpFile());
199 ASSERT_TRUE(fp); 219 ASSERT_TRUE(fp);
200 ASSERT_EQ(file_contents.size(), 220 ASSERT_EQ(file_contents.size(),
201 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); 221 fwrite(file_contents.data(), 1, file_contents.size(), fp.get()));
202 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); 222 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
203 platform_handles->push_back( 223 platform_handles->push_back(
204 test::PlatformHandleFromFILE(fp.Pass()).release()); 224 test::PlatformHandleFromFILE(fp.Pass()).release());
205 ASSERT_TRUE(platform_handles->back().is_valid()); 225 ASSERT_TRUE(platform_handles->back().is_valid());
206 226
207 // Send the FD (+ "hello"). 227 // Send the FD (+ "hello").
208 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; 228 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)};
(...skipping 29 matching lines...) Expand all
238 char read_buf[100]; 258 char read_buf[100];
239 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); 259 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get());
240 EXPECT_EQ(file_contents.size(), bytes_read); 260 EXPECT_EQ(file_contents.size(), bytes_read);
241 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); 261 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read));
242 } 262 }
243 } 263 }
244 264
245 } // namespace 265 } // namespace
246 } // namespace embedder 266 } // namespace embedder
247 } // namespace mojo 267 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698