1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/mac/mac_util.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_nsobject.h"
#include "base/sys_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include <errno.h>
#include <sys/xattr.h>
namespace base {
namespace mac {
namespace {
typedef PlatformTest MacUtilTest;
TEST_F(MacUtilTest, TestFSRef) {
FSRef ref;
std::string path("/System/Library");
ASSERT_TRUE(FSRefFromPath(path, &ref));
EXPECT_EQ(path, PathFromFSRef(ref));
}
TEST_F(MacUtilTest, GetUserDirectoryTest) {
// Try a few keys, make sure they come back with non-empty paths.
FilePath caches_dir;
EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir));
EXPECT_FALSE(caches_dir.empty());
FilePath application_support_dir;
EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory,
&application_support_dir));
EXPECT_FALSE(application_support_dir.empty());
FilePath library_dir;
EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir));
EXPECT_FALSE(library_dir.empty());
}
TEST_F(MacUtilTest, TestLibraryPath) {
FilePath library_dir = GetUserLibraryPath();
// Make sure the string isn't empty.
EXPECT_FALSE(library_dir.value().empty());
}
TEST_F(MacUtilTest, TestGetAppBundlePath) {
FilePath out;
// Make sure it doesn't crash.
out = GetAppBundlePath(FilePath());
EXPECT_TRUE(out.empty());
// Some more invalid inputs.
const char* invalid_inputs[] = {
"/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
"foo/bar./bazquux", "foo/.app", "//foo",
};
for (size_t i = 0; i < arraysize(invalid_inputs); i++) {
out = GetAppBundlePath(FilePath(invalid_inputs[i]));
EXPECT_TRUE(out.empty()) << "loop: " << i;
}
// Some valid inputs; this and |expected_outputs| should be in sync.
struct {
const char *in;
const char *expected_out;
} valid_inputs[] = {
{ "FooBar.app/", "FooBar.app" },
{ "/FooBar.app", "/FooBar.app" },
{ "/FooBar.app/", "/FooBar.app" },
{ "//FooBar.app", "//FooBar.app" },
{ "/Foo/Bar.app", "/Foo/Bar.app" },
{ "/Foo/Bar.app/", "/Foo/Bar.app" },
{ "/F/B.app", "/F/B.app" },
{ "/F/B.app/", "/F/B.app" },
{ "/Foo/Bar.app/baz", "/Foo/Bar.app" },
{ "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
{ "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
{ "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
"/Applications/Google Foo.app" },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) {
out = GetAppBundlePath(FilePath(valid_inputs[i].in));
EXPECT_FALSE(out.empty()) << "loop: " << i;
EXPECT_STREQ(valid_inputs[i].expected_out,
out.value().c_str()) << "loop: " << i;
}
}
TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
// The file must already exist in order to set its exclusion property.
ScopedTempDir temp_dir_;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
FilePath dummy_file_path = temp_dir_.path().Append("DummyFile");
const char dummy_data[] = "All your base are belong to us!";
// Dump something real into the file.
ASSERT_EQ(static_cast<int>(arraysize(dummy_data)),
WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
NSString* fileURLString =
[NSString stringWithUTF8String:dummy_file_path.value().c_str()];
NSURL* fileURL = [NSURL URLWithString:fileURLString];
// Initial state should be non-excluded.
EXPECT_FALSE(CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), NULL));
// Exclude the file.
EXPECT_TRUE(SetFileBackupExclusion(dummy_file_path));
// SetFileBackupExclusion never excludes by path.
Boolean excluded_by_path = FALSE;
Boolean excluded =
CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), &excluded_by_path);
EXPECT_TRUE(excluded);
EXPECT_FALSE(excluded_by_path);
}
TEST_F(MacUtilTest, NSObjectRetainRelease) {
base::scoped_nsobject<NSArray> array(
[[NSArray alloc] initWithObjects:@"foo", nil]);
EXPECT_EQ(1U, [array retainCount]);
NSObjectRetain(array);
EXPECT_EQ(2U, [array retainCount]);
NSObjectRelease(array);
EXPECT_EQ(1U, [array retainCount]);
}
TEST_F(MacUtilTest, IsOSEllipsis) {
int32 major, minor, bugfix;
base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
if (major == 10) {
if (minor == 6) {
EXPECT_TRUE(IsOSSnowLeopard());
EXPECT_FALSE(IsOSLion());
EXPECT_TRUE(IsOSLionOrEarlier());
EXPECT_FALSE(IsOSLionOrLater());
EXPECT_FALSE(IsOSMountainLion());
EXPECT_TRUE(IsOSMountainLionOrEarlier());
EXPECT_FALSE(IsOSMountainLionOrLater());
EXPECT_FALSE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrEarlier());
EXPECT_FALSE(IsOSMavericksOrLater());
EXPECT_FALSE(IsOSYosemite());
EXPECT_FALSE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
} else if (minor == 7) {
EXPECT_FALSE(IsOSSnowLeopard());
EXPECT_TRUE(IsOSLion());
EXPECT_TRUE(IsOSLionOrEarlier());
EXPECT_TRUE(IsOSLionOrLater());
EXPECT_FALSE(IsOSMountainLion());
EXPECT_TRUE(IsOSMountainLionOrEarlier());
EXPECT_FALSE(IsOSMountainLionOrLater());
EXPECT_FALSE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrEarlier());
EXPECT_FALSE(IsOSMavericksOrLater());
EXPECT_FALSE(IsOSYosemite());
EXPECT_FALSE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
} else if (minor == 8) {
EXPECT_FALSE(IsOSSnowLeopard());
EXPECT_FALSE(IsOSLion());
EXPECT_FALSE(IsOSLionOrEarlier());
EXPECT_TRUE(IsOSLionOrLater());
EXPECT_TRUE(IsOSMountainLion());
EXPECT_TRUE(IsOSMountainLionOrEarlier());
EXPECT_TRUE(IsOSMountainLionOrLater());
EXPECT_FALSE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrEarlier());
EXPECT_FALSE(IsOSMavericksOrLater());
EXPECT_FALSE(IsOSYosemite());
EXPECT_FALSE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
} else if (minor == 9) {
EXPECT_FALSE(IsOSSnowLeopard());
EXPECT_FALSE(IsOSLion());
EXPECT_FALSE(IsOSLionOrEarlier());
EXPECT_TRUE(IsOSLionOrLater());
EXPECT_FALSE(IsOSMountainLion());
EXPECT_FALSE(IsOSMountainLionOrEarlier());
EXPECT_TRUE(IsOSMountainLionOrLater());
EXPECT_TRUE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrEarlier());
EXPECT_TRUE(IsOSMavericksOrLater());
EXPECT_FALSE(IsOSYosemite());
EXPECT_FALSE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
} else if (minor == 10) {
EXPECT_FALSE(IsOSSnowLeopard());
EXPECT_FALSE(IsOSLion());
EXPECT_FALSE(IsOSLionOrEarlier());
EXPECT_TRUE(IsOSLionOrLater());
EXPECT_FALSE(IsOSMountainLion());
EXPECT_FALSE(IsOSMountainLionOrEarlier());
EXPECT_TRUE(IsOSMountainLionOrLater());
EXPECT_FALSE(IsOSMavericks());
EXPECT_FALSE(IsOSMavericksOrEarlier());
EXPECT_TRUE(IsOSMavericksOrLater());
EXPECT_TRUE(IsOSYosemite());
EXPECT_TRUE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSLaterThanYosemite_DontCallThis());
} else {
// Not six, seven, eight, nine, or ten. Ah, ah, ah.
EXPECT_TRUE(false);
}
} else {
// Not ten. What you gonna do?
EXPECT_FALSE(true);
}
}
TEST_F(MacUtilTest, ParseModelIdentifier) {
std::string model;
int32 major = 1, minor = 2;
EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
EXPECT_EQ(0U, model.length());
EXPECT_EQ(1, major);
EXPECT_EQ(2, minor);
EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
EXPECT_EQ(model, "MacPro");
EXPECT_EQ(4, major);
EXPECT_EQ(1, minor);
EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
EXPECT_EQ(model, "MacBookPro");
EXPECT_EQ(6, major);
EXPECT_EQ(2, minor);
}
TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
ScopedTempDir temp_dir_;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
const char* file_path_str = dummy_folder_path.value().c_str();
EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
quarantine_str, strlen(quarantine_str), 0, 0));
EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
getxattr(file_path_str, "com.apple.quarantine",
NULL, 0, 0, 0));
EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
EXPECT_EQ(ENOATTR, errno);
}
TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
ScopedTempDir temp_dir_;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
FilePath dummy_folder_path = temp_dir_.path().Append("DummyFolder");
const char* file_path_str = dummy_folder_path.value().c_str();
ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
// No quarantine attribute to begin with, but RemoveQuarantineAttribute still
// succeeds because in the end the folder still doesn't have the quarantine
// attribute set.
EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
EXPECT_EQ(ENOATTR, errno);
}
TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
ScopedTempDir temp_dir_;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
FilePath non_existent_path = temp_dir_.path().Append("DummyPath");
ASSERT_FALSE(PathExists(non_existent_path));
EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));
}
} // namespace
} // namespace mac
} // namespace base
|