OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sandbox/linux/services/broker_process.h" | 5 #include "sandbox/linux/services/broker_process.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
18 #include "base/bind.h" | 18 #include "base/bind.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
| 20 #include "base/files/scoped_file.h" |
20 #include "base/logging.h" | 21 #include "base/logging.h" |
21 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
22 #include "base/posix/eintr_wrapper.h" | 23 #include "base/posix/eintr_wrapper.h" |
23 #include "sandbox/linux/tests/test_utils.h" | 24 #include "sandbox/linux/tests/test_utils.h" |
24 #include "sandbox/linux/tests/unit_tests.h" | 25 #include "sandbox/linux/tests/unit_tests.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 using file_util::ScopedFD; | |
28 | |
29 namespace sandbox { | 28 namespace sandbox { |
30 | 29 |
31 namespace { | 30 namespace { |
32 | 31 |
33 // Creates and open a temporary file on creation and closes | 32 // Creates and open a temporary file on creation and closes |
34 // and removes it on destruction. | 33 // and removes it on destruction. |
35 // Unlike base/ helpers, this does not require JNI on Android. | 34 // Unlike base/ helpers, this does not require JNI on Android. |
36 class ScopedTemporaryFile { | 35 class ScopedTemporaryFile { |
37 public: | 36 public: |
38 ScopedTemporaryFile() | 37 ScopedTemporaryFile() |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 const char kFileCpuInfo[] = "/proc/cpuinfo"; | 261 const char kFileCpuInfo[] = "/proc/cpuinfo"; |
263 std::vector<std::string> read_whitelist; | 262 std::vector<std::string> read_whitelist; |
264 read_whitelist.push_back(kFileCpuInfo); | 263 read_whitelist.push_back(kFileCpuInfo); |
265 | 264 |
266 scoped_ptr<BrokerProcess> open_broker(new BrokerProcess( | 265 scoped_ptr<BrokerProcess> open_broker(new BrokerProcess( |
267 EPERM, read_whitelist, std::vector<std::string>(), fast_check_in_client)); | 266 EPERM, read_whitelist, std::vector<std::string>(), fast_check_in_client)); |
268 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback))); | 267 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback))); |
269 | 268 |
270 int fd = -1; | 269 int fd = -1; |
271 fd = open_broker->Open(kFileCpuInfo, O_RDWR); | 270 fd = open_broker->Open(kFileCpuInfo, O_RDWR); |
272 ScopedFD fd_closer(&fd); | 271 base::ScopedFD fd_closer(fd); |
273 ASSERT_EQ(fd, -EPERM); | 272 ASSERT_EQ(fd, -EPERM); |
274 | 273 |
275 // Check we can read /proc/cpuinfo. | 274 // Check we can read /proc/cpuinfo. |
276 int can_access = open_broker->Access(kFileCpuInfo, R_OK); | 275 int can_access = open_broker->Access(kFileCpuInfo, R_OK); |
277 ASSERT_EQ(can_access, 0); | 276 ASSERT_EQ(can_access, 0); |
278 can_access = open_broker->Access(kFileCpuInfo, W_OK); | 277 can_access = open_broker->Access(kFileCpuInfo, W_OK); |
279 ASSERT_EQ(can_access, -EPERM); | 278 ASSERT_EQ(can_access, -EPERM); |
280 // Check we can not write /proc/cpuinfo. | 279 // Check we can not write /proc/cpuinfo. |
281 | 280 |
282 // Open cpuinfo via the broker. | 281 // Open cpuinfo via the broker. |
283 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY); | 282 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY); |
284 ScopedFD cpuinfo_fd_closer(&cpuinfo_fd); | 283 base::ScopedFD cpuinfo_fd_closer(cpuinfo_fd); |
285 ASSERT_GE(cpuinfo_fd, 0); | 284 ASSERT_GE(cpuinfo_fd, 0); |
286 char buf[3]; | 285 char buf[3]; |
287 memset(buf, 0, sizeof(buf)); | 286 memset(buf, 0, sizeof(buf)); |
288 int read_len1 = read(cpuinfo_fd, buf, sizeof(buf)); | 287 int read_len1 = read(cpuinfo_fd, buf, sizeof(buf)); |
289 ASSERT_GT(read_len1, 0); | 288 ASSERT_GT(read_len1, 0); |
290 | 289 |
291 // Open cpuinfo directly. | 290 // Open cpuinfo directly. |
292 int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY); | 291 int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY); |
293 ScopedFD cpuinfo_fd2_closer(&cpuinfo_fd2); | 292 base::ScopedFD cpuinfo_fd2_closer(cpuinfo_fd2); |
294 ASSERT_GE(cpuinfo_fd2, 0); | 293 ASSERT_GE(cpuinfo_fd2, 0); |
295 char buf2[3]; | 294 char buf2[3]; |
296 memset(buf2, 1, sizeof(buf2)); | 295 memset(buf2, 1, sizeof(buf2)); |
297 int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2)); | 296 int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2)); |
298 ASSERT_GT(read_len1, 0); | 297 ASSERT_GT(read_len1, 0); |
299 | 298 |
300 // The following is not guaranteed true, but will be in practice. | 299 // The following is not guaranteed true, but will be in practice. |
301 ASSERT_EQ(read_len1, read_len2); | 300 ASSERT_EQ(read_len1, read_len2); |
302 // Compare the cpuinfo as returned by the broker with the one we opened | 301 // Compare the cpuinfo as returned by the broker with the one we opened |
303 // ourselves. | 302 // ourselves. |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 // expected. | 428 // expected. |
430 } | 429 } |
431 | 430 |
432 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { | 431 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { |
433 TestOpenComplexFlags(false /* fast_check_in_client */); | 432 TestOpenComplexFlags(false /* fast_check_in_client */); |
434 // Don't do anything here, so that ASSERT works in the subfunction as | 433 // Don't do anything here, so that ASSERT works in the subfunction as |
435 // expected. | 434 // expected. |
436 } | 435 } |
437 | 436 |
438 } // namespace sandbox | 437 } // namespace sandbox |
OLD | NEW |