File: request_content_script_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 (223 lines) | stat: -rw-r--r-- 8,449 bytes parent folder | download | duplicates (6)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2014 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/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/test_extension_dir.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace {

// Manifest permissions injected into |kManifest|:
constexpr auto kPermissions = std::to_array<const char*>({
    "*://*/*",               // ALL
    "http://127.0.0.1/*",    // PARTICULAR
    "http://nowhere.com/*",  // NOWHERE
});

// Script matchers for injected into |kBackgroundScriptSource|:
constexpr auto kScriptMatchers = std::to_array<const char*>({
    "{ pageUrl: { hostContains: '' } }",           // ALL
    "{ pageUrl: { hostEquals: '127.0.0.1' } }",    // PARTICULAR
    "{ pageUrl: { hostEquals: 'nowhere.com' } }",  // NOWHERE
});

enum PermissionOrMatcherType {
  ALL = 0,
  PARTICULAR,
  NOWHERE
};

// JSON/JS sources:
const char kManifest[] =
    "{\n"
    "  \"name\": \"Test DeclarativeContentScript\",\n"
    "  \"manifest_version\": 2,\n"
    "  \"version\": \"1.0\",\n"
    "  \"description\": \"Test declarative content script interface\",\n"
    "  \"permissions\": [\"declarativeContent\", \"%s\"],\n"
    "  \"background\": {\n"
    "    \"scripts\": [\"background.js\"]\n"
    "  }\n"
    "}\n";
const char kBackgroundScriptSource[] =
    "var declarativeContent = chrome.declarativeContent;\n"
    "var PageStateMatcher = declarativeContent.PageStateMatcher;\n"
    "var RequestContentScript = declarativeContent.RequestContentScript;\n"
    "var onPageChanged = declarativeContent.onPageChanged;\n"
    "onPageChanged.removeRules(undefined, function() {\n"
    "  onPageChanged.addRules(\n"
    "      [{\n"
    "        conditions: [new PageStateMatcher(%s)],\n"
    "        actions: [new RequestContentScript({js: ['script.js']}\n"
    "        )]\n"
    "      }],\n"
    "      function(details) {\n"
    "        if (!chrome.runtime.lastError)\n"
    "          chrome.test.sendMessage('injection setup');\n"
    "      }\n"
    "  );\n"
    "});\n";
const char kContentScriptSource[] =
    "chrome.test.sendMessage('injection succeeded');\n";

// Messages from scripts:
const char kInjectionSetup[] = "injection setup";
const char kInjectionSucceeded[] = "injection succeeded";

// Runs all pending tasks in the renderer associated with |web_contents|.
// Returns true on success.
bool RunAllPendingInRenderer(content::WebContents* web_contents) {
  // TODO(devlin): If too many tests start to need this, move it somewhere
  // common.
  // This is slight hack to achieve a RunPendingInRenderer() method. Since IPCs
  // are sent synchronously, anything started prior to this method will finish
  // before this method returns (as content::ExecJs() is synchronous).
  return content::ExecJs(web_contents, "1 == 1;");
}

}  // namespace

class RequestContentScriptAPITest : public ExtensionBrowserTest {
 public:
  RequestContentScriptAPITest();
  ~RequestContentScriptAPITest() override = default;

  // Performs script injection test on a common local URL using the given
  // |manifest_permission| and |script_matcher|. Does not return until
  // the renderer should have completed its task and any browser-side reactions
  // have been cleared from the task queue.
  testing::AssertionResult RunTest(PermissionOrMatcherType manifest_permission,
                                   PermissionOrMatcherType script_matcher,
                                   bool should_inject);

 private:
  testing::AssertionResult CreateAndLoadExtension(
      PermissionOrMatcherType manifest_permission,
      PermissionOrMatcherType script_matcher);

  std::unique_ptr<TestExtensionDir> test_extension_dir_;
  raw_ptr<const Extension> extension_;
};

RequestContentScriptAPITest::RequestContentScriptAPITest()
    : extension_(nullptr) {}

