Protect WebURLLoaderImpl::Context while receiving responses.
[chromium-blink-merge.git] / base / mac / mac_util_unittest.mm
blob44cc5ecaa890d9d0f47f015171949d853ec8b616
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/mac/mac_util.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/mac/foundation_util.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/memory/scoped_nsobject.h"
15 #include "base/sys_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
19 #include <errno.h>
20 #include <sys/xattr.h>
22 namespace base {
23 namespace mac {
25 namespace {
27 typedef PlatformTest MacUtilTest;
29 TEST_F(MacUtilTest, TestFSRef) {
30   FSRef ref;
31   std::string path("/System/Library");
33   ASSERT_TRUE(FSRefFromPath(path, &ref));
34   EXPECT_EQ(path, PathFromFSRef(ref));
37 TEST_F(MacUtilTest, GetUserDirectoryTest) {
38   // Try a few keys, make sure they come back with non-empty paths.
39   FilePath caches_dir;
40   EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir));
41   EXPECT_FALSE(caches_dir.empty());
43   FilePath application_support_dir;
44   EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory,
45                                &application_support_dir));
46   EXPECT_FALSE(application_support_dir.empty());
48   FilePath library_dir;
49   EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir));
50   EXPECT_FALSE(library_dir.empty());
53 TEST_F(MacUtilTest, TestLibraryPath) {
54   FilePath library_dir = GetUserLibraryPath();
55   // Make sure the string isn't empty.
56   EXPECT_FALSE(library_dir.value().empty());
59 TEST_F(MacUtilTest, TestGetAppBundlePath) {
60   FilePath out;
62   // Make sure it doesn't crash.
63   out = GetAppBundlePath(FilePath());
64   EXPECT_TRUE(out.empty());
66   // Some more invalid inputs.
67   const char* invalid_inputs[] = {
68     "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
69     "foo/bar./bazquux", "foo/.app", "//foo",
70   };
71   for (size_t i = 0; i < arraysize(invalid_inputs); i++) {
72     out = GetAppBundlePath(FilePath(invalid_inputs[i]));
73     EXPECT_TRUE(out.empty()) << "loop: " << i;
74   }
76   // Some valid inputs; this and |expected_outputs| should be in sync.
77   struct {
78     const char *in;
79     const char *expected_out;
80   } valid_inputs[] = {
81     { "FooBar.app/", "FooBar.app" },
82     { "/FooBar.app", "/FooBar.app" },
83     { "/FooBar.app/", "/FooBar.app" },
84     { "//FooBar.app", "//FooBar.app" },
85     { "/Foo/Bar.app", "/Foo/Bar.app" },
86     { "/Foo/Bar.app/", "/Foo/Bar.app" },
87     { "/F/B.app", "/F/B.app" },
88     { "/F/B.app/", "/F/B.app" },
89     { "/Foo/Bar.app/baz", "/Foo/Bar.app" },
90     { "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
91     { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
92     { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
93         "/Applications/Google Foo.app" },
94   };
95   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) {
96     out = GetAppBundlePath(FilePath(valid_inputs[i].in));
97     EXPECT_FALSE(out.empty()) << "loop: " << i;
98     EXPECT_STREQ(valid_inputs[i].expected_out,
99         out.value().c_str()) << "loop: " << i;
100   }
103 TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
104   // The file must already exist in order to set its exclusion property.
105   ScopedTempDir temp_dir_;
106   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
107   FilePath dummy_file_path = temp_dir_.path().Append("DummyFile");
108   const char dummy_data[] = "All your base are belong to us!";
109   // Dump something real into the file.
110   ASSERT_EQ(static_cast<int>(arraysize(dummy_data)),
111       file_util::WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
112   NSString* fileURLString =
113       [NSString stringWithUTF8String:dummy_file_path.value().c_str()];
114   NSURL* fileURL = [NSURL URLWithString:fileURLString];
115   // Initial state should be non-excluded.
116   EXPECT_FALSE(CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), NULL));
117   // Exclude the file.
118   EXPECT_TRUE(SetFileBackupExclusion(dummy_file_path));
119   // SetFileBackupExclusion never excludes by path.
120   Boolean excluded_by_path = FALSE;
121   Boolean excluded =
122       CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), &excluded_by_path);
123   EXPECT_TRUE(excluded);
124   EXPECT_FALSE(excluded_by_path);
127 TEST_F(MacUtilTest, CopyNSImageToCGImage) {
128   scoped_nsobject<NSImage> nsImage(
129       [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]);
130   [nsImage lockFocus];
131   [[NSColor redColor] set];
132   NSRect rect = NSZeroRect;
133   rect.size = [nsImage size];
134   NSRectFill(rect);
135   [nsImage unlockFocus];
137   ScopedCFTypeRef<CGImageRef> cgImage(CopyNSImageToCGImage(nsImage.get()));
138   EXPECT_TRUE(cgImage.get());
141 TEST_F(MacUtilTest, NSObjectRetainRelease) {
142   scoped_nsobject<NSArray> array([[NSArray alloc] initWithObjects:@"foo", nil]);
143   EXPECT_EQ(1U, [array retainCount]);
145   NSObjectRetain(array);
146   EXPECT_EQ(2U, [array retainCount]);
148   NSObjectRelease(array);
149   EXPECT_EQ(1U, [array retainCount]);
152 TEST_F(MacUtilTest, IsOSEllipsis) {
153   int32 major, minor, bugfix;
154   base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
156   if (major == 10) {
157     if (minor == 6) {
158       EXPECT_TRUE(IsOSSnowLeopard());
159       EXPECT_FALSE(IsOSLion());
160       EXPECT_TRUE(IsOSLionOrEarlier());
161       EXPECT_FALSE(IsOSLionOrLater());
162       EXPECT_FALSE(IsOSMountainLion());
163       EXPECT_FALSE(IsOSMountainLionOrLater());
164       EXPECT_FALSE(IsOSLaterThanMountainLion_DontCallThis());
165     } else if (minor == 7) {
166       EXPECT_FALSE(IsOSSnowLeopard());
167       EXPECT_TRUE(IsOSLion());
168       EXPECT_TRUE(IsOSLionOrEarlier());
169       EXPECT_TRUE(IsOSLionOrLater());
170       EXPECT_FALSE(IsOSMountainLion());
171       EXPECT_FALSE(IsOSMountainLionOrLater());
172       EXPECT_FALSE(IsOSLaterThanMountainLion_DontCallThis());
173     } else if (minor == 8) {
174       EXPECT_FALSE(IsOSSnowLeopard());
175       EXPECT_FALSE(IsOSLion());
176       EXPECT_FALSE(IsOSLionOrEarlier());
177       EXPECT_TRUE(IsOSLionOrLater());
178       EXPECT_TRUE(IsOSMountainLion());
179       EXPECT_TRUE(IsOSMountainLionOrLater());
180       EXPECT_FALSE(IsOSLaterThanMountainLion_DontCallThis());
181     } else {
182       // Not five, six, seven, or eight. Ah, ah, ah.
183       EXPECT_TRUE(false);
184     }
185   } else {
186     // Not ten. What you gonna do?
187     EXPECT_FALSE(true);
188   }
191 TEST_F(MacUtilTest, ParseModelIdentifier) {
192   std::string model;
193   int32 major = 1, minor = 2;
195   EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
196   EXPECT_EQ(0U, model.length());
197   EXPECT_EQ(1, major);
198   EXPECT_EQ(2, minor);
199   EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
201   EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
202   EXPECT_EQ(model, "MacPro");
203   EXPECT_EQ(4, major);
204   EXPECT_EQ(1, minor);
206   EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
207   EXPECT_EQ(model, "MacBookPro");
208   EXPECT_EQ(6, major);
209   EXPECT_EQ(2, minor);
212 TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
213   ScopedTempDir temp_dir_;
214   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
215   FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
216   ASSERT_TRUE(file_util::CreateDirectory(dummy_folder_path));
217   const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
218   const char* file_path_str = dummy_folder_path.value().c_str();
219   EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
220       quarantine_str, strlen(quarantine_str), 0, 0));
221   EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
222       getxattr(file_path_str, "com.apple.quarantine",
223           NULL, 0, 0, 0));
224   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
225   EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
226   EXPECT_EQ(ENOATTR, errno);
229 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
230   ScopedTempDir temp_dir_;
231   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
232   FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
233   const char* file_path_str = dummy_folder_path.value().c_str();
234   ASSERT_TRUE(file_util::CreateDirectory(dummy_folder_path));
235   EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
236   // No quarantine attribute to begin with, but RemoveQuarantineAttribute still
237   // succeeds because in the end the folder still doesn't have the quarantine
238   // attribute set.
239   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
240   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
241   EXPECT_EQ(ENOATTR, errno);
244 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
245   ScopedTempDir temp_dir_;
246   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
247   FilePath non_existent_path = temp_dir_.path().Append("DummyPath");
248   ASSERT_FALSE(file_util::PathExists(non_existent_path));
249   EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));
252 }  // namespace
254 }  // namespace mac
255 }  // namespace base