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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_file.h" | 8 #include "base/files/scoped_file.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "content/common/sandbox_mac.h" | 11 #include "content/common/sandbox_mac.h" |
12 #include "content/common/sandbox_mac_unittest_helper.h" | 12 #include "content/common/sandbox_mac_unittest_helper.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 #if defined(USE_OPENSSL) | 15 #if defined(USE_OPENSSL) |
16 #include <openssl/rand.h> | 16 #include <openssl/rand.h> |
17 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
18 #else | 18 #else |
19 #include "crypto/nss_util.h" | 19 #include "crypto/nss_util.h" |
20 #endif | 20 #endif |
21 | 21 |
22 namespace content { | 22 namespace content { |
23 | 23 |
24 //--------------------- Clipboard Sandboxing ---------------------- | 24 //--------------------- Clipboard Sandboxing ---------------------- |
25 // Test case for checking sandboxing of clipboard access. | 25 // Test case for checking sandboxing of clipboard access. |
26 class MacSandboxedClipboardTestCase : public MacSandboxTestCase { | 26 class MacSandboxedClipboardTestCase : public MacSandboxTestCase { |
27 public: | 27 public: |
28 MacSandboxedClipboardTestCase(); | 28 MacSandboxedClipboardTestCase(); |
29 virtual ~MacSandboxedClipboardTestCase(); | 29 ~MacSandboxedClipboardTestCase() override; |
30 | 30 |
31 virtual bool SandboxedTest() override; | 31 bool SandboxedTest() override; |
32 | 32 |
33 virtual void SetTestData(const char* test_data) override; | 33 void SetTestData(const char* test_data) override; |
| 34 |
34 private: | 35 private: |
35 NSString* clipboard_name_; | 36 NSString* clipboard_name_; |
36 }; | 37 }; |
37 | 38 |
38 REGISTER_SANDBOX_TEST_CASE(MacSandboxedClipboardTestCase); | 39 REGISTER_SANDBOX_TEST_CASE(MacSandboxedClipboardTestCase); |
39 | 40 |
40 MacSandboxedClipboardTestCase::MacSandboxedClipboardTestCase() : | 41 MacSandboxedClipboardTestCase::MacSandboxedClipboardTestCase() : |
41 clipboard_name_(nil) {} | 42 clipboard_name_(nil) {} |
42 | 43 |
43 MacSandboxedClipboardTestCase::~MacSandboxedClipboardTestCase() { | 44 MacSandboxedClipboardTestCase::~MacSandboxedClipboardTestCase() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 pasteboard_name.c_str())); | 81 pasteboard_name.c_str())); |
81 | 82 |
82 // After executing the test, the clipboard should still be empty. | 83 // After executing the test, the clipboard should still be empty. |
83 EXPECT_EQ([[pb types] count], 0U); | 84 EXPECT_EQ([[pb types] count], 0U); |
84 } | 85 } |
85 | 86 |
86 //--------------------- File Access Sandboxing ---------------------- | 87 //--------------------- File Access Sandboxing ---------------------- |
87 // Test case for checking sandboxing of filesystem apis. | 88 // Test case for checking sandboxing of filesystem apis. |
88 class MacSandboxedFileAccessTestCase : public MacSandboxTestCase { | 89 class MacSandboxedFileAccessTestCase : public MacSandboxTestCase { |
89 public: | 90 public: |
90 virtual bool SandboxedTest() override; | 91 bool SandboxedTest() override; |
91 }; | 92 }; |
92 | 93 |
93 REGISTER_SANDBOX_TEST_CASE(MacSandboxedFileAccessTestCase); | 94 REGISTER_SANDBOX_TEST_CASE(MacSandboxedFileAccessTestCase); |
94 | 95 |
95 bool MacSandboxedFileAccessTestCase::SandboxedTest() { | 96 bool MacSandboxedFileAccessTestCase::SandboxedTest() { |
96 base::ScopedFD fdes(HANDLE_EINTR(open("/etc/passwd", O_RDONLY))); | 97 base::ScopedFD fdes(HANDLE_EINTR(open("/etc/passwd", O_RDONLY))); |
97 return !fdes.is_valid(); | 98 return !fdes.is_valid(); |
98 } | 99 } |
99 | 100 |
100 TEST_F(MacSandboxTest, FileAccess) { | 101 TEST_F(MacSandboxTest, FileAccess) { |
101 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedFileAccessTestCase", NULL)); | 102 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedFileAccessTestCase", NULL)); |
102 } | 103 } |
103 | 104 |
104 //--------------------- /dev/urandom Sandboxing ---------------------- | 105 //--------------------- /dev/urandom Sandboxing ---------------------- |
105 // /dev/urandom is available to any sandboxed process. | 106 // /dev/urandom is available to any sandboxed process. |
106 class MacSandboxedUrandomTestCase : public MacSandboxTestCase { | 107 class MacSandboxedUrandomTestCase : public MacSandboxTestCase { |
107 public: | 108 public: |
108 virtual bool SandboxedTest() override; | 109 bool SandboxedTest() override; |
109 }; | 110 }; |
110 | 111 |
111 REGISTER_SANDBOX_TEST_CASE(MacSandboxedUrandomTestCase); | 112 REGISTER_SANDBOX_TEST_CASE(MacSandboxedUrandomTestCase); |
112 | 113 |
113 bool MacSandboxedUrandomTestCase::SandboxedTest() { | 114 bool MacSandboxedUrandomTestCase::SandboxedTest() { |
114 base::ScopedFD fdes(HANDLE_EINTR(open("/dev/urandom", O_RDONLY))); | 115 base::ScopedFD fdes(HANDLE_EINTR(open("/dev/urandom", O_RDONLY))); |
115 | 116 |
116 // Opening /dev/urandom succeeds under the sandbox. | 117 // Opening /dev/urandom succeeds under the sandbox. |
117 if (!fdes.is_valid()) | 118 if (!fdes.is_valid()) |
118 return false; | 119 return false; |
119 | 120 |
120 char buf[16]; | 121 char buf[16]; |
121 int rc = HANDLE_EINTR(read(fdes.get(), buf, sizeof(buf))); | 122 int rc = HANDLE_EINTR(read(fdes.get(), buf, sizeof(buf))); |
122 return rc == sizeof(buf); | 123 return rc == sizeof(buf); |
123 } | 124 } |
124 | 125 |
125 TEST_F(MacSandboxTest, UrandomAccess) { | 126 TEST_F(MacSandboxTest, UrandomAccess) { |
126 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedUrandomTestCase", NULL)); | 127 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedUrandomTestCase", NULL)); |
127 } | 128 } |
128 | 129 |
129 #if defined(USE_OPENSSL) | 130 #if defined(USE_OPENSSL) |
130 | 131 |
131 //--------------------- OpenSSL Sandboxing ---------------------- | 132 //--------------------- OpenSSL Sandboxing ---------------------- |
132 // Test case for checking sandboxing of OpenSSL initialization. | 133 // Test case for checking sandboxing of OpenSSL initialization. |
133 class MacSandboxedOpenSSLTestCase : public MacSandboxTestCase { | 134 class MacSandboxedOpenSSLTestCase : public MacSandboxTestCase { |
134 public: | 135 public: |
135 virtual bool SandboxedTest() override; | 136 bool SandboxedTest() override; |
136 }; | 137 }; |
137 | 138 |
138 REGISTER_SANDBOX_TEST_CASE(MacSandboxedOpenSSLTestCase); | 139 REGISTER_SANDBOX_TEST_CASE(MacSandboxedOpenSSLTestCase); |
139 | 140 |
140 bool MacSandboxedOpenSSLTestCase::SandboxedTest() { | 141 bool MacSandboxedOpenSSLTestCase::SandboxedTest() { |
141 crypto::EnsureOpenSSLInit(); | 142 crypto::EnsureOpenSSLInit(); |
142 | 143 |
143 // Ensure that RAND_bytes is functional within the sandbox. | 144 // Ensure that RAND_bytes is functional within the sandbox. |
144 uint8_t byte; | 145 uint8_t byte; |
145 return RAND_bytes(&byte, 1) == 1; | 146 return RAND_bytes(&byte, 1) == 1; |
(...skipping 22 matching lines...) Expand all Loading... |
168 return true; | 169 return true; |
169 } | 170 } |
170 | 171 |
171 TEST_F(MacSandboxTest, NSSAccess) { | 172 TEST_F(MacSandboxTest, NSSAccess) { |
172 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedNSSTestCase", NULL)); | 173 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedNSSTestCase", NULL)); |
173 } | 174 } |
174 | 175 |
175 #endif // defined(USE_OPENSSL) | 176 #endif // defined(USE_OPENSSL) |
176 | 177 |
177 } // namespace content | 178 } // namespace content |
OLD | NEW |