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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
// Copyright 2014 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/command_line.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/manifest.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
namespace content {
class ManifestBrowserTest;
// Mock of a WebContentsDelegate that catches messages sent to the console.
class MockWebContentsDelegate : public WebContentsDelegate {
public:
MockWebContentsDelegate(WebContents* web_contents, ManifestBrowserTest* test)
: web_contents_(web_contents),
test_(test) {
}
bool AddMessageToConsole(WebContents* source,
int32 level,
const base::string16& message,
int32 line_no,
const base::string16& source_id) override;
private:
WebContents* web_contents_;
ManifestBrowserTest* test_;
};
class ManifestBrowserTest : public ContentBrowserTest {
protected:
friend MockWebContentsDelegate;
ManifestBrowserTest()
: console_error_count_(0) {
cors_embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
base::FilePath test_data_dir;
CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
cors_embedded_test_server_->ServeFilesFromDirectory(
test_data_dir.AppendASCII("content/test/data/"));
}
~ManifestBrowserTest() override {}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
DCHECK(shell()->web_contents());
mock_web_contents_delegate_.reset(
new MockWebContentsDelegate(shell()->web_contents(), this));
shell()->web_contents()->SetDelegate(mock_web_contents_delegate_.get());
}
void GetManifestAndWait() {
shell()->web_contents()->GetManifest(
base::Bind(&ManifestBrowserTest::OnGetManifest,
base::Unretained(this)));
message_loop_runner_ = new MessageLoopRunner();
message_loop_runner_->Run();
}
void OnGetManifest(const Manifest& manifest) {
manifest_ = manifest;
message_loop_runner_->Quit();
}
const Manifest& manifest() const {
return manifest_;
}
unsigned int console_error_count() const {
return console_error_count_;
}
void OnReceivedConsoleError() {
console_error_count_++;
}
net::test_server::EmbeddedTestServer* cors_embedded_test_server() const {
return cors_embedded_test_server_.get();
}
private:
scoped_refptr<MessageLoopRunner> message_loop_runner_;
scoped_ptr<MockWebContentsDelegate> mock_web_contents_delegate_;
scoped_ptr<net::test_server::EmbeddedTestServer> cors_embedded_test_server_;
Manifest manifest_;
int console_error_count_;
DISALLOW_COPY_AND_ASSIGN(ManifestBrowserTest);
};
// The implementation of AddMessageToConsole isn't inlined because it needs
// to know about |test_|.
bool MockWebContentsDelegate::AddMessageToConsole(
WebContents* source,
int32 level,
const base::string16& message,
int32 line_no,
const base::string16& source_id) {
DCHECK(source == web_contents_);
if (level == logging::LOG_ERROR)
test_->OnReceivedConsoleError();
return false;
}
// If a page has no manifest, requesting a manifest should return the empty
// manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, NoManifest) {
GURL test_url = GetTestUrl("manifest", "no-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page manifest points to a 404 URL, requesting the manifest should return
// the empty manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, 404Manifest) {
GURL test_url = GetTestUrl("manifest", "404-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page has an empty manifest, requesting the manifest should return the
// empty manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, EmptyManifest) {
GURL test_url = GetTestUrl("manifest", "empty-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page's manifest can't be parsed correctly, requesting the manifest
// should return an empty manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParseErrorManifest) {
GURL test_url = GetTestUrl("manifest", "parse-error-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(1u, console_error_count());
}
// If a page has a manifest that can be fetched and parsed, requesting the
// manifest should return a properly filled manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DummyManifest) {
GURL test_url = GetTestUrl("manifest", "dummy-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page changes manifest during its life-time, requesting the manifest
// should return the current manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DynamicManifest) {
GURL test_url = GetTestUrl("manifest", "dynamic-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
{
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
}
{
std::string manifest_url =
GetTestUrl("manifest", "dummy-manifest.json").spec();
ASSERT_TRUE(content::ExecuteScript(
shell()->web_contents(), "setManifestTo('" + manifest_url + "')"));
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
}
{
std::string manifest_url =
GetTestUrl("manifest", "empty-manifest.json").spec();
ASSERT_TRUE(content::ExecuteScript(
shell()->web_contents(), "setManifestTo('" + manifest_url + "')"));
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
}
EXPECT_EQ(0u, console_error_count());
}
// If a page's manifest lives in a different origin, it should follow the CORS
// rules and requesting the manifest should return an empty manifest (unless the
// response contains CORS headers).
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, CORSManifest) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
ASSERT_TRUE(cors_embedded_test_server()->InitializeAndWaitUntilReady());
ASSERT_NE(embedded_test_server()->port(),
cors_embedded_test_server()->port());
GURL test_url =
embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
std::string manifest_url = cors_embedded_test_server()->GetURL(
"/manifest/dummy-manifest.json").spec();
ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
"setManifestTo('" + manifest_url + "')"));
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
// The purpose of this second load is to make sure the first load is fully
// finished. The first load will fail because of Access Control error but the
// underlying Blink loader will continue fetching the file. There is no
// reliable way to know when the fetch is finished from the browser test
// except by fetching the same file from same origin, making it succeed when
// it is actually fully loaded.
manifest_url =
embedded_test_server()->GetURL("/manifest/dummy-manifest.json").spec();
ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
"setManifestTo('" + manifest_url + "')"));
GetManifestAndWait();
}
// If a page's manifest lives in a different origin, it should be accessible if
// it has valid access controls headers.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, CORSManifestWithAcessControls) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
ASSERT_TRUE(cors_embedded_test_server()->InitializeAndWaitUntilReady());
ASSERT_NE(embedded_test_server()->port(),
cors_embedded_test_server()->port());
GURL test_url =
embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
std::string manifest_url = cors_embedded_test_server()->GetURL(
"/manifest/manifest-cors.json").spec();
ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
"setManifestTo('" + manifest_url + "')"));
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page's manifest is in an insecure origin while the page is in a secure
// origin, requesting the manifest should return the empty manifest.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, MixedContentManifest) {
scoped_ptr<net::SpawnedTestServer> https_server(new net::SpawnedTestServer(
net::SpawnedTestServer::TYPE_HTTPS,
net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK),
base::FilePath(FILE_PATH_LITERAL("content/test/data"))));
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
ASSERT_TRUE(https_server->Start());
GURL test_url =
embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
std::string manifest_url =
https_server->GetURL("/manifest/dummy-manifest.json").spec();
ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
"setManifestTo('" + manifest_url + "')"));
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page's manifest has some parsing errors, they should show up in the
// developer console.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParsingErrorsManifest) {
GURL test_url = GetTestUrl("manifest", "parsing-errors.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(6u, console_error_count());
}
// If a page has a manifest and the page is navigated to a page without a
// manifest, the page's manifest should be updated.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, Navigation) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
{
GURL test_url =
embedded_test_server()->GetURL("/manifest/dummy-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
{
GURL test_url =
embedded_test_server()->GetURL("/manifest/no-manifest.html");
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_TRUE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
}
// If a page has a manifest and the page is navigated using pushState (ie. same
// page), it should keep its manifest state.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, PushStateNavigation) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL test_url =
embedded_test_server()->GetURL("/manifest/dummy-manifest.html");
{
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
}
{
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
ASSERT_TRUE(content::ExecuteScript(
shell()->web_contents(),
"history.pushState({foo: \"bar\"}, 'page', 'page.html');"));
navigation_observer.Wait();
}
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
// If a page has a manifest and is navigated using an anchor (ie. same page), it
// should keep its manifest state.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, AnchorNavigation) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL test_url =
embedded_test_server()->GetURL("/manifest/dummy-manifest.html");
{
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(test_url);
navigation_observer.Wait();
}
{
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
ASSERT_TRUE(content::ExecuteScript(
shell()->web_contents(),
"var a = document.createElement('a'); a.href='#foo';"
"document.body.appendChild(a); a.click();"));
navigation_observer.Wait();
}
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
}
namespace {
scoped_ptr<net::test_server::HttpResponse> CustomHandleRequestForCookies(
const net::test_server::HttpRequest& request) {
if (request.relative_url == "/index.html") {
scoped_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse());
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("text/html");
http_response->set_content(
"<html><head>"
"<link rel=manifest crossorigin='use-credentials' href=/manifest.json>"
"</head></html>");
return http_response.Pass();
}
const auto& iter = request.headers.find("Cookie");
if (iter == request.headers.end() || request.relative_url != "/manifest.json")
return scoped_ptr<net::test_server::HttpResponse>();
scoped_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse());
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("application/json");
http_response->set_content(
base::StringPrintf("{\"name\": \"%s\"}", iter->second.c_str()));
return http_response.Pass();
}
} // anonymous namespace
// This tests that when fetching a Manifest with 'use-credentials' set, the
// cookies associated with it are passed along the request.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, UseCredentialsSendCookies) {
scoped_ptr<net::test_server::EmbeddedTestServer> custom_embedded_test_server(
new net::test_server::EmbeddedTestServer());
custom_embedded_test_server->RegisterRequestHandler(
base::Bind(&CustomHandleRequestForCookies));
ASSERT_TRUE(custom_embedded_test_server->InitializeAndWaitUntilReady());
ASSERT_TRUE(SetCookie(shell()->web_contents()->GetBrowserContext(),
custom_embedded_test_server->base_url(),
"foobar"));
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(custom_embedded_test_server->GetURL("/index.html"));
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
// The custom embedded test server will fill the name field with the cookie
// content.
EXPECT_TRUE(base::EqualsASCII(manifest().name.string(), "foobar"));
}
namespace {
scoped_ptr<net::test_server::HttpResponse> CustomHandleRequestForNoCookies(
const net::test_server::HttpRequest& request) {
if (request.relative_url == "/index.html") {
scoped_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse());
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("text/html");
http_response->set_content(
"<html><head><link rel=manifest href=/manifest.json></head></html>");
return http_response.Pass();
}
const auto& iter = request.headers.find("Cookie");
if (iter != request.headers.end() || request.relative_url != "/manifest.json")
return scoped_ptr<net::test_server::HttpResponse>();
scoped_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse());
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("application/json");
http_response->set_content("{\"name\": \"no cookies\"}");
return http_response.Pass();
}
} // anonymous namespace
// This tests that when fetching a Manifest without 'use-credentials' set, the
// cookies associated with it are not passed along the request.
IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, NoUseCredentialsNoCookies) {
scoped_ptr<net::test_server::EmbeddedTestServer> custom_embedded_test_server(
new net::test_server::EmbeddedTestServer());
custom_embedded_test_server->RegisterRequestHandler(
base::Bind(&CustomHandleRequestForNoCookies));
ASSERT_TRUE(custom_embedded_test_server->InitializeAndWaitUntilReady());
ASSERT_TRUE(SetCookie(shell()->web_contents()->GetBrowserContext(),
custom_embedded_test_server->base_url(),
"foobar"));
TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
shell()->LoadURL(custom_embedded_test_server->GetURL("/index.html"));
navigation_observer.Wait();
GetManifestAndWait();
EXPECT_FALSE(manifest().IsEmpty());
EXPECT_EQ(0u, console_error_count());
// The custom embedded test server will fill set the name to 'no cookies' if
// it did not find cookies.
EXPECT_TRUE(base::EqualsASCII(manifest().name.string(), "no cookies"));
}
} // namespace content
|