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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_file.h"
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "content/common/sandbox_mac.h"
12 #include "content/common/sandbox_mac_unittest_helper.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 #if defined(USE_OPENSSL)
16 #include <openssl/rand.h>
17 #include "crypto/openssl_util.h"
19 #include "crypto/nss_util.h"
24 //--------------------- Clipboard Sandboxing ----------------------
25 // Test case for checking sandboxing of clipboard access.
26 class MacSandboxedClipboardTestCase : public MacSandboxTestCase {
28 MacSandboxedClipboardTestCase();
29 ~MacSandboxedClipboardTestCase() override;
31 bool SandboxedTest() override;
33 void SetTestData(const char* test_data) override;
36 NSString* clipboard_name_;
39 REGISTER_SANDBOX_TEST_CASE(MacSandboxedClipboardTestCase);
41 MacSandboxedClipboardTestCase::MacSandboxedClipboardTestCase() :
42 clipboard_name_(nil) {}
44 MacSandboxedClipboardTestCase::~MacSandboxedClipboardTestCase() {
45 [clipboard_name_ release];
48 bool MacSandboxedClipboardTestCase::SandboxedTest() {
49 // Shouldn't be able to open the pasteboard in the sandbox.
51 if ([clipboard_name_ length] == 0) {
52 LOG(ERROR) << "Clipboard name is empty";
56 NSPasteboard* pb = [NSPasteboard pasteboardWithName:clipboard_name_];
58 LOG(ERROR) << "Was able to access named clipboard";
62 pb = [NSPasteboard generalPasteboard];
64 LOG(ERROR) << "Was able to access system clipboard";
71 void MacSandboxedClipboardTestCase::SetTestData(const char* test_data) {
72 clipboard_name_ = [base::SysUTF8ToNSString(test_data) retain];
75 TEST_F(MacSandboxTest, ClipboardAccess) {
76 NSPasteboard* pb = [NSPasteboard pasteboardWithUniqueName];
77 EXPECT_EQ([[pb types] count], 0U);
79 std::string pasteboard_name = base::SysNSStringToUTF8([pb name]);
80 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedClipboardTestCase",
81 pasteboard_name.c_str()));
83 // After executing the test, the clipboard should still be empty.
84 EXPECT_EQ([[pb types] count], 0U);
87 //--------------------- File Access Sandboxing ----------------------
88 // Test case for checking sandboxing of filesystem apis.
89 class MacSandboxedFileAccessTestCase : public MacSandboxTestCase {
91 bool SandboxedTest() override;
94 REGISTER_SANDBOX_TEST_CASE(MacSandboxedFileAccessTestCase);
96 bool MacSandboxedFileAccessTestCase::SandboxedTest() {
97 base::ScopedFD fdes(HANDLE_EINTR(open("/etc/passwd", O_RDONLY)));
98 return !fdes.is_valid();
101 TEST_F(MacSandboxTest, FileAccess) {
102 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedFileAccessTestCase", NULL));
105 //--------------------- /dev/urandom Sandboxing ----------------------
106 // /dev/urandom is available to any sandboxed process.
107 class MacSandboxedUrandomTestCase : public MacSandboxTestCase {
109 bool SandboxedTest() override;
112 REGISTER_SANDBOX_TEST_CASE(MacSandboxedUrandomTestCase);
114 bool MacSandboxedUrandomTestCase::SandboxedTest() {
115 base::ScopedFD fdes(HANDLE_EINTR(open("/dev/urandom", O_RDONLY)));
117 // Opening /dev/urandom succeeds under the sandbox.
118 if (!fdes.is_valid())
122 int rc = HANDLE_EINTR(read(fdes.get(), buf, sizeof(buf)));
123 return rc == sizeof(buf);
126 TEST_F(MacSandboxTest, UrandomAccess) {
127 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedUrandomTestCase", NULL));
130 #if defined(USE_OPENSSL)
132 //--------------------- OpenSSL Sandboxing ----------------------
133 // Test case for checking sandboxing of OpenSSL initialization.
134 class MacSandboxedOpenSSLTestCase : public MacSandboxTestCase {
136 bool SandboxedTest() override;
139 REGISTER_SANDBOX_TEST_CASE(MacSandboxedOpenSSLTestCase);
141 bool MacSandboxedOpenSSLTestCase::SandboxedTest() {
142 crypto::EnsureOpenSSLInit();
144 // Ensure that RAND_bytes is functional within the sandbox.
146 return RAND_bytes(&byte, 1) == 1;
149 TEST_F(MacSandboxTest, OpenSSLAccess) {
150 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedOpenSSLTestCase", NULL));
153 #else // !defined(USE_OPENSSL)
155 //--------------------- NSS Sandboxing ----------------------
156 // Test case for checking sandboxing of NSS initialization.
157 class MacSandboxedNSSTestCase : public MacSandboxTestCase {
159 bool SandboxedTest() override;
162 REGISTER_SANDBOX_TEST_CASE(MacSandboxedNSSTestCase);
164 bool MacSandboxedNSSTestCase::SandboxedTest() {
165 // If NSS cannot read from /dev/urandom, NSS initialization will call abort(),
166 // which will cause this test case to fail.
167 crypto::ForceNSSNoDBInit();
168 crypto::EnsureNSSInit();
172 TEST_F(MacSandboxTest, NSSAccess) {
173 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedNSSTestCase", NULL));
176 #endif // defined(USE_OPENSSL)
178 } // namespace content