summaryrefslogtreecommitdiffstats
path: root/chrome/browser/enumerate_modules_model_unittest_win.cc
blob: 1992e7a509cf3bfb4d7749c80513ab3ab71c762e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/enumerate_modules_model_win.h"
#include "testing/gtest/include/gtest/gtest.h"

typedef public testing::Test EnumerateModulesTest;

// Set up some constants to use as default when creating the structs.
static const ModuleEnumerator::ModuleType kType =
    ModuleEnumerator::LOADED_MODULE;

static const ModuleEnumerator::ModuleStatus kStatus =
    ModuleEnumerator::NOT_MATCHED;

static const ModuleEnumerator::RecommendedAction kAction =
    ModuleEnumerator::NONE;

// This is a list of test cases to normalize.
static const struct NormalizationEntryList {
  ModuleEnumerator::Module test_case;
  ModuleEnumerator::Module expected;
} kNormalizationTestCases[] = {
  {
    // Only path normalization needed.
    {kType, kStatus, L"c:\\foo\\bar.dll", L"",        L"Prod", L"Desc", L"1.0",
         L"Sig", kAction},
    {kType, kStatus, L"c:\\foo\\",        L"bar.dll", L"Prod", L"Desc", L"1.0",
         L"Sig", kAction},
  }, {
    // Lower case normalization.
    {kType, kStatus, L"C:\\Foo\\Bar.dll", L"",        L"", L"", L"1.0",
         L"", kAction},
    {kType, kStatus, L"c:\\foo\\",        L"bar.dll", L"", L"", L"1.0",
         L"", kAction},
  }, {
    // Version can include strings after the version number. Strip that away.
    {kType, kStatus, L"c:\\foo.dll", L"",        L"", L"", L"1.0 asdf",
         L"", kAction},
    {kType, kStatus, L"c:\\",        L"foo.dll", L"", L"", L"1.0",
         L"", kAction},
  }, {
    // Corner case: No path (not sure this will ever happen).
    {kType, kStatus, L"bar.dll", L"",        L"", L"", L"", L"", kAction},
    {kType, kStatus, L"",        L"bar.dll", L"", L"", L"", L"", kAction},
  }, {
    // Error case: Missing filename (not sure this will ever happen).
    {kType, kStatus, L"", L"", L"", L"", L"1.0", L"", kAction},
    {kType, kStatus, L"", L"", L"", L"", L"1.0", L"", kAction},
  },
};

TEST_F(EnumerateModulesTest, NormalizeEntry) {
  for (size_t i = 0; i < arraysize(kNormalizationTestCases); ++i) {
    ModuleEnumerator::Module test = kNormalizationTestCases[i].test_case;
    EXPECT_FALSE(test.normalized);
    ModuleEnumerator::NormalizeModule(&test);
    ModuleEnumerator::Module expected = kNormalizationTestCases[i].expected;

    SCOPED_TRACE("Test case no: " + base::IntToString(i));
    EXPECT_EQ(expected.type, test.type);
    EXPECT_EQ(expected.status, test.status);
    EXPECT_STREQ(expected.location.c_str(), test.location.c_str());
    EXPECT_STREQ(expected.name.c_str(), test.name.c_str());
    EXPECT_STREQ(expected.product_name.c_str(), test.product_name.c_str());
    EXPECT_STREQ(expected.description.c_str(), test.description.c_str());
    EXPECT_STREQ(expected.version.c_str(), test.version.c_str());
    EXPECT_STREQ(expected.digital_signer.c_str(), test.digital_signer.c_str());
    EXPECT_EQ(expected.recommended_action, test.recommended_action);
    EXPECT_TRUE(test.normalized);
  }
}

const ModuleEnumerator::Module kStandardModule =
  { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"Sig",
    ModuleEnumerator::NONE };
const ModuleEnumerator::Module kStandardModuleNoDescription =
  { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"", L"1.0", L"Sig",
    ModuleEnumerator::NONE };
const ModuleEnumerator::Module kStandardModuleNoSignature =
  { kType, kStatus, L"c:\\foo\\bar.dll", L"", L"Prod", L"Desc", L"1.0", L"",
    ModuleEnumerator::NONE };

// Name, location, description and signature are compared by hashing.
static const char kMatchName[] = "88e8c9e0";             // "bar.dll".
static const char kNoMatchName[] = "barfoo.dll";
static const char kMatchLocation[] = "e6ca7b1c";         // "c:\\foo\\".
static const char kNoMatchLocation[] = "c:\\foobar\\";
static const char kMatchDesc[] = "5c4419a6";             // "Desc".
static const char kNoMatchDesc[] = "NoDesc";
static const char kVersionHigh[] = "2.0";
static const char kVersionLow[] = "0.5";
static const char kMatchSignature[] = "7bfd87e1";        // "Sig".
static const char kNoMatchSignature[] = "giS";
static const char kEmpty[] = "";

