Add an extension override bubble and warning box for proxy extensions.
[chromium-blink-merge.git] / base / mac / mac_util_unittest.mm
blob079f3c2ced8bf495d483c334060cad213bb164cd
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/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* 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             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, NSObjectRetainRelease) {
128   base::scoped_nsobject<NSArray> array(
129       [[NSArray alloc] initWithObjects:@"foo", nil]);
130   EXPECT_EQ(1U, [array retainCount]);
132   NSObjectRetain(array);
133   EXPECT_EQ(2U, [array retainCount]);
135   NSObjectRelease(array);
136   EXPECT_EQ(1U, [array retainCount]);
139 TEST_F(MacUtilTest, IsOSEllipsis) {
140   int32 major, minor, bugfix;
141   base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
143   if (major == 10) {
144     if (minor == 6) {
145       EXPECT_TRUE(IsOSSnowLeopard());
146       EXPECT_FALSE(IsOSLion());
147       EXPECT_TRUE(IsOSLionOrEarlier());
148       EXPECT_FALSE(IsOSLionOrLater());
149       EXPECT_FALSE(IsOSMountainLion());
150       EXPECT_TRUE(IsOSMountainLionOrEarlier());
151       EXPECT_FALSE(IsOSMountainLionOrLater());
152       EXPECT_FALSE(IsOSMavericks());
153       EXPECT_FALSE(IsOSMavericksOrLater());
154       EXPECT_FALSE(IsOSLaterThanMavericks_DontCallThis());
155     } else if (minor == 7) {
156       EXPECT_FALSE(IsOSSnowLeopard());
157       EXPECT_TRUE(IsOSLion());
158       EXPECT_TRUE(IsOSLionOrEarlier());
159       EXPECT_TRUE(IsOSLionOrLater());
160       EXPECT_FALSE(IsOSMountainLion());
161       EXPECT_TRUE(IsOSMountainLionOrEarlier());
162       EXPECT_FALSE(IsOSMountainLionOrLater());
163       EXPECT_FALSE(IsOSMavericks());
164       EXPECT_FALSE(IsOSMavericksOrLater());
165       EXPECT_FALSE(IsOSLaterThanMavericks_DontCallThis());
166     } else if (minor == 8) {
167       EXPECT_FALSE(IsOSSnowLeopard());
168       EXPECT_FALSE(IsOSLion());
169       EXPECT_FALSE(IsOSLionOrEarlier());
170       EXPECT_TRUE(IsOSLionOrLater());
171       EXPECT_TRUE(IsOSMountainLion());
172       EXPECT_TRUE(IsOSMountainLionOrEarlier());
173       EXPECT_TRUE(IsOSMountainLionOrLater());
174       EXPECT_FALSE(IsOSMavericks());
175       EXPECT_FALSE(IsOSMavericksOrLater());
176       EXPECT_FALSE(IsOSLaterThanMavericks_DontCallThis());
177     } else if (minor == 9) {
178       EXPECT_FALSE(IsOSSnowLeopard());
179       EXPECT_FALSE(IsOSLion());
180       EXPECT_FALSE(IsOSLionOrEarlier());
181       EXPECT_TRUE(IsOSLionOrLater());
182       EXPECT_FALSE(IsOSMountainLion());
183       EXPECT_FALSE(IsOSMountainLionOrEarlier());
184       EXPECT_TRUE(IsOSMountainLionOrLater());
185       EXPECT_TRUE(IsOSMavericks());
186       EXPECT_TRUE(IsOSMavericksOrLater());
187       EXPECT_FALSE(IsOSLaterThanMavericks_DontCallThis());
188     } else {
189       // Not five, six, seven, eight, or nine. Ah, ah, ah.
190       EXPECT_TRUE(false);
191     }
192   } else {
193     // Not ten. What you gonna do?
194     EXPECT_FALSE(true);
195   }
198 TEST_F(MacUtilTest, ParseModelIdentifier) {
199   std::string model;
200   int32 major = 1, minor = 2;
202   EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
203   EXPECT_EQ(0U, model.length());
204   EXPECT_EQ(1, major);
205   EXPECT_EQ(2, minor);
206   EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
208   EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
209   EXPECT_EQ(model, "MacPro");
210   EXPECT_EQ(4, major);
211   EXPECT_EQ(1, minor);
213   EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
214   EXPECT_EQ(model, "MacBookPro");
215   EXPECT_EQ(6, major);
216   EXPECT_EQ(2, minor);
219 TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
220   ScopedTempDir temp_dir_;
221   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
222   FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
223   ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
224   const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
225   const char* file_path_str = dummy_folder_path.value().c_str();
226   EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
227       quarantine_str, strlen(quarantine_str), 0, 0));
228   EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
229       getxattr(file_path_str, "com.apple.quarantine",
230           NULL, 0, 0, 0));
231   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
232   EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
233   EXPECT_EQ(ENOATTR, errno);
236 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
237   ScopedTempDir temp_dir_;
238   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
239   FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
240   const char* file_path_str = dummy_folder_path.value().c_str();
241   ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
242   EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
243   // No quarantine attribute to begin with, but RemoveQuarantineAttribute still
244   // succeeds because in the end the folder still doesn't have the quarantine
245   // attribute set.
246   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
247   EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
248   EXPECT_EQ(ENOATTR, errno);
251 TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
252   ScopedTempDir temp_dir_;
253   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
254   FilePath non_existent_path = temp_dir_.path().Append("DummyPath");
255   ASSERT_FALSE(PathExists(non_existent_path));
256   EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));
259 }  // namespace
261 }  // namespace mac
262 }  // namespace base