OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 |
| 5 #include "base/string_number_conversions.h" |
| 6 #include "base/string_util.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/enumerate_modules_model_win.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 typedef public testing::Test EnumerateModulesTest; |
| 12 |
| 13 // Set up some constants to use as default when creating the structs. |
| 14 static const ModuleEnumerator::ModuleType kType = |
| 15 ModuleEnumerator::LOADED_MODULE; |
| 16 |
| 17 static const ModuleEnumerator::ModuleStatus kStatus = |
| 18 ModuleEnumerator::NOT_MATCHED; |
| 19 |
| 20 static const ModuleEnumerator::RecommendedAction kAction = |
| 21 ModuleEnumerator::NONE; |
| 22 |
| 23 // This is a list of test cases to normalize. |
| 24 static const struct NormalizationEntryList { |
| 25 ModuleEnumerator::Module test_case; |
| 26 ModuleEnumerator::Module expected; |
| 27 } kNormalizationTestCases[] = { |
| 28 { |
| 29 // Only path normalization needed. |
| 30 {kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", |
| 31 L"Sig", kAction}, |
| 32 {kType, kStatus, L"c:\\foo\\", L"bar.dll", L"Prod", L"Desc", L"1.0", |
| 33 L"Sig", kAction}, |
| 34 }, { |
| 35 // Lower case normalization. |
| 36 {kType, kStatus, L"C:\\Foo\\Bar.dll", L"", L"", L"", L"1.0", |
| 37 L"", kAction}, |
| 38 {kType, kStatus, L"c:\\foo\\", L"bar.dll", L"", L"", L"1.0", |
| 39 L"", kAction}, |
| 40 }, { |
| 41 // Version can include strings after the version number. Strip that away. |
| 42 {kType, kStatus, L"c:\\foo.dll", L"", L"", L"", L"1.0 asdf", |
| 43 L"", kAction}, |
| 44 {kType, kStatus, L"c:\\", L"foo.dll", L"", L"", L"1.0", |
| 45 L"", kAction}, |
| 46 }, { |
| 47 // Corner case: No path (not sure this will ever happen). |
| 48 {kType, kStatus, L"bar.dll", L"", L"", L"", L"", L"", kAction}, |
| 49 {kType, kStatus, L"", L"bar.dll", L"", L"", L"", L"", kAction}, |
| 50 }, { |
| 51 // Error case: Missing filename (not sure this will ever happen). |
| 52 {kType, kStatus, L"", L"", L"", L"", L"1.0", L"", kAction}, |
| 53 {kType, kStatus, L"", L"", L"", L"", L"1.0", L"", kAction}, |
| 54 }, |
| 55 }; |
| 56 |
| 57 TEST_F(EnumerateModulesTest, NormalizeEntry) { |
| 58 for (size_t i = 0; i < arraysize(kNormalizationTestCases); ++i) { |
| 59 ModuleEnumerator::Module test = kNormalizationTestCases[i].test_case; |
| 60 EXPECT_FALSE(test.normalized); |
| 61 ModuleEnumerator::NormalizeModule(&test); |
| 62 ModuleEnumerator::Module expected = kNormalizationTestCases[i].expected; |
| 63 |
| 64 SCOPED_TRACE("Test case no: " + base::IntToString(i)); |
| 65 EXPECT_EQ(expected.type, test.type); |
| 66 EXPECT_EQ(expected.status, test.status); |
| 67 EXPECT_STREQ(expected.location.c_str(), test.location.c_str()); |
| 68 EXPECT_STREQ(expected.name.c_str(), test.name.c_str()); |
| 69 EXPECT_STREQ(expected.product_name.c_str(), test.product_name.c_str()); |
| 70 EXPECT_STREQ(expected.description.c_str(), test.description.c_str()); |
| 71 EXPECT_STREQ(expected.version.c_str(), test.version.c_str()); |
| 72 EXPECT_STREQ(expected.digital_signer.c_str(), test.digital_signer.c_str()); |
| 73 EXPECT_EQ(expected.recommended_action, test.recommended_action); |
| 74 EXPECT_TRUE(test.normalized); |
| 75 } |
| 76 } |
| 77 |
| 78 const ModuleEnumerator::Module kStandardModule = |
| 79 { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"Sig", |
| 80 ModuleEnumerator::NONE }; |
| 81 |
| 82 // Name, location, description and signature are compared by hashing. |
| 83 static const char kMatchName[] = "88e8c9e0"; // "bar.dll". |
| 84 static const char kNoMatchName[] = "barfoo.dll"; |
| 85 static const char kMatchLocation[] = "e6ca7b1c"; // "c:\\foo\\". |
| 86 static const char kNoMatchLocation[] = "c:\\foobar\\"; |
| 87 static const char kMatchDesc[] = "5c4419a6"; // "Desc". |
| 88 static const char kNoMatchDesc[] = "NoDesc"; |
| 89 static const char kVersionHigh[] = "2.0"; |
| 90 static const char kVersionLow[] = "0.5"; |
| 91 static const char kMatchSignature[] = "7bfd87e1"; // "Sig". |
| 92 static const char kNoMatchSignature[] = "giS"; |
| 93 static const char kEmpty[] = ""; |
| 94 |
| 95 const struct MatchingEntryList { |
| 96 ModuleEnumerator::ModuleStatus expected_result; |
| 97 ModuleEnumerator::Module test_case; |
| 98 ModuleEnumerator::BlacklistEntry blacklist; |
| 99 } kMatchineEntryList[] = { |
| 100 // Each BlacklistEntry is: |
| 101 // Filename, location, desc_or_signer, version from, version to, help_tip. |
| 102 |
| 103 { // Matches: Name (location doesn't match) => Not enough for a match. |
| 104 ModuleEnumerator::NOT_MATCHED, |
| 105 kStandardModule, |
| 106 { kMatchName, kNoMatchLocation, kEmpty, kEmpty, kEmpty, |
| 107 ModuleEnumerator::SEE_LINK } |
| 108 }, { // Matches: Name (location not given) => Suspected match. |
| 109 ModuleEnumerator::SUSPECTED_BAD, |
| 110 kStandardModule, |
| 111 { kMatchName, kEmpty, kEmpty, kEmpty, kEmpty, |
| 112 ModuleEnumerator::SEE_LINK } |
| 113 }, { // Matches: Name, not version (location not given) => Not a match. |
| 114 ModuleEnumerator::NOT_MATCHED, |
| 115 kStandardModule, |
| 116 { kMatchName, kEmpty, kEmpty, kVersionHigh, kVersionHigh, |
| 117 ModuleEnumerator::SEE_LINK } |
| 118 }, { // Matches: Name, location => Suspected match. |
| 119 ModuleEnumerator::SUSPECTED_BAD, |
| 120 kStandardModule, |
| 121 { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty, |
| 122 ModuleEnumerator::SEE_LINK } |
| 123 }, { // Matches: Name, location (not version) => Not a match. |
| 124 ModuleEnumerator::NOT_MATCHED, |
| 125 kStandardModule, |
| 126 { kMatchName, kMatchLocation, kEmpty, kVersionHigh, kVersionLow, |
| 127 ModuleEnumerator::SEE_LINK } |
| 128 }, { // Matches: Name, location, signature => Confirmed match. |
| 129 ModuleEnumerator::CONFIRMED_BAD, |
| 130 kStandardModule, |
| 131 { kMatchName, kMatchLocation, kMatchSignature, kEmpty, kEmpty, |
| 132 ModuleEnumerator::SEE_LINK } |
| 133 }, { // Matches: Name, location, signature (not version) => No match. |
| 134 ModuleEnumerator::NOT_MATCHED, |
| 135 kStandardModule, |
| 136 { kMatchName, kMatchLocation, kMatchSignature, |
| 137 kVersionLow, kVersionLow, ModuleEnumerator::SEE_LINK } |
| 138 }, { // Matches: Name, location, description => Confirmed match. |
| 139 ModuleEnumerator::CONFIRMED_BAD, |
| 140 kStandardModule, |
| 141 { kMatchName, kMatchLocation, kMatchDesc, kEmpty, kEmpty, |
| 142 ModuleEnumerator::SEE_LINK } |
| 143 }, { // Matches: Name, location, description (not version) => No match. |
| 144 ModuleEnumerator::NOT_MATCHED, |
| 145 kStandardModule, |
| 146 { kMatchName, kMatchLocation, kMatchDesc, |
| 147 kVersionHigh, kVersionHigh, ModuleEnumerator::SEE_LINK } |
| 148 }, { // Matches: Name, location, signature, version => Confirmed match. |
| 149 ModuleEnumerator::CONFIRMED_BAD, |
| 150 kStandardModule, |
| 151 { kMatchName, kMatchLocation, kMatchSignature, |
| 152 kVersionLow, kVersionHigh, ModuleEnumerator::SEE_LINK } |
| 153 }, { // Matches: Name, location, signature, version (lower) => Confirmed. |
| 154 ModuleEnumerator::CONFIRMED_BAD, |
| 155 kStandardModule, |
| 156 { kMatchName, kMatchLocation, kMatchSignature, |
| 157 kVersionLow, kEmpty, ModuleEnumerator::SEE_LINK } |
| 158 }, { // Matches: Name, location, signature, version (upper) => Confirmed. |
| 159 ModuleEnumerator::CONFIRMED_BAD, |
| 160 kStandardModule, |
| 161 { kMatchName, kMatchLocation, kMatchSignature, |
| 162 kEmpty, kVersionHigh, ModuleEnumerator::SEE_LINK } |
| 163 }, { // All empty fields doesn't produce a match. |
| 164 ModuleEnumerator::NOT_MATCHED, |
| 165 {kType, kStatus, L"", L"", L"", L"", L""}, |
| 166 { "a.dll", "", "", "", "", ModuleEnumerator::SEE_LINK } |
| 167 }, |
| 168 }; |
| 169 |
| 170 TEST_F(EnumerateModulesTest, MatchFunction) { |
| 171 for (size_t i = 0; i < arraysize(kMatchineEntryList); ++i) { |
| 172 ModuleEnumerator::Module test = kMatchineEntryList[i].test_case; |
| 173 ModuleEnumerator::NormalizeModule(&test); |
| 174 ModuleEnumerator::BlacklistEntry blacklist = |
| 175 kMatchineEntryList[i].blacklist; |
| 176 |
| 177 SCOPED_TRACE("Test case no " + base::IntToString(i) + |
| 178 ": '" + UTF16ToASCII(test.name) + "'"); |
| 179 EXPECT_EQ(kMatchineEntryList[i].expected_result, |
| 180 ModuleEnumerator::Match(test, blacklist)); |
| 181 } |
| 182 } |
OLD | NEW |