mac_util_unittest.mm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.
  4. #include "base/mac/mac_util.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include <errno.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <sys/xattr.h>
  10. #include "base/files/file_path.h"
  11. #include "base/files/file_util.h"
  12. #include "base/files/scoped_temp_dir.h"
  13. #include "base/mac/foundation_util.h"
  14. #include "base/mac/scoped_cftyperef.h"
  15. #include "base/mac/scoped_nsobject.h"
  16. #include "base/system/sys_info.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "testing/platform_test.h"
  19. namespace base::mac {
  20. namespace {
  21. using MacUtilTest = PlatformTest;
  22. TEST_F(MacUtilTest, GetUserDirectoryTest) {
  23. // Try a few keys, make sure they come back with non-empty paths.
  24. FilePath caches_dir;
  25. EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir));
  26. EXPECT_FALSE(caches_dir.empty());
  27. FilePath application_support_dir;
  28. EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory,
  29. &application_support_dir));
  30. EXPECT_FALSE(application_support_dir.empty());
  31. FilePath library_dir;
  32. EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir));
  33. EXPECT_FALSE(library_dir.empty());
  34. }
  35. TEST_F(MacUtilTest, TestLibraryPath) {
  36. FilePath library_dir = GetUserLibraryPath();
  37. // Make sure the string isn't empty.
  38. EXPECT_FALSE(library_dir.value().empty());
  39. }
  40. TEST_F(MacUtilTest, TestGetAppBundlePath) {
  41. FilePath out;
  42. // Make sure it doesn't crash.
  43. out = GetAppBundlePath(FilePath());
  44. EXPECT_TRUE(out.empty());
  45. // Some more invalid inputs.
  46. const char* const invalid_inputs[] = {
  47. "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
  48. "foo/bar./bazquux", "foo/.app", "//foo",
  49. };
  50. for (size_t i = 0; i < std::size(invalid_inputs); i++) {
  51. out = GetAppBundlePath(FilePath(invalid_inputs[i]));
  52. EXPECT_TRUE(out.empty()) << "loop: " << i;
  53. }
  54. // Some valid inputs; this and |expected_outputs| should be in sync.
  55. struct {
  56. const char *in;
  57. const char *expected_out;
  58. } valid_inputs[] = {
  59. { "FooBar.app/", "FooBar.app" },
  60. { "/FooBar.app", "/FooBar.app" },
  61. { "/FooBar.app/", "/FooBar.app" },
  62. { "//FooBar.app", "//FooBar.app" },
  63. { "/Foo/Bar.app", "/Foo/Bar.app" },
  64. { "/Foo/Bar.app/", "/Foo/Bar.app" },
  65. { "/F/B.app", "/F/B.app" },
  66. { "/F/B.app/", "/F/B.app" },
  67. { "/Foo/Bar.app/baz", "/Foo/Bar.app" },
  68. { "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
  69. { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
  70. { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
  71. "/Applications/Google Foo.app" },
  72. };
  73. for (size_t i = 0; i < std::size(valid_inputs); i++) {
  74. out = GetAppBundlePath(FilePath(valid_inputs[i].in));
  75. EXPECT_FALSE(out.empty()) << "loop: " << i;
  76. EXPECT_STREQ(valid_inputs[i].expected_out,
  77. out.value().c_str()) << "loop: " << i;
  78. }
  79. }
  80. TEST_F(MacUtilTest, IsOSEllipsis) {
  81. int32_t major, minor, bugfix;
  82. base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
  83. // The patterns here are:
  84. // - FALSE/FALSE/TRUE (it is not the earlier version, it is not "at most" the
  85. // earlier version, it is "at least" the earlier version)
  86. // - TRUE/TRUE/TRUE (it is the same version, it is "at most" the same version,
  87. // it is "at least" the same version)
  88. // - FALSE/TRUE/FALSE (it is not the later version, it is "at most" the later
  89. // version, it is not "at least" the later version)
  90. #define TEST_FOR_PAST_10_OS(V) \
  91. EXPECT_FALSE(IsOS10_##V()); \
  92. EXPECT_FALSE(IsAtMostOS10_##V()); \
  93. EXPECT_TRUE(IsAtLeastOS10_##V());
  94. #define TEST_FOR_PAST_OS(V) \
  95. EXPECT_FALSE(IsOS##V()); \
  96. EXPECT_FALSE(IsAtMostOS##V()); \
  97. EXPECT_TRUE(IsAtLeastOS##V());
  98. #define TEST_FOR_SAME_10_OS(V) \
  99. EXPECT_TRUE(IsOS10_##V()); \
  100. EXPECT_TRUE(IsAtMostOS10_##V()); \
  101. EXPECT_TRUE(IsAtLeastOS10_##V());
  102. #define TEST_FOR_SAME_OS(V) \
  103. EXPECT_TRUE(IsOS##V()); \
  104. EXPECT_TRUE(IsAtMostOS##V()); \
  105. EXPECT_TRUE(IsAtLeastOS##V());
  106. #define TEST_FOR_FUTURE_10_OS(V) \
  107. EXPECT_FALSE(IsOS10_##V()); \
  108. EXPECT_TRUE(IsAtMostOS10_##V()); \
  109. EXPECT_FALSE(IsAtLeastOS10_##V());
  110. #define TEST_FOR_FUTURE_OS(V) \
  111. EXPECT_FALSE(IsOS##V()); \
  112. EXPECT_TRUE(IsAtMostOS##V()); \
  113. EXPECT_FALSE(IsAtLeastOS##V());
  114. if (major == 10) {
  115. if (minor == 13) {
  116. EXPECT_TRUE(IsOS10_13());
  117. EXPECT_TRUE(IsAtMostOS10_13());
  118. TEST_FOR_FUTURE_10_OS(14);
  119. TEST_FOR_FUTURE_10_OS(15);
  120. TEST_FOR_FUTURE_OS(11);
  121. TEST_FOR_FUTURE_OS(12);
  122. TEST_FOR_FUTURE_OS(13);
  123. EXPECT_FALSE(IsOSLaterThan13_DontCallThis());
  124. } else if (minor == 14) {
  125. EXPECT_FALSE(IsOS10_13());
  126. EXPECT_FALSE(IsAtMostOS10_13());
  127. TEST_FOR_SAME_10_OS(14);
  128. TEST_FOR_FUTURE_10_OS(15);
  129. TEST_FOR_FUTURE_OS(11);
  130. TEST_FOR_FUTURE_OS(12);
  131. TEST_FOR_FUTURE_OS(13);
  132. EXPECT_FALSE(IsOSLaterThan13_DontCallThis());
  133. } else if (minor == 15) {
  134. EXPECT_FALSE(IsOS10_13());
  135. EXPECT_FALSE(IsAtMostOS10_13());
  136. TEST_FOR_PAST_10_OS(14);
  137. TEST_FOR_SAME_10_OS(15);
  138. TEST_FOR_FUTURE_OS(11);
  139. TEST_FOR_FUTURE_OS(12);
  140. TEST_FOR_FUTURE_OS(13);
  141. EXPECT_FALSE(IsOSLaterThan13_DontCallThis());
  142. } else {
  143. // macOS 10.15 was the end of the line.
  144. FAIL() << "Unexpected 10.x macOS.";
  145. }
  146. } else if (major == 11) {
  147. EXPECT_FALSE(IsOS10_13());
  148. EXPECT_FALSE(IsAtMostOS10_13());
  149. TEST_FOR_PAST_10_OS(14);
  150. TEST_FOR_PAST_10_OS(15);
  151. TEST_FOR_SAME_OS(11);
  152. TEST_FOR_FUTURE_OS(12);
  153. TEST_FOR_FUTURE_OS(13);
  154. EXPECT_FALSE(IsOSLaterThan13_DontCallThis());
  155. } else if (major == 12) {
  156. EXPECT_FALSE(IsOS10_13());
  157. EXPECT_FALSE(IsAtMostOS10_13());
  158. TEST_FOR_PAST_10_OS(14);
  159. TEST_FOR_PAST_10_OS(15);
  160. TEST_FOR_PAST_OS(11);
  161. TEST_FOR_SAME_OS(12);
  162. TEST_FOR_FUTURE_OS(13);
  163. EXPECT_FALSE(IsOSLaterThan13_DontCallThis());
  164. } else if (major == 13) {
  165. EXPECT_FALSE(IsOS10_13());
  166. EXPECT_FALSE(IsAtMostOS10_13());
  167. TEST_FOR_PAST_10_OS(14);
  168. TEST_FOR_PAST_10_OS(15);
  169. TEST_FOR_PAST_OS(11);
  170. TEST_FOR_PAST_OS(12);
  171. TEST_FOR_SAME_OS(13);
  172. EXPECT_FALSE(IsOSLaterThan13_DontCallThis());
  173. } else {
  174. // The spooky future.
  175. FAIL() << "Time to update the OS macros!";
  176. }
  177. }
  178. #undef TEST_FOR_PAST_10_OS
  179. #undef TEST_FOR_PAST_OS
  180. #undef TEST_FOR_SAME_10_OS
  181. #undef TEST_FOR_SAME_OS
  182. #undef TEST_FOR_FUTURE_10_OS
  183. #undef TEST_FOR_FUTURE_OS
  184. TEST_F(MacUtilTest, ParseModelIdentifier) {
  185. std::string model;
  186. int32_t major = 1, minor = 2;
  187. EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
  188. EXPECT_EQ(0U, model.length());
  189. EXPECT_EQ(1, major);
  190. EXPECT_EQ(2, minor);
  191. EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
  192. EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
  193. EXPECT_EQ(model, "MacPro");
  194. EXPECT_EQ(4, major);
  195. EXPECT_EQ(1, minor);
  196. EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
  197. EXPECT_EQ(model, "MacBookPro");
  198. EXPECT_EQ(6, major);
  199. EXPECT_EQ(2, minor);
  200. }
  201. TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
  202. ScopedTempDir temp_dir_;
  203. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  204. FilePath dummy_folder_path = temp_dir_.GetPath().Append("DummyFolder");
  205. ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
  206. const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
  207. const char* file_path_str = dummy_folder_path.value().c_str();
  208. EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
  209. quarantine_str, strlen(quarantine_str), 0, 0));
  210. EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
  211. getxattr(file_path_str, "com.apple.quarantine",
  212. NULL, 0, 0, 0));
  213. EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
  214. EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
  215. EXPECT_EQ(ENOATTR, errno);
  216. }
  217. TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
  218. ScopedTempDir temp_dir_;
  219. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  220. FilePath dummy_folder_path = temp_dir_.GetPath().Append("DummyFolder");
  221. const char* file_path_str = dummy_folder_path.value().c_str();
  222. ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
  223. EXPECT_EQ(-1, getxattr(file_path_str, "com.apple.quarantine", NULL, 0, 0, 0));
  224. // No quarantine attribute to begin with, but RemoveQuarantineAttribute still
  225. // succeeds because in the end the folder still doesn't have the quarantine
  226. // attribute set.
  227. EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
  228. EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
  229. EXPECT_EQ(ENOATTR, errno);
  230. }
  231. TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
  232. ScopedTempDir temp_dir_;
  233. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  234. FilePath non_existent_path = temp_dir_.GetPath().Append("DummyPath");
  235. ASSERT_FALSE(PathExists(non_existent_path));
  236. EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));
  237. }
  238. } // namespace
  239. } // namespace base::mac