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/browser/win/enumerate_modules_model.h" | 5 #include "chrome/browser/win/enumerate_modules_model.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 typedef testing::Test EnumerateModulesTest; | 15 typedef testing::Test EnumerateModulesTest; |
16 | 16 |
17 // Set up some constants to use as default when creating the structs. | 17 // Set up some constants to use as default when creating the structs. |
18 static const ModuleEnumerator::ModuleType kType = | 18 static const ModuleEnumerator::ModuleType kType = |
19 ModuleEnumerator::LOADED_MODULE; | 19 ModuleEnumerator::LOADED_MODULE; |
20 | 20 |
21 static const ModuleEnumerator::ModuleStatus kStatus = | 21 static const ModuleEnumerator::ModuleStatus kStatus = |
22 ModuleEnumerator::NOT_MATCHED; | 22 ModuleEnumerator::NOT_MATCHED; |
23 | 23 |
24 static const ModuleEnumerator::RecommendedAction kAction = | 24 static const ModuleEnumerator::RecommendedAction kAction = |
25 ModuleEnumerator::NONE; | 25 ModuleEnumerator::NONE; |
26 | 26 |
27 static const ModuleEnumerator::OperatingSystem kOs = | |
28 ModuleEnumerator::ALL; | |
29 | |
30 // This is a list of test cases to normalize. | 27 // This is a list of test cases to normalize. |
31 static const struct NormalizationEntryList { | 28 static const struct NormalizationEntryList { |
32 ModuleEnumerator::Module test_case; | 29 ModuleEnumerator::Module test_case; |
33 ModuleEnumerator::Module expected; | 30 ModuleEnumerator::Module expected; |
34 } kNormalizationTestCases[] = { | 31 } kNormalizationTestCases[] = { |
35 { | 32 { |
36 // Only path normalization needed. | 33 // Only path normalization needed. |
37 {kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", | 34 {kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", |
38 L"Sig", kAction}, | 35 L"Sig", kAction}, |
39 {kType, kStatus, L"c:\\foo\\", L"bar.dll", L"Prod", L"Desc", L"1.0", | 36 {kType, kStatus, L"c:\\foo\\", L"bar.dll", L"Prod", L"Desc", L"1.0", |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 const ModuleEnumerator::Module kStandardModule = | 82 const ModuleEnumerator::Module kStandardModule = |
86 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"Sig", | 83 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"Sig", |
87 ModuleEnumerator::NONE }; | 84 ModuleEnumerator::NONE }; |
88 const ModuleEnumerator::Module kStandardModuleNoDescription = | 85 const ModuleEnumerator::Module kStandardModuleNoDescription = |
89 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"", L"1.0", L"Sig", | 86 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"", L"1.0", L"Sig", |
90 ModuleEnumerator::NONE }; | 87 ModuleEnumerator::NONE }; |
91 const ModuleEnumerator::Module kStandardModuleNoSignature = | 88 const ModuleEnumerator::Module kStandardModuleNoSignature = |
92 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"", | 89 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"", |
93 ModuleEnumerator::NONE }; | 90 ModuleEnumerator::NONE }; |
94 | 91 |
95 // Name, location, description and signature are compared by hashing. | |
96 static const char kMatchName[] = "88e8c9e0"; // "bar.dll". | |
97 static const char kMatchLocation[] = "e6ca7b1c"; // "c:\\foo\\". | |
98 static const char kNoMatchLocation[] = "c:\\foobar\\"; | |
99 static const char kMatchDesc[] = "5c4419a6"; // "Desc". | |
100 static const char kVersionHigh[] = "2.0"; | |
101 static const char kVersionLow[] = "0.5"; | |
102 static const char kMatchSignature[] = "7bfd87e1"; // "Sig". | |
103 static const char kEmpty[] = ""; | |
104 | |
105 const struct MatchingEntryList { | |
106 ModuleEnumerator::ModuleStatus expected_result; | |
107 ModuleEnumerator::Module test_case; | |
108 ModuleEnumerator::BlacklistEntry blacklist; | |
109 } kMatchineEntryList[] = { | |
110 // Each BlacklistEntry is: | |
111 // Filename, location, desc_or_signer, version from, version to, help_tip. | |
112 | |
113 { // Matches: Name (location doesn't match) => Not enough for a match. | |
114 ModuleEnumerator::NOT_MATCHED, | |
115 kStandardModule, | |
116 { kMatchName, kNoMatchLocation, kEmpty, kEmpty, kEmpty, kOs, | |
117 ModuleEnumerator::SEE_LINK } | |
118 }, { // Matches: Name (location not given) => Suspected match. | |
119 ModuleEnumerator::SUSPECTED_BAD, | |
120 kStandardModule, | |
121 { kMatchName, kEmpty, kEmpty, kEmpty, kEmpty, kOs, | |
122 ModuleEnumerator::SEE_LINK } | |
123 }, { // Matches: Name, not version (location not given) => Not a match. | |
124 ModuleEnumerator::NOT_MATCHED, | |
125 kStandardModule, | |
126 { kMatchName, kEmpty, kEmpty, kVersionHigh, kVersionHigh, kOs, | |
127 ModuleEnumerator::SEE_LINK } | |
128 }, { // Matches: Name, location => Suspected match. | |
129 ModuleEnumerator::SUSPECTED_BAD, | |
130 kStandardModule, | |
131 { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty, kOs, | |
132 ModuleEnumerator::SEE_LINK } | |
133 }, { // Matches: Name, location, (description not given) => Confirmed match. | |
134 ModuleEnumerator::CONFIRMED_BAD, | |
135 kStandardModuleNoDescription, // Note: No description. | |
136 { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty, kOs, | |
137 ModuleEnumerator::SEE_LINK } | |
138 }, { // Matches: Name, location, (signature not given) => Confirmed match. | |
139 ModuleEnumerator::CONFIRMED_BAD, | |
140 kStandardModuleNoSignature, // Note: No signature. | |
141 { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty, kOs, | |
142 ModuleEnumerator::SEE_LINK } | |
143 }, { // Matches: Name, location (not version) => Not a match. | |
144 ModuleEnumerator::NOT_MATCHED, | |
145 kStandardModule, | |
146 { kMatchName, kMatchLocation, kEmpty, kVersionHigh, kVersionLow, kOs, | |
147 ModuleEnumerator::SEE_LINK } | |
148 }, { // Matches: Name, location, signature => Confirmed match. | |
149 ModuleEnumerator::CONFIRMED_BAD, | |
150 kStandardModule, | |
151 { kMatchName, kMatchLocation, kMatchSignature, kEmpty, kEmpty, kOs, | |
152 ModuleEnumerator::SEE_LINK } | |
153 }, { // Matches: Name, location, signature (not version) => No match. | |
154 ModuleEnumerator::NOT_MATCHED, | |
155 kStandardModule, | |
156 { kMatchName, kMatchLocation, kMatchSignature, | |
157 kVersionLow, kVersionLow, kOs, ModuleEnumerator::SEE_LINK } | |
158 }, { // Matches: Name, location, description => Confirmed match. | |
159 ModuleEnumerator::CONFIRMED_BAD, | |
160 kStandardModule, | |
161 { kMatchName, kMatchLocation, kMatchDesc, kEmpty, kEmpty, kOs, | |
162 ModuleEnumerator::SEE_LINK } | |
163 }, { // Matches: Name, location, description (not version) => No match. | |
164 ModuleEnumerator::NOT_MATCHED, | |
165 kStandardModule, | |
166 { kMatchName, kMatchLocation, kMatchDesc, | |
167 kVersionHigh, kVersionHigh, kOs, ModuleEnumerator::SEE_LINK } | |
168 }, { // Matches: Name, location, signature, version => Confirmed match. | |
169 ModuleEnumerator::CONFIRMED_BAD, | |
170 kStandardModule, | |
171 { kMatchName, kMatchLocation, kMatchSignature, | |
172 kVersionLow, kVersionHigh, kOs, ModuleEnumerator::SEE_LINK } | |
173 }, { // Matches: Name, location, signature, version (lower) => Confirmed. | |
174 ModuleEnumerator::CONFIRMED_BAD, | |
175 kStandardModule, | |
176 { kMatchName, kMatchLocation, kMatchSignature, | |
177 kVersionLow, kEmpty, kOs, ModuleEnumerator::SEE_LINK } | |
178 }, { // Matches: Name, location, signature, version (upper) => Confirmed. | |
179 ModuleEnumerator::CONFIRMED_BAD, | |
180 kStandardModule, | |
181 { kMatchName, kMatchLocation, kMatchSignature, | |
182 kEmpty, kVersionHigh, kOs, ModuleEnumerator::SEE_LINK } | |
183 }, { // Matches: Name, Location, Version lower is inclusive => Confirmed. | |
184 ModuleEnumerator::CONFIRMED_BAD, | |
185 kStandardModule, | |
186 { kMatchName, kMatchLocation, kMatchSignature, | |
187 "1.0", "2.0", kOs, ModuleEnumerator::SEE_LINK } | |
188 }, { // Matches: Name, Location, Version higher is exclusive => No match. | |
189 ModuleEnumerator::NOT_MATCHED, | |
190 kStandardModule, | |
191 { kMatchName, kMatchLocation, kEmpty, | |
192 "0.0", "1.0", kOs, ModuleEnumerator::SEE_LINK } | |
193 }, { // All empty fields doesn't produce a match. | |
194 ModuleEnumerator::NOT_MATCHED, | |
195 { kType, kStatus, L"", L"", L"", L"", L"", L"", ModuleEnumerator::NONE }, | |
196 { "a.dll", "", "", "", "", kOs, ModuleEnumerator::SEE_LINK } | |
197 }, | |
198 }; | |
199 | |
200 TEST_F(EnumerateModulesTest, MatchFunction) { | |
201 for (size_t i = 0; i < arraysize(kMatchineEntryList); ++i) { | |
202 ModuleEnumerator::Module test = kMatchineEntryList[i].test_case; | |
203 ModuleEnumerator::NormalizeModule(&test); | |
204 ModuleEnumerator::BlacklistEntry blacklist = | |
205 kMatchineEntryList[i].blacklist; | |
206 | |
207 SCOPED_TRACE("Test case no " + base::IntToString(i) + | |
208 ": '" + base::UTF16ToASCII(test.name) + "'"); | |
209 EXPECT_EQ(kMatchineEntryList[i].expected_result, | |
210 ModuleEnumerator::Match(test, blacklist)); | |
211 } | |
212 } | |
213 | |
214 const struct CollapsePathList { | 92 const struct CollapsePathList { |
215 base::string16 expected_result; | 93 base::string16 expected_result; |
216 base::string16 test_case; | 94 base::string16 test_case; |
217 } kCollapsePathList[] = { | 95 } kCollapsePathList[] = { |
218 // Negative testing (should not collapse this path). | 96 // Negative testing (should not collapse this path). |
219 { base::ASCIIToUTF16("c:\\a\\a.dll"), base::ASCIIToUTF16("c:\\a\\a.dll") }, | 97 { base::ASCIIToUTF16("c:\\a\\a.dll"), base::ASCIIToUTF16("c:\\a\\a.dll") }, |
220 // These two are to test that we select the maximum collapsed path. | 98 // These two are to test that we select the maximum collapsed path. |
221 { base::ASCIIToUTF16("%foo%\\a.dll"), base::ASCIIToUTF16("c:\\foo\\a.dll") }, | 99 { base::ASCIIToUTF16("%foo%\\a.dll"), base::ASCIIToUTF16("c:\\foo\\a.dll") }, |
222 { base::ASCIIToUTF16("%x%\\a.dll"), | 100 { base::ASCIIToUTF16("%x%\\a.dll"), |
223 base::ASCIIToUTF16("c:\\foo\\bar\\a.dll") }, | 101 base::ASCIIToUTF16("c:\\foo\\bar\\a.dll") }, |
224 }; | 102 }; |
225 | 103 |
226 TEST_F(EnumerateModulesTest, CollapsePath) { | 104 TEST_F(EnumerateModulesTest, CollapsePath) { |
227 scoped_refptr<ModuleEnumerator> module_enumerator(new ModuleEnumerator(NULL)); | 105 ModuleEnumerator module_enumerator(nullptr); |
228 module_enumerator->path_mapping_.clear(); | 106 module_enumerator.path_mapping_.clear(); |
229 module_enumerator->path_mapping_.push_back( | 107 module_enumerator.path_mapping_.push_back( |
230 std::make_pair(L"c:\\foo\\", L"%foo%")); | 108 std::make_pair(L"c:\\foo\\", L"%foo%")); |
231 module_enumerator->path_mapping_.push_back( | 109 module_enumerator.path_mapping_.push_back( |
232 std::make_pair(L"c:\\foo\\bar\\", L"%x%")); | 110 std::make_pair(L"c:\\foo\\bar\\", L"%x%")); |
233 | 111 |
234 for (size_t i = 0; i < arraysize(kCollapsePathList); ++i) { | 112 for (size_t i = 0; i < arraysize(kCollapsePathList); ++i) { |
235 ModuleEnumerator::Module module; | 113 ModuleEnumerator::Module module; |
236 module.location = kCollapsePathList[i].test_case; | 114 module.location = kCollapsePathList[i].test_case; |
237 module_enumerator->CollapsePath(&module); | 115 module_enumerator.CollapsePath(&module); |
238 | 116 |
239 SCOPED_TRACE("Test case no " + base::IntToString(i) + ": '" + | 117 SCOPED_TRACE("Test case no " + base::IntToString(i) + ": '" + |
240 base::UTF16ToASCII(kCollapsePathList[i].expected_result) + | 118 base::UTF16ToASCII(kCollapsePathList[i].expected_result) + |
241 "'"); | 119 "'"); |
242 EXPECT_EQ(kCollapsePathList[i].expected_result, module.location); | 120 EXPECT_EQ(kCollapsePathList[i].expected_result, module.location); |
243 } | 121 } |
244 } | 122 } |
OLD | NEW |