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 "crypto/nss_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 //--------------------- Clipboard Sandboxing ----------------------
19 // Test case for checking sandboxing of clipboard access.
20 class MacSandboxedClipboardTestCase : public MacSandboxTestCase {
22 MacSandboxedClipboardTestCase();
23 virtual ~MacSandboxedClipboardTestCase();
25 virtual bool SandboxedTest() OVERRIDE;
27 virtual void SetTestData(const char* test_data) OVERRIDE;
29 NSString* clipboard_name_;
32 REGISTER_SANDBOX_TEST_CASE(MacSandboxedClipboardTestCase);
34 MacSandboxedClipboardTestCase::MacSandboxedClipboardTestCase() :
35 clipboard_name_(nil) {}
37 MacSandboxedClipboardTestCase::~MacSandboxedClipboardTestCase() {
38 [clipboard_name_ release];
41 bool MacSandboxedClipboardTestCase::SandboxedTest() {
42 // Shouldn't be able to open the pasteboard in the sandbox.
44 if ([clipboard_name_ length] == 0) {
45 LOG(ERROR) << "Clipboard name is empty";
49 NSPasteboard* pb = [NSPasteboard pasteboardWithName:clipboard_name_];
51 LOG(ERROR) << "Was able to access named clipboard";
55 pb = [NSPasteboard generalPasteboard];
57 LOG(ERROR) << "Was able to access system clipboard";
64 void MacSandboxedClipboardTestCase::SetTestData(const char* test_data) {
65 clipboard_name_ = [base::SysUTF8ToNSString(test_data) retain];
68 TEST_F(MacSandboxTest, ClipboardAccess) {
69 NSPasteboard* pb = [NSPasteboard pasteboardWithUniqueName];
70 EXPECT_EQ([[pb types] count], 0U);
72 std::string pasteboard_name = base::SysNSStringToUTF8([pb name]);
73 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedClipboardTestCase",
74 pasteboard_name.c_str()));
76 // After executing the test, the clipboard should still be empty.
77 EXPECT_EQ([[pb types] count], 0U);
80 //--------------------- File Access Sandboxing ----------------------
81 // Test case for checking sandboxing of filesystem apis.
82 class MacSandboxedFileAccessTestCase : public MacSandboxTestCase {
84 virtual bool SandboxedTest() OVERRIDE;
87 REGISTER_SANDBOX_TEST_CASE(MacSandboxedFileAccessTestCase);
89 bool MacSandboxedFileAccessTestCase::SandboxedTest() {
90 base::ScopedFD fdes(HANDLE_EINTR(open("/etc/passwd", O_RDONLY)));
91 return !fdes.is_valid();
94 TEST_F(MacSandboxTest, FileAccess) {
95 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedFileAccessTestCase", NULL));
98 //--------------------- /dev/urandom Sandboxing ----------------------
99 // /dev/urandom is available to any sandboxed process.
100 class MacSandboxedUrandomTestCase : public MacSandboxTestCase {
102 virtual bool SandboxedTest() OVERRIDE;
105 REGISTER_SANDBOX_TEST_CASE(MacSandboxedUrandomTestCase);
107 bool MacSandboxedUrandomTestCase::SandboxedTest() {
108 base::ScopedFD fdes(HANDLE_EINTR(open("/dev/urandom", O_RDONLY)));
110 // Opening /dev/urandom succeeds under the sandbox.
111 if (!fdes.is_valid())
115 int rc = HANDLE_EINTR(read(fdes.get(), buf, sizeof(buf)));
116 return rc == sizeof(buf);
119 TEST_F(MacSandboxTest, UrandomAccess) {
120 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedUrandomTestCase", NULL));
123 //--------------------- NSS Sandboxing ----------------------
124 // Test case for checking sandboxing of NSS initialization.
125 class MacSandboxedNSSTestCase : public MacSandboxTestCase {
127 virtual bool SandboxedTest() OVERRIDE;
130 REGISTER_SANDBOX_TEST_CASE(MacSandboxedNSSTestCase);
132 bool MacSandboxedNSSTestCase::SandboxedTest() {
133 // If NSS cannot read from /dev/urandom, NSS initialization will call abort(),
134 // which will cause this test case to fail.
135 crypto::ForceNSSNoDBInit();
136 crypto::EnsureNSSInit();
140 TEST_F(MacSandboxTest, NSSAccess) {
141 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedNSSTestCase", NULL));
144 } // namespace content