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"
20 #include <sys/xattr.h>
27 typedef PlatformTest MacUtilTest;
29 TEST_F(MacUtilTest, TestFSRef) {
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.
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());
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) {
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",
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;
76 // Some valid inputs; this and |expected_outputs| should be in sync.
79 const char *expected_out;
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" },
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;
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));
119 EXPECT_TRUE(SetFileBackupExclusion(dummy_file_path));
120 // SetFileBackupExclusion never excludes by path.
121 Boolean excluded_by_path = FALSE;
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);
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_TRUE(IsOSYosemiteOrEarlier());
158 EXPECT_FALSE(IsOSYosemiteOrLater());
159 EXPECT_FALSE(IsOSElCapitan());
160 EXPECT_FALSE(IsOSElCapitanOrLater());
161 EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
162 } else if (minor == 7) {
163 EXPECT_FALSE(IsOSSnowLeopard());
164 EXPECT_TRUE(IsOSLion());
165 EXPECT_TRUE(IsOSLionOrEarlier());
166 EXPECT_TRUE(IsOSLionOrLater());
167 EXPECT_FALSE(IsOSMountainLion());
168 EXPECT_TRUE(IsOSMountainLionOrEarlier());
169 EXPECT_FALSE(IsOSMountainLionOrLater());
170 EXPECT_FALSE(IsOSMavericks());
171 EXPECT_TRUE(IsOSMavericksOrEarlier());
172 EXPECT_FALSE(IsOSMavericksOrLater());
173 EXPECT_FALSE(IsOSYosemite());
174 EXPECT_TRUE(IsOSYosemiteOrEarlier());
175 EXPECT_FALSE(IsOSYosemiteOrLater());
176 EXPECT_FALSE(IsOSElCapitan());
177 EXPECT_FALSE(IsOSElCapitanOrLater());
178 EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
179 } else if (minor == 8) {
180 EXPECT_FALSE(IsOSSnowLeopard());
181 EXPECT_FALSE(IsOSLion());
182 EXPECT_FALSE(IsOSLionOrEarlier());
183 EXPECT_TRUE(IsOSLionOrLater());
184 EXPECT_TRUE(IsOSMountainLion());
185 EXPECT_TRUE(IsOSMountainLionOrEarlier());
186 EXPECT_TRUE(IsOSMountainLionOrLater());
187 EXPECT_FALSE(IsOSMavericks());
188 EXPECT_TRUE(IsOSMavericksOrEarlier());
189 EXPECT_FALSE(IsOSMavericksOrLater());
190 EXPECT_FALSE(IsOSYosemite());
191 EXPECT_TRUE(IsOSYosemiteOrEarlier());
192 EXPECT_FALSE(IsOSYosemiteOrLater());
193 EXPECT_FALSE(IsOSElCapitan());
194 EXPECT_FALSE(IsOSElCapitanOrLater());
195 EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
196 } else if (minor == 9) {
197 EXPECT_FALSE(IsOSSnowLeopard());
198 EXPECT_FALSE(IsOSLion());
199 EXPECT_FALSE(IsOSLionOrEarlier());
200 EXPECT_TRUE(IsOSLionOrLater());
201 EXPECT_FALSE(IsOSMountainLion());
202 EXPECT_FALSE(IsOSMountainLionOrEarlier());
203 EXPECT_TRUE(IsOSMountainLionOrLater());
204 EXPECT_TRUE(IsOSMavericks());
205 EXPECT_TRUE(IsOSMavericksOrEarlier());
206 EXPECT_TRUE(IsOSMavericksOrLater());
207 EXPECT_FALSE(IsOSYosemite());
208 EXPECT_TRUE(IsOSYosemiteOrEarlier());
209 EXPECT_FALSE(IsOSYosemiteOrLater());
210 EXPECT_FALSE(IsOSElCapitan());
211 EXPECT_FALSE(IsOSElCapitanOrLater());
212 EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
213 } else if (minor == 10) {
214 EXPECT_FALSE(IsOSSnowLeopard());
215 EXPECT_FALSE(IsOSLion());
216 EXPECT_FALSE(IsOSLionOrEarlier());
217 EXPECT_TRUE(IsOSLionOrLater());
218 EXPECT_FALSE(IsOSMountainLion());
219 EXPECT_FALSE(IsOSMountainLionOrEarlier());
220 EXPECT_TRUE(IsOSMountainLionOrLater());
221 EXPECT_FALSE(IsOSMavericks());
222 EXPECT_FALSE(IsOSMavericksOrEarlier());
223 EXPECT_TRUE(IsOSMavericksOrLater());
224 EXPECT_TRUE(IsOSYosemite());
225 EXPECT_TRUE(IsOSYosemiteOrEarlier());
226 EXPECT_TRUE(IsOSYosemiteOrLater());
227 EXPECT_FALSE(IsOSElCapitan());
228 EXPECT_FALSE(IsOSElCapitanOrLater());
229 EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
230 } else if (minor == 11) {
231 EXPECT_FALSE(IsOSSnowLeopard());
232 EXPECT_FALSE(IsOSLion());
233 EXPECT_FALSE(IsOSLionOrEarlier());
234 EXPECT_TRUE(IsOSLionOrLater());
235 EXPECT_FALSE(IsOSMountainLion());
236 EXPECT_FALSE(IsOSMountainLionOrEarlier());
237 EXPECT_TRUE(IsOSMountainLionOrLater());
238 EXPECT_FALSE(IsOSMavericks());
239 EXPECT_FALSE(IsOSMavericksOrEarlier());
240 EXPECT_TRUE(IsOSMavericksOrLater());
241 EXPECT_FALSE(IsOSYosemite());
242 EXPECT_FALSE(IsOSYosemiteOrEarlier());
243 EXPECT_TRUE(IsOSYosemiteOrLater());
244 EXPECT_TRUE(IsOSElCapitan());
245 EXPECT_TRUE(IsOSElCapitanOrLater());
246 EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
248 // Not six, seven, eight, nine, ten, or eleven. Ah, ah, ah.
252 // Not ten. What you gonna do?
257 TEST_F(MacUtilTest, ParseModelIdentifier) {
259 int32 major = 1, minor = 2;
261 EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
262 EXPECT_EQ(0U, model.length());
265 EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
267 EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
268 EXPECT_EQ(model, "MacPro");
272 EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
273 EXPECT_EQ(model, "MacBookPro");
278 TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
279 ScopedTempDir temp_dir_;
280 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
281 FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
282 ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
283 const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
284 const char* file_path_str = dummy_folder_path.value().c_str();
285 EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
286 quarantine_str, strlen(quarantine_str), 0, 0));
287 EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
288 getxattr(file_path_str, "com.apple.quarantine",
290 EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
291 EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
292 EXPECT_EQ(ENOATTR, errno);
295 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
296 ScopedTempDir temp_dir_;
297 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
298 FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
299 const char* file_path_str = dummy_folder_path.value().c_str();
300 ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
301 EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
302 // No quarantine attribute to begin with, but RemoveQuarantineAttribute still
303 // succeeds because in the end the folder still doesn't have the quarantine
305 EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
306 EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
307 EXPECT_EQ(ENOATTR, errno);
310 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
311 ScopedTempDir temp_dir_;
312 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
313 FilePath non_existent_path = temp_dir_.path().Append("DummyPath");
314 ASSERT_FALSE(PathExists(non_existent_path));
315 EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));