| 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/unit_tests.h" | 24 #include "sandbox/linux/tests/unit_tests.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using file_util::ScopedFD; | |
| 27 | |
| 28 namespace sandbox { | 27 namespace sandbox { |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 // Creates and open a temporary file on creation and closes | 31 // Creates and open a temporary file on creation and closes |
| 33 // and removes it on destruction. | 32 // and removes it on destruction. |
| 34 // Unlike base/ helpers, this does not require JNI on Android. | 33 // Unlike base/ helpers, this does not require JNI on Android. |
| 35 class ScopedTemporaryFile { | 34 class ScopedTemporaryFile { |
| 36 public: | 35 public: |
| 37 ScopedTemporaryFile() | 36 ScopedTemporaryFile() |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 std::vector<std::string> read_whitelist; | 264 std::vector<std::string> read_whitelist; |
| 266 read_whitelist.push_back(kFileCpuInfo); | 265 read_whitelist.push_back(kFileCpuInfo); |
| 267 | 266 |
| 268 scoped_ptr<BrokerProcess> open_broker(new BrokerProcess( | 267 scoped_ptr<BrokerProcess> open_broker(new BrokerProcess( |
| 269 EPERM, read_whitelist, std::vector<std::string>(), fast_check_in_client)); | 268 EPERM, read_whitelist, std::vector<std::string>(), fast_check_in_client)); |
| 270 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback))); | 269 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback))); |
| 271 pid_t broker_pid = open_broker->broker_pid(); | 270 pid_t broker_pid = open_broker->broker_pid(); |
| 272 | 271 |
| 273 int fd = -1; | 272 int fd = -1; |
| 274 fd = open_broker->Open(kFileCpuInfo, O_RDWR); | 273 fd = open_broker->Open(kFileCpuInfo, O_RDWR); |
| 275 ScopedFD fd_closer(&fd); | 274 base::ScopedFD fd_closer(fd); |
| 276 ASSERT_EQ(fd, -EPERM); | 275 ASSERT_EQ(fd, -EPERM); |
| 277 | 276 |
| 278 // Check we can read /proc/cpuinfo. | 277 // Check we can read /proc/cpuinfo. |
| 279 int can_access = open_broker->Access(kFileCpuInfo, R_OK); | 278 int can_access = open_broker->Access(kFileCpuInfo, R_OK); |
| 280 ASSERT_EQ(can_access, 0); | 279 ASSERT_EQ(can_access, 0); |
| 281 can_access = open_broker->Access(kFileCpuInfo, W_OK); | 280 can_access = open_broker->Access(kFileCpuInfo, W_OK); |
| 282 ASSERT_EQ(can_access, -EPERM); | 281 ASSERT_EQ(can_access, -EPERM); |
| 283 // Check we can not write /proc/cpuinfo. | 282 // Check we can not write /proc/cpuinfo. |
| 284 | 283 |
| 285 // Open cpuinfo via the broker. | 284 // Open cpuinfo via the broker. |
| 286 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY); | 285 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY); |
| 287 ScopedFD cpuinfo_fd_closer(&cpuinfo_fd); | 286 base::ScopedFD cpuinfo_fd_closer(cpuinfo_fd); |
| 288 ASSERT_GE(cpuinfo_fd, 0); | 287 ASSERT_GE(cpuinfo_fd, 0); |
| 289 char buf[3]; | 288 char buf[3]; |
| 290 memset(buf, 0, sizeof(buf)); | 289 memset(buf, 0, sizeof(buf)); |
| 291 int read_len1 = read(cpuinfo_fd, buf, sizeof(buf)); | 290 int read_len1 = read(cpuinfo_fd, buf, sizeof(buf)); |
| 292 ASSERT_GT(read_len1, 0); | 291 ASSERT_GT(read_len1, 0); |
| 293 | 292 |
| 294 // Open cpuinfo directly. | 293 // Open cpuinfo directly. |
| 295 int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY); | 294 int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY); |
| 296 ScopedFD cpuinfo_fd2_closer(&cpuinfo_fd2); | 295 base::ScopedFD cpuinfo_fd2_closer(cpuinfo_fd2); |
| 297 ASSERT_GE(cpuinfo_fd2, 0); | 296 ASSERT_GE(cpuinfo_fd2, 0); |
| 298 char buf2[3]; | 297 char buf2[3]; |
| 299 memset(buf2, 1, sizeof(buf2)); | 298 memset(buf2, 1, sizeof(buf2)); |
| 300 int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2)); | 299 int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2)); |
| 301 ASSERT_GT(read_len1, 0); | 300 ASSERT_GT(read_len1, 0); |
| 302 | 301 |
| 303 // The following is not guaranteed true, but will be in practice. | 302 // The following is not guaranteed true, but will be in practice. |
| 304 ASSERT_EQ(read_len1, read_len2); | 303 ASSERT_EQ(read_len1, read_len2); |
| 305 // Compare the cpuinfo as returned by the broker with the one we opened | 304 // Compare the cpuinfo as returned by the broker with the one we opened |
| 306 // ourselves. | 305 // ourselves. |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 // expected. | 431 // expected. |
| 433 } | 432 } |
| 434 | 433 |
| 435 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { | 434 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { |
| 436 TestOpenComplexFlags(false /* fast_check_in_client */); | 435 TestOpenComplexFlags(false /* fast_check_in_client */); |
| 437 // Don't do anything here, so that ASSERT works in the subfunction as | 436 // Don't do anything here, so that ASSERT works in the subfunction as |
| 438 // expected. | 437 // expected. |
| 439 } | 438 } |
| 440 | 439 |
| 441 } // namespace sandbox | 440 } // namespace sandbox |
| OLD | NEW |