[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / common / sandbox_mac_system_access_unittest.mm
blobc216786a27644225b7a71b164179f4e28184df41
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"
14 namespace content {
16 //--------------------- Clipboard Sandboxing ----------------------
17 // Test case for checking sandboxing of clipboard access.
18 class MacSandboxedClipboardTestCase : public MacSandboxTestCase {
19  public:
20   MacSandboxedClipboardTestCase();
21   virtual ~MacSandboxedClipboardTestCase();
23   virtual bool SandboxedTest();
25   virtual void SetTestData(const char* test_data);
26  private:
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";
44     return false;
45   }
47   NSPasteboard* pb = [NSPasteboard pasteboardWithName:clipboard_name_];
48   if (pb != nil) {
49     LOG(ERROR) << "Was able to access named clipboard";
50     return false;
51   }
53   pb = [NSPasteboard generalPasteboard];
54   if (pb != nil) {
55     LOG(ERROR) << "Was able to access system clipboard";
56     return false;
57   }
59   return true;
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 {
81  public:
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);
90   return fdes == -1;
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 {
100  public:
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") {
112     if (fdes == -1)
113       return false;
115     char buf[16];
116     int rc = read(fdes, buf, sizeof(buf));
117     return rc == sizeof(buf);
118   } else {
119     return fdes == -1 && errno == EPERM;
120   }
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"));
131     } else {
132       EXPECT_TRUE(RunTestInSandbox(i, "MacSandboxedUrandomTestCase", ""))
133           << "for sandbox type " << i;
134     }
135   }
138 }  // namespace content