| 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/memory/ref_counted.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/version.h" |
| 17 #include "base/win/windows_version.h" |
| 18 #include "components/variations/metrics_util.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "content/public/test/test_utils.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace metrics { |
| 25 |
| 26 class AntiVirusMetricsProviderTest : public testing::Test { |
| 27 public: |
| 28 AntiVirusMetricsProviderTest() |
| 29 : got_results_(false), |
| 30 provider_(new AntiVirusMetricsProvider( |
| 31 content::BrowserThread::GetMessageLoopProxyForThread( |
| 32 content::BrowserThread::FILE))), |
| 33 thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD), |
| 34 weak_ptr_factory_(this) {} |
| 35 |
| 36 void GetMetricsCallback() { |
| 37 ASSERT_TRUE(base::MessageLoop::current()->is_running()); |
| 38 base::MessageLoop::current()->QuitWhenIdle(); |
| 39 |
| 40 got_results_ = true; |
| 41 |
| 42 metrics::SystemProfileProto system_profile; |
| 43 provider_->ProvideSystemProfileMetrics(&system_profile); |
| 44 |
| 45 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| 46 bool defender_found = false; |
| 47 for (const auto& av : system_profile.antivirus_product()) { |
| 48 if (av.product_name_hash() == HashName("Windows Defender")) { |
| 49 defender_found = true; |
| 50 break; |
| 51 } |
| 52 } |
| 53 EXPECT_TRUE(defender_found); |
| 54 } |
| 55 } |
| 56 |
| 57 bool got_results_; |
| 58 std::unique_ptr<AntiVirusMetricsProvider> provider_; |
| 59 content::TestBrowserThreadBundle thread_bundle_; |
| 60 base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest); |
| 63 }; |
| 64 |
| 65 TEST_F(AntiVirusMetricsProviderTest, GetAvProducts) { |
| 66 // Windows 8 and above ships with a built-in AV product called |
| 67 // "Windows Defender" so it is possible to check for this on those platforms. |
| 68 // |
| 69 // On previous versions of Windows, the testing has to be manual since the |
| 70 // API to add a new AV is private. |
| 71 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| 72 AntiVirusMetricsProvider::AvProductList products; |
| 73 bool result = AntiVirusMetricsProvider::FillAntiVirusProducts(&products); |
| 74 ASSERT_TRUE(result); |
| 75 |
| 76 bool defender_found = false; |
| 77 for (const auto& product : products) { |
| 78 if (product.product_name == L"Windows Defender") { |
| 79 base::Version defender_version( |
| 80 base::SysWideToUTF8(product.product_version)); |
| 81 EXPECT_TRUE(defender_version.IsValid()); |
| 82 defender_found = true; |
| 83 break; |
| 84 } |
| 85 } |
| 86 |
| 87 EXPECT_TRUE(defender_found); |
| 88 } |
| 89 } |
| 90 |
| 91 TEST_F(AntiVirusMetricsProviderTest, GetMetrics) { |
| 92 // Make sure the I/O is happening on the FILE thread by disallowing it on the |
| 93 // main thread. |
| 94 bool previous_value = base::ThreadRestrictions::SetIOAllowed(false); |
| 95 provider_->GetAntiVirusMetrics( |
| 96 base::Bind(&AntiVirusMetricsProviderTest::GetMetricsCallback, |
| 97 weak_ptr_factory_.GetWeakPtr())); |
| 98 content::RunMessageLoop(); |
| 99 EXPECT_TRUE(got_results_); |
| 100 base::ThreadRestrictions::SetIOAllowed(previous_value); |
| 101 } |
| 102 |
| 103 } // namespace metrics |
| OLD | NEW |