OLD | NEW |
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/files/scoped_temp_dir.h" |
8 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
15 #include "chrome/common/extensions/extension_manifest_constants.h" | 15 #include "chrome/common/extensions/extension_manifest_constants.h" |
16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
20 | 20 |
21 using extensions::Extension; | 21 using extensions::Extension; |
22 | 22 |
23 namespace keys = extension_manifest_keys; | 23 namespace keys = extension_manifest_keys; |
24 | 24 |
25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
26 // http://crbug.com/106381 | 26 // http://crbug.com/106381 |
27 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect | 27 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect |
28 #endif | 28 #endif |
29 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) { | 29 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) { |
30 ScopedTempDir temp; | 30 base::ScopedTempDir temp; |
31 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 31 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
32 | 32 |
33 // Create a source extension. | 33 // Create a source extension. |
34 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | 34 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
35 std::string version("1.0"); | 35 std::string version("1.0"); |
36 FilePath src = temp.path().AppendASCII(extension_id); | 36 FilePath src = temp.path().AppendASCII(extension_id); |
37 ASSERT_TRUE(file_util::CreateDirectory(src)); | 37 ASSERT_TRUE(file_util::CreateDirectory(src)); |
38 | 38 |
39 // Create a extensions tree. | 39 // Create a extensions tree. |
40 FilePath all_extensions = temp.path().AppendASCII("extensions"); | 40 FilePath all_extensions = temp.path().AppendASCII("extensions"); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 ASSERT_FALSE(extension == NULL); | 128 ASSERT_FALSE(extension == NULL); |
129 EXPECT_TRUE(error.empty()); | 129 EXPECT_TRUE(error.empty()); |
130 } | 130 } |
131 | 131 |
132 #if defined(OS_WIN) | 132 #if defined(OS_WIN) |
133 // http://crbug.com/106381 | 133 // http://crbug.com/106381 |
134 #define CheckIllegalFilenamesNoUnderscores \ | 134 #define CheckIllegalFilenamesNoUnderscores \ |
135 DISABLED_CheckIllegalFilenamesNoUnderscores | 135 DISABLED_CheckIllegalFilenamesNoUnderscores |
136 #endif | 136 #endif |
137 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { | 137 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { |
138 ScopedTempDir temp; | 138 base::ScopedTempDir temp; |
139 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 139 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
140 | 140 |
141 FilePath src_path = temp.path().AppendASCII("some_dir"); | 141 FilePath src_path = temp.path().AppendASCII("some_dir"); |
142 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 142 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
143 | 143 |
144 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; | 144 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; |
145 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), | 145 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), |
146 data.c_str(), data.length())); | 146 data.c_str(), data.length())); |
147 std::string error; | 147 std::string error; |
148 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 148 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
149 &error)); | 149 &error)); |
150 } | 150 } |
151 | 151 |
152 #if defined(OS_WIN) | 152 #if defined(OS_WIN) |
153 // http://crbug.com/106381 | 153 // http://crbug.com/106381 |
154 #define CheckIllegalFilenamesOnlyReserved \ | 154 #define CheckIllegalFilenamesOnlyReserved \ |
155 DISABLED_CheckIllegalFilenamesOnlyReserved | 155 DISABLED_CheckIllegalFilenamesOnlyReserved |
156 #endif | 156 #endif |
157 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { | 157 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { |
158 ScopedTempDir temp; | 158 base::ScopedTempDir temp; |
159 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 159 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
160 | 160 |
161 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 161 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
162 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 162 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
163 | 163 |
164 std::string error; | 164 std::string error; |
165 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 165 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
166 &error)); | 166 &error)); |
167 } | 167 } |
168 | 168 |
169 #if defined(OS_WIN) | 169 #if defined(OS_WIN) |
170 // http://crbug.com/106381 | 170 // http://crbug.com/106381 |
171 #define CheckIllegalFilenamesReservedAndIllegal \ | 171 #define CheckIllegalFilenamesReservedAndIllegal \ |
172 DISABLED_CheckIllegalFilenamesReservedAndIllegal | 172 DISABLED_CheckIllegalFilenamesReservedAndIllegal |
173 #endif | 173 #endif |
174 TEST(ExtensionFileUtil, CheckIllegalFilenamesReservedAndIllegal) { | 174 TEST(ExtensionFileUtil, CheckIllegalFilenamesReservedAndIllegal) { |
175 ScopedTempDir temp; | 175 base::ScopedTempDir temp; |
176 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 176 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
177 | 177 |
178 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 178 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
179 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 179 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
180 | 180 |
181 src_path = temp.path().AppendASCII("_some_dir"); | 181 src_path = temp.path().AppendASCII("_some_dir"); |
182 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 182 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
183 | 183 |
184 std::string error; | 184 std::string error; |
185 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 185 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 location, | 375 location, |
376 extra_flags, | 376 extra_flags, |
377 error); | 377 error); |
378 } | 378 } |
379 | 379 |
380 #if defined(OS_WIN) | 380 #if defined(OS_WIN) |
381 // http://crbug.com/108279 | 381 // http://crbug.com/108279 |
382 #define ValidateThemeUTF8 DISABLED_ValidateThemeUTF8 | 382 #define ValidateThemeUTF8 DISABLED_ValidateThemeUTF8 |
383 #endif | 383 #endif |
384 TEST(ExtensionFileUtil, ValidateThemeUTF8) { | 384 TEST(ExtensionFileUtil, ValidateThemeUTF8) { |
385 ScopedTempDir temp; | 385 base::ScopedTempDir temp; |
386 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 386 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
387 | 387 |
388 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. | 388 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. |
389 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; | 389 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; |
390 FilePath non_ascii_path = temp.path().Append(FilePath::FromUTF8Unsafe( | 390 FilePath non_ascii_path = temp.path().Append(FilePath::FromUTF8Unsafe( |
391 non_ascii_file)); | 391 non_ascii_file)); |
392 file_util::WriteFile(non_ascii_path, "", 0); | 392 file_util::WriteFile(non_ascii_path, "", 0); |
393 | 393 |
394 std::string kManifest = | 394 std::string kManifest = |
395 base::StringPrintf( | 395 base::StringPrintf( |
(...skipping 12 matching lines...) Expand all Loading... |
408 EXPECT_EQ(0U, warnings.size()); | 408 EXPECT_EQ(0U, warnings.size()); |
409 } | 409 } |
410 | 410 |
411 #if defined(OS_WIN) | 411 #if defined(OS_WIN) |
412 // This test hangs on Windows sometimes. http://crbug.com/110279 | 412 // This test hangs on Windows sometimes. http://crbug.com/110279 |
413 #define MAYBE_BackgroundScriptsMustExist DISABLED_BackgroundScriptsMustExist | 413 #define MAYBE_BackgroundScriptsMustExist DISABLED_BackgroundScriptsMustExist |
414 #else | 414 #else |
415 #define MAYBE_BackgroundScriptsMustExist BackgroundScriptsMustExist | 415 #define MAYBE_BackgroundScriptsMustExist BackgroundScriptsMustExist |
416 #endif | 416 #endif |
417 TEST(ExtensionFileUtil, MAYBE_BackgroundScriptsMustExist) { | 417 TEST(ExtensionFileUtil, MAYBE_BackgroundScriptsMustExist) { |
418 ScopedTempDir temp; | 418 base::ScopedTempDir temp; |
419 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 419 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
420 | 420 |
421 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 421 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
422 value->SetString("name", "test"); | 422 value->SetString("name", "test"); |
423 value->SetString("version", "1"); | 423 value->SetString("version", "1"); |
424 value->SetInteger("manifest_version", 1); | 424 value->SetInteger("manifest_version", 1); |
425 | 425 |
426 ListValue* scripts = new ListValue(); | 426 ListValue* scripts = new ListValue(); |
427 scripts->Append(Value::CreateStringValue("foo.js")); | 427 scripts->Append(Value::CreateStringValue("foo.js")); |
428 value->Set("background.scripts", scripts); | 428 value->Set("background.scripts", scripts); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 "KhLjtJNIGrQg5iC0OVLWFuvPJs0t3z62A1ckCQQDPq2JZuwTwu5Pl4DJ0r9O1FdqN\n" | 471 "KhLjtJNIGrQg5iC0OVLWFuvPJs0t3z62A1ckCQQDPq2JZuwTwu5Pl4DJ0r9O1FdqN\n" |
472 "JgqPZyMptokCDQ3khLLGakIu+TqB9YtrzI69rJMSG2Egb+6McaDX+dh3XmR/AkB9t\n" | 472 "JgqPZyMptokCDQ3khLLGakIu+TqB9YtrzI69rJMSG2Egb+6McaDX+dh3XmR/AkB9t\n" |
473 "xJf6qDnmA2td/tMtTc0NOk8Qdg/fD8xbZ/YfYMnVoYYs9pQoilBaWRePDRNURMLYZ\n" | 473 "xJf6qDnmA2td/tMtTc0NOk8Qdg/fD8xbZ/YfYMnVoYYs9pQoilBaWRePDRNURMLYZ\n" |
474 "vHAI0Llmw7tj7jv17pAkEAz44uXRpjRKtllUIvi5pUENAHwDz+HvdpGH68jpU3hmb\n" | 474 "vHAI0Llmw7tj7jv17pAkEAz44uXRpjRKtllUIvi5pUENAHwDz+HvdpGH68jpU3hmb\n" |
475 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n" | 475 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n" |
476 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n" | 476 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n" |
477 "g==\n" | 477 "g==\n" |
478 "-----END PRIVATE KEY-----\n"; | 478 "-----END PRIVATE KEY-----\n"; |
479 | 479 |
480 TEST(ExtensionFileUtil, FindPrivateKeyFiles) { | 480 TEST(ExtensionFileUtil, FindPrivateKeyFiles) { |
481 ScopedTempDir temp; | 481 base::ScopedTempDir temp; |
482 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 482 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
483 | 483 |
484 FilePath src_path = temp.path().AppendASCII("some_dir"); | 484 FilePath src_path = temp.path().AppendASCII("some_dir"); |
485 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 485 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
486 | 486 |
487 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), | 487 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), |
488 private_key, arraysize(private_key))); | 488 private_key, arraysize(private_key))); |
489 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), | 489 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), |
490 private_key, arraysize(private_key))); | 490 private_key, arraysize(private_key))); |
491 // Shouldn't find a key with a different extension. | 491 // Shouldn't find a key with a different extension. |
492 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), | 492 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), |
493 private_key, arraysize(private_key))); | 493 private_key, arraysize(private_key))); |
494 // Shouldn't find a key that isn't parsable. | 494 // Shouldn't find a key that isn't parsable. |
495 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), | 495 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), |
496 private_key, arraysize(private_key) - 30)); | 496 private_key, arraysize(private_key) - 30)); |
497 std::vector<FilePath> private_keys = | 497 std::vector<FilePath> private_keys = |
498 extension_file_util::FindPrivateKeyFiles(temp.path()); | 498 extension_file_util::FindPrivateKeyFiles(temp.path()); |
499 EXPECT_EQ(2U, private_keys.size()); | 499 EXPECT_EQ(2U, private_keys.size()); |
500 EXPECT_THAT(private_keys, | 500 EXPECT_THAT(private_keys, |
501 testing::Contains(src_path.AppendASCII("a_key.pem"))); | 501 testing::Contains(src_path.AppendASCII("a_key.pem"))); |
502 EXPECT_THAT(private_keys, | 502 EXPECT_THAT(private_keys, |
503 testing::Contains(src_path.AppendASCII("second_key.pem"))); | 503 testing::Contains(src_path.AppendASCII("second_key.pem"))); |
504 } | 504 } |
505 | 505 |
506 TEST(ExtensionFileUtil, WarnOnPrivateKey) { | 506 TEST(ExtensionFileUtil, WarnOnPrivateKey) { |
507 ScopedTempDir temp; | 507 base::ScopedTempDir temp; |
508 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 508 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
509 | 509 |
510 FilePath ext_path = temp.path().AppendASCII("ext_root"); | 510 FilePath ext_path = temp.path().AppendASCII("ext_root"); |
511 ASSERT_TRUE(file_util::CreateDirectory(ext_path)); | 511 ASSERT_TRUE(file_util::CreateDirectory(ext_path)); |
512 | 512 |
513 const char manifest[] = | 513 const char manifest[] = |
514 "{\n" | 514 "{\n" |
515 " \"name\": \"Test Extension\",\n" | 515 " \"name\": \"Test Extension\",\n" |
516 " \"version\": \"1.0\",\n" | 516 " \"version\": \"1.0\",\n" |
517 " \"manifest_version\": 2,\n" | 517 " \"manifest_version\": 2,\n" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( | 584 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( |
585 ext_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 585 ext_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
586 ASSERT_TRUE(extension3 == NULL); | 586 ASSERT_TRUE(extension3 == NULL); |
587 ASSERT_STREQ("Could not load icon 'icon.png' for page action.", | 587 ASSERT_STREQ("Could not load icon 'icon.png' for page action.", |
588 error.c_str()); | 588 error.c_str()); |
589 } | 589 } |
590 | 590 |
591 // TODO(aa): More tests as motivation allows. Maybe steal some from | 591 // TODO(aa): More tests as motivation allows. Maybe steal some from |
592 // ExtensionService? Many of them could probably be tested here without the | 592 // ExtensionService? Many of them could probably be tested here without the |
593 // MessageLoop shenanigans. | 593 // MessageLoop shenanigans. |
OLD | NEW |