File: antivirus_metrics_provider_win_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 4,765 bytes parent folder | download | duplicates (6)
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());