| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/public/test/browser_test.h" | 9 #include "content/public/test/browser_test.h" |
| 10 #include "headless/public/domains/page.h" | 10 #include "headless/public/domains/page.h" |
| 11 #include "headless/public/domains/runtime.h" | |
| 12 #include "headless/public/domains/types.h" | |
| 13 #include "headless/public/headless_browser.h" | 11 #include "headless/public/headless_browser.h" |
| 14 #include "headless/public/headless_browser_context.h" | |
| 15 #include "headless/public/headless_devtools_client.h" | 12 #include "headless/public/headless_devtools_client.h" |
| 16 #include "headless/public/headless_devtools_target.h" | 13 #include "headless/public/headless_devtools_target.h" |
| 17 #include "headless/public/headless_web_contents.h" | 14 #include "headless/public/headless_web_contents.h" |
| 18 #include "headless/test/headless_browser_test.h" | 15 #include "headless/test/headless_browser_test.h" |
| 19 #include "net/base/io_buffer.h" | 16 #include "headless/test/test_protocol_handler.h" |
| 20 #include "net/http/http_response_headers.h" | |
| 21 #include "net/test/spawned_test_server/spawned_test_server.h" | 17 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 22 #include "net/url_request/url_request_job.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
| 25 | 20 |
| 26 namespace headless { | 21 namespace headless { |
| 27 namespace { | |
| 28 | |
| 29 class TestURLRequestJob : public net::URLRequestJob { | |
| 30 public: | |
| 31 TestURLRequestJob(net::URLRequest* request, | |
| 32 net::NetworkDelegate* network_delegate, | |
| 33 const std::string& body); | |
| 34 ~TestURLRequestJob() override {} | |
| 35 | |
| 36 // net::URLRequestJob implementation: | |
| 37 void Start() override; | |
| 38 void GetResponseInfo(net::HttpResponseInfo* info) override; | |
| 39 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | |
| 40 | |
| 41 private: | |
| 42 scoped_refptr<net::StringIOBuffer> body_; | |
| 43 scoped_refptr<net::DrainableIOBuffer> src_buf_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob); | |
| 46 }; | |
| 47 | |
| 48 TestURLRequestJob::TestURLRequestJob(net::URLRequest* request, | |
| 49 net::NetworkDelegate* network_delegate, | |
| 50 const std::string& body) | |
| 51 : net::URLRequestJob(request, network_delegate), | |
| 52 body_(new net::StringIOBuffer(body)), | |
| 53 src_buf_(new net::DrainableIOBuffer(body_.get(), body_->size())) {} | |
| 54 | |
| 55 void TestURLRequestJob::Start() { | |
| 56 NotifyHeadersComplete(); | |
| 57 } | |
| 58 | |
| 59 void TestURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { | |
| 60 info->headers = | |
| 61 new net::HttpResponseHeaders("Content-Type: text/html\r\n\r\n"); | |
| 62 } | |
| 63 | |
| 64 int TestURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | |
| 65 scoped_refptr<net::DrainableIOBuffer> dest_buf( | |
| 66 new net::DrainableIOBuffer(buf, buf_size)); | |
| 67 while (src_buf_->BytesRemaining() > 0 && dest_buf->BytesRemaining() > 0) { | |
| 68 *dest_buf->data() = *src_buf_->data(); | |
| 69 src_buf_->DidConsume(1); | |
| 70 dest_buf->DidConsume(1); | |
| 71 } | |
| 72 return dest_buf->BytesConsumed(); | |
| 73 } | |
| 74 | |
| 75 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { | |
| 76 public: | |
| 77 TestProtocolHandler(const std::string& body); | |
| 78 ~TestProtocolHandler() override {} | |
| 79 | |
| 80 net::URLRequestJob* MaybeCreateJob( | |
| 81 net::URLRequest* request, | |
| 82 net::NetworkDelegate* network_delegate) const override; | |
| 83 | |
| 84 private: | |
| 85 std::string body_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler); | |
| 88 }; | |
| 89 | |
| 90 TestProtocolHandler::TestProtocolHandler(const std::string& body) | |
| 91 : body_(body) {} | |
| 92 | |
| 93 net::URLRequestJob* TestProtocolHandler::MaybeCreateJob( | |
| 94 net::URLRequest* request, | |
| 95 net::NetworkDelegate* network_delegate) const { | |
| 96 return new TestURLRequestJob(request, network_delegate, body_); | |
| 97 } | |
| 98 | |
| 99 } // namespace | |
| 100 | 22 |
| 101 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { | 23 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { |
| 102 HeadlessWebContents* web_contents = | 24 HeadlessWebContents* web_contents = |
| 103 browser()->CreateWebContentsBuilder().Build(); | 25 browser()->CreateWebContentsBuilder().Build(); |
| 104 EXPECT_TRUE(web_contents); | 26 EXPECT_TRUE(web_contents); |
| 105 | 27 |
| 106 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); | 28 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); |
| 107 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); | 29 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); |
| 108 // TODO(skyostil): Verify viewport dimensions once we can. | 30 // TODO(skyostil): Verify viewport dimensions once we can. |
| 109 web_contents->Close(); | 31 web_contents->Close(); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 EXPECT_TRUE(WaitForLoad(web_contents)); | 152 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 231 | 153 |
| 232 std::string inner_html; | 154 std::string inner_html; |
| 233 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") | 155 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") |
| 234 ->GetResult() | 156 ->GetResult() |
| 235 ->GetValue() | 157 ->GetValue() |
| 236 ->GetAsString(&inner_html)); | 158 ->GetAsString(&inner_html)); |
| 237 EXPECT_EQ(kResponseBody, inner_html); | 159 EXPECT_EQ(kResponseBody, inner_html); |
| 238 } | 160 } |
| 239 | 161 |
| 240 namespace { | |
| 241 const char kMainPageCookie[] = "mood=quizzical"; | |
| 242 const char kIsolatedPageCookie[] = "mood=quixotic"; | |
| 243 } // namespace | |
| 244 | |
| 245 // This test creates two tabs pointing to the same security origin in two | |
| 246 // different browser contexts and checks that they are isolated by creating two | |
| 247 // cookies with the same name in both tabs. The steps are: | |
| 248 // | |
| 249 // 1. Wait for tab #1 to become ready for DevTools. | |
| 250 // 2. Create tab #2 and wait for it to become ready for DevTools. | |
| 251 // 3. Navigate tab #1 to the test page and wait for it to finish loading. | |
| 252 // 4. Navigate tab #2 to the test page and wait for it to finish loading. | |
| 253 // 5. Set a cookie in tab #1. | |
| 254 // 6. Set the same cookie in tab #2 to a different value. | |
| 255 // 7. Read the cookie in tab #1 and check that it has the first value. | |
| 256 // 8. Read the cookie in tab #2 and check that it has the second value. | |
| 257 // | |
| 258 // If the tabs aren't properly isolated, step 7 will fail. | |
| 259 class HeadlessBrowserContextIsolationTest | |
| 260 : public HeadlessAsyncDevTooledBrowserTest { | |
| 261 public: | |
| 262 HeadlessBrowserContextIsolationTest() | |
| 263 : web_contents2_(nullptr), | |
| 264 devtools_client2_(HeadlessDevToolsClient::Create()) { | |
| 265 EXPECT_TRUE(embedded_test_server()->Start()); | |
| 266 } | |
| 267 | |
| 268 // HeadlessWebContentsObserver implementation: | |
| 269 void DevToolsTargetReady() override { | |
| 270 if (!web_contents2_) { | |
| 271 browser_context_ = browser()->CreateBrowserContextBuilder().Build(); | |
| 272 web_contents2_ = browser() | |
| 273 ->CreateWebContentsBuilder() | |
| 274 .SetBrowserContext(browser_context_.get()) | |
| 275 .Build(); | |
| 276 web_contents2_->AddObserver(this); | |
| 277 return; | |
| 278 } | |
| 279 | |
| 280 web_contents2_->GetDevToolsTarget()->AttachClient(devtools_client2_.get()); | |
| 281 HeadlessAsyncDevTooledBrowserTest::DevToolsTargetReady(); | |
| 282 } | |
| 283 | |
| 284 void RunDevTooledTest() override { | |
| 285 load_observer_.reset(new LoadObserver( | |
| 286 devtools_client_.get(), | |
| 287 base::Bind(&HeadlessBrowserContextIsolationTest::OnFirstLoadComplete, | |
| 288 base::Unretained(this)))); | |
| 289 devtools_client_->GetPage()->Navigate( | |
| 290 embedded_test_server()->GetURL("/hello.html").spec()); | |
| 291 } | |
| 292 | |
| 293 void OnFirstLoadComplete() { | |
| 294 EXPECT_TRUE(load_observer_->navigation_succeeded()); | |
| 295 load_observer_.reset(new LoadObserver( | |
| 296 devtools_client2_.get(), | |
| 297 base::Bind(&HeadlessBrowserContextIsolationTest::OnSecondLoadComplete, | |
| 298 base::Unretained(this)))); | |
| 299 devtools_client2_->GetPage()->Navigate( | |
| 300 embedded_test_server()->GetURL("/hello.html").spec()); | |
| 301 } | |
| 302 | |
| 303 void OnSecondLoadComplete() { | |
| 304 EXPECT_TRUE(load_observer_->navigation_succeeded()); | |
| 305 load_observer_.reset(); | |
| 306 | |
| 307 devtools_client_->GetRuntime()->Evaluate( | |
| 308 base::StringPrintf("document.cookie = '%s'", kMainPageCookie), | |
| 309 base::Bind(&HeadlessBrowserContextIsolationTest::OnFirstSetCookieResult, | |
| 310 base::Unretained(this))); | |
| 311 } | |
| 312 | |
| 313 void OnFirstSetCookieResult(std::unique_ptr<runtime::EvaluateResult> result) { | |
| 314 std::string cookie; | |
| 315 EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); | |
| 316 EXPECT_EQ(kMainPageCookie, cookie); | |
| 317 | |
| 318 devtools_client2_->GetRuntime()->Evaluate( | |
| 319 base::StringPrintf("document.cookie = '%s'", kIsolatedPageCookie), | |
| 320 base::Bind( | |
| 321 &HeadlessBrowserContextIsolationTest::OnSecondSetCookieResult, | |
| 322 base::Unretained(this))); | |
| 323 } | |
| 324 | |
| 325 void OnSecondSetCookieResult( | |
| 326 std::unique_ptr<runtime::EvaluateResult> result) { | |
| 327 std::string cookie; | |
| 328 EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); | |
| 329 EXPECT_EQ(kIsolatedPageCookie, cookie); | |
| 330 | |
| 331 devtools_client_->GetRuntime()->Evaluate( | |
| 332 "document.cookie", | |
| 333 base::Bind(&HeadlessBrowserContextIsolationTest::OnFirstGetCookieResult, | |
| 334 base::Unretained(this))); | |
| 335 } | |
| 336 | |
| 337 void OnFirstGetCookieResult(std::unique_ptr<runtime::EvaluateResult> result) { | |
| 338 std::string cookie; | |
| 339 EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); | |
| 340 EXPECT_EQ(kMainPageCookie, cookie); | |
| 341 | |
| 342 devtools_client2_->GetRuntime()->Evaluate( | |
| 343 "document.cookie", | |
| 344 base::Bind( | |
| 345 &HeadlessBrowserContextIsolationTest::OnSecondGetCookieResult, | |
| 346 base::Unretained(this))); | |
| 347 } | |
| 348 | |
| 349 void OnSecondGetCookieResult( | |
| 350 std::unique_ptr<runtime::EvaluateResult> result) { | |
| 351 std::string cookie; | |
| 352 EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); | |
| 353 EXPECT_EQ(kIsolatedPageCookie, cookie); | |
| 354 FinishTest(); | |
| 355 } | |
| 356 | |
| 357 void FinishTest() { | |
| 358 web_contents2_->RemoveObserver(this); | |
| 359 web_contents2_->Close(); | |
| 360 browser_context_.reset(); | |
| 361 FinishAsynchronousTest(); | |
| 362 } | |
| 363 | |
| 364 private: | |
| 365 std::unique_ptr<HeadlessBrowserContext> browser_context_; | |
| 366 HeadlessWebContents* web_contents2_; | |
| 367 std::unique_ptr<HeadlessDevToolsClient> devtools_client2_; | |
| 368 std::unique_ptr<LoadObserver> load_observer_; | |
| 369 }; | |
| 370 | |
| 371 HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessBrowserContextIsolationTest); | |
| 372 | |
| 373 } // namespace headless | 162 } // namespace headless |
| OLD | NEW |