diff --git a/tests/ceftests/scheme_handler_unittest.cc b/tests/ceftests/scheme_handler_unittest.cc index c7e903028..486a4f5c2 100644 --- a/tests/ceftests/scheme_handler_unittest.cc +++ b/tests/ceftests/scheme_handler_unittest.cc @@ -18,44 +18,16 @@ namespace { class TestResults { public: - TestResults() : status_code(200), sub_status_code(200), delay(0) {} - - void reset() { - url.clear(); - html.clear(); - status_code = 200; - response_error_code = ERR_NONE; - expected_error_code = ERR_NONE; - redirect_url.clear(); - sub_url.clear(); - sub_html.clear(); - sub_status_code = 200; - sub_allow_origin.clear(); - exit_url.clear(); - accept_language.clear(); - console_messages.clear(); - delay = 0; - got_request.reset(); - got_read.reset(); - got_output.reset(); - got_sub_output.reset(); - got_redirect.reset(); - got_error.reset(); - got_sub_error.reset(); - got_sub_request.reset(); - got_sub_read.reset(); - got_sub_success.reset(); - got_exit_request.reset(); - } + TestResults() = default; std::string url; std::string html; - int status_code; + int status_code = 200; // Error code set on the response. - cef_errorcode_t response_error_code; + cef_errorcode_t response_error_code = ERR_NONE; // Error code expected in OnLoadError. - cef_errorcode_t expected_error_code; + cef_errorcode_t expected_error_code = ERR_NONE; // Used for testing redirects std::string redirect_url; @@ -63,11 +35,14 @@ class TestResults { // Used for testing XHR requests std::string sub_url; std::string sub_html; - int sub_status_code; + int sub_status_code = 200; std::string sub_allow_origin; std::string sub_redirect_url; std::string exit_url; + // Used for testing XSS requests + bool needs_same_origin_policy_relaxation = false; + // Used for testing per-browser Accept-Language. std::string accept_language; @@ -75,7 +50,7 @@ class TestResults { std::vector console_messages; // Delay for returning scheme handler results. - int delay; + int delay = 0; TrackCallback got_request, got_read, got_output, got_sub_output, got_redirect, got_error, got_sub_error, got_sub_redirect, got_sub_request, got_sub_read, @@ -251,7 +226,7 @@ class TestSchemeHandler : public TestHandler { } protected: - TestResults* test_results_; + TestResults* const test_results_; IMPLEMENT_REFCOUNTING(TestSchemeHandler); }; @@ -336,16 +311,16 @@ class ClientSchemeHandlerOld : public CefResourceHandler { void GetResponseHeaders(CefRefPtr response, int64& response_length, CefString& redirectUrl) override { + CefResponse::HeaderMap headers; + if (is_sub_) { response->SetStatus(test_results_->sub_status_code); if (!test_results_->sub_allow_origin.empty()) { // Set the Access-Control-Allow-Origin header to allow cross-domain // scripting. - CefResponse::HeaderMap headers; headers.insert(std::make_pair("Access-Control-Allow-Origin", test_results_->sub_allow_origin)); - response->SetHeaderMap(headers); } if (!test_results_->sub_html.empty()) { @@ -364,6 +339,15 @@ class ClientSchemeHandlerOld : public CefResourceHandler { response_length = test_results_->html.size(); } } + + if (test_results_->needs_same_origin_policy_relaxation) { + // Apply same-origin policy relaxation for document.domain. + headers.insert(std::make_pair("Origin-Agent-Cluster", "?0")); + } + + if (!headers.empty()) { + response->SetHeaderMap(headers); + } } void Cancel() override { EXPECT_TRUE(CefCurrentlyOn(TID_IO)); } @@ -422,7 +406,7 @@ class ClientSchemeHandlerOld : public CefResourceHandler { callback->Continue(); } - TestResults* test_results_; + TestResults* const test_results_; size_t offset_; bool is_sub_; bool has_delayed_; @@ -529,16 +513,16 @@ class ClientSchemeHandler : public CefResourceHandler { void GetResponseHeaders(CefRefPtr response, int64& response_length, CefString& redirectUrl) override { + CefResponse::HeaderMap headers; + if (is_sub_) { response->SetStatus(test_results_->sub_status_code); if (!test_results_->sub_allow_origin.empty()) { // Set the Access-Control-Allow-Origin header to allow cross-domain // scripting. - CefResponse::HeaderMap headers; headers.insert(std::make_pair("Access-Control-Allow-Origin", test_results_->sub_allow_origin)); - response->SetHeaderMap(headers); } if (!test_results_->sub_html.empty()) { @@ -557,6 +541,15 @@ class ClientSchemeHandler : public CefResourceHandler { response_length = test_results_->html.size(); } } + + if (test_results_->needs_same_origin_policy_relaxation) { + // Apply same-origin policy relaxation for document.domain. + headers.insert(std::make_pair("Origin-Agent-Cluster", "?0")); + } + + if (!headers.empty()) { + response->SetHeaderMap(headers); + } } void Cancel() override { EXPECT_TRUE(CefCurrentlyOn(TID_IO)); } @@ -636,7 +629,7 @@ class ClientSchemeHandler : public CefResourceHandler { return has_data; } - TestResults* test_results_; + TestResults* const test_results_; size_t offset_; bool is_sub_; bool has_delayed_; @@ -660,21 +653,18 @@ class ClientSchemeHandlerFactory : public CefSchemeHandlerFactory { return new ClientSchemeHandler(test_results_); } - TestResults* test_results_; + TestResults* const test_results_; IMPLEMENT_REFCOUNTING(ClientSchemeHandlerFactory); DISALLOW_COPY_AND_ASSIGN(ClientSchemeHandlerFactory); }; -// Global test results object. -TestResults g_TestResults; - // If |domain| is empty the scheme will be registered as non-standard. -void RegisterTestScheme(const std::string& scheme, const std::string& domain) { - g_TestResults.reset(); - +void RegisterTestScheme(TestResults* test_results, + const std::string& scheme, + const std::string& domain) { EXPECT_TRUE(CefRegisterSchemeHandlerFactory( - scheme, domain, new ClientSchemeHandlerFactory(&g_TestResults))); + scheme, domain, new ClientSchemeHandlerFactory(test_results))); WaitForIOThread(); } @@ -684,20 +674,20 @@ void ClearTestSchemes() { } struct XHRTestSettings { - XHRTestSettings() : synchronous(true) {} + XHRTestSettings() = default; std::string url; std::string sub_url; std::string sub_allow_origin; std::string sub_redirect_url; - bool synchronous; + bool synchronous = true; }; -void SetUpXHR(const XHRTestSettings& settings) { - g_TestResults.sub_url = settings.sub_url; - g_TestResults.sub_html = "SUCCESS"; - g_TestResults.sub_allow_origin = settings.sub_allow_origin; - g_TestResults.sub_redirect_url = settings.sub_redirect_url; +void SetUpXHR(TestResults* test_results, const XHRTestSettings& settings) { + test_results->sub_url = settings.sub_url; + test_results->sub_html = "SUCCESS"; + test_results->sub_allow_origin = settings.sub_allow_origin; + test_results->sub_redirect_url = settings.sub_redirect_url; std::string request_url; if (!settings.sub_redirect_url.empty()) { @@ -706,7 +696,7 @@ void SetUpXHR(const XHRTestSettings& settings) { request_url = settings.sub_url; } - g_TestResults.url = settings.url; + test_results->url = settings.url; std::stringstream ss; ss << "" "