Invoke LeakSanitizer on teardown of RenderFrameImplTest
[chromium-blink-merge.git] / base / mac / mac_util_unittest.mm
blob3982ab00dfcb602dde5fcbaea32b3a177cf4c99d
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/files/file_path.h"
10 #include "base/files/file_util.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/mac/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* const 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(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 // http://crbug.com/425745
104 TEST_F(MacUtilTest, DISABLED_TestExcludeFileFromBackups) {
105   // The file must already exist in order to set its exclusion property.
106   ScopedTempDir temp_dir_;
107   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
108   FilePath dummy_file_path = temp_dir_.path().Append("DummyFile");
109   const char dummy_data[] = "All your base are belong to us!";
110   // Dump something real into the file.
111   ASSERT_EQ(static_cast<int>(arraysize(dummy_data)),
112             WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
113   NSString* fileURLString =
114       [NSString stringWithUTF8String:dummy_file_path.value().c_str()];
115   NSURL* fileURL = [NSURL URLWithString:fileURLString];
116   // Initial state should be non-excluded.
117   EXPECT_FALSE(CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), NULL));
118   // Exclude the file.
119   EXPECT_TRUE(SetFileBackupExclusion(dummy_file_path));
120   // SetFileBackupExclusion never excludes by path.
121   Boolean excluded_by_path = FALSE;
122   Boolean excluded =
123       CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), &excluded_by_path);
124   EXPECT_TRUE(excluded);
125   EXPECT_FALSE(excluded_by_path);
128 TEST_F(MacUtilTest, NSObjectRetainRelease) {
129   base::scoped_nsobject<NSArray> array(
130       [[NSArray alloc] initWithObjects:@"foo", nil]);
131   EXPECT_EQ(1U, [array retainCount]);
133   NSObjectRetain(array);
134   EXPECT_EQ(2U, [array retainCount]);
136   NSObjectRelease(array);
137   EXPECT_EQ(1U, [array retainCount]);
140 TEST_F(MacUtilTest, IsOSEllipsis) {
141   int32 major, minor, bugfix;
142   base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
144   if (major == 10) {
145     if (minor == 6) {
146       EXPECT_TRUE(IsOSSnowLeopard());
147       EXPECT_FALSE(IsOSLion());
148       EXPECT_TRUE(IsOSLionOrEarlier());
149       EXPECT_FALSE(IsOSLionOrLater());
150       EXPECT_FALSE(IsOSMountainLion());
151       EXPECT_TRUE(IsOSMountainLionOrEarlier());
152       EXPECT_FALSE(IsOSMountainLionOrLater());
153       EXPECT_FALSE(IsOSMavericks());
154       EXPECT_TRUE(IsOSMavericksOrEarlier());
155       EXPECT_FALSE(IsOSMavericksOrLater());
156       EXPECT_FALSE(IsOSYosemite());
157       EXPECT_FALSE(IsOSYosemiteOrLater());
158       EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
159     } else if (minor == 7) {
160       EXPECT_FALSE(IsOSSnowLeopard());
161       EXPECT_TRUE(IsOSLion());
162       EXPECT_TRUE(IsOSLionOrEarlier());
163       EXPECT_TRUE(IsOSLionOrLater());
164       EXPECT_FALSE(IsOSMountainLion());
165       EXPECT_TRUE(IsOSMountainLionOrEarlier());
166       EXPECT_FALSE(IsOSMountainLionOrLater());
167       EXPECT_FALSE(IsOSMavericks());
168       EXPECT_TRUE(IsOSMavericksOrEarlier());
169       EXPECT_FALSE(IsOSMavericksOrLater());
170       EXPECT_FALSE(IsOSYosemite());
171       EXPECT_FALSE(IsOSYosemiteOrLater());
172       EXPECT_FALSE(IsOSLaterThanYosemite_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(IsOSMountainLionOrEarlier());
180       EXPECT_TRUE(IsOSMountainLionOrLater());
181       EXPECT_FALSE(IsOSMavericks());
182       EXPECT_TRUE(IsOSMavericksOrEarlier());
183       EXPECT_FALSE(IsOSMavericksOrLater());
184       EXPECT_FALSE(IsOSYosemite());
185       EXPECT_FALSE(IsOSYosemiteOrLater());
186       EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
187     } else if (minor == 9) {
188       EXPECT_FALSE(IsOSSnowLeopard());
189       EXPECT_FALSE(IsOSLion());
190       EXPECT_FALSE(IsOSLionOrEarlier());
191       EXPECT_TRUE(IsOSLionOrLater());
192       EXPECT_FALSE(IsOSMountainLion());
193       EXPECT_FALSE(IsOSMountainLionOrEarlier());
194       EXPECT_TRUE(IsOSMountainLionOrLater());
195       EXPECT_TRUE(IsOSMavericks());
196       EXPECT_TRUE(IsOSMavericksOrEarlier());
197       EXPECT_TRUE(IsOSMavericksOrLater());
198       EXPECT_FALSE(IsOSYosemite());
199       EXPECT_FALSE(IsOSYosemiteOrLater());
200       EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
201     } else if (minor == 10) {
202       EXPECT_FALSE(IsOSSnowLeopard());
203       EXPECT_FALSE(IsOSLion());
204       EXPECT_FALSE(IsOSLionOrEarlier());
205       EXPECT_TRUE(IsOSLionOrLater());
206       EXPECT_FALSE(IsOSMountainLion());
207       EXPECT_FALSE(IsOSMountainLionOrEarlier());
208       EXPECT_TRUE(IsOSMountainLionOrLater());
209       EXPECT_FALSE(IsOSMavericks());
210       EXPECT_FALSE(IsOSMavericksOrEarlier());
211       EXPECT_TRUE(IsOSMavericksOrLater());
212       EXPECT_TRUE(IsOSYosemite());
213       EXPECT_TRUE(IsOSYosemiteOrLater());
214       EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
215     } else {
216       // Not six, seven, eight, nine, or ten. Ah, ah, ah.
217       EXPECT_TRUE(false);
218     }
219   } else {
220     // Not ten. What you gonna do?
221     EXPECT_FALSE(true);
222   }
225 TEST_F(MacUtilTest, ParseModelIdentifier) {
226   std::string model;
227   int32 major = 1, minor = 2;
229   EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
230   EXPECT_EQ(0U, model.length());
231   EXPECT_EQ(1, major);
232   EXPECT_EQ(2, minor);
233   EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
235   EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
236   EXPECT_EQ(model, "MacPro");
237   EXPECT_EQ(4, major);
238   EXPECT_EQ(1, minor);
240   EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
241   EXPECT_EQ(model, "MacBookPro");
242   EXPECT_EQ(6, major);
243   EXPECT_EQ(2, minor);
246 TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
247   ScopedTempDir temp_dir_;
248   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
249   FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
250   ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
251   const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
252   const char* file_path_str = dummy_folder_path.value().c_str();
253   EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
254       quarantine_str, strlen(quarantine_str), 0, 0));
255   EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
256       getxattr(file_path_str, "com.apple.quarantine",
257           NULL, 0, 0, 0));
258   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
259   EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
260   EXPECT_EQ(ENOATTR, errno);
263 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
264   ScopedTempDir temp_dir_;
265   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
266   FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
267   const char* file_path_str = dummy_folder_path.value().c_str();
268   ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
269   EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
270   // No quarantine attribute to begin with, but RemoveQuarantineAttribute still
271   // succeeds because in the end the folder still doesn't have the quarantine
272   // attribute set.
273   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
274   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
275   EXPECT_EQ(ENOATTR, errno);
278 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
279   ScopedTempDir temp_dir_;
280   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
281   FilePath non_existent_path = temp_dir_.path().Append("DummyPath");
282   ASSERT_FALSE(PathExists(non_existent_path));
283   EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));
286 }  // namespace
288 }  // namespace mac
289 }  // namespace base