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/resource.h> | 9 #include <sys/resource.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 #include <sys/wait.h> | 12 #include <sys/wait.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
20 #include "base/bind.h" | 20 #include "base/bind.h" |
21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
22 #include "base/files/scoped_file.h" | 22 #include "base/files/scoped_file.h" |
23 #include "base/logging.h" | 23 #include "base/logging.h" |
24 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
25 #include "base/posix/eintr_wrapper.h" | 25 #include "base/posix/eintr_wrapper.h" |
26 #include "base/posix/unix_domain_socket_linux.h" | 26 #include "base/posix/unix_domain_socket_linux.h" |
| 27 #include "sandbox/linux/tests/scoped_temporary_file.h" |
27 #include "sandbox/linux/tests/test_utils.h" | 28 #include "sandbox/linux/tests/test_utils.h" |
28 #include "sandbox/linux/tests/unit_tests.h" | 29 #include "sandbox/linux/tests/unit_tests.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 | 31 |
31 namespace sandbox { | 32 namespace sandbox { |
32 | 33 |
33 class BrokerProcessTestHelper { | 34 class BrokerProcessTestHelper { |
34 public: | 35 public: |
35 static int get_ipc_socketpair(const BrokerProcess* broker) { | 36 static int get_ipc_socketpair(const BrokerProcess* broker) { |
36 return broker->ipc_socketpair_; | 37 return broker->ipc_socketpair_; |
37 } | 38 } |
38 }; | 39 }; |
39 | 40 |
40 namespace { | 41 namespace { |
41 | 42 |
42 // Creates and open a temporary file on creation and closes | |
43 // and removes it on destruction. | |
44 // Unlike base/ helpers, this does not require JNI on Android. | |
45 class ScopedTemporaryFile { | |
46 public: | |
47 ScopedTemporaryFile() | |
48 : fd_(-1) { | |
49 #if defined(OS_ANDROID) | |
50 static const char file_template[] = "/data/local/tmp/ScopedTempFileXXXXXX"; | |
51 #else | |
52 static const char file_template[] = "/tmp/ScopedTempFileXXXXXX"; | |
53 #endif // defined(OS_ANDROID) | |
54 COMPILE_ASSERT(sizeof(full_file_name_) >= sizeof(file_template), | |
55 full_file_name_is_large_enough); | |
56 memcpy(full_file_name_, file_template, sizeof(file_template)); | |
57 fd_ = mkstemp(full_file_name_); | |
58 CHECK_LE(0, fd_); | |
59 } | |
60 ~ScopedTemporaryFile() { | |
61 CHECK_EQ(0, unlink(full_file_name_)); | |
62 CHECK_EQ(0, IGNORE_EINTR(close(fd_))); | |
63 } | |
64 | |
65 int fd() const { return fd_; } | |
66 const char* full_file_name() const { return full_file_name_; } | |
67 | |
68 private: | |
69 int fd_; | |
70 char full_file_name_[128]; | |
71 DISALLOW_COPY_AND_ASSIGN(ScopedTemporaryFile); | |
72 }; | |
73 | |
74 bool NoOpCallback() { return true; } | 43 bool NoOpCallback() { return true; } |
75 | 44 |
76 } // namespace | 45 } // namespace |
77 | 46 |
78 TEST(BrokerProcess, CreateAndDestroy) { | 47 TEST(BrokerProcess, CreateAndDestroy) { |
79 std::vector<std::string> read_whitelist; | 48 std::vector<std::string> read_whitelist; |
80 read_whitelist.push_back("/proc/cpuinfo"); | 49 read_whitelist.push_back("/proc/cpuinfo"); |
81 | 50 |
82 scoped_ptr<BrokerProcess> open_broker( | 51 scoped_ptr<BrokerProcess> open_broker( |
83 new BrokerProcess(EPERM, read_whitelist, std::vector<std::string>())); | 52 new BrokerProcess(EPERM, read_whitelist, std::vector<std::string>())); |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 SANDBOX_ASSERT( | 463 SANDBOX_ASSERT( |
495 UnixDomainSocket::SendMsg(ipc_fd, kBogus, sizeof(kBogus), fds)); | 464 UnixDomainSocket::SendMsg(ipc_fd, kBogus, sizeof(kBogus), fds)); |
496 } | 465 } |
497 | 466 |
498 const int fd = open_broker.Open(kCpuInfo, O_RDONLY); | 467 const int fd = open_broker.Open(kCpuInfo, O_RDONLY); |
499 SANDBOX_ASSERT(fd >= 0); | 468 SANDBOX_ASSERT(fd >= 0); |
500 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(fd))); | 469 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(fd))); |
501 } | 470 } |
502 | 471 |
503 } // namespace sandbox | 472 } // namespace sandbox |
OLD | NEW |