File: extension_modules_apitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (92 lines) | stat: -rw-r--r-- 3,461 bytes parent folder | download | duplicates (7)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/test/browser_test.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;

  ExtensionModuleTest(const ExtensionModuleTest&) = delete;
  ExtensionModuleTest& operator=(const ExtensionModuleTest&) = delete;

  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();
  }
};

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