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

Side by Side Diff: chrome/common/extensions/extension_file_util_unittest.cc

Issue 9909019: Add schema chrome-extension-resource:// for extension resources (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add tests Created 8 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/extensions/extension_file_util.h" 5 #include "chrome/common/extensions/extension_file_util.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_string_value_serializer.h" 8 #include "base/json/json_string_value_serializer.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h" 10 #include "base/scoped_temp_dir.h"
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 .AppendASCII("bad_encoding"); 208 .AppendASCII("bad_encoding");
209 209
210 std::string error; 210 std::string error;
211 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( 211 scoped_refptr<Extension> extension(extension_file_util::LoadExtension(
212 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); 212 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error));
213 ASSERT_TRUE(extension == NULL); 213 ASSERT_TRUE(extension == NULL);
214 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " 214 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. "
215 "It isn't UTF-8 encoded.", error.c_str()); 215 "It isn't UTF-8 encoded.", error.c_str());
216 } 216 }
217 217
218 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) {
218 #define URL_PREFIX "chrome-extension://extension-id/" 219 #define URL_PREFIX "chrome-extension://extension-id/"
219
220 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) {
221 struct TestCase { 220 struct TestCase {
222 const char* url; 221 const char* url;
223 const char* expected_relative_path; 222 const char* expected_relative_path;
224 } test_cases[] = { 223 } test_cases[] = {
225 { URL_PREFIX "simple.html", 224 { URL_PREFIX "simple.html",
226 "simple.html" }, 225 "simple.html" },
227 { URL_PREFIX "directory/to/file.html", 226 { URL_PREFIX "directory/to/file.html",
228 "directory/to/file.html" }, 227 "directory/to/file.html" },
229 { URL_PREFIX "escape%20spaces.html", 228 { URL_PREFIX "escape%20spaces.html",
230 "escape spaces.html" }, 229 "escape spaces.html" },
231 { URL_PREFIX "%C3%9Cber.html", 230 { URL_PREFIX "%C3%9Cber.html",
232 "\xC3\x9C" "ber.html" }, 231 "\xC3\x9C" "ber.html" },
233 #if defined(OS_WIN) 232 #if defined(OS_WIN)
234 { URL_PREFIX "C%3A/simple.html", 233 { URL_PREFIX "C%3A/simple.html",
235 "" }, 234 "" },
236 #endif 235 #endif
237 { URL_PREFIX "////simple.html", 236 { URL_PREFIX "////simple.html",
238 "simple.html" }, 237 "simple.html" },
239 { URL_PREFIX "/simple.html", 238 { URL_PREFIX "/simple.html",
240 "simple.html" }, 239 "simple.html" },
241 { URL_PREFIX "\\simple.html", 240 { URL_PREFIX "\\simple.html",
242 "simple.html" }, 241 "simple.html" },
243 { URL_PREFIX "\\\\foo\\simple.html", 242 { URL_PREFIX "\\\\foo\\simple.html",
244 "foo/simple.html" }, 243 "foo/simple.html" },
245 }; 244 };
245 #undef URL_PREFIX
246 246
247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
248 GURL url(test_cases[i].url); 248 GURL url(test_cases[i].url);
249 #if defined(OS_POSIX) 249 #if defined(OS_POSIX)
250 FilePath expected_path(test_cases[i].expected_relative_path); 250 FilePath expected_path(test_cases[i].expected_relative_path);
251 #elif defined(OS_WIN) 251 #elif defined(OS_WIN)
252 FilePath expected_path(UTF8ToWide(test_cases[i].expected_relative_path)); 252 FilePath expected_path(UTF8ToWide(test_cases[i].expected_relative_path));
253 #endif 253 #endif
254 254
255 FilePath actual_path = 255 FilePath actual_path =
256 extension_file_util::ExtensionURLToRelativeFilePath(url); 256 extension_file_util::ExtensionURLToRelativeFilePath(url);
257 EXPECT_FALSE(actual_path.IsAbsolute()) << 257 EXPECT_FALSE(actual_path.IsAbsolute()) <<
258 " For the path " << actual_path.value(); 258 " For the path " << actual_path.value();
259 EXPECT_EQ(expected_path.value(), actual_path.value()) << 259 EXPECT_EQ(expected_path.value(), actual_path.value()) <<
260 " For the path " << url; 260 " For the path " << url;
261 } 261 }
262 } 262 }
263 263
264 TEST(ExtensionFileUtil, ExtensionResourceURLToFilePath) {
265 // Setup filesystem for testing.
266 FilePath root_path;
267 ASSERT_TRUE(file_util::CreateNewTempDirectory(
268 FILE_PATH_LITERAL(""), &root_path));
269
270 FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname"));
271 ASSERT_TRUE(file_util::CreateDirectory(api_path));
272
273 const char data[] = "Test Data";
274 FilePath resource_path = api_path.Append(FILE_PATH_LITERAL("test.js"));
275 ASSERT_TRUE(file_util::WriteFile(resource_path, data, sizeof(data)));
276 resource_path = api_path.Append(FILE_PATH_LITERAL("escape spaces.js"));
277 ASSERT_TRUE(file_util::WriteFile(resource_path, data, sizeof(data)));
278
279
280 #define URL_PREFIX "chrome-extension-resource://"
281 struct TestCase {
282 const char* url;
283 const FilePath::CharType* expected_path;
284 } test_cases[] = {
285 { URL_PREFIX "apiname/test.js",
286 FILE_PATH_LITERAL("apiname/test.js") },
287 { URL_PREFIX "/apiname/test.js",
288 FILE_PATH_LITERAL("apiname/test.js") },
289 // Test % escape
290 { URL_PREFIX "apiname/%74%65st.js",
291 FILE_PATH_LITERAL("apiname/test.js") },
292 { URL_PREFIX "apiname/escape%20spaces.js",
293 FILE_PATH_LITERAL("apiname/escape spaces.js") },
294 // Test file does not exist.
295 { URL_PREFIX "apiname/directory/to/file.js",
296 NULL },
297 // Test apiname/../../test.js
298 { URL_PREFIX "apiname/../../test.js",
299 FILE_PATH_LITERAL("apiname/test.js") },
300 { URL_PREFIX "apiname/..%2F../test.js",
301 NULL },
302 { URL_PREFIX "apiname/f/../../../test.js",
303 FILE_PATH_LITERAL("apiname/test.js") },
304 { URL_PREFIX "apiname/f%2F..%2F..%2F../test.js",
305 NULL },
306 };
307
308 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
309 GURL url(test_cases[i].url);
310 FilePath expected_path;
311 if (test_cases[i].expected_path)
312 expected_path = root_path.Append(test_cases[i].expected_path);
313 FilePath actual_path =
314 extension_file_util::ExtensionResourceURLToFilePath(url, root_path);
315 EXPECT_EQ(expected_path.value(), actual_path.value()) <<
316 " For the path " << url;
317 }
318 // Remove temp files.
319 ASSERT_TRUE(file_util::Delete(root_path, true));
320 }
321
264 static scoped_refptr<Extension> LoadExtensionManifest( 322 static scoped_refptr<Extension> LoadExtensionManifest(
265 DictionaryValue* manifest, 323 DictionaryValue* manifest,
266 const FilePath& manifest_dir, 324 const FilePath& manifest_dir,
267 Extension::Location location, 325 Extension::Location location,
268 int extra_flags, 326 int extra_flags,
269 std::string* error) { 327 std::string* error) {
270 scoped_refptr<Extension> extension = Extension::Create( 328 scoped_refptr<Extension> extension = Extension::Create(
271 manifest_dir, location, *manifest, extra_flags, error); 329 manifest_dir, location, *manifest, extra_flags, error);
272 return extension; 330 return extension;
273 } 331 }
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error)); 415 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error));
358 EXPECT_EQ(l10n_util::GetStringFUTF8( 416 EXPECT_EQ(l10n_util::GetStringFUTF8(
359 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, 417 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED,
360 ASCIIToUTF16("http://google.com/foo.js")), 418 ASCIIToUTF16("http://google.com/foo.js")),
361 error); 419 error);
362 } 420 }
363 421
364 // TODO(aa): More tests as motivation allows. Maybe steal some from 422 // TODO(aa): More tests as motivation allows. Maybe steal some from
365 // ExtensionService? Many of them could probably be tested here without the 423 // ExtensionService? Many of them could probably be tested here without the
366 // MessageLoop shenanigans. 424 // MessageLoop shenanigans.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698