| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/scoped_temp_dir.h" | 8 #include "base/memory/scoped_temp_dir.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 FilePath install_dir; | 72 FilePath install_dir; |
| 73 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 73 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 74 install_dir = install_dir.AppendASCII("extensions") | 74 install_dir = install_dir.AppendASCII("extensions") |
| 75 .AppendASCII("good") | 75 .AppendASCII("good") |
| 76 .AppendASCII("Extensions") | 76 .AppendASCII("Extensions") |
| 77 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 77 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 78 .AppendASCII("1.0.0.0"); | 78 .AppendASCII("1.0.0.0"); |
| 79 | 79 |
| 80 std::string error; | 80 std::string error; |
| 81 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 81 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 82 install_dir, Extension::LOAD, false, true, &error)); | 82 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); |
| 83 ASSERT_TRUE(extension != NULL); | 83 ASSERT_TRUE(extension != NULL); |
| 84 EXPECT_EQ("The first extension that I made.", extension->description()); | 84 EXPECT_EQ("The first extension that I made.", extension->description()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { | 87 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { |
| 88 FilePath install_dir; | 88 FilePath install_dir; |
| 89 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 89 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 90 install_dir = install_dir.AppendASCII("extensions") | 90 install_dir = install_dir.AppendASCII("extensions") |
| 91 .AppendASCII("good") | 91 .AppendASCII("good") |
| 92 .AppendASCII("Extensions") | 92 .AppendASCII("Extensions") |
| 93 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") | 93 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") |
| 94 .AppendASCII("1.0"); | 94 .AppendASCII("1.0"); |
| 95 | 95 |
| 96 std::string error; | 96 std::string error; |
| 97 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 97 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 98 install_dir, Extension::LOAD, false, true, &error)); | 98 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); |
| 99 ASSERT_FALSE(extension == NULL); | 99 ASSERT_FALSE(extension == NULL); |
| 100 EXPECT_TRUE(error.empty()); | 100 EXPECT_TRUE(error.empty()); |
| 101 } | 101 } |
| 102 | 102 |
| 103 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { | 103 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { |
| 104 ScopedTempDir temp; | 104 ScopedTempDir temp; |
| 105 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 105 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 106 | 106 |
| 107 FilePath src_path = temp.path().AppendASCII("some_dir"); | 107 FilePath src_path = temp.path().AppendASCII("some_dir"); |
| 108 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 108 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 FilePath install_dir; | 146 FilePath install_dir; |
| 147 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 147 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 148 install_dir = install_dir.AppendASCII("extensions") | 148 install_dir = install_dir.AppendASCII("extensions") |
| 149 .AppendASCII("bad") | 149 .AppendASCII("bad") |
| 150 .AppendASCII("Extensions") | 150 .AppendASCII("Extensions") |
| 151 .AppendASCII("dddddddddddddddddddddddddddddddd") | 151 .AppendASCII("dddddddddddddddddddddddddddddddd") |
| 152 .AppendASCII("1.0"); | 152 .AppendASCII("1.0"); |
| 153 | 153 |
| 154 std::string error; | 154 std::string error; |
| 155 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 155 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 156 install_dir, Extension::LOAD, false, true, &error)); | 156 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); |
| 157 ASSERT_TRUE(extension == NULL); | 157 ASSERT_TRUE(extension == NULL); |
| 158 ASSERT_FALSE(error.empty()); | 158 ASSERT_FALSE(error.empty()); |
| 159 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); | 159 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); |
| 160 } | 160 } |
| 161 | 161 |
| 162 TEST(ExtensionFileUtil, LoadExtensionGivesHelpfullErrorOnBadManifest) { | 162 TEST(ExtensionFileUtil, LoadExtensionGivesHelpfullErrorOnBadManifest) { |
| 163 FilePath install_dir; | 163 FilePath install_dir; |
| 164 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 164 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 165 install_dir = install_dir.AppendASCII("extensions") | 165 install_dir = install_dir.AppendASCII("extensions") |
| 166 .AppendASCII("bad") | 166 .AppendASCII("bad") |
| 167 .AppendASCII("Extensions") | 167 .AppendASCII("Extensions") |
| 168 .AppendASCII("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") | 168 .AppendASCII("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") |
| 169 .AppendASCII("1.0"); | 169 .AppendASCII("1.0"); |
| 170 | 170 |
| 171 std::string error; | 171 std::string error; |
| 172 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 172 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 173 install_dir, Extension::LOAD, false, true, &error)); | 173 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); |
| 174 ASSERT_TRUE(extension == NULL); | 174 ASSERT_TRUE(extension == NULL); |
| 175 ASSERT_FALSE(error.empty()); | 175 ASSERT_FALSE(error.empty()); |
| 176 ASSERT_STREQ("Manifest is not valid JSON. " | 176 ASSERT_STREQ("Manifest is not valid JSON. " |
| 177 "Line: 2, column: 16, Syntax error.", error.c_str()); | 177 "Line: 2, column: 16, Syntax error.", error.c_str()); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST(ExtensionFileUtil, FailLoadingNonUTF8Scripts) { | 180 TEST(ExtensionFileUtil, FailLoadingNonUTF8Scripts) { |
| 181 FilePath install_dir; | 181 FilePath install_dir; |
| 182 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 182 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 183 install_dir = install_dir.AppendASCII("extensions") | 183 install_dir = install_dir.AppendASCII("extensions") |
| 184 .AppendASCII("bad") | 184 .AppendASCII("bad") |
| 185 .AppendASCII("bad_encoding"); | 185 .AppendASCII("bad_encoding"); |
| 186 | 186 |
| 187 std::string error; | 187 std::string error; |
| 188 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 188 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 189 install_dir, Extension::LOAD, false, true, &error)); | 189 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); |
| 190 ASSERT_TRUE(extension == NULL); | 190 ASSERT_TRUE(extension == NULL); |
| 191 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " | 191 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " |
| 192 "It isn't UTF-8 encoded.", error.c_str()); | 192 "It isn't UTF-8 encoded.", error.c_str()); |
| 193 } | 193 } |
| 194 | 194 |
| 195 #define URL_PREFIX "chrome-extension://extension-id/" | 195 #define URL_PREFIX "chrome-extension://extension-id/" |
| 196 | 196 |
| 197 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { | 197 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { |
| 198 struct TestCase { | 198 struct TestCase { |
| 199 const char* url; | 199 const char* url; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 EXPECT_FALSE(actual_path.IsAbsolute()) << | 234 EXPECT_FALSE(actual_path.IsAbsolute()) << |
| 235 " For the path " << actual_path.value(); | 235 " For the path " << actual_path.value(); |
| 236 EXPECT_EQ(expected_path.value(), actual_path.value()) << | 236 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
| 237 " For the path " << url; | 237 " For the path " << url; |
| 238 } | 238 } |
| 239 } | 239 } |
| 240 | 240 |
| 241 // TODO(aa): More tests as motivation allows. Maybe steal some from | 241 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 242 // ExtensionService? Many of them could probably be tested here without the | 242 // ExtensionService? Many of them could probably be tested here without the |
| 243 // MessageLoop shenanigans. | 243 // MessageLoop shenanigans. |
| OLD | NEW |