testing::AssertionResult RequestContentScriptAPITest::RunTest(
    PermissionOrMatcherType manifest_permission,
    PermissionOrMatcherType script_matcher,
    bool should_inject) {
  if (extension_)
    UnloadExtension(extension_->id());
  testing::AssertionResult result = CreateAndLoadExtension(manifest_permission,
                                                           script_matcher);
  if (!result)
    return result;

  // Setup listener for actual injection of script.
  ExtensionTestMessageListener injection_succeeded_listener(
      kInjectionSucceeded);
  injection_succeeded_listener.set_extension_id(extension_->id());

  EXPECT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/extensions/test_file.html")));

  content::WebContents* web_contents =
      browser() ? browser()->tab_strip_model()->GetActiveWebContents()
                : nullptr;
  if (!web_contents)
    return testing::AssertionFailure() << "No web contents.";

  // Give the extension plenty of time to inject.
  if (!RunAllPendingInRenderer(web_contents))
    return testing::AssertionFailure() << "Could not run pending in renderer.";

  // Make sure all running tasks are complete.
  content::RunAllPendingInMessageLoop();

  if (injection_succeeded_listener.was_satisfied() != should_inject) {
    return testing::AssertionFailure()
        << (should_inject ?
            "Expected injection, but got none." :
            "Expected no injection, but got one.");
  }

  return testing::AssertionSuccess();
}

testing::AssertionResult RequestContentScriptAPITest::CreateAndLoadExtension(
    PermissionOrMatcherType manifest_permission,
    PermissionOrMatcherType script_matcher) {
  // Setup a listener to note when injection rules have been setup.
  ExtensionTestMessageListener injection_setup_listener(kInjectionSetup);

  std::string manifest = base::StringPrintf(kManifest,
                                            kPermissions[manifest_permission]);
  std::string background_src = base::StringPrintf(
      kBackgroundScriptSource,
      kScriptMatchers[script_matcher]);

  auto dir = std::make_unique<TestExtensionDir>();
  dir->WriteManifest(manifest);
  dir->WriteFile(FILE_PATH_LITERAL("background.js"), background_src);
  dir->WriteFile(FILE_PATH_LITERAL("script.js"),
                 kContentScriptSource);

  const Extension* extension = LoadExtension(dir->UnpackedPath());
  if (!extension)
    return testing::AssertionFailure() << "Failed to load extension.";

  test_extension_dir_ = std::move(dir);
  extension_ = extension;

  // Wait for rules to be setup before navigating to trigger script injection.
  EXPECT_TRUE(injection_setup_listener.WaitUntilSatisfied());

  return testing::AssertionSuccess();
}


// Try different permutations of "match all", "match particular domain (that is
// visited by test)", and "match nonsense domain (not visited by test)" for
// both manifest permissions and injection matcher conditions.
// http://crbug.com/421118
IN_PROC_BROWSER_TEST_F(RequestContentScriptAPITest,
                       DISABLED_PermissionMatcherAgreementInjection) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // Positive tests: permissions and matcher contain conditions that match URL
  // visited during test.
  EXPECT_TRUE(RunTest(ALL, ALL, true));
  EXPECT_TRUE(RunTest(ALL, PARTICULAR, true));
  EXPECT_TRUE(RunTest(PARTICULAR, ALL, true));
  EXPECT_TRUE(RunTest(PARTICULAR, PARTICULAR, true));

  // Negative tests: permissions or matcher (or both) contain conditions that
  // do not match URL visited during test.
  EXPECT_TRUE(RunTest(NOWHERE, ALL, false));
  EXPECT_TRUE(RunTest(NOWHERE, PARTICULAR, false));
  EXPECT_TRUE(RunTest(NOWHERE, NOWHERE, false));
  EXPECT_TRUE(RunTest(ALL, NOWHERE, false));
  EXPECT_TRUE(RunTest(PARTICULAR, NOWHERE, false));

  // TODO(markdittmer): Add more tests:
  // - Inject script with multiple files
  // - Inject multiple scripts
  // - Match on CSS selector conditions
  // - Match all frames in document containing frames
}

}  // namespace extensions