File: webstore_installer_test.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 4,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/webstore_installer_test.h"

#include <memory>

#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/host_port_pair.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/switches.h"

using net::test_server::HttpRequest;

WebstoreInstallerTest::WebstoreInstallerTest(
    const std::string& webstore_domain,
    const std::string& test_data_path,
    const std::string& crx_filename,
    const std::string& verified_domain,
    const std::string& unverified_domain)
    : webstore_domain_(webstore_domain),
      test_data_path_(test_data_path),
      crx_filename_(crx_filename),
      verified_domain_(verified_domain),
      unverified_domain_(unverified_domain) {
}

WebstoreInstallerTest::~WebstoreInstallerTest() = default;

void WebstoreInstallerTest::SetUpCommandLine(base::CommandLine* command_line) {
  extensions::ExtensionBrowserTest::SetUpCommandLine(command_line);

  embedded_test_server()->RegisterRequestMonitor(base::BindRepeating(
      &WebstoreInstallerTest::ProcessServerRequest, base::Unretained(this)));
  // We start the test server now instead of in
  // SetUpInProcessBrowserTestFixture so that we can get its port number.
  ASSERT_TRUE(embedded_test_server()->Start());

  net::HostPortPair host_port = embedded_test_server()->host_port_pair();
  test_gallery_url_ =
      base::StringPrintf("http://%s:%d/%s", webstore_domain_.c_str(),
                         host_port.port(), test_data_path_.c_str());
  command_line->AppendSwitchASCII(
      switches::kAppsGalleryURL, test_gallery_url_);

  GURL crx_url = GenerateTestServerUrl(webstore_domain_, crx_filename_);
  base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      switches::kAppsGalleryUpdateURL, crx_url.spec());

  // Allow tests to call window.gc(), so that we can check that callback
  // functions don't get collected prematurely.
  command_line->AppendSwitchASCII(blink::switches::kJavaScriptFlags,
                                  "--expose-gc");
}

void WebstoreInstallerTest::SetUpOnMainThread() {
  extensions::ExtensionBrowserTest::SetUpOnMainThread();

  host_resolver()->AddRule(webstore_domain_, "127.0.0.1");
  host_resolver()->AddRule(verified_domain_, "127.0.0.1");
  host_resolver()->AddRule(unverified_domain_, "127.0.0.1");

  // This policy set to 1 should not restrict webstore extension installs, even
  // if .crx files technically fit the definition of a dangerous file type.
  // Setting this value for every `WebstoreInstaller` allows for validation that
  // this policy doesn't interfere with the normal extension install workflow.
  profile()->GetPrefs()->SetInteger(policy::policy_prefs::kDownloadRestrictions,
                                    1);
}

GURL WebstoreInstallerTest::GenerateTestServerUrl(
    const std::string& domain,
    const std::string& page_filename) {
  GURL page_url = embedded_test_server()->GetURL(base::StringPrintf(
      "/%s/%s", test_data_path_.c_str(), page_filename.c_str()));

  GURL::Replacements replace_host;
  replace_host.SetHostStr(domain);
  return page_url.ReplaceComponents(replace_host);
}

void WebstoreInstallerTest::ProcessServerRequest(const HttpRequest& request) {}

void WebstoreInstallerTest::AutoAcceptInstall() {
  install_auto_confirm_.reset();  // Destroy any old override first.
  install_auto_confirm_ =
      std::make_unique<extensions::ScopedTestDialogAutoConfirm>(
          extensions::ScopedTestDialogAutoConfirm::ACCEPT);
}

void WebstoreInstallerTest::AutoCancelInstall() {
  install_auto_confirm_.reset();  // Destroy any old override first.
  install_auto_confirm_ =
      std::make_unique<extensions::ScopedTestDialogAutoConfirm>(
          extensions::ScopedTestDialogAutoConfirm::CANCEL);
}