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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/metrics/antivirus_metrics_provider_win.h"
#include <optional>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/threading/thread_checker.h"
#include "base/threading/thread_restrictions.h"
#include "base/version.h"
#include "chrome/services/util_win/util_win_impl.h"
#include "components/variations/hashing.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
void VerifySystemProfileData(const metrics::SystemProfileProto& system_profile,
bool expect_unhashed_value,
bool second_run) {
// The name of Windows Defender changed sometime in Windows 10, so any of the
// following is possible.
constexpr char kWindowsDefender[] = "Windows Defender";
constexpr char kWindowsDefenderAntivirus[] = "Windows Defender Antivirus";
bool defender_found = false;
uint32_t last_hash = 0xdeadbeef;
for (const auto& av : system_profile.antivirus_product()) {
if (av.has_product_name_hash()) {
last_hash = av.product_name_hash();
}
if (av.product_name_hash() == variations::HashName(kWindowsDefender) ||
av.product_name_hash() ==
variations::HashName(kWindowsDefenderAntivirus)) {
defender_found = true;
if (expect_unhashed_value) {
EXPECT_TRUE(av.has_product_name());
EXPECT_TRUE(av.product_name() == kWindowsDefender ||
av.product_name() == kWindowsDefenderAntivirus);
} else {
EXPECT_FALSE(av.has_product_name());
}
break;
}
}
EXPECT_TRUE(defender_found)
<< "expect_unhashed_value = " << expect_unhashed_value
<< ", second_run = " << second_run << ", "
<< system_profile.antivirus_product().size()
<< " antivirus products found. Last hash is " << last_hash << ".";
}
} // namespace
class AntiVirusMetricsProviderTest : public ::testing::TestWithParam<bool> {
public:
AntiVirusMetricsProviderTest()
: got_results_(false), expect_unhashed_value_(GetParam()) {
mojo::PendingRemote<chrome::mojom::UtilWin> remote;
util_win_impl_.emplace(remote.InitWithNewPipeAndPassReceiver());
provider_.SetRemoteUtilWinForTesting(std::move(remote));
}
AntiVirusMetricsProviderTest(const AntiVirusMetricsProviderTest&) = delete;
AntiVirusMetricsProviderTest& operator=(const AntiVirusMetricsProviderTest&) =
delete;
void GetMetricsCallback() {
// Check that the callback runs on the main loop.
ASSERT_TRUE(thread_checker_.CalledOnValidThread());
got_results_ = true;
metrics::SystemProfileProto system_profile;
provider_.ProvideSystemProfileMetrics(&system_profile);
VerifySystemProfileData(system_profile, expect_unhashed_value_, false);
// This looks weird, but it's to make sure that reading the data out of the
// AntiVirusMetricsProvider does not invalidate it, as the class should be
// resilient to this.
system_profile.Clear();
provider_.ProvideSystemProfileMetrics(&system_profile);
VerifySystemProfileData(system_profile, expect_unhashed_value_, true);
}
// Helper function to toggle whether the ReportFullAVProductDetails feature is
// enabled or not.
void SetFullNamesFeatureEnabled(bool enabled) {
if (enabled) {
scoped_feature_list_.InitAndEnableFeature(kReportFullAVProductDetails);
} else {
scoped_feature_list_.InitAndDisableFeature(kReportFullAVProductDetails);
}
}
bool got_results_;
bool expect_unhashed_value_;
base::test::TaskEnvironment task_environment_;
std::optional<UtilWinImpl> util_win_impl_;
AntiVirusMetricsProvider provider_;
base::test::ScopedFeatureList scoped_feature_list_;
base::ThreadCheckerImpl thread_checker_;
};
// TODO(crbug.com/41295648): Flaky on Windows 10.
TEST_P(AntiVirusMetricsProviderTest, DISABLED_GetMetricsFullName) {
base::ScopedAllowBlockingForTesting scoped_allow_blocking_;
ASSERT_TRUE(thread_checker_.CalledOnValidThread());
SetFullNamesFeatureEnabled(expect_unhashed_value_);
// The usage of base::Unretained(this) is safe here because |provider_|, who
// owns the callback, will go away before |this|.
provider_.AsyncInit(
base::BindOnce(&AntiVirusMetricsProviderTest::GetMetricsCallback,
base::Unretained(this)));
task_environment_.RunUntilIdle();
EXPECT_TRUE(got_results_);
}
INSTANTIATE_TEST_SUITE_P(, AntiVirusMetricsProviderTest, ::testing::Bool());
|