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
3 // found in the LICENSE file.
5 #include "base/strings/string_number_conversions.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/enumerate_modules_model_win.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 typedef testing::Test EnumerateModulesTest
;
13 // Set up some constants to use as default when creating the structs.
14 static const ModuleEnumerator::ModuleType kType
=
15 ModuleEnumerator::LOADED_MODULE
;
17 static const ModuleEnumerator::ModuleStatus kStatus
=
18 ModuleEnumerator::NOT_MATCHED
;
20 static const ModuleEnumerator::RecommendedAction kAction
=
21 ModuleEnumerator::NONE
;
23 static const ModuleEnumerator::OperatingSystem kOs
=
24 ModuleEnumerator::ALL
;
26 // This is a list of test cases to normalize.
27 static const struct NormalizationEntryList
{
28 ModuleEnumerator::Module test_case
;
29 ModuleEnumerator::Module expected
;
30 } kNormalizationTestCases
[] = {
32 // Only path normalization needed.
33 {kType
, kStatus
, L
"c:\\foo\\bar.dll", L
"", L
"Prod", L
"Desc", L
"1.0",
35 {kType
, kStatus
, L
"c:\\foo\\", L
"bar.dll", L
"Prod", L
"Desc", L
"1.0",
38 // Lower case normalization.
39 {kType
, kStatus
, L
"C:\\Foo\\Bar.dll", L
"", L
"", L
"", L
"1.0",
41 {kType
, kStatus
, L
"c:\\foo\\", L
"bar.dll", L
"", L
"", L
"1.0",
44 // Version can include strings after the version number. Strip that away.
45 {kType
, kStatus
, L
"c:\\foo.dll", L
"", L
"", L
"", L
"1.0 asdf",
47 {kType
, kStatus
, L
"c:\\", L
"foo.dll", L
"", L
"", L
"1.0",
50 // Corner case: No path (not sure this will ever happen).
51 {kType
, kStatus
, L
"bar.dll", L
"", L
"", L
"", L
"", L
"", kAction
},
52 {kType
, kStatus
, L
"", L
"bar.dll", L
"", L
"", L
"", L
"", kAction
},
54 // Error case: Missing filename (not sure this will ever happen).
55 {kType
, kStatus
, L
"", L
"", L
"", L
"", L
"1.0", L
"", kAction
},
56 {kType
, kStatus
, L
"", L
"", L
"", L
"", L
"1.0", L
"", kAction
},
60 TEST_F(EnumerateModulesTest
, NormalizeEntry
) {
61 for (size_t i
= 0; i
< arraysize(kNormalizationTestCases
); ++i
) {
62 ModuleEnumerator::Module test
= kNormalizationTestCases
[i
].test_case
;
63 EXPECT_FALSE(test
.normalized
);
64 ModuleEnumerator::NormalizeModule(&test
);
65 ModuleEnumerator::Module expected
= kNormalizationTestCases
[i
].expected
;
67 SCOPED_TRACE("Test case no: " + base::IntToString(i
));
68 EXPECT_EQ(expected
.type
, test
.type
);
69 EXPECT_EQ(expected
.status
, test
.status
);
70 EXPECT_STREQ(expected
.location
.c_str(), test
.location
.c_str());
71 EXPECT_STREQ(expected
.name
.c_str(), test
.name
.c_str());
72 EXPECT_STREQ(expected
.product_name
.c_str(), test
.product_name
.c_str());
73 EXPECT_STREQ(expected
.description
.c_str(), test
.description
.c_str());
74 EXPECT_STREQ(expected
.version
.c_str(), test
.version
.c_str());
75 EXPECT_STREQ(expected
.digital_signer
.c_str(), test
.digital_signer
.c_str());
76 EXPECT_EQ(expected
.recommended_action
, test
.recommended_action
);
77 EXPECT_TRUE(test
.normalized
);
81 const ModuleEnumerator::Module kStandardModule
=
82 { kType
, kStatus
, L
"c:\\foo\\bar.dll", L
"", L
"Prod", L
"Desc", L
"1.0", L
"Sig",
83 ModuleEnumerator::NONE
};
84 const ModuleEnumerator::Module kStandardModuleNoDescription
=
85 { kType
, kStatus
, L
"c:\\foo\\bar.dll", L
"", L
"Prod", L
"", L
"1.0", L
"Sig",
86 ModuleEnumerator::NONE
};
87 const ModuleEnumerator::Module kStandardModuleNoSignature
=
88 { kType
, kStatus
, L
"c:\\foo\\bar.dll", L
"", L
"Prod", L
"Desc", L
"1.0", L
"",
89 ModuleEnumerator::NONE
};
91 // Name, location, description and signature are compared by hashing.
92 static const char kMatchName
[] = "88e8c9e0"; // "bar.dll".
93 static const char kMatchLocation
[] = "e6ca7b1c"; // "c:\\foo\\".
94 static const char kNoMatchLocation
[] = "c:\\foobar\\";
95 static const char kMatchDesc
[] = "5c4419a6"; // "Desc".
96 static const char kVersionHigh
[] = "2.0";
97 static const char kVersionLow
[] = "0.5";
98 static const char kMatchSignature
[] = "7bfd87e1"; // "Sig".
99 static const char kEmpty
[] = "";
101 const struct MatchingEntryList
{
102 ModuleEnumerator::ModuleStatus expected_result
;
103 ModuleEnumerator::Module test_case
;
104 ModuleEnumerator::BlacklistEntry blacklist
;
105 } kMatchineEntryList
[] = {
106 // Each BlacklistEntry is:
107 // Filename, location, desc_or_signer, version from, version to, help_tip.
109 { // Matches: Name (location doesn't match) => Not enough for a match.
110 ModuleEnumerator::NOT_MATCHED
,
112 { kMatchName
, kNoMatchLocation
, kEmpty
, kEmpty
, kEmpty
, kOs
,
113 ModuleEnumerator::SEE_LINK
}
114 }, { // Matches: Name (location not given) => Suspected match.
115 ModuleEnumerator::SUSPECTED_BAD
,
117 { kMatchName
, kEmpty
, kEmpty
, kEmpty
, kEmpty
, kOs
,
118 ModuleEnumerator::SEE_LINK
}
119 }, { // Matches: Name, not version (location not given) => Not a match.
120 ModuleEnumerator::NOT_MATCHED
,
122 { kMatchName
, kEmpty
, kEmpty
, kVersionHigh
, kVersionHigh
, kOs
,
123 ModuleEnumerator::SEE_LINK
}
124 }, { // Matches: Name, location => Suspected match.
125 ModuleEnumerator::SUSPECTED_BAD
,
127 { kMatchName
, kMatchLocation
, kEmpty
, kEmpty
, kEmpty
, kOs
,
128 ModuleEnumerator::SEE_LINK
}
129 }, { // Matches: Name, location, (description not given) => Confirmed match.
130 ModuleEnumerator::CONFIRMED_BAD
,
131 kStandardModuleNoDescription
, // Note: No description.
132 { kMatchName
, kMatchLocation
, kEmpty
, kEmpty
, kEmpty
, kOs
,
133 ModuleEnumerator::SEE_LINK
}
134 }, { // Matches: Name, location, (signature not given) => Confirmed match.
135 ModuleEnumerator::CONFIRMED_BAD
,
136 kStandardModuleNoSignature
, // Note: No signature.
137 { kMatchName
, kMatchLocation
, kEmpty
, kEmpty
, kEmpty
, kOs
,
138 ModuleEnumerator::SEE_LINK
}
139 }, { // Matches: Name, location (not version) => Not a match.
140 ModuleEnumerator::NOT_MATCHED
,
142 { kMatchName
, kMatchLocation
, kEmpty
, kVersionHigh
, kVersionLow
, kOs
,
143 ModuleEnumerator::SEE_LINK
}
144 }, { // Matches: Name, location, signature => Confirmed match.
145 ModuleEnumerator::CONFIRMED_BAD
,
147 { kMatchName
, kMatchLocation
, kMatchSignature
, kEmpty
, kEmpty
, kOs
,
148 ModuleEnumerator::SEE_LINK
}
149 }, { // Matches: Name, location, signature (not version) => No match.
150 ModuleEnumerator::NOT_MATCHED
,
152 { kMatchName
, kMatchLocation
, kMatchSignature
,
153 kVersionLow
, kVersionLow
, kOs
, ModuleEnumerator::SEE_LINK
}
154 }, { // Matches: Name, location, description => Confirmed match.
155 ModuleEnumerator::CONFIRMED_BAD
,
157 { kMatchName
, kMatchLocation
, kMatchDesc
, kEmpty
, kEmpty
, kOs
,
158 ModuleEnumerator::SEE_LINK
}
159 }, { // Matches: Name, location, description (not version) => No match.
160 ModuleEnumerator::NOT_MATCHED
,
162 { kMatchName
, kMatchLocation
, kMatchDesc
,
163 kVersionHigh
, kVersionHigh
, kOs
, ModuleEnumerator::SEE_LINK
}
164 }, { // Matches: Name, location, signature, version => Confirmed match.
165 ModuleEnumerator::CONFIRMED_BAD
,
167 { kMatchName
, kMatchLocation
, kMatchSignature
,
168 kVersionLow
, kVersionHigh
, kOs
, ModuleEnumerator::SEE_LINK
}
169 }, { // Matches: Name, location, signature, version (lower) => Confirmed.
170 ModuleEnumerator::CONFIRMED_BAD
,
172 { kMatchName
, kMatchLocation
, kMatchSignature
,
173 kVersionLow
, kEmpty
, kOs
, ModuleEnumerator::SEE_LINK
}
174 }, { // Matches: Name, location, signature, version (upper) => Confirmed.
175 ModuleEnumerator::CONFIRMED_BAD
,
177 { kMatchName
, kMatchLocation
, kMatchSignature
,
178 kEmpty
, kVersionHigh
, kOs
, ModuleEnumerator::SEE_LINK
}
179 }, { // Matches: Name, Location, Version lower is inclusive => Confirmed.
180 ModuleEnumerator::CONFIRMED_BAD
,
182 { kMatchName
, kMatchLocation
, kMatchSignature
,
183 "1.0", "2.0", kOs
, ModuleEnumerator::SEE_LINK
}
184 }, { // Matches: Name, Location, Version higher is exclusive => No match.
185 ModuleEnumerator::NOT_MATCHED
,
187 { kMatchName
, kMatchLocation
, kEmpty
,
188 "0.0", "1.0", kOs
, ModuleEnumerator::SEE_LINK
}
189 }, { // All empty fields doesn't produce a match.
190 ModuleEnumerator::NOT_MATCHED
,
191 { kType
, kStatus
, L
"", L
"", L
"", L
"", L
"", L
"", ModuleEnumerator::NONE
},
192 { "a.dll", "", "", "", "", kOs
, ModuleEnumerator::SEE_LINK
}
196 TEST_F(EnumerateModulesTest
, MatchFunction
) {
197 for (size_t i
= 0; i
< arraysize(kMatchineEntryList
); ++i
) {
198 ModuleEnumerator::Module test
= kMatchineEntryList
[i
].test_case
;
199 ModuleEnumerator::NormalizeModule(&test
);
200 ModuleEnumerator::BlacklistEntry blacklist
=
201 kMatchineEntryList
[i
].blacklist
;
203 SCOPED_TRACE("Test case no " + base::IntToString(i
) +
204 ": '" + base::UTF16ToASCII(test
.name
) + "'");
205 EXPECT_EQ(kMatchineEntryList
[i
].expected_result
,
206 ModuleEnumerator::Match(test
, blacklist
));
210 const struct CollapsePathList
{
211 base::string16 expected_result
;
212 base::string16 test_case
;
213 } kCollapsePathList
[] = {
214 // Negative testing (should not collapse this path).
215 { base::ASCIIToUTF16("c:\\a\\a.dll"), base::ASCIIToUTF16("c:\\a\\a.dll") },
216 // These two are to test that we select the maximum collapsed path.
217 { base::ASCIIToUTF16("%foo%\\a.dll"), base::ASCIIToUTF16("c:\\foo\\a.dll") },
218 { base::ASCIIToUTF16("%x%\\a.dll"),
219 base::ASCIIToUTF16("c:\\foo\\bar\\a.dll") },
222 TEST_F(EnumerateModulesTest
, CollapsePath
) {
223 scoped_refptr
<ModuleEnumerator
> module_enumerator(new ModuleEnumerator(NULL
));
224 module_enumerator
->path_mapping_
.clear();
225 module_enumerator
->path_mapping_
.push_back(
226 std::make_pair(L
"c:\\foo\\", L
"%foo%"));
227 module_enumerator
->path_mapping_
.push_back(
228 std::make_pair(L
"c:\\foo\\bar\\", L
"%x%"));
230 for (size_t i
= 0; i
< arraysize(kCollapsePathList
); ++i
) {
231 ModuleEnumerator::Module module
;
232 module
.location
= kCollapsePathList
[i
].test_case
;
233 module_enumerator
->CollapsePath(&module
);
235 SCOPED_TRACE("Test case no " + base::IntToString(i
) + ": '" +
236 base::UTF16ToASCII(kCollapsePathList
[i
].expected_result
) +
238 EXPECT_EQ(kCollapsePathList
[i
].expected_result
, module
.location
);