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