xref: /aosp_15_r20/external/cronet/base/mac/mac_util_unittest.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2012 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/mac/mac_util.h"
6
7#import <Cocoa/Cocoa.h>
8#include <errno.h>
9#include <stddef.h>
10#include <stdint.h>
11#include <sys/xattr.h>
12
13#include "base/apple/foundation_util.h"
14#include "base/apple/scoped_cftyperef.h"
15#include "base/files/file_path.h"
16#include "base/files/file_util.h"
17#include "base/files/scoped_temp_dir.h"
18#include "base/system/sys_info.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "testing/platform_test.h"
21
22namespace base::mac {
23
24namespace {
25
26using MacUtilTest = PlatformTest;
27
28TEST_F(MacUtilTest, GetUserDirectoryTest) {
29  // Try a few keys, make sure they come back with non-empty paths.
30  FilePath caches_dir;
31  EXPECT_TRUE(apple::GetUserDirectory(NSCachesDirectory, &caches_dir));
32  EXPECT_FALSE(caches_dir.empty());
33
34  FilePath application_support_dir;
35  EXPECT_TRUE(apple::GetUserDirectory(NSApplicationSupportDirectory,
36                                      &application_support_dir));
37  EXPECT_FALSE(application_support_dir.empty());
38
39  FilePath library_dir;
40  EXPECT_TRUE(apple::GetUserDirectory(NSLibraryDirectory, &library_dir));
41  EXPECT_FALSE(library_dir.empty());
42}
43
44TEST_F(MacUtilTest, TestLibraryPath) {
45  FilePath library_dir = apple::GetUserLibraryPath();
46  // Make sure the string isn't empty.
47  EXPECT_FALSE(library_dir.value().empty());
48}
49
50TEST_F(MacUtilTest, TestGetAppBundlePath) {
51  FilePath out;
52
53  // Make sure it doesn't crash.
54  out = apple::GetAppBundlePath(FilePath());
55  EXPECT_TRUE(out.empty());
56
57  // Some more invalid inputs.
58  const char* const invalid_inputs[] = {
59    "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
60    "foo/bar./bazquux", "foo/.app", "//foo",
61  };
62  for (size_t i = 0; i < std::size(invalid_inputs); i++) {
63    out = apple::GetAppBundlePath(FilePath(invalid_inputs[i]));
64    EXPECT_TRUE(out.empty()) << "loop: " << i;
65  }
66
67  // Some valid inputs; this and |expected_outputs| should be in sync.
68  struct {
69    const char *in;
70    const char *expected_out;
71  } valid_inputs[] = {
72    { "FooBar.app/", "FooBar.app" },
73    { "/FooBar.app", "/FooBar.app" },
74    { "/FooBar.app/", "/FooBar.app" },
75    { "//FooBar.app", "//FooBar.app" },
76    { "/Foo/Bar.app", "/Foo/Bar.app" },
77    { "/Foo/Bar.app/", "/Foo/Bar.app" },
78    { "/F/B.app", "/F/B.app" },
79    { "/F/B.app/", "/F/B.app" },
80    { "/Foo/Bar.app/baz", "/Foo/Bar.app" },
81    { "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
82    { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
83    { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
84        "/Applications/Google Foo.app" },
85  };
86  for (size_t i = 0; i < std::size(valid_inputs); i++) {
87    out = apple::GetAppBundlePath(FilePath(valid_inputs[i].in));
88    EXPECT_FALSE(out.empty()) << "loop: " << i;
89    EXPECT_STREQ(valid_inputs[i].expected_out,
90        out.value().c_str()) << "loop: " << i;
91  }
92}
93
94TEST_F(MacUtilTest, TestGetInnermostAppBundlePath) {
95  FilePath out;
96
97  // Make sure it doesn't crash.
98  out = apple::GetInnermostAppBundlePath(FilePath());
99  EXPECT_TRUE(out.empty());
100
101  // Some more invalid inputs.
102  const char* const invalid_inputs[] = {
103      "/",
104      "/foo",
105      "foo",
106      "/foo/bar.",
107      "foo/bar.",
108      "/foo/bar./bazquux",
109      "foo/bar./bazquux",
110      "foo/.app",
111      "//foo",
112  };
113  for (size_t i = 0; i < std::size(invalid_inputs); i++) {
114    SCOPED_TRACE(testing::Message()
115                 << "case #" << i << ", input: " << invalid_inputs[i]);
116    out = apple::GetInnermostAppBundlePath(FilePath(invalid_inputs[i]));
117    EXPECT_TRUE(out.empty());
118  }
119
120  // Some valid inputs; this and |expected_outputs| should be in sync.
121  struct {
122    const char* in;
123    const char* expected_out;
124  } valid_inputs[] = {
125      {"FooBar.app/", "FooBar.app"},
126      {"/FooBar.app", "/FooBar.app"},
127      {"/FooBar.app/", "/FooBar.app"},
128      {"//FooBar.app", "//FooBar.app"},
129      {"/Foo/Bar.app", "/Foo/Bar.app"},
130      {"/Foo/Bar.app/", "/Foo/Bar.app"},
131      {"/F/B.app", "/F/B.app"},
132      {"/F/B.app/", "/F/B.app"},
133      {"/Foo/Bar.app/baz", "/Foo/Bar.app"},
134      {"/Foo/Bar.app/baz/", "/Foo/Bar.app"},
135      {"/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app/baz/quux.app"},
136      {"/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
137       "/Applications/Google Foo.app/bar/Foo Helper.app"},
138  };
139  for (size_t i = 0; i < std::size(valid_inputs); i++) {
140    SCOPED_TRACE(testing::Message()
141                 << "case #" << i << ", input " << valid_inputs[i].in);
142    out = apple::GetInnermostAppBundlePath(FilePath(valid_inputs[i].in));
143    EXPECT_FALSE(out.empty());
144    EXPECT_STREQ(valid_inputs[i].expected_out, out.value().c_str());
145  }
146}
147
148TEST_F(MacUtilTest, MacOSVersion) {
149  int32_t major, minor, bugfix;
150  base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
151
152  EXPECT_EQ(major * 1'00'00 + minor * 1'00 + bugfix, MacOSVersion());
153  EXPECT_EQ(major, MacOSMajorVersion());
154}
155
156TEST_F(MacUtilTest, ParseOSProductVersion) {
157  // Various strings in shapes that would be expected to be returned from the
158  // API that would need to be parsed.
159  EXPECT_EQ(10'06'02, ParseOSProductVersionForTesting("10.6.2"));
160  EXPECT_EQ(10'15'00, ParseOSProductVersionForTesting("10.15"));
161  EXPECT_EQ(13'05'01, ParseOSProductVersionForTesting("13.5.1"));
162  EXPECT_EQ(14'00'00, ParseOSProductVersionForTesting("14.0"));
163
164  // Various strings in shapes that would not be expected, but that should parse
165  // without CHECKing.
166  EXPECT_EQ(13'04'01, ParseOSProductVersionForTesting("13.4.1 (c)"));
167  EXPECT_EQ(14'00'00, ParseOSProductVersionForTesting("14.0.0"));
168  EXPECT_EQ(18'00'00, ParseOSProductVersionForTesting("18"));
169  EXPECT_EQ(18'03'04, ParseOSProductVersionForTesting("18.3.4.3.2.5"));
170
171  // Various strings in shapes that are so unexpected that they should not
172  // parse.
173  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("Mac OS X 10.0"),
174                            "");
175  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting(""), "");
176  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("  "), "");
177  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("."), "");
178  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("10.a.5"), "");
179  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("१०.१५.७"), "");
180  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("7.6.1"), "");
181  EXPECT_DEATH_IF_SUPPORTED(ParseOSProductVersionForTesting("10.16"), "");
182}
183
184TEST_F(MacUtilTest, TestRemoveQuarantineAttribute) {
185  ScopedTempDir temp_dir_;
186  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
187  FilePath dummy_folder_path = temp_dir_.GetPath().Append("DummyFolder");
188  ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
189  const char* quarantine_str = "0000;4b392bb2;Chromium;|org.chromium.Chromium";
190  const char* file_path_str = dummy_folder_path.value().c_str();
191  EXPECT_EQ(0, setxattr(file_path_str, "com.apple.quarantine",
192      quarantine_str, strlen(quarantine_str), 0, 0));
193  EXPECT_EQ(static_cast<long>(strlen(quarantine_str)),
194            getxattr(file_path_str, "com.apple.quarantine", /*value=*/nullptr,
195                     /*size=*/0, /*position=*/0, /*options=*/0));
196  EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
197  EXPECT_EQ(-1,
198            getxattr(file_path_str, "com.apple.quarantine", /*value=*/nullptr,
199                     /*size=*/0, /*position=*/0, /*options=*/0));
200  EXPECT_EQ(ENOATTR, errno);
201}
202
203TEST_F(MacUtilTest, TestRemoveQuarantineAttributeTwice) {
204  ScopedTempDir temp_dir_;
205  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
206  FilePath dummy_folder_path = temp_dir_.GetPath().Append("DummyFolder");
207  const char* file_path_str = dummy_folder_path.value().c_str();
208  ASSERT_TRUE(base::CreateDirectory(dummy_folder_path));
209  EXPECT_EQ(-1,
210            getxattr(file_path_str, "com.apple.quarantine", /*value=*/nullptr,
211                     /*size=*/0, /*position=*/0, /*options=*/0));
212  // No quarantine attribute to begin with, but RemoveQuarantineAttribute still
213  // succeeds because in the end the folder still doesn't have the quarantine
214  // attribute set.
215  EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
216  EXPECT_TRUE(RemoveQuarantineAttribute(dummy_folder_path));
217  EXPECT_EQ(ENOATTR, errno);
218}
219
220TEST_F(MacUtilTest, TestRemoveQuarantineAttributeNonExistentPath) {
221  ScopedTempDir temp_dir_;
222  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
223  FilePath non_existent_path = temp_dir_.GetPath().Append("DummyPath");
224  ASSERT_FALSE(PathExists(non_existent_path));
225  EXPECT_FALSE(RemoveQuarantineAttribute(non_existent_path));
226}
227
228}  // namespace
229
230}  // namespace base::mac
231