const struct MatchingEntryList {
  ModuleEnumerator::ModuleStatus expected_result;
  ModuleEnumerator::Module test_case;
  ModuleEnumerator::BlacklistEntry blacklist;
} kMatchineEntryList[] = {
  // Each BlacklistEntry is:
  // Filename, location, desc_or_signer, version from, version to, help_tip.

  {  // Matches: Name (location doesn't match) => Not enough for a match.
    ModuleEnumerator::NOT_MATCHED,
    kStandardModule,
    { kMatchName, kNoMatchLocation, kEmpty, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name (location not given) => Suspected match.
    ModuleEnumerator::SUSPECTED_BAD,
    kStandardModule,
    { kMatchName, kEmpty, kEmpty, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, not version (location not given) => Not a match.
    ModuleEnumerator::NOT_MATCHED,
    kStandardModule,
    { kMatchName, kEmpty, kEmpty, kVersionHigh, kVersionHigh,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location => Suspected match.
    ModuleEnumerator::SUSPECTED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, (description not given) => Confirmed match.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModuleNoDescription,  // Note: No description.
    { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, (signature not given) => Confirmed match.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModuleNoSignature,  // Note: No signature.
    { kMatchName, kMatchLocation, kEmpty, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location (not version) => Not a match.
    ModuleEnumerator::NOT_MATCHED,
    kStandardModule,
    { kMatchName, kMatchLocation, kEmpty, kVersionHigh, kVersionLow,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, signature => Confirmed match.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchSignature, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, signature (not version) => No match.
    ModuleEnumerator::NOT_MATCHED,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchSignature,
      kVersionLow, kVersionLow, ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, description => Confirmed match.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchDesc, kEmpty, kEmpty,
      ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, description (not version) => No match.
    ModuleEnumerator::NOT_MATCHED,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchDesc,
      kVersionHigh, kVersionHigh, ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, signature, version => Confirmed match.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchSignature,
      kVersionLow, kVersionHigh, ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, signature, version (lower) => Confirmed.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchSignature,
      kVersionLow, kEmpty, ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, location, signature, version (upper) => Confirmed.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchSignature,
      kEmpty, kVersionHigh, ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, Location, Version lower is inclusive => Confirmed.
    ModuleEnumerator::CONFIRMED_BAD,
    kStandardModule,
    { kMatchName, kMatchLocation, kMatchSignature,
      "1.0", "2.0", ModuleEnumerator::SEE_LINK }
  }, {  // Matches: Name, Location, Version higher is exclusive => No match.
    ModuleEnumerator::NOT_MATCHED,
    kStandardModule,
    { kMatchName, kMatchLocation, kEmpty,
      "0.0", "1.0", ModuleEnumerator::SEE_LINK }
  }, {  // All empty fields doesn't produce a match.
    ModuleEnumerator::NOT_MATCHED,
    { kType, kStatus, L"", L"", L"", L"", L""},
    { "a.dll", "", "", "", "", ModuleEnumerator::SEE_LINK }
  },
};

TEST_F(EnumerateModulesTest, MatchFunction) {
  for (size_t i = 0; i < arraysize(kMatchineEntryList); ++i) {
    ModuleEnumerator::Module test = kMatchineEntryList[i].test_case;
    ModuleEnumerator::NormalizeModule(&test);
    ModuleEnumerator::BlacklistEntry blacklist =
        kMatchineEntryList[i].blacklist;

    SCOPED_TRACE("Test case no " + base::IntToString(i) +
                 ": '" + UTF16ToASCII(test.name) + "'");
    EXPECT_EQ(kMatchineEntryList[i].expected_result,
              ModuleEnumerator::Match(test, blacklist));
  }
}

const struct CollapsePathList {
  string16 expected_result;
  string16 test_case;
} kCollapsePathList[] = {
  // Negative testing (should not collapse this path).
  { ASCIIToUTF16("c:\\a\\a.dll"), ASCIIToUTF16("c:\\a\\a.dll") },
  // These two are to test that we select the maximum collapsed path.
  { ASCIIToUTF16("%foo%\\a.dll"), ASCIIToUTF16("c:\\foo\\a.dll") },
  { ASCIIToUTF16("%x%\\a.dll"), ASCIIToUTF16("c:\\foo\\bar\\a.dll") },
};

TEST_F(EnumerateModulesTest, CollapsePath) {
  scoped_refptr<ModuleEnumerator> module_enumerator(new ModuleEnumerator(NULL));
  module_enumerator->path_mapping_.clear();
  module_enumerator->path_mapping_.push_back(
      std::make_pair(L"c:\\foo\\", L"%foo%"));
  module_enumerator->path_mapping_.push_back(
      std::make_pair(L"c:\\foo\\bar\\", L"%x%"));

  for (size_t i = 0; i < arraysize(kCollapsePathList); ++i) {
    ModuleEnumerator::Module module;
    module.location = kCollapsePathList[i].test_case;
    module_enumerator->CollapsePath(&module);

    SCOPED_TRACE("Test case no " + base::IntToString(i) +
                 ": '" + UTF16ToASCII(kCollapsePathList[i].expected_result) +
                 "'");
    EXPECT_EQ(kCollapsePathList[i].expected_result, module.location);
  }
}