Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(338)

Side by Side Diff: chrome/browser/platform_util_unittest.cc

Issue 352393002: Be explicit about target type in platform_util::OpenItem() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/platform_util_internal.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 #if defined(OS_CHROMEOS)
17 #include "base/json/json_string_value_serializer.h"
18 #include "base/values.h"
19 #include "chrome/browser/chrome_content_browser_client.h"
20 #include "chrome/browser/chromeos/file_manager/app_id.h"
21 #include "chrome/browser/chromeos/fileapi/file_system_backend.h"
22 #include "chrome/browser/extensions/extension_special_storage_policy.h"
23 #include "chrome/test/base/browser_with_test_window_test.h"
24 #include "content/public/browser/browser_context.h"
25 #include "content/public/common/content_client.h"
26 #include "content/public/test/mock_special_storage_policy.h"
27 #include "extensions/browser/extension_registry.h"
28 #include "extensions/common/extension.h"
29 #include "storage/browser/fileapi/external_mount_points.h"
30 #include "storage/common/fileapi/file_system_types.h"
31 #else
32 #include "content/public/test/test_browser_thread_bundle.h"
33 #endif
34
35 namespace platform_util {
36
37 namespace {
38
39 #if defined(OS_CHROMEOS)
40
41 // ChromeContentBrowserClient subclass that sets up a custom file system backend
42 // that allows the test to grant file access to the file manager extension ID
43 // without having to install the extension.
44 class PlatformUtilTestContentBrowserClient
45 : public chrome::ChromeContentBrowserClient {
46 public:
47 void GetAdditionalFileSystemBackends(
48 content::BrowserContext* browser_context,
49 const base::FilePath& storage_partition_path,
50 ScopedVector<storage::FileSystemBackend>* additional_backends) override {
51 storage::ExternalMountPoints* external_mount_points =
52 content::BrowserContext::GetMountPoints(browser_context);
53 scoped_refptr<content::MockSpecialStoragePolicy> special_storage_policy =
54 new content::MockSpecialStoragePolicy();
55
56 // New FileSystemBackend that uses our MockSpecialStoragePolicy.
57 chromeos::FileSystemBackend* backend = new chromeos::FileSystemBackend(
58 NULL, NULL, NULL, special_storage_policy, external_mount_points,
59 storage::ExternalMountPoints::GetSystemInstance());
60 additional_backends->push_back(backend);
61
62 // The only change we need to make is to add kFileManagerAppId as a file
63 // handler.
64 special_storage_policy->AddFileHandler(file_manager::kFileManagerAppId);
65 }
66 };
67
68 // Base test fixture class to be used on Chrome OS.
69 class PlatformUtilTestBase : public BrowserWithTestWindowTest {
70 protected:
71 void SetUpPlatformFixture(const base::FilePath& test_directory) {
72 content::SetBrowserClientForTesting(
73 new PlatformUtilTestContentBrowserClient());
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 JSONStringValueSerializer json_string_serializer(json_manifest);
101 scoped_ptr<base::Value> manifest(
102 json_string_serializer.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
118 #else
119
120 // Test fixture used by all desktop platforms other than Chrome OS.
121 class PlatformUtilTestBase : public testing::Test {
122 protected:
123 Profile* GetProfile() { return NULL; }
124 void SetUpPlatformFixture(const base::FilePath&) {}
125
126 private:
127 content::TestBrowserThreadBundle thread_bundle_;
128 };
129
130 #endif
131
132 class PlatformUtilTest : public PlatformUtilTestBase {
133 public:
134 void SetUp() override {
135 ASSERT_NO_FATAL_FAILURE(PlatformUtilTestBase::SetUp());
136
137 static const char kTestFileData[] = "Cow says moo!";
138 const int kTestFileDataLength = arraysize(kTestFileData) - 1;
139
140 // This prevents platfrom_util from invoking any shell or external APIs
141 // during tests. Doing so may result in external applications being launched
142 // and intefering with tests.
143 internal::DisableShellOperationsForTesting();
144
145 ASSERT_TRUE(directory_.CreateUniqueTempDir());
146
147 // A valid file.
148 existing_file_ = directory_.path().AppendASCII("test_file.txt");
149 ASSERT_EQ(
150 kTestFileDataLength,
151 base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength));
152
153 // A valid folder.
154 existing_folder_ = directory_.path().AppendASCII("test_folder");
155 ASSERT_TRUE(base::CreateDirectory(existing_folder_));
156
157 // A non-existent path.
158 nowhere_ = directory_.path().AppendASCII("nowhere");
159
160 SetUpPlatformFixture(directory_.path());
161 }
162
163 OpenOperationResult CallOpenFile(const base::FilePath& path) {
164 return InvokeOpenMethodAndReturnResult(&OpenFile, path);
165 }
166
167 OpenOperationResult CallOpenFolder(const base::FilePath& path) {
168 return InvokeOpenMethodAndReturnResult(&OpenFolder, path);
169 }
170
171 base::FilePath existing_file_;
172 base::FilePath existing_folder_;
173 base::FilePath nowhere_;
174
175 protected:
176 base::ScopedTempDir directory_;
177
178 private:
179 scoped_ptr<base::RunLoop> run_loop_;
180
181 OpenOperationResult InvokeOpenMethodAndReturnResult(
182 void (*function)(Profile*,
183 const base::FilePath&,
184 const OpenOperationCallback&),
185 const base::FilePath& path) {
186 base::RunLoop run_loop;
187 OpenOperationResult result = OPEN_SUCCEEDED;
188 OpenOperationCallback callback =
189 base::Bind(&OnOpenOperationDone, run_loop.QuitClosure(), &result);
190 (*function)(GetProfile(), path, callback);
191 run_loop.Run();
192 return result;
193 }
194
195 static void OnOpenOperationDone(const base::Closure& closure,
196 OpenOperationResult* store_result,
197 OpenOperationResult result) {
198 *store_result = result;
199 closure.Run();
200 }
201 };
202
203 } // namespace
204
205 TEST_F(PlatformUtilTest, OpenFile) {
206 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenFile(existing_file_));
207 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, CallOpenFile(existing_folder_));
208 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFile(nowhere_));
209 }
210
211 TEST_F(PlatformUtilTest, OpenFolder) {
212 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenFolder(existing_folder_));
213 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, CallOpenFolder(existing_file_));
214 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFolder(nowhere_));
215 }
216
217 #if defined(OS_POSIX)
218 // Symbolic links are currently only supported on Posix. Windows technically
219 // supports it as well, but not on Windows XP.
220 class PlatformUtilPosixTest : public PlatformUtilTest {
221 public:
222 void SetUp() override {
223 ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp());
224
225 symlink_to_file_ = directory_.path().AppendASCII("l_file.txt");
226 ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_));
227 symlink_to_folder_ = directory_.path().AppendASCII("l_folder");
228 ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_));
229 symlink_to_nowhere_ = directory_.path().AppendASCII("l_nowhere");
230 ASSERT_TRUE(base::CreateSymbolicLink(nowhere_, symlink_to_nowhere_));
231 }
232
233 protected:
234 base::FilePath symlink_to_file_;
235 base::FilePath symlink_to_folder_;
236 base::FilePath symlink_to_nowhere_;
237 };
238 #endif // OS_POSIX
239
240 #if defined(OS_CHROMEOS)
241 // ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the
242 // symbolic link tests should return PATH_NOT_FOUND.
243
244 TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinksChromeOS) {
245 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFile(symlink_to_file_));
246 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFile(symlink_to_folder_));
247 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFile(symlink_to_nowhere_));
248 }
249
250 TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinksChromeOS) {
251 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFolder(symlink_to_folder_));
252 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFolder(symlink_to_file_));
253 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFolder(symlink_to_nowhere_));
254 }
255
256 TEST_F(PlatformUtilTest, OpenFileWithUnhandledFileType) {
257 base::FilePath unhandled_file =
258 directory_.path().AppendASCII("myfile.filetype");
259 ASSERT_EQ(3, base::WriteFile(unhandled_file, "cat", 3));
260 EXPECT_EQ(OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE, CallOpenFile(unhandled_file));
261 }
262 #endif // OS_CHROMEOS
263
264 #if defined(OS_POSIX) && !defined(OS_CHROMEOS)
265 // On all other Posix platforms, the symbolic link tests should work as
266 // expected.
267
268 TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinks) {
269 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenFile(symlink_to_file_));
270 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, CallOpenFile(symlink_to_folder_));
271 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFile(symlink_to_nowhere_));
272 }
273
274 TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinks) {
275 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenFolder(symlink_to_folder_));
276 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, CallOpenFolder(symlink_to_file_));
277 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenFolder(symlink_to_nowhere_));
278 }
279 #endif // OS_POSIX && !OS_CHROMEOS
280
281 } // namespace platform_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698