| OLD | NEW |
| (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::kFeature.name, std::string()); |
| 35 } else { |
| 36 feature_list->InitializeFromCommandLine( |
| 37 std::string(), AntiVirusMetricsProvider::kFeature.name); |
| 38 } |
| 39 base::FeatureList::SetInstance(std::move(feature_list)); |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 class AntiVirusMetricsProviderTest : public ::testing::TestWithParam<bool> { |
| 45 public: |
| 46 AntiVirusMetricsProviderTest() |
| 47 : got_results_(false), |
| 48 expect_unhashed_value_(GetParam()), |
| 49 provider_(new AntiVirusMetricsProvider( |
| 50 content::BrowserThread::GetMessageLoopProxyForThread( |
| 51 content::BrowserThread::FILE))), |
| 52 thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD), |
| 53 weak_ptr_factory_(this) {} |
| 54 |
| 55 void GetMetricsCallback() { |
| 56 ASSERT_TRUE(base::MessageLoop::current()->is_running()); |
| 57 base::MessageLoop::current()->QuitWhenIdle(); |
| 58 |
| 59 got_results_ = true; |
| 60 |
| 61 metrics::SystemProfileProto system_profile; |
| 62 provider_->ProvideSystemProfileMetrics(&system_profile); |
| 63 |
| 64 const char kWindowsDefender[] = "Windows Defender"; |
| 65 |
| 66 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| 67 bool defender_found = false; |
| 68 for (const auto& av : system_profile.antivirus_product()) { |
| 69 if (av.product_name_hash() == metrics::HashName(kWindowsDefender)) { |
| 70 defender_found = true; |
| 71 if (expect_unhashed_value_) { |
| 72 EXPECT_TRUE(av.has_product_name()); |
| 73 EXPECT_EQ(kWindowsDefender, av.product_name()); |
| 74 } else { |
| 75 EXPECT_FALSE(av.has_product_name()); |
| 76 } |
| 77 break; |
| 78 } |
| 79 } |
| 80 EXPECT_TRUE(defender_found); |
| 81 } |
| 82 } |
| 83 |
| 84 bool got_results_; |
| 85 bool expect_unhashed_value_; |
| 86 std::unique_ptr<AntiVirusMetricsProvider> provider_; |
| 87 content::TestBrowserThreadBundle thread_bundle_; |
| 88 base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_; |
| 89 |
| 90 private: |
| 91 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest); |
| 92 }; |
| 93 |
| 94 TEST_P(AntiVirusMetricsProviderTest, GetMetricsFullName) { |
| 95 SetFullNamesFeatureEnabled(expect_unhashed_value_); |
| 96 // Make sure the I/O is happening on the FILE thread by disallowing it on |
| 97 // the main thread. |
| 98 bool previous_value = base::ThreadRestrictions::SetIOAllowed(false); |
| 99 provider_->GetAntiVirusMetrics( |
| 100 base::Bind(&AntiVirusMetricsProviderTest::GetMetricsCallback, |
| 101 weak_ptr_factory_.GetWeakPtr())); |
| 102 content::RunMessageLoop(); |
| 103 EXPECT_TRUE(got_results_); |
| 104 base::ThreadRestrictions::SetIOAllowed(previous_value); |
| 105 } |
| 106 |
| 107 INSTANTIATE_TEST_CASE_P(, AntiVirusMetricsProviderTest, ::testing::Bool()); |
| OLD | NEW |