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

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

Issue 2136423003: Merge M52: Add AntiVirus information to the system profile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2743
Patch Set: Created 4 years, 5 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/test/histogram_tester.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/version.h"
19 #include "base/win/windows_version.h"
20 #include "components/variations/metrics_util.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/test_utils.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace {
27
28 // Helper function to toggle whether the ReportFullAVProductDetails feature is
29 // enabled or not.
30 void SetFullNamesFeatureEnabled(bool enabled) {
31 base::FeatureList::ClearInstanceForTesting();
32 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
33 if (enabled) {
34 feature_list->InitializeFromCommandLine(
35 AntiVirusMetricsProvider::kReportNamesFeature.name, std::string());
36 } else {
37 feature_list->InitializeFromCommandLine(
38 std::string(), AntiVirusMetricsProvider::kReportNamesFeature.name);
39 }
40 base::FeatureList::SetInstance(std::move(feature_list));
41 }
42
43 void VerifySystemProfileData(const metrics::SystemProfileProto& system_profile,
44 bool expect_unhashed_value) {
45 const char kWindowsDefender[] = "Windows Defender";
46
47 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
48 bool defender_found = false;
49 for (const auto& av : system_profile.antivirus_product()) {
50 if (av.product_name_hash() == metrics::HashName(kWindowsDefender)) {
51 defender_found = true;
52 if (expect_unhashed_value) {
53 EXPECT_TRUE(av.has_product_name());
54 EXPECT_EQ(kWindowsDefender, av.product_name());
55 } else {
56 EXPECT_FALSE(av.has_product_name());
57 }
58 break;
59 }
60 }
61 EXPECT_TRUE(defender_found);
62 }
63 }
64
65 } // namespace
66
67 class AntiVirusMetricsProviderTest : public ::testing::TestWithParam<bool> {
68 public:
69 AntiVirusMetricsProviderTest()
70 : got_results_(false),
71 expect_unhashed_value_(GetParam()),
72 provider_(new AntiVirusMetricsProvider(
73 content::BrowserThread::GetMessageLoopProxyForThread(
74 content::BrowserThread::FILE))),
75 thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD),
76 weak_ptr_factory_(this) {}
77
78 void GetMetricsCallback() {
79 ASSERT_TRUE(base::MessageLoop::current()->is_running());
80 base::MessageLoop::current()->QuitWhenIdle();
81
82 got_results_ = true;
83
84 metrics::SystemProfileProto system_profile;
85 provider_->ProvideSystemProfileMetrics(&system_profile);
86
87 VerifySystemProfileData(system_profile, expect_unhashed_value_);
88 // This looks weird, but it's to make sure that reading the data out of the
89 // AntiVirusMetricsProvider does not invalidate it, as the class should be
90 // resilient to this.
91 system_profile.Clear();
92 provider_->ProvideSystemProfileMetrics(&system_profile);
93 VerifySystemProfileData(system_profile, expect_unhashed_value_);
94 }
95
96 bool got_results_;
97 bool expect_unhashed_value_;
98 std::unique_ptr<AntiVirusMetricsProvider> provider_;
99 content::TestBrowserThreadBundle thread_bundle_;
100 base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_;
101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest);
104 };
105
106 TEST_P(AntiVirusMetricsProviderTest, GetMetricsFullName) {
107 base::HistogramTester histograms;
108 SetFullNamesFeatureEnabled(expect_unhashed_value_);
109 // Make sure the I/O is happening on the FILE thread by disallowing it on
110 // the main thread.
111 bool previous_value = base::ThreadRestrictions::SetIOAllowed(false);
112 provider_->GetAntiVirusMetrics(
113 base::Bind(&AntiVirusMetricsProviderTest::GetMetricsCallback,
114 weak_ptr_factory_.GetWeakPtr()));
115 content::RunMessageLoop();
116 EXPECT_TRUE(got_results_);
117 base::ThreadRestrictions::SetIOAllowed(previous_value);
118
119 AntiVirusMetricsProvider::ResultCode expected_result =
120 AntiVirusMetricsProvider::RESULT_SUCCESS;
121 if (base::win::OSInfo::GetInstance()->version_type() ==
122 base::win::SUITE_SERVER)
123 expected_result = AntiVirusMetricsProvider::RESULT_WSC_NOT_AVAILABLE;
124 histograms.ExpectUniqueSample("UMA.AntiVirusMetricsProvider.Result",
125 expected_result, 1);
126 }
127
128 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