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

Side by Side Diff: chrome/browser/component_updater/sw_reporter_installer_win_unittest.cc

Issue 2226133005: Add support for the ExperimentalSwReporterEngine field trial. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Set flags explicitly in srt_fetcher browsertest Created 4 years, 3 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h" 5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h"
6 6
7 #include <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector>
12 11
13 #include "base/bind.h" 12 #include "base/bind.h"
14 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
15 #include "base/command_line.h" 14 #include "base/command_line.h"
16 #include "base/feature_list.h" 15 #include "base/feature_list.h"
17 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
18 #include "base/json/json_reader.h" 17 #include "base/json/json_reader.h"
19 #include "base/macros.h" 18 #include "base/macros.h"
20 #include "base/metrics/field_trial.h" 19 #include "base/metrics/field_trial.h"
21 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
22 #include "base/test/histogram_tester.h" 21 #include "base/test/histogram_tester.h"
23 #include "base/test/scoped_feature_list.h" 22 #include "base/test/scoped_feature_list.h"
24 #include "base/values.h" 23 #include "base/values.h"
25 #include "base/version.h" 24 #include "base/version.h"
26 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" 25 #include "chrome/browser/safe_browsing/srt_fetcher_win.h"
27 #include "components/variations/variations_associated_data.h" 26 #include "components/variations/variations_associated_data.h"
28 #include "content/public/test/test_browser_thread_bundle.h" 27 #include "content/public/test/test_browser_thread_bundle.h"
29 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
30 29
31 namespace component_updater { 30 namespace component_updater {
32 31
33 namespace { 32 namespace {
34 33
35 constexpr char kRegistrySuffixSwitch[] = "registry-suffix"; 34 constexpr char kRegistrySuffixSwitch[] = "registry-suffix";
36 constexpr char kErrorHistogramName[] = "SoftwareReporter.ExperimentErrors"; 35 constexpr char kErrorHistogramName[] = "SoftwareReporter.ExperimentErrors";
37 constexpr char kExperimentTag[] = "experiment_tag"; 36 constexpr char kExperimentTag[] = "experiment_tag";
38 constexpr char kMissingTag[] = "missing_tag"; 37 constexpr char kMissingTag[] = "missing_tag";
39 38
39 using safe_browsing::SwReporterInvocation;
40
40 } // namespace 41 } // namespace
41 42
42 class SwReporterInstallerTest : public ::testing::Test { 43 class SwReporterInstallerTest : public ::testing::Test {
43 public: 44 public:
44 SwReporterInstallerTest() 45 SwReporterInstallerTest()
45 : launched_callback_( 46 : launched_callback_(
46 base::Bind(&SwReporterInstallerTest::SwReporterLaunched, 47 base::Bind(&SwReporterInstallerTest::SwReporterLaunched,
47 base::Unretained(this))), 48 base::Unretained(this))),
48 default_version_("1.2.3"), 49 default_version_("1.2.3"),
49 default_path_(L"C:\\full\\path\\to\\download") {} 50 default_path_(L"C:\\full\\path\\to\\download"),
51 launched_version_("0.0.0") {}
50 52
51 ~SwReporterInstallerTest() override {} 53 ~SwReporterInstallerTest() override {}
52 54
53 protected: 55 protected:
54 void SwReporterLaunched(const safe_browsing::SwReporterInvocation& invocation, 56 void SwReporterLaunched(const safe_browsing::SwReporterQueue& invocations,
55 const base::Version& version) { 57 const base::Version& version) {
56 ASSERT_TRUE(launched_invocations_.empty()); 58 ASSERT_TRUE(launched_invocations_.empty());
57 launched_invocations_.push_back(invocation); 59 launched_invocations_ = invocations;
58 launched_version_ = version; 60 launched_version_ = version;
59 } 61 }
60 62
61 base::FilePath MakeTestFilePath(const base::FilePath& path) const { 63 base::FilePath MakeTestFilePath(const base::FilePath& path) const {
62 return path.Append(L"software_reporter_tool.exe"); 64 return path.Append(L"software_reporter_tool.exe");
63 } 65 }
64 66
65 void ExpectEmptyAttributes(const SwReporterInstallerTraits& traits) const { 67 void ExpectEmptyAttributes(const SwReporterInstallerTraits& traits) const {
66 update_client::InstallerAttributes attributes = 68 update_client::InstallerAttributes attributes =
67 traits.GetInstallerAttributes(); 69 traits.GetInstallerAttributes();
68 EXPECT_TRUE(attributes.empty()); 70 EXPECT_TRUE(attributes.empty());
69 } 71 }
70 72
71 // Expects that the SwReporter was launched exactly once, with no arguments. 73 // Expects that the SwReporter was launched exactly once, with no arguments.
72 void ExpectDefaultInvocation() const { 74 void ExpectDefaultInvocation() const {
73 EXPECT_EQ(default_version_, launched_version_); 75 EXPECT_EQ(default_version_, launched_version_);
74 ASSERT_EQ(1U, launched_invocations_.size()); 76 ASSERT_EQ(1U, launched_invocations_.size());
75 77
76 const safe_browsing::SwReporterInvocation& invocation = 78 const SwReporterInvocation& invocation = launched_invocations_.front();
77 launched_invocations_[0];
78 EXPECT_EQ(MakeTestFilePath(default_path_), 79 EXPECT_EQ(MakeTestFilePath(default_path_),
79 invocation.command_line.GetProgram()); 80 invocation.command_line.GetProgram());
80 EXPECT_TRUE(invocation.command_line.GetSwitches().empty()); 81 EXPECT_TRUE(invocation.command_line.GetSwitches().empty());
81 EXPECT_TRUE(invocation.command_line.GetArgs().empty()); 82 EXPECT_TRUE(invocation.command_line.GetArgs().empty());
82 EXPECT_TRUE(invocation.suffix.empty()); 83 EXPECT_TRUE(invocation.suffix.empty());
83 EXPECT_FALSE(invocation.is_experimental); 84 EXPECT_EQ(SwReporterInvocation::FLAG_LOG_TO_RAPPOR |
85 SwReporterInvocation::FLAG_LOG_EXIT_CODE_TO_PREFS |
86 SwReporterInvocation::FLAG_TRIGGER_PROMPT |
87 SwReporterInvocation::FLAG_SEND_REPORTER_LOGS,
88 invocation.flags);
84 } 89 }
85 90
86 // |ComponentReady| asserts that it is run on the UI thread, so we must 91 // |ComponentReady| asserts that it is run on the UI thread, so we must
87 // create test threads before calling it. 92 // create test threads before calling it.
88 content::TestBrowserThreadBundle threads_; 93 content::TestBrowserThreadBundle threads_;
89 94
90 // Bound callback to the |SwReporterLaunched| method. 95 // Bound callback to the |SwReporterLaunched| method.
91 SwReporterRunner launched_callback_; 96 SwReporterRunner launched_callback_;
92 97
93 // Default parameters for |ComponentReady|. 98 // Default parameters for |ComponentReady|.
94 base::Version default_version_; 99 base::Version default_version_;
95 base::FilePath default_path_; 100 base::FilePath default_path_;
96 101
97 // Results of running |ComponentReady|. 102 // Results of running |ComponentReady|.
98 std::vector<safe_browsing::SwReporterInvocation> launched_invocations_; 103 safe_browsing::SwReporterQueue launched_invocations_;
99 base::Version launched_version_; 104 base::Version launched_version_;
100 105
101 private: 106 private:
102 DISALLOW_COPY_AND_ASSIGN(SwReporterInstallerTest); 107 DISALLOW_COPY_AND_ASSIGN(SwReporterInstallerTest);
103 }; 108 };
104 109
105 // This class contains extended setup that is only used for tests of the 110 // This class contains extended setup that is only used for tests of the
106 // experimental reporter. 111 // experimental reporter.
107 class ExperimentalSwReporterInstallerTest : public SwReporterInstallerTest { 112 class ExperimentalSwReporterInstallerTest : public SwReporterInstallerTest {
108 public: 113 public:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 // |expected_suffix| and one |expected_additional_argument| on the 159 // |expected_suffix| and one |expected_additional_argument| on the
155 // command-line. (|expected_additional_argument| mainly exists to test that 160 // command-line. (|expected_additional_argument| mainly exists to test that
156 // arguments are included at all, so there is no need to test for 161 // arguments are included at all, so there is no need to test for
157 // combinations of multiple arguments and switches in this function.) 162 // combinations of multiple arguments and switches in this function.)
158 void ExpectExperimentalInvocation( 163 void ExpectExperimentalInvocation(
159 const std::string& expected_suffix, 164 const std::string& expected_suffix,
160 const base::string16& expected_additional_argument) { 165 const base::string16& expected_additional_argument) {
161 EXPECT_EQ(default_version_, launched_version_); 166 EXPECT_EQ(default_version_, launched_version_);
162 ASSERT_EQ(1U, launched_invocations_.size()); 167 ASSERT_EQ(1U, launched_invocations_.size());
163 168
164 const safe_browsing::SwReporterInvocation& invocation = 169 const SwReporterInvocation& invocation = launched_invocations_.front();
165 launched_invocations_[0];
166 EXPECT_EQ(MakeTestFilePath(default_path_), 170 EXPECT_EQ(MakeTestFilePath(default_path_),
167 invocation.command_line.GetProgram()); 171 invocation.command_line.GetProgram());
168 172
169 if (expected_suffix.empty()) { 173 if (expected_suffix.empty()) {
170 EXPECT_TRUE(invocation.command_line.GetSwitches().empty()); 174 EXPECT_TRUE(invocation.command_line.GetSwitches().empty());
171 EXPECT_TRUE(invocation.suffix.empty()); 175 EXPECT_TRUE(invocation.suffix.empty());
172 } else { 176 } else {
173 EXPECT_EQ(1U, invocation.command_line.GetSwitches().size()); 177 EXPECT_EQ(1U, invocation.command_line.GetSwitches().size());
174 EXPECT_EQ(expected_suffix, invocation.command_line.GetSwitchValueASCII( 178 EXPECT_EQ(expected_suffix, invocation.command_line.GetSwitchValueASCII(
175 kRegistrySuffixSwitch)); 179 kRegistrySuffixSwitch));
176 EXPECT_EQ(expected_suffix, invocation.suffix); 180 EXPECT_EQ(expected_suffix, invocation.suffix);
177 } 181 }
178 182
179 if (expected_additional_argument.empty()) { 183 if (expected_additional_argument.empty()) {
180 EXPECT_TRUE(invocation.command_line.GetArgs().empty()); 184 EXPECT_TRUE(invocation.command_line.GetArgs().empty());
181 } else { 185 } else {
182 EXPECT_EQ(1U, invocation.command_line.GetArgs().size()); 186 EXPECT_EQ(1U, invocation.command_line.GetArgs().size());
183 EXPECT_EQ(expected_additional_argument, 187 EXPECT_EQ(expected_additional_argument,
184 invocation.command_line.GetArgs()[0]); 188 invocation.command_line.GetArgs()[0]);
185 } 189 }
186 190
187 EXPECT_TRUE(invocation.is_experimental); 191 EXPECT_EQ(0U, invocation.flags);
188 histograms_.ExpectTotalCount(kErrorHistogramName, 0); 192 histograms_.ExpectTotalCount(kErrorHistogramName, 0);
189 } 193 }
190 194
191 void ExpectLaunchError() { 195 void ExpectLaunchError() {
192 // The SwReporter should not be launched, and an error should be logged. 196 // The SwReporter should not be launched, and an error should be logged.
193 EXPECT_TRUE(launched_invocations_.empty()); 197 EXPECT_TRUE(launched_invocations_.empty());
194 histograms_.ExpectUniqueSample(kErrorHistogramName, 198 histograms_.ExpectUniqueSample(kErrorHistogramName,
195 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1); 199 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1);
196 } 200 }
197 201
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 271
268 TEST_F(ExperimentalSwReporterInstallerTest, SingleInvocation) { 272 TEST_F(ExperimentalSwReporterInstallerTest, SingleInvocation) {
269 SwReporterInstallerTraits traits(launched_callback_, true); 273 SwReporterInstallerTraits traits(launched_callback_, true);
270 CreateFeatureWithTag(kExperimentTag); 274 CreateFeatureWithTag(kExperimentTag);
271 ExpectAttributesWithTag(traits, kExperimentTag); 275 ExpectAttributesWithTag(traits, kExperimentTag);
272 276
273 static constexpr char kTestManifest[] = 277 static constexpr char kTestManifest[] =
274 "{\"launch_params\": [" 278 "{\"launch_params\": ["
275 " {" 279 " {"
276 " \"arguments\": [\"--engine=experimental\", \"random argument\"]," 280 " \"arguments\": [\"--engine=experimental\", \"random argument\"],"
277 " \"suffix\": \"TestSuffix\"" 281 " \"suffix\": \"TestSuffix\","
282 " \"prompt\": false"
278 " }" 283 " }"
279 "]}"; 284 "]}";
280 traits.ComponentReady( 285 traits.ComponentReady(
281 default_version_, default_path_, 286 default_version_, default_path_,
282 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest))); 287 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
283 288
284 // The SwReporter should be launched once with the given arguments. 289 // The SwReporter should be launched once with the given arguments.
285 EXPECT_EQ(default_version_, launched_version_); 290 EXPECT_EQ(default_version_, launched_version_);
286 ASSERT_EQ(1U, launched_invocations_.size()); 291 ASSERT_EQ(1U, launched_invocations_.size());
287 292
288 const safe_browsing::SwReporterInvocation& invocation = 293 const SwReporterInvocation& invocation = launched_invocations_.front();
289 launched_invocations_[0];
290 EXPECT_EQ(MakeTestFilePath(default_path_), 294 EXPECT_EQ(MakeTestFilePath(default_path_),
291 invocation.command_line.GetProgram()); 295 invocation.command_line.GetProgram());
292 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size()); 296 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
293 EXPECT_EQ("experimental", 297 EXPECT_EQ("experimental",
294 invocation.command_line.GetSwitchValueASCII("engine")); 298 invocation.command_line.GetSwitchValueASCII("engine"));
295 EXPECT_EQ("TestSuffix", 299 EXPECT_EQ("TestSuffix",
296 invocation.command_line.GetSwitchValueASCII(kRegistrySuffixSwitch)); 300 invocation.command_line.GetSwitchValueASCII(kRegistrySuffixSwitch));
297 ASSERT_EQ(1U, invocation.command_line.GetArgs().size()); 301 ASSERT_EQ(1U, invocation.command_line.GetArgs().size());
298 EXPECT_EQ(L"random argument", invocation.command_line.GetArgs()[0]); 302 EXPECT_EQ(L"random argument", invocation.command_line.GetArgs()[0]);
299 EXPECT_EQ("TestSuffix", invocation.suffix); 303 EXPECT_EQ("TestSuffix", invocation.suffix);
300 EXPECT_TRUE(invocation.is_experimental); 304 EXPECT_EQ(0U, invocation.flags);
301 histograms_.ExpectTotalCount(kErrorHistogramName, 0); 305 histograms_.ExpectTotalCount(kErrorHistogramName, 0);
302 } 306 }
303 307
304 TEST_F(ExperimentalSwReporterInstallerTest, MultipleInvocations) { 308 TEST_F(ExperimentalSwReporterInstallerTest, MultipleInvocations) {
305 SwReporterInstallerTraits traits(launched_callback_, true); 309 SwReporterInstallerTraits traits(launched_callback_, true);
306 CreateFeatureWithTag(kExperimentTag); 310 CreateFeatureWithTag(kExperimentTag);
307 ExpectAttributesWithTag(traits, kExperimentTag); 311 ExpectAttributesWithTag(traits, kExperimentTag);
308 312
309 static constexpr char kTestManifest[] = 313 static constexpr char kTestManifest[] =
310 "{\"launch_params\": [" 314 "{\"launch_params\": ["
311 " {" 315 " {"
312 " \"arguments\": [\"--engine=experimental\", \"random argument\"]," 316 " \"arguments\": [\"--engine=experimental\", \"random argument\"],"
313 " \"suffix\": \"TestSuffix\"" 317 " \"suffix\": \"TestSuffix\","
318 " \"prompt\": false"
314 " }," 319 " },"
315 " {" 320 " {"
316 " \"arguments\": [\"--engine=second\"]," 321 " \"arguments\": [\"--engine=second\"],"
317 " \"suffix\": \"SecondSuffix\"" 322 " \"suffix\": \"SecondSuffix\","
323 " \"prompt\": true"
324 " },"
325 " {"
326 " \"arguments\": [\"--engine=third\"],"
327 " \"suffix\": \"ThirdSuffix\""
318 " }" 328 " }"
319 "]}"; 329 "]}";
320 traits.ComponentReady( 330 traits.ComponentReady(
321 default_version_, default_path_, 331 default_version_, default_path_,
322 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest))); 332 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
323 333
324 // Not supported yet. 334 // The SwReporter should be launched three times with the given arguments.
325 ExpectLaunchError(); 335 EXPECT_EQ(default_version_, launched_version_);
336 ASSERT_EQ(3U, launched_invocations_.size());
337
338 {
339 SwReporterInvocation invocation = launched_invocations_.front();
340 launched_invocations_.pop();
341 EXPECT_EQ(MakeTestFilePath(default_path_),
342 invocation.command_line.GetProgram());
343 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
344 EXPECT_EQ("experimental",
345 invocation.command_line.GetSwitchValueASCII("engine"));
346 EXPECT_EQ("TestSuffix", invocation.command_line.GetSwitchValueASCII(
347 kRegistrySuffixSwitch));
348 ASSERT_EQ(1U, invocation.command_line.GetArgs().size());
349 EXPECT_EQ(L"random argument", invocation.command_line.GetArgs()[0]);
350 EXPECT_EQ("TestSuffix", invocation.suffix);
351 EXPECT_EQ(0U, invocation.flags);
352 }
353
354 {
355 SwReporterInvocation invocation = launched_invocations_.front();
356 launched_invocations_.pop();
357 EXPECT_EQ(MakeTestFilePath(default_path_),
358 invocation.command_line.GetProgram());
359 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
360 EXPECT_EQ("second", invocation.command_line.GetSwitchValueASCII("engine"));
361 EXPECT_EQ("SecondSuffix", invocation.command_line.GetSwitchValueASCII(
362 kRegistrySuffixSwitch));
363 ASSERT_TRUE(invocation.command_line.GetArgs().empty());
364 EXPECT_EQ("SecondSuffix", invocation.suffix);
365 EXPECT_EQ(SwReporterInvocation::FLAG_TRIGGER_PROMPT, invocation.flags);
366 }
367
368 {
369 SwReporterInvocation invocation = launched_invocations_.front();
370 launched_invocations_.pop();
371 EXPECT_EQ(MakeTestFilePath(default_path_),
372 invocation.command_line.GetProgram());
373 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
374 EXPECT_EQ("third", invocation.command_line.GetSwitchValueASCII("engine"));
375 EXPECT_EQ("ThirdSuffix", invocation.command_line.GetSwitchValueASCII(
376 kRegistrySuffixSwitch));
377 ASSERT_TRUE(invocation.command_line.GetArgs().empty());
378 EXPECT_EQ("ThirdSuffix", invocation.suffix);
379 // A missing "prompt" key means "false".
380 EXPECT_EQ(0U, invocation.flags);
381 }
382
383 histograms_.ExpectTotalCount(kErrorHistogramName, 0);
326 } 384 }
327 385
328 TEST_F(ExperimentalSwReporterInstallerTest, MissingSuffix) { 386 TEST_F(ExperimentalSwReporterInstallerTest, MissingSuffix) {
329 SwReporterInstallerTraits traits(launched_callback_, true); 387 SwReporterInstallerTraits traits(launched_callback_, true);
330 CreateFeatureWithTag(kExperimentTag); 388 CreateFeatureWithTag(kExperimentTag);
331 389
332 static constexpr char kTestManifest[] = 390 static constexpr char kTestManifest[] =
333 "{\"launch_params\": [" 391 "{\"launch_params\": ["
334 " {" 392 " {"
335 " \"arguments\": [\"random argument\"]" 393 " \"arguments\": [\"random argument\"]"
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 traits.ComponentReady( 674 traits.ComponentReady(
617 default_version_, default_path_, 675 default_version_, default_path_,
618 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest))); 676 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
619 677
620 // The SwReporter should not be launched, and an error should be logged. 678 // The SwReporter should not be launched, and an error should be logged.
621 EXPECT_TRUE(launched_invocations_.empty()); 679 EXPECT_TRUE(launched_invocations_.empty());
622 histograms_.ExpectUniqueSample(kErrorHistogramName, 680 histograms_.ExpectUniqueSample(kErrorHistogramName,
623 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1); 681 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1);
624 } 682 }
625 683
684 TEST_F(ExperimentalSwReporterInstallerTest, BadTypesInManifest4) {
685 SwReporterInstallerTraits traits(launched_callback_, true);
686 CreateFeatureWithTag(kExperimentTag);
687
688 // This has an int instead of a bool for prompt.
689 static constexpr char kTestManifest[] =
690 "{\"launch_params\": ["
691 " {"
692 " \"arguments\": [\"--engine=experimental\"],"
693 " \"suffix\": \"TestSuffix\","
694 " \"prompt\": 1"
695 " }"
696 "]}";
697 traits.ComponentReady(
698 default_version_, default_path_,
699 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
700
701 // The SwReporter should not be launched, and an error should be logged.
702 EXPECT_TRUE(launched_invocations_.empty());
703 histograms_.ExpectUniqueSample(kErrorHistogramName,
704 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1);
705 }
626 } // namespace component_updater 706 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698