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/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 virtual ~MacSandboxedClipboardTestCase();
31 virtual bool SandboxedTest() OVERRIDE;
33 virtual void SetTestData(const char* test_data) OVERRIDE;
35 NSString* clipboard_name_;
38 REGISTER_SANDBOX_TEST_CASE(MacSandboxedClipboardTestCase);
40 MacSandboxedClipboardTestCase::MacSandboxedClipboardTestCase() :
41 clipboard_name_(nil) {}
43 MacSandboxedClipboardTestCase::~MacSandboxedClipboardTestCase() {
44 [clipboard_name_ release];
47 bool MacSandboxedClipboardTestCase::SandboxedTest() {
48 // Shouldn't be able to open the pasteboard in the sandbox.
50 if ([clipboard_name_ length] == 0) {
51 LOG(ERROR) << "Clipboard name is empty";
55 NSPasteboard* pb = [NSPasteboard pasteboardWithName:clipboard_name_];
57 LOG(ERROR) << "Was able to access named clipboard";
61 pb = [NSPasteboard generalPasteboard];
63 LOG(ERROR) << "Was able to access system clipboard";
70 void MacSandboxedClipboardTestCase::SetTestData(const char* test_data) {
71 clipboard_name_ = [base::SysUTF8ToNSString(test_data) retain];
74 TEST_F(MacSandboxTest, ClipboardAccess) {
75 NSPasteboard* pb = [NSPasteboard pasteboardWithUniqueName];
76 EXPECT_EQ([[pb types] count], 0U);
78 std::string pasteboard_name = base::SysNSStringToUTF8([pb name]);
79 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedClipboardTestCase",
80 pasteboard_name.c_str()));
82 // After executing the test, the clipboard should still be empty.
83 EXPECT_EQ([[pb types] count], 0U);
86 //--------------------- File Access Sandboxing ----------------------
87 // Test case for checking sandboxing of filesystem apis.
88 class MacSandboxedFileAccessTestCase : public MacSandboxTestCase {
90 virtual bool SandboxedTest() OVERRIDE;
93 REGISTER_SANDBOX_TEST_CASE(MacSandboxedFileAccessTestCase);
95 bool MacSandboxedFileAccessTestCase::SandboxedTest() {
96 base::ScopedFD fdes(HANDLE_EINTR(open("/etc/passwd", O_RDONLY)));
97 return !fdes.is_valid();
100 TEST_F(MacSandboxTest, FileAccess) {
101 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedFileAccessTestCase", NULL));
104 //--------------------- /dev/urandom Sandboxing ----------------------
105 // /dev/urandom is available to any sandboxed process.
106 class MacSandboxedUrandomTestCase : public MacSandboxTestCase {
108 virtual bool SandboxedTest() OVERRIDE;
111 REGISTER_SANDBOX_TEST_CASE(MacSandboxedUrandomTestCase);
113 bool MacSandboxedUrandomTestCase::SandboxedTest() {
114 base::ScopedFD fdes(HANDLE_EINTR(open("/dev/urandom", O_RDONLY)));
116 // Opening /dev/urandom succeeds under the sandbox.
117 if (!fdes.is_valid())
121 int rc = HANDLE_EINTR(read(fdes.get(), buf, sizeof(buf)));
122 return rc == sizeof(buf);
125 TEST_F(MacSandboxTest, UrandomAccess) {
126 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedUrandomTestCase", NULL));
129 #if defined(USE_OPENSSL)
131 //--------------------- OpenSSL Sandboxing ----------------------
132 // Test case for checking sandboxing of OpenSSL initialization.
133 class MacSandboxedOpenSSLTestCase : public MacSandboxTestCase {
135 virtual bool SandboxedTest() OVERRIDE;
138 REGISTER_SANDBOX_TEST_CASE(MacSandboxedOpenSSLTestCase);
140 bool MacSandboxedOpenSSLTestCase::SandboxedTest() {
141 crypto::EnsureOpenSSLInit();
143 // Ensure that RAND_bytes is functional within the sandbox.
145 return RAND_bytes(&byte, 1) == 1;
148 TEST_F(MacSandboxTest, OpenSSLAccess) {
149 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedOpenSSLTestCase", NULL));
152 #else // !defined(USE_OPENSSL)
154 //--------------------- NSS Sandboxing ----------------------
155 // Test case for checking sandboxing of NSS initialization.
156 class MacSandboxedNSSTestCase : public MacSandboxTestCase {
158 virtual bool SandboxedTest() OVERRIDE;
161 REGISTER_SANDBOX_TEST_CASE(MacSandboxedNSSTestCase);
163 bool MacSandboxedNSSTestCase::SandboxedTest() {
164 // If NSS cannot read from /dev/urandom, NSS initialization will call abort(),
165 // which will cause this test case to fail.
166 crypto::ForceNSSNoDBInit();
167 crypto::EnsureNSSInit();
171 TEST_F(MacSandboxTest, NSSAccess) {
172 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedNSSTestCase", NULL));
175 #endif // defined(USE_OPENSSL)
177 } // namespace content