Chromium Code Reviews| 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 void setFeatureEnabled(bool enabled) { | |
|
Alexei Svitkine (slow)
2016/06/02 21:46:57
Nit: set -> Set
Will Harris
2016/06/02 23:57:17
Done.
| |
| 28 base::FeatureList::ClearInstanceForTesting(); | |
| 29 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 30 if (enabled) { | |
| 31 feature_list->InitializeFromCommandLine( | |
| 32 AntiVirusMetricsProvider::kReportFullAvProductDetailsName, | |
| 33 std::string()); | |
| 34 } else { | |
| 35 feature_list->InitializeFromCommandLine( | |
| 36 std::string(), | |
| 37 AntiVirusMetricsProvider::kReportFullAvProductDetailsName); | |
| 38 } | |
| 39 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 class AntiVirusMetricsProviderTest : public testing::Test { | |
| 45 public: | |
| 46 AntiVirusMetricsProviderTest() | |
| 47 : got_results_(false), | |
| 48 expect_unhashed_value_(false), | |
| 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 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 65 bool defender_found = false; | |
| 66 for (const auto& av : system_profile.antivirus_product()) { | |
| 67 if (av.product_name_hash() == metrics::HashName("Windows Defender")) { | |
| 68 defender_found = true; | |
| 69 if (expect_unhashed_value_) { | |
| 70 EXPECT_TRUE(av.has_product_name()); | |
| 71 EXPECT_EQ(av.product_name(), "Windows Defender"); | |
| 72 } else { | |
| 73 EXPECT_FALSE(av.has_product_name()); | |
| 74 } | |
| 75 break; | |
| 76 } | |
| 77 } | |
| 78 EXPECT_TRUE(defender_found); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void internalRunTest(bool enabled) { | |
| 83 expect_unhashed_value_ = enabled; | |
| 84 setFeatureEnabled(enabled); | |
| 85 // Make sure the I/O is happening on the FILE thread by disallowing it on | |
| 86 // the main thread. | |
| 87 bool previous_value = base::ThreadRestrictions::SetIOAllowed(false); | |
| 88 provider_->GetAntiVirusMetrics( | |
| 89 base::Bind(&AntiVirusMetricsProviderTest::GetMetricsCallback, | |
| 90 weak_ptr_factory_.GetWeakPtr())); | |
| 91 content::RunMessageLoop(); | |
| 92 EXPECT_TRUE(got_results_); | |
| 93 base::ThreadRestrictions::SetIOAllowed(previous_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 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest); | |
| 103 }; | |
| 104 | |
| 105 TEST_F(AntiVirusMetricsProviderTest, GetAvProducts) { | |
|
Will Harris
2016/06/02 23:57:18
this test wasn't adding any value any more so I re
| |
| 106 // Windows 8 and above ships with a built-in AV product called | |
| 107 // "Windows Defender" so it is possible to check for this on those platforms. | |
| 108 // | |
| 109 // On previous versions of Windows, the testing has to be manual since the | |
| 110 // API to add a new AV is private. | |
| 111 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 112 AntiVirusMetricsProvider::AvProductList products; | |
| 113 bool result = AntiVirusMetricsProvider::FillAntiVirusProducts(&products); | |
| 114 ASSERT_TRUE(result); | |
| 115 | |
| 116 bool defender_found = false; | |
| 117 for (const auto& product : products) { | |
| 118 if (product.product_name == L"Windows Defender") { | |
|
Alexei Svitkine (slow)
2016/06/02 21:46:57
Nit: Make this a constant at the top of the file.
Will Harris
2016/06/02 23:57:18
Done, but put it closest to first use.
| |
| 119 base::Version defender_version( | |
| 120 base::SysWideToUTF8(product.product_version)); | |
| 121 EXPECT_TRUE(defender_version.IsValid()); | |
| 122 defender_found = true; | |
| 123 break; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 EXPECT_TRUE(defender_found); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 TEST_F(AntiVirusMetricsProviderTest, GetMetricsFullName) { | |
| 132 internalRunTest(true); | |
|
Alexei Svitkine (slow)
2016/06/02 21:46:57
I think you're looking for TEST_P() - please use t
Will Harris
2016/06/02 23:57:17
I did not know about TEST_P. That's cool. Thanks!
| |
| 133 } | |
| 134 | |
| 135 TEST_F(AntiVirusMetricsProviderTest, GetMetricsHashOnly) { | |
| 136 internalRunTest(false); | |
| 137 } | |
| OLD | NEW |