File: extension_modules_apitest.cc

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (89 lines) | stat: -rw-r--r-- 3,305 bytes parent folder | download | duplicates (2)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/test/test_host_resolver.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/http_request.h"

namespace extensions {

namespace {

constexpr const char kExampleURL[] = "www.example.com";

void MonitorModuleRequest(bool* seen_module_request,
                          const net::test_server::HttpRequest& request) {
  const GURL url = request.GetURL();
  if (url.SchemeIsCryptographic() && url.path() == "/hello_module.js")
    *seen_module_request = true;
}

}  // namespace

class ExtensionModuleTest : public ExtensionApiTest {
 public:
  ExtensionModuleTest() = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
    ExtensionApiTest::SetUpCommandLine(command_line);
  }

  void SetUpOnMainThread() override {
    host_resolver()->AddRule(kExampleURL, "127.0.0.1");
    ExtensionApiTest::SetUpOnMainThread();
  }

  DISALLOW_COPY_AND_ASSIGN(ExtensionModuleTest);
};

IN_PROC_BROWSER_TEST_F(ExtensionModuleTest, TestModulesAvailable) {
  ASSERT_TRUE(RunExtensionTest("modules")) << message_;
}

// Tests that extensions can load modules from web (e.g. https:// url).
IN_PROC_BROWSER_TEST_F(ExtensionModuleTest, ModuleFromWeb) {
  net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
  https_server.AddDefaultHandlers(base::FilePath(FILE_PATH_LITERAL(
      "chrome/test/data/extensions/api_test/module_from_web")));
  bool https_server_seen_module_request = false;
  https_server.RegisterRequestMonitor(base::BindRepeating(
      &MonitorModuleRequest, &https_server_seen_module_request));
  ASSERT_TRUE(https_server.Start());

  const GURL https_module_src =
      https_server.GetURL(kExampleURL, "/hello_module.js");

  TestExtensionDir test_dir;
  // The manifest requires host and CSP permission to |kExampleURL|.
  test_dir.WriteManifest(base::StringPrintf(R"({
      "name": "Modules over https",
      "manifest_version": 2,
      "version": "0.1",
      "background": {"page": "background.html"},
      "content_security_policy":
          "script-src 'self' https://%s:*; object-src 'self'",
      "permissions": ["https://%s:*/*"]
  })", kExampleURL, kExampleURL));
  test_dir.WriteFile(FILE_PATH_LITERAL("background.html"),
                     R"(<script src="background.js" type="module"></script>)");
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"),
                     base::StringPrintf(
                         R"(import {helloModule} from '%s'; helloModule();)",
                         https_module_src.spec().c_str()));

  ResultCatcher catcher;
  const Extension* extension = LoadExtension(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);
  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();

  EXPECT_TRUE(https_server_seen_module_request);
}

}  // namespace extensions