| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/browser/extensions/error_console/error_console.h" | 5 #include "chrome/browser/extensions/error_console/error_console.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "chrome/common/extensions/features/feature_channel.h" | 12 #include "chrome/common/extensions/features/feature_channel.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/crx_file/id_util.h" |
| 15 #include "extensions/browser/extension_error.h" | 16 #include "extensions/browser/extension_error.h" |
| 16 #include "extensions/browser/extension_error_test_util.h" | 17 #include "extensions/browser/extension_error_test_util.h" |
| 17 #include "extensions/browser/extension_registry.h" | 18 #include "extensions/browser/extension_registry.h" |
| 18 #include "extensions/common/constants.h" | 19 #include "extensions/common/constants.h" |
| 19 #include "extensions/common/extension_builder.h" | 20 #include "extensions/common/extension_builder.h" |
| 20 #include "extensions/common/feature_switch.h" | 21 #include "extensions/common/feature_switch.h" |
| 21 #include "extensions/common/id_util.h" | |
| 22 #include "extensions/common/value_builder.h" | 22 #include "extensions/common/value_builder.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace extensions { | 25 namespace extensions { |
| 26 | 26 |
| 27 using error_test_util::CreateNewManifestError; | 27 using error_test_util::CreateNewManifestError; |
| 28 using error_test_util::CreateNewRuntimeError; | 28 using error_test_util::CreateNewRuntimeError; |
| 29 | 29 |
| 30 class ErrorConsoleUnitTest : public testing::Test { | 30 class ErrorConsoleUnitTest : public testing::Test { |
| 31 public: | 31 public: |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 registry->TriggerOnUnloaded(adt, UnloadedExtensionInfo::REASON_DISABLE); | 121 registry->TriggerOnUnloaded(adt, UnloadedExtensionInfo::REASON_DISABLE); |
| 122 EXPECT_FALSE(error_console_->enabled()); | 122 EXPECT_FALSE(error_console_->enabled()); |
| 123 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); | 123 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 124 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); | 124 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Test that errors are successfully reported. This is a simple test, since it | 127 // Test that errors are successfully reported. This is a simple test, since it |
| 128 // is tested more thoroughly in extensions/browser/error_map_unittest.cc | 128 // is tested more thoroughly in extensions/browser/error_map_unittest.cc |
| 129 TEST_F(ErrorConsoleUnitTest, ReportErrors) { | 129 TEST_F(ErrorConsoleUnitTest, ReportErrors) { |
| 130 const size_t kNumTotalErrors = 6; | 130 const size_t kNumTotalErrors = 6; |
| 131 const std::string kId = id_util::GenerateId("id"); | 131 const std::string kId = crx_file::id_util::GenerateId("id"); |
| 132 error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, | 132 error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, |
| 133 true); | 133 true); |
| 134 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); | 134 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); |
| 135 | 135 |
| 136 for (size_t i = 0; i < kNumTotalErrors; ++i) { | 136 for (size_t i = 0; i < kNumTotalErrors; ++i) { |
| 137 error_console_->ReportError( | 137 error_console_->ReportError( |
| 138 CreateNewManifestError(kId, base::UintToString(i))); | 138 CreateNewManifestError(kId, base::UintToString(i))); |
| 139 } | 139 } |
| 140 | 140 |
| 141 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); | 141 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); |
| 142 } | 142 } |
| 143 | 143 |
| 144 TEST_F(ErrorConsoleUnitTest, DontStoreErrorsWithoutEnablingType) { | 144 TEST_F(ErrorConsoleUnitTest, DontStoreErrorsWithoutEnablingType) { |
| 145 // Disable default runtime error reporting, and enable default manifest error | 145 // Disable default runtime error reporting, and enable default manifest error |
| 146 // reporting. | 146 // reporting. |
| 147 error_console_->set_default_reporting_for_test(ExtensionError::RUNTIME_ERROR, | 147 error_console_->set_default_reporting_for_test(ExtensionError::RUNTIME_ERROR, |
| 148 false); | 148 false); |
| 149 error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, | 149 error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, |
| 150 true); | 150 true); |
| 151 | 151 |
| 152 const std::string kId = id_util::GenerateId("id"); | 152 const std::string kId = crx_file::id_util::GenerateId("id"); |
| 153 | 153 |
| 154 // Try to report a runtime error - it should be ignored. | 154 // Try to report a runtime error - it should be ignored. |
| 155 error_console_->ReportError(CreateNewRuntimeError(kId, "a")); | 155 error_console_->ReportError(CreateNewRuntimeError(kId, "a")); |
| 156 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); | 156 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); |
| 157 | 157 |
| 158 // Check that manifest errors are reported successfully. | 158 // Check that manifest errors are reported successfully. |
| 159 error_console_->ReportError(CreateNewManifestError(kId, "b")); | 159 error_console_->ReportError(CreateNewManifestError(kId, "b")); |
| 160 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); | 160 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); |
| 161 | 161 |
| 162 // We should still ignore runtime errors. | 162 // We should still ignore runtime errors. |
| 163 error_console_->ReportError(CreateNewRuntimeError(kId, "c")); | 163 error_console_->ReportError(CreateNewRuntimeError(kId, "c")); |
| 164 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); | 164 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); |
| 165 | 165 |
| 166 // Enable runtime errors specifically for this extension, and disable the use | 166 // Enable runtime errors specifically for this extension, and disable the use |
| 167 // of the default mask. | 167 // of the default mask. |
| 168 error_console_->SetReportingForExtension( | 168 error_console_->SetReportingForExtension( |
| 169 kId, ExtensionError::RUNTIME_ERROR, true); | 169 kId, ExtensionError::RUNTIME_ERROR, true); |
| 170 | 170 |
| 171 // We should now accept runtime and manifest errors. | 171 // We should now accept runtime and manifest errors. |
| 172 error_console_->ReportError(CreateNewManifestError(kId, "d")); | 172 error_console_->ReportError(CreateNewManifestError(kId, "d")); |
| 173 ASSERT_EQ(2u, error_console_->GetErrorsForExtension(kId).size()); | 173 ASSERT_EQ(2u, error_console_->GetErrorsForExtension(kId).size()); |
| 174 error_console_->ReportError(CreateNewRuntimeError(kId, "e")); | 174 error_console_->ReportError(CreateNewRuntimeError(kId, "e")); |
| 175 ASSERT_EQ(3u, error_console_->GetErrorsForExtension(kId).size()); | 175 ASSERT_EQ(3u, error_console_->GetErrorsForExtension(kId).size()); |
| 176 | 176 |
| 177 // All other extensions should still use the default mask, and ignore runtime | 177 // All other extensions should still use the default mask, and ignore runtime |
| 178 // errors but report manifest errors. | 178 // errors but report manifest errors. |
| 179 const std::string kId2 = id_util::GenerateId("id2"); | 179 const std::string kId2 = crx_file::id_util::GenerateId("id2"); |
| 180 error_console_->ReportError(CreateNewRuntimeError(kId2, "f")); | 180 error_console_->ReportError(CreateNewRuntimeError(kId2, "f")); |
| 181 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId2).size()); | 181 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId2).size()); |
| 182 error_console_->ReportError(CreateNewManifestError(kId2, "g")); | 182 error_console_->ReportError(CreateNewManifestError(kId2, "g")); |
| 183 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId2).size()); | 183 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId2).size()); |
| 184 | 184 |
| 185 // By reverting to default reporting, we should again allow manifest errors, | 185 // By reverting to default reporting, we should again allow manifest errors, |
| 186 // but not runtime errors. | 186 // but not runtime errors. |
| 187 error_console_->UseDefaultReportingForExtension(kId); | 187 error_console_->UseDefaultReportingForExtension(kId); |
| 188 error_console_->ReportError(CreateNewManifestError(kId, "h")); | 188 error_console_->ReportError(CreateNewManifestError(kId, "h")); |
| 189 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); | 189 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); |
| 190 error_console_->ReportError(CreateNewRuntimeError(kId, "i")); | 190 error_console_->ReportError(CreateNewRuntimeError(kId, "i")); |
| 191 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); | 191 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); |
| 192 } | 192 } |
| 193 | 193 |
| 194 // Test that we only store errors by default for unpacked extensions, and that | 194 // Test that we only store errors by default for unpacked extensions, and that |
| 195 // assigning a preference to any extension overrides the defaults. | 195 // assigning a preference to any extension overrides the defaults. |
| 196 TEST_F(ErrorConsoleUnitTest, TestDefaultStoringPrefs) { | 196 TEST_F(ErrorConsoleUnitTest, TestDefaultStoringPrefs) { |
| 197 // For this, we need actual extensions. | 197 // For this, we need actual extensions. |
| 198 scoped_refptr<const Extension> unpacked_extension = | 198 scoped_refptr<const Extension> unpacked_extension = |
| 199 ExtensionBuilder() | 199 ExtensionBuilder() |
| 200 .SetManifest(DictionaryBuilder().Set("name", "unpacked") | 200 .SetManifest(DictionaryBuilder() |
| 201 .Set("version", "0.0.1") | 201 .Set("name", "unpacked") |
| 202 .Set("manifest_version", 2) | 202 .Set("version", "0.0.1") |
| 203 .Build()) | 203 .Set("manifest_version", 2) |
| 204 .Build()) |
| 204 .SetLocation(Manifest::UNPACKED) | 205 .SetLocation(Manifest::UNPACKED) |
| 205 .SetID(id_util::GenerateId("unpacked")) | 206 .SetID(crx_file::id_util::GenerateId("unpacked")) |
| 206 .Build(); | 207 .Build(); |
| 207 scoped_refptr<const Extension> packed_extension = | 208 scoped_refptr<const Extension> packed_extension = |
| 208 ExtensionBuilder() | 209 ExtensionBuilder() |
| 209 .SetManifest(DictionaryBuilder().Set("name", "packed") | 210 .SetManifest(DictionaryBuilder() |
| 210 .Set("version", "0.0.1") | 211 .Set("name", "packed") |
| 211 .Set("manifest_version", 2) | 212 .Set("version", "0.0.1") |
| 212 .Build()) | 213 .Set("manifest_version", 2) |
| 214 .Build()) |
| 213 .SetLocation(Manifest::INTERNAL) | 215 .SetLocation(Manifest::INTERNAL) |
| 214 .SetID(id_util::GenerateId("packed")) | 216 .SetID(crx_file::id_util::GenerateId("packed")) |
| 215 .Build(); | 217 .Build(); |
| 216 | 218 |
| 217 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); | 219 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); |
| 218 registry->AddEnabled(unpacked_extension); | 220 registry->AddEnabled(unpacked_extension); |
| 219 registry->AddEnabled(packed_extension); | 221 registry->AddEnabled(packed_extension); |
| 220 | 222 |
| 221 // We should start with a clean slate. | 223 // We should start with a clean slate. |
| 222 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( | 224 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( |
| 223 unpacked_extension->id()).size()); | 225 unpacked_extension->id()).size()); |
| 224 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( | 226 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 error_console_->ReportError( | 267 error_console_->ReportError( |
| 266 CreateNewRuntimeError(packed_extension->id(), "runtime error 4")); | 268 CreateNewRuntimeError(packed_extension->id(), "runtime error 4")); |
| 267 EXPECT_EQ(2u, // We should still have the first two errors. | 269 EXPECT_EQ(2u, // We should still have the first two errors. |
| 268 error_console_->GetErrorsForExtension( | 270 error_console_->GetErrorsForExtension( |
| 269 unpacked_extension->id()).size()); | 271 unpacked_extension->id()).size()); |
| 270 EXPECT_FALSE(error_console_->IsReportingEnabledForExtension( | 272 EXPECT_FALSE(error_console_->IsReportingEnabledForExtension( |
| 271 unpacked_extension->id())); | 273 unpacked_extension->id())); |
| 272 } | 274 } |
| 273 | 275 |
| 274 } // namespace extensions | 276 } // namespace extensions |
| OLD | NEW |