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/logging.h"
9 #include "base/sys_string_conversions.h"
10 #include "content/common/sandbox_mac.h"
11 #include "content/common/sandbox_mac_unittest_helper.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 //--------------------- Clipboard Sandboxing ----------------------
17 // Test case for checking sandboxing of clipboard access.
18 class MacSandboxedClipboardTestCase : public MacSandboxTestCase {
20 MacSandboxedClipboardTestCase();
21 virtual ~MacSandboxedClipboardTestCase();
23 virtual bool SandboxedTest();
25 virtual void SetTestData(const char* test_data);
27 NSString* clipboard_name_;
30 REGISTER_SANDBOX_TEST_CASE(MacSandboxedClipboardTestCase);
32 MacSandboxedClipboardTestCase::MacSandboxedClipboardTestCase() :
33 clipboard_name_(nil) {}
35 MacSandboxedClipboardTestCase::~MacSandboxedClipboardTestCase() {
36 [clipboard_name_ release];
39 bool MacSandboxedClipboardTestCase::SandboxedTest() {
40 // Shouldn't be able to open the pasteboard in the sandbox.
42 if ([clipboard_name_ length] == 0) {
43 LOG(ERROR) << "Clipboard name is empty";
47 NSPasteboard* pb = [NSPasteboard pasteboardWithName:clipboard_name_];
49 LOG(ERROR) << "Was able to access named clipboard";
53 pb = [NSPasteboard generalPasteboard];
55 LOG(ERROR) << "Was able to access system clipboard";
62 void MacSandboxedClipboardTestCase::SetTestData(const char* test_data) {
63 clipboard_name_ = [base::SysUTF8ToNSString(test_data) retain];
66 TEST_F(MacSandboxTest, ClipboardAccess) {
67 NSPasteboard* pb = [NSPasteboard pasteboardWithUniqueName];
68 EXPECT_EQ([[pb types] count], 0U);
70 std::string pasteboard_name = base::SysNSStringToUTF8([pb name]);
71 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedClipboardTestCase",
72 pasteboard_name.c_str()));
74 // After executing the test, the clipboard should still be empty.
75 EXPECT_EQ([[pb types] count], 0U);
78 //--------------------- File Access Sandboxing ----------------------
79 // Test case for checking sandboxing of filesystem apis.
80 class MacSandboxedFileAccessTestCase : public MacSandboxTestCase {
82 virtual bool SandboxedTest();
85 REGISTER_SANDBOX_TEST_CASE(MacSandboxedFileAccessTestCase);
87 bool MacSandboxedFileAccessTestCase::SandboxedTest() {
88 int fdes = open("/etc/passwd", O_RDONLY);
89 file_util::ScopedFD file_closer(&fdes);
93 TEST_F(MacSandboxTest, FileAccess) {
94 EXPECT_TRUE(RunTestInAllSandboxTypes("MacSandboxedFileAccessTestCase", NULL));
97 //--------------------- /dev/urandom Sandboxing ----------------------
98 // /dev/urandom is available to ppapi sandbox only.
99 class MacSandboxedUrandomTestCase : public MacSandboxTestCase {
101 virtual bool SandboxedTest();
104 REGISTER_SANDBOX_TEST_CASE(MacSandboxedUrandomTestCase);
106 bool MacSandboxedUrandomTestCase::SandboxedTest() {
107 int fdes = open("/dev/urandom", O_RDONLY);
108 file_util::ScopedFD file_closer(&fdes);
110 // Open succeeds under ppapi sandbox, else it is not permitted.
111 if (test_data_ == "ppapi") {
116 int rc = read(fdes, buf, sizeof(buf));
117 return rc == sizeof(buf);
119 return fdes == -1 && errno == EPERM;
123 TEST_F(MacSandboxTest, UrandomAccess) {
124 // Similar to RunTestInAllSandboxTypes(), except changing
125 // |test_data| for the ppapi case. Passing "" in the non-ppapi case
126 // to overwrite the test data (NULL means not to change it).
127 for (SandboxType i = SANDBOX_TYPE_FIRST_TYPE;
128 i < SANDBOX_TYPE_AFTER_LAST_TYPE; ++i) {
129 if (i == SANDBOX_TYPE_PPAPI) {
130 EXPECT_TRUE(RunTestInSandbox(i, "MacSandboxedUrandomTestCase", "ppapi"));
132 EXPECT_TRUE(RunTestInSandbox(i, "MacSandboxedUrandomTestCase", ""))
133 << "for sandbox type " << i;
138 } // namespace content