Revert of Clean up V4L2 codec-related flags and defines (patchset #3 id:40001 of...
[chromium-blink-merge.git] / extensions / common / file_util_unittest.cc
blob7e2f51d3c63094bda7deb62ab2adeb6969719341
1 // Copyright 2013 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.
5 #include "extensions/common/file_util.h"
7 #include "base/basictypes.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/path_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "extensions/common/constants.h"
15 #include "extensions/common/extension.h"
16 #include "extensions/common/extension_paths.h"
17 #include "extensions/common/manifest.h"
18 #include "extensions/common/manifest_constants.h"
19 #include "grit/extensions_strings.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "url/gurl.h"
25 namespace extensions {
27 namespace {
29 scoped_refptr<Extension> LoadExtensionManifest(
30 base::DictionaryValue* manifest,
31 const base::FilePath& manifest_dir,
32 Manifest::Location location,
33 int extra_flags,
34 std::string* error) {
35 scoped_refptr<Extension> extension =
36 Extension::Create(manifest_dir, location, *manifest, extra_flags, error);
37 return extension;
40 scoped_refptr<Extension> LoadExtensionManifest(
41 const std::string& manifest_value,
42 const base::FilePath& manifest_dir,
43 Manifest::Location location,
44 int extra_flags,
45 std::string* error) {
46 JSONStringValueSerializer serializer(manifest_value);
47 scoped_ptr<base::Value> result(serializer.Deserialize(NULL, error));
48 if (!result.get())
49 return NULL;
50 CHECK_EQ(base::Value::TYPE_DICTIONARY, result->GetType());
51 return LoadExtensionManifest(
52 static_cast<base::DictionaryValue*>(result.get()),
53 manifest_dir,
54 location,
55 extra_flags,
56 error);
59 } // namespace
61 typedef testing::Test FileUtilTest;
63 TEST_F(FileUtilTest, InstallUninstallGarbageCollect) {
64 base::ScopedTempDir temp;
65 ASSERT_TRUE(temp.CreateUniqueTempDir());
67 // Create a source extension.
68 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
69 std::string version("1.0");
70 base::FilePath src = temp.path().AppendASCII(extension_id);
71 ASSERT_TRUE(base::CreateDirectory(src));
73 // Create a extensions tree.
74 base::FilePath all_extensions = temp.path().AppendASCII("extensions");
75 ASSERT_TRUE(base::CreateDirectory(all_extensions));
77 // Install in empty directory. Should create parent directories as needed.
78 base::FilePath version_1 =
79 file_util::InstallExtension(src, extension_id, version, all_extensions);
80 ASSERT_EQ(
81 version_1.value(),
82 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_0").value());
83 ASSERT_TRUE(base::DirectoryExists(version_1));
85 // Should have moved the source.
86 ASSERT_FALSE(base::DirectoryExists(src));
88 // Install again. Should create a new one with different name.
89 ASSERT_TRUE(base::CreateDirectory(src));
90 base::FilePath version_2 =
91 file_util::InstallExtension(src, extension_id, version, all_extensions);
92 ASSERT_EQ(
93 version_2.value(),
94 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_1").value());
95 ASSERT_TRUE(base::DirectoryExists(version_2));
97 // Should have moved the source.
98 ASSERT_FALSE(base::DirectoryExists(src));
100 // Install yet again. Should create a new one with a different name.
101 ASSERT_TRUE(base::CreateDirectory(src));
102 base::FilePath version_3 =
103 file_util::InstallExtension(src, extension_id, version, all_extensions);
104 ASSERT_EQ(
105 version_3.value(),
106 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_2").value());
107 ASSERT_TRUE(base::DirectoryExists(version_3));
109 // Uninstall. Should remove entire extension subtree.
110 file_util::UninstallExtension(all_extensions, extension_id);
111 ASSERT_FALSE(base::DirectoryExists(version_1.DirName()));
112 ASSERT_FALSE(base::DirectoryExists(version_2.DirName()));
113 ASSERT_FALSE(base::DirectoryExists(version_3.DirName()));
114 ASSERT_TRUE(base::DirectoryExists(all_extensions));
117 TEST_F(FileUtilTest, LoadExtensionWithValidLocales) {
118 base::FilePath install_dir;
119 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir));
120 install_dir = install_dir.AppendASCII("extension_with_locales");
122 std::string error;
123 scoped_refptr<Extension> extension(file_util::LoadExtension(
124 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
125 ASSERT_TRUE(extension.get() != NULL);
126 EXPECT_EQ("The first extension that I made.", extension->description());
129 TEST_F(FileUtilTest, LoadExtensionWithoutLocalesFolder) {
130 base::FilePath install_dir;
131 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir));
132 install_dir = install_dir.AppendASCII("extension_without_locales");
134 std::string error;
135 scoped_refptr<Extension> extension(file_util::LoadExtension(
136 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
137 ASSERT_FALSE(extension.get() == NULL);
138 EXPECT_TRUE(error.empty());
141 TEST_F(FileUtilTest, CheckIllegalFilenamesNoUnderscores) {
142 base::ScopedTempDir temp;
143 ASSERT_TRUE(temp.CreateUniqueTempDir());
145 base::FilePath src_path = temp.path().AppendASCII("some_dir");
146 ASSERT_TRUE(base::CreateDirectory(src_path));
148 std::string data = "{ \"name\": { \"message\": \"foobar\" } }";
149 ASSERT_TRUE(base::WriteFile(
150 src_path.AppendASCII("some_file.txt"), data.c_str(), data.length()));
151 std::string error;
152 EXPECT_TRUE(file_util::CheckForIllegalFilenames(temp.path(), &error));
155 TEST_F(FileUtilTest, CheckIllegalFilenamesOnlyReserved) {
156 base::ScopedTempDir temp;
157 ASSERT_TRUE(temp.CreateUniqueTempDir());
159 const base::FilePath::CharType* folders[] = {
160 extensions::kLocaleFolder, extensions::kPlatformSpecificFolder};
162 for (size_t i = 0; i < arraysize(folders); i++) {
163 base::FilePath src_path = temp.path().Append(folders[i]);
164 ASSERT_TRUE(base::CreateDirectory(src_path));
167 std::string error;
168 EXPECT_TRUE(file_util::CheckForIllegalFilenames(temp.path(), &error));
171 TEST_F(FileUtilTest, CheckIllegalFilenamesReservedAndIllegal) {
172 base::ScopedTempDir temp;
173 ASSERT_TRUE(temp.CreateUniqueTempDir());
175 base::FilePath src_path = temp.path().Append(extensions::kLocaleFolder);
176 ASSERT_TRUE(base::CreateDirectory(src_path));
178 src_path = temp.path().AppendASCII("_some_dir");
179 ASSERT_TRUE(base::CreateDirectory(src_path));
181 std::string error;
182 EXPECT_FALSE(file_util::CheckForIllegalFilenames(temp.path(), &error));
185 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnMissingManifest) {
186 base::FilePath install_dir;
187 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir));
188 install_dir =
189 install_dir.AppendASCII("file_util").AppendASCII("missing_manifest");
191 std::string error;
192 scoped_refptr<Extension> extension(file_util::LoadExtension(
193 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
194 ASSERT_TRUE(extension.get() == NULL);
195 ASSERT_FALSE(error.empty());
196 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str());
199 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnBadManifest) {
200 base::FilePath install_dir;
201 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir));
202 install_dir =
203 install_dir.AppendASCII("file_util").AppendASCII("bad_manifest");
205 std::string error;
206 scoped_refptr<Extension> extension(file_util::LoadExtension(
207 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
208 ASSERT_TRUE(extension.get() == NULL);
209 ASSERT_FALSE(error.empty());
210 ASSERT_STREQ(
211 "Manifest is not valid JSON. "
212 "Line: 2, column: 16, Syntax error.",
213 error.c_str());
216 TEST_F(FileUtilTest, ValidateThemeUTF8) {
217 base::ScopedTempDir temp;
218 ASSERT_TRUE(temp.CreateUniqueTempDir());
220 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
221 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png";
222 base::FilePath non_ascii_path =
223 temp.path().Append(base::FilePath::FromUTF8Unsafe(non_ascii_file));
224 base::WriteFile(non_ascii_path, "", 0);
226 std::string kManifest = base::StringPrintf(
227 "{ \"name\": \"Test\", \"version\": \"1.0\", "
228 " \"theme\": { \"images\": { \"theme_frame\": \"%s\" } }"
229 "}",
230 non_ascii_file.c_str());
231 std::string error;
232 scoped_refptr<Extension> extension = LoadExtensionManifest(
233 kManifest, temp.path(), Manifest::UNPACKED, 0, &error);
234 ASSERT_TRUE(extension.get()) << error;
236 std::vector<extensions::InstallWarning> warnings;
237 EXPECT_TRUE(file_util::ValidateExtension(extension.get(), &error, &warnings))
238 << error;
239 EXPECT_EQ(0U, warnings.size());
242 TEST_F(FileUtilTest, BackgroundScriptsMustExist) {
243 base::ScopedTempDir temp;
244 ASSERT_TRUE(temp.CreateUniqueTempDir());
246 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
247 value->SetString("name", "test");
248 value->SetString("version", "1");
249 value->SetInteger("manifest_version", 1);
251 base::ListValue* scripts = new base::ListValue();
252 scripts->Append(new base::StringValue("foo.js"));
253 value->Set("background.scripts", scripts);
255 std::string error;
256 std::vector<extensions::InstallWarning> warnings;
257 scoped_refptr<Extension> extension = LoadExtensionManifest(
258 value.get(), temp.path(), Manifest::UNPACKED, 0, &error);
259 ASSERT_TRUE(extension.get()) << error;
261 EXPECT_FALSE(
262 file_util::ValidateExtension(extension.get(), &error, &warnings));
263 EXPECT_EQ(
264 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED,
265 base::ASCIIToUTF16("foo.js")),
266 error);
267 EXPECT_EQ(0U, warnings.size());
269 scripts->Clear();
270 scripts->Append(new base::StringValue("http://google.com/foo.js"));
272 extension = LoadExtensionManifest(
273 value.get(), temp.path(), Manifest::UNPACKED, 0, &error);
274 ASSERT_TRUE(extension.get()) << error;
276 warnings.clear();
277 EXPECT_FALSE(
278 file_util::ValidateExtension(extension.get(), &error, &warnings));
279 EXPECT_EQ(
280 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED,
281 base::ASCIIToUTF16("http://google.com/foo.js")),
282 error);
283 EXPECT_EQ(0U, warnings.size());
286 // Private key, generated by Chrome specifically for this test, and
287 // never used elsewhere.
288 const char private_key[] =
289 "-----BEGIN PRIVATE KEY-----\n"
290 "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKt02SR0FYaYy6fpW\n"
291 "MAA+kU1BgK3d+OmmWfdr+JATIjhRkyeSF4lTd/71JQsyKqPzYkQPi3EeROWM+goTv\n"
292 "EhJqq07q63BolpsFmlV+S4ny+sBA2B4aWwRYXlBWikdrQSA0mJMzvEHc6nKzBgXik\n"
293 "QSVbyyBNAsxlDB9WaCxRVOpK3AgMBAAECgYBGvSPlrVtAOAQ2V8j9FqorKZA8SLPX\n"
294 "IeJC/yzU3RB2nPMjI17aMOvrUHxJUhzMeh4jwabVvSzzDtKFozPGupW3xaI8sQdi2\n"
295 "WWMTQIk/Q9HHDWoQ9qA6SwX2qWCc5SyjCKqVp78ye+000kqTJYjBsDgXeAlzKcx2B\n"
296 "4GAAeWonDdkQJBANNb8wrqNWFn7DqyQTfELzcRTRnqQ/r1pdeJo6obzbnwGnlqe3t\n"
297 "KhLjtJNIGrQg5iC0OVLWFuvPJs0t3z62A1ckCQQDPq2JZuwTwu5Pl4DJ0r9O1FdqN\n"
298 "JgqPZyMptokCDQ3khLLGakIu+TqB9YtrzI69rJMSG2Egb+6McaDX+dh3XmR/AkB9t\n"
299 "xJf6qDnmA2td/tMtTc0NOk8Qdg/fD8xbZ/YfYMnVoYYs9pQoilBaWRePDRNURMLYZ\n"
300 "vHAI0Llmw7tj7jv17pAkEAz44uXRpjRKtllUIvi5pUENAHwDz+HvdpGH68jpU3hmb\n"
301 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n"
302 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n"
303 "g==\n"
304 "-----END PRIVATE KEY-----\n";
306 TEST_F(FileUtilTest, FindPrivateKeyFiles) {
307 base::ScopedTempDir temp;
308 ASSERT_TRUE(temp.CreateUniqueTempDir());
310 base::FilePath src_path = temp.path().AppendASCII("some_dir");
311 ASSERT_TRUE(base::CreateDirectory(src_path));
313 ASSERT_TRUE(base::WriteFile(
314 src_path.AppendASCII("a_key.pem"), private_key, arraysize(private_key)));
315 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("second_key.pem"),
316 private_key,
317 arraysize(private_key)));
318 // Shouldn't find a key with a different extension.
319 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("key.diff_ext"),
320 private_key,
321 arraysize(private_key)));
322 // Shouldn't find a key that isn't parsable.
323 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("unparsable_key.pem"),
324 private_key,
325 arraysize(private_key) - 30));
326 std::vector<base::FilePath> private_keys =
327 file_util::FindPrivateKeyFiles(temp.path());
328 EXPECT_EQ(2U, private_keys.size());
329 EXPECT_THAT(private_keys,
330 testing::Contains(src_path.AppendASCII("a_key.pem")));
331 EXPECT_THAT(private_keys,
332 testing::Contains(src_path.AppendASCII("second_key.pem")));
335 TEST_F(FileUtilTest, WarnOnPrivateKey) {
336 base::ScopedTempDir temp;
337 ASSERT_TRUE(temp.CreateUniqueTempDir());
339 base::FilePath ext_path = temp.path().AppendASCII("ext_root");
340 ASSERT_TRUE(base::CreateDirectory(ext_path));
342 const char manifest[] =
343 "{\n"
344 " \"name\": \"Test Extension\",\n"
345 " \"version\": \"1.0\",\n"
346 " \"manifest_version\": 2,\n"
347 " \"description\": \"The first extension that I made.\"\n"
348 "}\n";
349 ASSERT_TRUE(base::WriteFile(
350 ext_path.AppendASCII("manifest.json"), manifest, strlen(manifest)));
351 ASSERT_TRUE(base::WriteFile(
352 ext_path.AppendASCII("a_key.pem"), private_key, strlen(private_key)));
354 std::string error;
355 scoped_refptr<Extension> extension(
356 file_util::LoadExtension(ext_path,
357 "the_id",
358 Manifest::EXTERNAL_PREF,
359 Extension::NO_FLAGS,
360 &error));
361 ASSERT_TRUE(extension.get()) << error;
362 ASSERT_EQ(1u, extension->install_warnings().size());
363 EXPECT_THAT(extension->install_warnings(),
364 testing::ElementsAre(testing::Field(
365 &extensions::InstallWarning::message,
366 testing::ContainsRegex(
367 "extension includes the key file.*ext_root.a_key.pem"))));
369 // Turn the warning into an error with ERROR_ON_PRIVATE_KEY.
370 extension = file_util::LoadExtension(ext_path,
371 "the_id",
372 Manifest::EXTERNAL_PREF,
373 Extension::ERROR_ON_PRIVATE_KEY,
374 &error);
375 EXPECT_FALSE(extension.get());
376 EXPECT_THAT(error,
377 testing::ContainsRegex(
378 "extension includes the key file.*ext_root.a_key.pem"));
381 TEST_F(FileUtilTest, CheckZeroLengthIconFile) {
382 base::FilePath install_dir;
383 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir));
385 // Try to install an extension with a zero-length icon file.
386 base::FilePath ext_dir =
387 install_dir.AppendASCII("file_util").AppendASCII("bad_icon");
389 std::string error;
390 scoped_refptr<Extension> extension(file_util::LoadExtension(
391 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
392 EXPECT_TRUE(extension.get() == NULL);
393 EXPECT_STREQ("Could not load extension icon 'icon.png'.", error.c_str());
396 TEST_F(FileUtilTest, ExtensionURLToRelativeFilePath) {
397 #define URL_PREFIX "chrome-extension://extension-id/"
398 struct TestCase {
399 const char* url;
400 const char* expected_relative_path;
401 } test_cases[] = {
402 { URL_PREFIX "simple.html",
403 "simple.html" },
404 { URL_PREFIX "directory/to/file.html",
405 "directory/to/file.html" },
406 { URL_PREFIX "escape%20spaces.html",
407 "escape spaces.html" },
408 { URL_PREFIX "%C3%9Cber.html",
409 "\xC3\x9C" "ber.html" },
410 #if defined(OS_WIN)
411 { URL_PREFIX "C%3A/simple.html",
412 "" },
413 #endif
414 { URL_PREFIX "////simple.html",
415 "simple.html" },
416 { URL_PREFIX "/simple.html",
417 "simple.html" },
418 { URL_PREFIX "\\simple.html",
419 "simple.html" },
420 { URL_PREFIX "\\\\foo\\simple.html",
421 "foo/simple.html" },
423 #undef URL_PREFIX
425 for (size_t i = 0; i < arraysize(test_cases); ++i) {
426 GURL url(test_cases[i].url);
427 base::FilePath expected_path =
428 base::FilePath::FromUTF8Unsafe(test_cases[i].expected_relative_path);
429 base::FilePath actual_path =
430 extensions::file_util::ExtensionURLToRelativeFilePath(url);
431 EXPECT_FALSE(actual_path.IsAbsolute()) <<
432 " For the path " << actual_path.value();
433 EXPECT_EQ(expected_path.value(), actual_path.value()) <<
434 " For the path " << url;
438 TEST_F(FileUtilTest, ExtensionResourceURLToFilePath) {
439 // Setup filesystem for testing.
440 base::FilePath root_path;
441 ASSERT_TRUE(base::CreateNewTempDirectory(
442 base::FilePath::StringType(), &root_path));
443 root_path = base::MakeAbsoluteFilePath(root_path);
444 ASSERT_FALSE(root_path.empty());
446 base::FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname"));
447 ASSERT_TRUE(base::CreateDirectory(api_path));
449 const char data[] = "Test Data";
450 base::FilePath resource_path = api_path.Append(FILE_PATH_LITERAL("test.js"));
451 ASSERT_TRUE(base::WriteFile(resource_path, data, sizeof(data)));
452 resource_path = api_path.Append(FILE_PATH_LITERAL("escape spaces.js"));
453 ASSERT_TRUE(base::WriteFile(resource_path, data, sizeof(data)));
455 #ifdef FILE_PATH_USES_WIN_SEPARATORS
456 #define SEP "\\"
457 #else
458 #define SEP "/"
459 #endif
460 #define URL_PREFIX "chrome-extension-resource://"
461 struct TestCase {
462 const char* url;
463 const base::FilePath::CharType* expected_path;
464 } test_cases[] = {
465 { URL_PREFIX "apiname/test.js",
466 FILE_PATH_LITERAL("test.js") },
467 { URL_PREFIX "/apiname/test.js",
468 FILE_PATH_LITERAL("test.js") },
469 // Test % escape
470 { URL_PREFIX "apiname/%74%65st.js",
471 FILE_PATH_LITERAL("test.js") },
472 { URL_PREFIX "apiname/escape%20spaces.js",
473 FILE_PATH_LITERAL("escape spaces.js") },
474 // Test file does not exist.
475 { URL_PREFIX "apiname/directory/to/file.js",
476 NULL },
477 // Test apiname/../../test.js
478 { URL_PREFIX "apiname/../../test.js",
479 FILE_PATH_LITERAL("test.js") },
480 { URL_PREFIX "apiname/..%2F../test.js",
481 NULL },
482 { URL_PREFIX "apiname/f/../../../test.js",
483 FILE_PATH_LITERAL("test.js") },
484 { URL_PREFIX "apiname/f%2F..%2F..%2F../test.js",
485 NULL },
487 #undef SEP
488 #undef URL_PREFIX
490 for (size_t i = 0; i < arraysize(test_cases); ++i) {
491 GURL url(test_cases[i].url);
492 base::FilePath expected_path;
493 if (test_cases[i].expected_path)
494 expected_path = root_path.Append(FILE_PATH_LITERAL("apiname")).Append(
495 test_cases[i].expected_path);
496 base::FilePath actual_path =
497 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path);
498 EXPECT_EQ(expected_path.value(), actual_path.value()) <<
499 " For the path " << url;
501 // Remove temp files.
502 ASSERT_TRUE(base::DeleteFile(root_path, true));
505 } // namespace extensions