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