[M85 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / chrome / browser / platform_util_unittest.cc
1 // Copyright 2015 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
5 #include "chrome/browser/platform_util.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/run_loop.h"
14 #include "base/stl_util.h"
15 #include "build/build_config.h"
16 #include "chrome/browser/platform_util_internal.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 #if defined(OS_CHROMEOS)
20 #include "base/json/json_string_value_serializer.h"
21 #include "base/values.h"
22 #include "chrome/browser/chrome_content_browser_client.h"
23 #include "chrome/browser/chromeos/file_manager/app_id.h"
24 #include "chrome/browser/chromeos/fileapi/file_system_backend.h"
25 #include "chrome/browser/chromeos/fileapi/file_system_backend_delegate.h"
26 #include "chrome/browser/extensions/extension_special_storage_policy.h"
27 #include "chrome/test/base/browser_with_test_window_test.h"
28 #include "content/public/browser/browser_context.h"
29 #include "content/public/common/content_client.h"
30 #include "extensions/browser/extension_registry.h"
31 #include "extensions/common/extension.h"
32 #include "storage/browser/file_system/external_mount_points.h"
33 #include "storage/browser/test/mock_special_storage_policy.h"
34 #include "storage/common/file_system/file_system_types.h"
35 #else
36 #include "content/public/test/browser_task_environment.h"
37 #endif
38
39 namespace platform_util {
40
41 namespace {
42
43 #if defined(OS_CHROMEOS)
44
45 // ChromeContentBrowserClient subclass that sets up a custom file system backend
46 // that allows the test to grant file access to the file manager extension ID
47 // without having to install the extension.
48 class PlatformUtilTestContentBrowserClient : public ChromeContentBrowserClient {
49  public:
50   void GetAdditionalFileSystemBackends(
51       content::BrowserContext* browser_context,
52       const base::FilePath& storage_partition_path,
53       std::vector<std::unique_ptr<storage::FileSystemBackend>>*
54           additional_backends) override {
55     storage::ExternalMountPoints* external_mount_points =
56         content::BrowserContext::GetMountPoints(browser_context);
57
58     // New FileSystemBackend that uses our MockSpecialStoragePolicy.
59     additional_backends->push_back(
60         std::make_unique<chromeos::FileSystemBackend>(
61             nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
62             external_mount_points,
63             storage::ExternalMountPoints::GetSystemInstance()));
64   }
65 };
66
67 // Base test fixture class to be used on Chrome OS.
68 class PlatformUtilTestBase : public BrowserWithTestWindowTest {
69  protected:
70   void SetUpPlatformFixture(const base::FilePath& test_directory) {
71     content_browser_client_.reset(new PlatformUtilTestContentBrowserClient());
72     old_content_browser_client_ =
73         content::SetBrowserClientForTesting(content_browser_client_.get());
74
75     // The test_directory needs to be mounted for it to be accessible.
76     content::BrowserContext::GetMountPoints(GetProfile())
77         ->RegisterFileSystem("test", storage::kFileSystemTypeNativeLocal,
78                              storage::FileSystemMountOption(), test_directory);
79
80     // To test opening a file, we are going to register a mock extension that
81     // handles .txt files. The extension doesn't actually need to exist due to
82     // the DisableShellOperationsForTesting() call which prevents the extension
83     // from being invoked.
84     std::string error;
85     int error_code = 0;
86
87     std::string json_manifest =
88         "{"
89         "  \"manifest_version\": 2,"
90         "  \"name\": \"Test extension\","
91         "  \"version\": \"0\","
92         "  \"app\": { \"background\": { \"scripts\": [\"main.js\"] }},"
93         "  \"file_handlers\": {"
94         "    \"text\": {"
95         "      \"extensions\": [ \"txt\" ],"
96         "      \"title\": \"Text\""
97         "      }"
98         "    }"
99         "}";
100     JSONStringValueDeserializer json_string_deserializer(json_manifest);
101     std::unique_ptr<base::Value> manifest =
102         json_string_deserializer.Deserialize(&error_code, &error);
103     base::DictionaryValue* manifest_dictionary;
104
105     manifest->GetAsDictionary(&manifest_dictionary);
106     ASSERT_TRUE(manifest_dictionary);
107
108     scoped_refptr<extensions::Extension> extension =
109         extensions::Extension::Create(
110             test_directory.AppendASCII("invalid-extension"),
111             extensions::Manifest::INVALID_LOCATION, *manifest_dictionary,
112             extensions::Extension::NO_FLAGS, &error);
113     ASSERT_TRUE(error.empty()) << error;
114     extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension);
115   }
116
117   void SetUp() override {
118     BrowserWithTestWindowTest::SetUp();
119     base::RunLoop().RunUntilIdle();
120   }
121
122   void TearDown() override {
123     content::ContentBrowserClient* content_browser_client =
124         content::SetBrowserClientForTesting(old_content_browser_client_);
125     old_content_browser_client_ = nullptr;
126     DCHECK_EQ(static_cast<content::ContentBrowserClient*>(
127                   content_browser_client_.get()),
128               content_browser_client)
129         << "ContentBrowserClient changed during test.";
130     BrowserWithTestWindowTest::TearDown();
131   }
132
133  private:
134   std::unique_ptr<content::ContentBrowserClient> content_browser_client_;
135   content::ContentBrowserClient* old_content_browser_client_ = nullptr;
136 };
137
138 #else
139
140 // Test fixture used by all desktop platforms other than Chrome OS.
141 class PlatformUtilTestBase : public testing::Test {
142  protected:
143   Profile* GetProfile() { return nullptr; }
144   void SetUpPlatformFixture(const base::FilePath&) {}
145
146  private:
147   content::BrowserTaskEnvironment task_environment_;
148 };
149
150 #endif
151
152 class PlatformUtilTest : public PlatformUtilTestBase {
153  public:
154   void SetUp() override {
155     ASSERT_NO_FATAL_FAILURE(PlatformUtilTestBase::SetUp());
156
157     static const char kTestFileData[] = "Cow says moo!";
158     const int kTestFileDataLength = base::size(kTestFileData) - 1;
159
160     // This prevents platform_util from invoking any shell or external APIs
161     // during tests. Doing so may result in external applications being launched
162     // and intefering with tests.
163     internal::DisableShellOperationsForTesting();
164
165     ASSERT_TRUE(directory_.CreateUniqueTempDir());
166
167     // A valid file.
168     existing_file_ = directory_.GetPath().AppendASCII("test_file.txt");
169     ASSERT_EQ(
170         kTestFileDataLength,
171         base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength));
172
173     // A valid folder.
174     existing_folder_ = directory_.GetPath().AppendASCII("test_folder");
175     ASSERT_TRUE(base::CreateDirectory(existing_folder_));
176
177     // A non-existent path.
178     nowhere_ = directory_.GetPath().AppendASCII("nowhere");
179
180     SetUpPlatformFixture(directory_.GetPath());
181   }
182
183   OpenOperationResult CallOpenItem(const base::FilePath& path,
184                                    OpenItemType item_type) {
185     base::RunLoop run_loop;
186     OpenOperationResult result = OPEN_SUCCEEDED;
187     OpenOperationCallback callback =
188         base::Bind(&OnOpenOperationDone, run_loop.QuitClosure(), &result);
189     OpenItem(GetProfile(), path, item_type, callback);
190     run_loop.Run();
191     return result;
192   }
193
194   base::FilePath existing_file_;
195   base::FilePath existing_folder_;
196   base::FilePath nowhere_;
197
198  protected:
199   base::ScopedTempDir directory_;
200
201  private:
202   std::unique_ptr<base::RunLoop> run_loop_;
203
204   static void OnOpenOperationDone(const base::Closure& closure,
205                                   OpenOperationResult* store_result,
206                                   OpenOperationResult result) {
207     *store_result = result;
208     closure.Run();
209   }
210 };
211
212 }  // namespace
213
214 TEST_F(PlatformUtilTest, OpenFile) {
215   EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_file_, OPEN_FILE));
216   EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
217             CallOpenItem(existing_folder_, OPEN_FILE));
218   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FILE));
219 }
220
221 TEST_F(PlatformUtilTest, OpenFolder) {
222   EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_folder_, OPEN_FOLDER));
223   EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
224             CallOpenItem(existing_file_, OPEN_FOLDER));
225   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FOLDER));
226 }
227
228 #if defined(OS_POSIX)
229 // Symbolic links are currently only supported on Posix. Windows technically
230 // supports it as well, but not on Windows XP.
231 class PlatformUtilPosixTest : public PlatformUtilTest {
232  public:
233   void SetUp() override {
234     ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp());
235
236     symlink_to_file_ = directory_.GetPath().AppendASCII("l_file.txt");
237     ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_));
238     symlink_to_folder_ = directory_.GetPath().AppendASCII("l_folder");
239     ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_));
240     symlink_to_nowhere_ = directory_.GetPath().AppendASCII("l_nowhere");
241     ASSERT_TRUE(base::CreateSymbolicLink(nowhere_, symlink_to_nowhere_));
242   }
243
244  protected:
245   base::FilePath symlink_to_file_;
246   base::FilePath symlink_to_folder_;
247   base::FilePath symlink_to_nowhere_;
248 };
249 #endif  // OS_POSIX
250
251 #if defined(OS_CHROMEOS)
252 // ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the
253 // symbolic link tests should return PATH_NOT_FOUND.
254
255 TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinksChromeOS) {
256   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
257             CallOpenItem(symlink_to_file_, OPEN_FILE));
258   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
259             CallOpenItem(symlink_to_folder_, OPEN_FILE));
260   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
261             CallOpenItem(symlink_to_nowhere_, OPEN_FILE));
262 }
263
264 TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinksChromeOS) {
265   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
266             CallOpenItem(symlink_to_folder_, OPEN_FOLDER));
267   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
268             CallOpenItem(symlink_to_file_, OPEN_FOLDER));
269   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
270             CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER));
271 }
272
273 TEST_F(PlatformUtilTest, OpenFileWithUnhandledFileType) {
274   base::FilePath unhandled_file =
275       directory_.GetPath().AppendASCII("myfile.filetype");
276   ASSERT_EQ(3, base::WriteFile(unhandled_file, "cat", 3));
277   EXPECT_EQ(OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE,
278             CallOpenItem(unhandled_file, OPEN_FILE));
279 }
280 #endif  // OS_CHROMEOS
281
282 #if defined(OS_POSIX) && !defined(OS_CHROMEOS)
283 // On all other Posix platforms, the symbolic link tests should work as
284 // expected.
285
286 TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinks) {
287   EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_file_, OPEN_FILE));
288   EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
289             CallOpenItem(symlink_to_folder_, OPEN_FILE));
290   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
291             CallOpenItem(symlink_to_nowhere_, OPEN_FILE));
292 }
293
294 TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinks) {
295   EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_folder_, OPEN_FOLDER));
296   EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
297             CallOpenItem(symlink_to_file_, OPEN_FOLDER));
298   EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
299             CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER));
300 }
301 #endif  // OS_POSIX && !OS_CHROMEOS
302
303 }  // namespace platform_util