error_map_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2014 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.
  4. #include "extensions/browser/error_map.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/strings/string_number_conversions.h"
  9. #include "components/crx_file/id_util.h"
  10. #include "extensions/browser/extension_error.h"
  11. #include "extensions/browser/extension_error_test_util.h"
  12. #include "extensions/common/constants.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace extensions {
  15. using error_test_util::CreateNewRuntimeError;
  16. using error_test_util::CreateNewManifestError;
  17. class ErrorMapUnitTest : public testing::Test {
  18. public:
  19. ErrorMapUnitTest() { }
  20. ~ErrorMapUnitTest() override {}
  21. protected:
  22. ErrorMap errors_;
  23. };
  24. // Test adding errors, and removing them by reference, by incognito status,
  25. // and in bulk.
  26. TEST_F(ErrorMapUnitTest, AddAndRemoveErrors) {
  27. ASSERT_EQ(0u, errors_.size());
  28. const size_t kNumTotalErrors = 6;
  29. const size_t kNumNonIncognitoErrors = 3;
  30. const std::string kId = crx_file::id_util::GenerateId("id");
  31. // Populate with both incognito and non-incognito errors (evenly distributed).
  32. for (size_t i = 0; i < kNumTotalErrors; ++i) {
  33. ASSERT_TRUE(errors_.AddError(
  34. CreateNewRuntimeError(kId, base::NumberToString(i), i % 2 == 0)));
  35. }
  36. // There should only be one entry in the map, since errors are stored in lists
  37. // keyed by extension id.
  38. EXPECT_EQ(1u, errors_.size());
  39. EXPECT_EQ(kNumTotalErrors, errors_.GetErrorsForExtension(kId).size());
  40. // Remove the incognito errors; three errors should remain, and all should
  41. // be from non-incognito contexts.
  42. std::set<std::string> affected_ids;
  43. errors_.RemoveErrors(ErrorMap::Filter::IncognitoErrors(), &affected_ids);
  44. const ErrorList& list = errors_.GetErrorsForExtension(kId);
  45. EXPECT_EQ(kNumNonIncognitoErrors, list.size());
  46. for (size_t i = 0; i < list.size(); ++i)
  47. EXPECT_FALSE(list[i]->from_incognito());
  48. EXPECT_EQ(1u, affected_ids.size());
  49. EXPECT_TRUE(affected_ids.count(kId));
  50. // Add another error for a different extension id.
  51. const std::string kSecondId = crx_file::id_util::GenerateId("id2");
  52. EXPECT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo")));
  53. // There should be two entries now, one for each id, and there should be one
  54. // error for the second extension.
  55. EXPECT_EQ(2u, errors_.size());
  56. EXPECT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size());
  57. // Remove all errors for the second id.
  58. affected_ids.clear();
  59. errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtension(kSecondId),
  60. &affected_ids);
  61. EXPECT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size());
  62. // First extension should be unaffected.
  63. EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
  64. EXPECT_EQ(1u, affected_ids.size());
  65. EXPECT_TRUE(affected_ids.count(kSecondId));
  66. errors_.AddError(CreateNewManifestError(kId, "manifest error"));
  67. EXPECT_EQ(kNumNonIncognitoErrors + 1,
  68. errors_.GetErrorsForExtension(kId).size());
  69. errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithType(
  70. kId, ExtensionError::MANIFEST_ERROR), nullptr);
  71. EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
  72. const ExtensionError* added_error =
  73. errors_.AddError(CreateNewManifestError(kId, "manifest error2"));
  74. EXPECT_EQ(kNumNonIncognitoErrors + 1,
  75. errors_.GetErrorsForExtension(kId).size());
  76. std::set<int> ids;
  77. ids.insert(added_error->id());
  78. errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithIds(kId, ids),
  79. nullptr);
  80. EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
  81. // Remove remaining errors.
  82. errors_.RemoveAllErrors();
  83. EXPECT_EQ(0u, errors_.size());
  84. EXPECT_EQ(0u, errors_.GetErrorsForExtension(kId).size());
  85. }
  86. // Test that if we add enough errors, only the most recent
  87. // kMaxErrorsPerExtension are kept.
  88. TEST_F(ErrorMapUnitTest, ExcessiveErrorsGetCropped) {
  89. ASSERT_EQ(0u, errors_.size());
  90. // This constant matches one of the same name in error_console.cc.
  91. const size_t kMaxErrorsPerExtension = 100;
  92. const size_t kNumExtraErrors = 5;
  93. const std::string kId = crx_file::id_util::GenerateId("id");
  94. // Add new errors, with each error's message set to its number.
  95. for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) {
  96. ASSERT_TRUE(
  97. errors_.AddError(CreateNewRuntimeError(kId, base::NumberToString(i))));
  98. }
  99. ASSERT_EQ(1u, errors_.size());
  100. const ErrorList& list = errors_.GetErrorsForExtension(kId);
  101. ASSERT_EQ(kMaxErrorsPerExtension, list.size());
  102. // We should have popped off errors in the order they arrived, so the
  103. // first stored error should be the 6th reported (zero-based)...
  104. ASSERT_EQ(base::NumberToString16(kNumExtraErrors), list.front()->message());
  105. // ..and the last stored should be the 105th reported.
  106. ASSERT_EQ(
  107. base::NumberToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1),
  108. list.back()->message());
  109. }
  110. // Test to ensure that the error console will not add duplicate errors, but will
  111. // keep the latest version of an error.
  112. TEST_F(ErrorMapUnitTest, DuplicateErrorsAreReplaced) {
  113. ASSERT_EQ(0u, errors_.size());
  114. const std::string kId = crx_file::id_util::GenerateId("id");
  115. const size_t kNumErrors = 3u;
  116. // Report three errors.
  117. for (size_t i = 0; i < kNumErrors; ++i) {
  118. ASSERT_TRUE(
  119. errors_.AddError(CreateNewRuntimeError(kId, base::NumberToString(i))));
  120. }
  121. // Create an error identical to the second error reported, save its
  122. // location, and add it to the error map.
  123. std::unique_ptr<ExtensionError> runtime_error2 =
  124. CreateNewRuntimeError(kId, base::NumberToString(1u));
  125. const ExtensionError* weak_error = runtime_error2.get();
  126. ASSERT_TRUE(errors_.AddError(std::move(runtime_error2)));
  127. // We should only have three errors stored, since two of the four reported
  128. // were identical, and the older should have been replaced.
  129. ASSERT_EQ(1u, errors_.size());
  130. const ErrorList& list = errors_.GetErrorsForExtension(kId);
  131. ASSERT_EQ(kNumErrors, list.size());
  132. // The duplicate error should be the last reported (pointer comparison)...
  133. ASSERT_EQ(weak_error, list.back().get());
  134. // ... and should have two reported occurrences.
  135. ASSERT_EQ(2u, list.back()->occurrences());
  136. }
  137. } // namespace extensions