Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/metrics/antivirus_metrics_provider_win_unittest.cc

Issue 2009773007: Add AntiVirus information to the system profile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "chrome/browser/metrics/antivirus_metrics_provider_win.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/feature_list.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "base/version.h"
18 #include "base/win/windows_version.h"
19 #include "components/variations/metrics_util.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "content/public/test/test_utils.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace {
26
27 // Helper function to toggle whether the ReportFullAVProductDetails feature is
28 // enabled or not.
29 void SetFullNamesFeatureEnabled(bool enabled) {
30 base::FeatureList::ClearInstanceForTesting();
31 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
32 if (enabled) {
33 feature_list->InitializeFromCommandLine(
34 AntiVirusMetricsProvider::kReportNamesFeature.name, std::string());
35 } else {
36 feature_list->InitializeFromCommandLine(
37 std::string(), AntiVirusMetricsProvider::kReportNamesFeature.name);
38 }
39 base::FeatureList::SetInstance(std::move(feature_list));
40 }
41
42 void VerifySystemProfileData(const metrics::SystemProfileProto& system_profile,
43 bool expect_unhashed_value) {
44 const char kWindowsDefender[] = "Windows Defender";
45
46 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
47 bool defender_found = false;
48 for (const auto& av : system_profile.antivirus_product()) {
49 if (av.product_name_hash() == metrics::HashName(kWindowsDefender)) {
50 defender_found = true;
51 if (expect_unhashed_value) {
52 EXPECT_TRUE(av.has_product_name());
53 EXPECT_EQ(kWindowsDefender, av.product_name());
54 } else {
55 EXPECT_FALSE(av.has_product_name());
56 }
57 break;
58 }
59 }
60 EXPECT_TRUE(defender_found);
61 }
62 }
63
64 } // namespace
65
66 class AntiVirusMetricsProviderTest : public ::testing::TestWithParam<bool> {
67 public:
68 AntiVirusMetricsProviderTest()
69 : got_results_(false),
70 expect_unhashed_value_(GetParam()),
71 provider_(new AntiVirusMetricsProvider(
72 content::BrowserThread::GetMessageLoopProxyForThread(
73 content::BrowserThread::FILE))),
74 thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD),
75 weak_ptr_factory_(this) {}
76
77 void GetMetricsCallback() {
78 ASSERT_TRUE(base::MessageLoop::current()->is_running());
79 base::MessageLoop::current()->QuitWhenIdle();
80
81 got_results_ = true;
82
83 metrics::SystemProfileProto system_profile;
84 provider_->ProvideSystemProfileMetrics(&system_profile);
85
86 VerifySystemProfileData(system_profile, expect_unhashed_value_);
87 // This looks weird, but it's to make sure that reading the data out of the
88 // AntiVirusMetricsProvider does not invalidate it, as the class should be
89 // resilient to this.
90 system_profile.Clear();
91 provider_->ProvideSystemProfileMetrics(&system_profile);
92 VerifySystemProfileData(system_profile, expect_unhashed_value_);
93 }
94
95 bool got_results_;
96 bool expect_unhashed_value_;
97 std::unique_ptr<AntiVirusMetricsProvider> provider_;
98 content::TestBrowserThreadBundle thread_bundle_;
99 base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_;
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest);
103 };
104
105 TEST_P(AntiVirusMetricsProviderTest, GetMetricsFullName) {
106 SetFullNamesFeatureEnabled(expect_unhashed_value_);
107 // Make sure the I/O is happening on the FILE thread by disallowing it on
108 // the main thread.
109 bool previous_value = base::ThreadRestrictions::SetIOAllowed(false);
110 provider_->GetAntiVirusMetrics(
111 base::Bind(&AntiVirusMetricsProviderTest::GetMetricsCallback,
112 weak_ptr_factory_.GetWeakPtr()));
113 content::RunMessageLoop();
114 EXPECT_TRUE(got_results_);
115 base::ThreadRestrictions::SetIOAllowed(previous_value);
116 }
117
118 INSTANTIATE_TEST_CASE_P(, AntiVirusMetricsProviderTest, ::testing::Bool());
OLDNEW
« no previous file with comments | « chrome/browser/metrics/antivirus_metrics_provider_win.cc ('k') | chrome/browser/metrics/chrome_metrics_service_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698