1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/browsing_data/same_site_data_remover_impl.h"
6
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/run_loop.h"
13 #include "content/browser/browsing_data/browsing_data_browsertest_utils.h"
14 #include "content/browser/browsing_data/browsing_data_test_utils.h"
15 #include "content/public/browser/same_site_data_remover.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/storage_usage_info.h"
18 #include "content/public/common/network_service_util.h"
19 #include "content/public/test/content_browser_test.h"
20 #include "content/shell/browser/shell.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "net/test/embedded_test_server/http_request.h"
23 #include "net/test/embedded_test_server/http_response.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 using testing::IsEmpty;
28
29 namespace content {
30
31 class SameSiteDataRemoverBrowserTest : public ContentBrowserTest {
32 public:
SameSiteDataRemoverBrowserTest()33 SameSiteDataRemoverBrowserTest() {}
34
SetUpOnMainThread()35 void SetUpOnMainThread() override {
36 ContentBrowserTest::SetUpOnMainThread();
37
38 // Set up HTTP and HTTPS test servers that handle all hosts.
39 host_resolver()->AddRule("*", "127.0.0.1");
40
41 if (IsOutOfProcessNetworkService())
42 browsing_data_browsertest_utils::SetUpMockCertVerifier(net::OK);
43
44 https_server_.reset(new net::EmbeddedTestServer(
45 net::test_server::EmbeddedTestServer::TYPE_HTTPS));
46 https_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_OK);
47 https_server_->RegisterRequestHandler(
48 base::BindRepeating(&SameSiteDataRemoverBrowserTest::HandleRequest,
49 base::Unretained(this)));
50 ASSERT_TRUE(https_server_->Start());
51 }
52
SetUpCommandLine(base::CommandLine * command_line)53 void SetUpCommandLine(base::CommandLine* command_line) override {
54 ContentBrowserTest::SetUpCommandLine(command_line);
55 browsing_data_browsertest_utils::SetIgnoreCertificateErrors(command_line);
56 }
57
GetBrowserContext()58 BrowserContext* GetBrowserContext() {
59 return shell()->web_contents()->GetBrowserContext();
60 }
61
GetStoragePartition()62 StoragePartition* GetStoragePartition() {
63 return BrowserContext::GetDefaultStoragePartition(GetBrowserContext());
64 }
65
GetHttpsServer()66 net::EmbeddedTestServer* GetHttpsServer() { return https_server_.get(); }
67
ClearData(bool clear_storage)68 void ClearData(bool clear_storage) {
69 base::RunLoop run_loop;
70 ClearSameSiteNoneData(run_loop.QuitClosure(), GetBrowserContext(),
71 clear_storage);
72 run_loop.Run();
73 }
74
75 private:
76 // Handles all requests.
77 //
78 // Supports the following <key>=<value> query parameters in the url:
79 // <key>="file" responds with the content of file <value>
HandleRequest(const net::test_server::HttpRequest & request)80 std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
81 const net::test_server::HttpRequest& request) {
82 auto response(std::make_unique<net::test_server::BasicHttpResponse>());
83
84 std::string value;
85 browsing_data_browsertest_utils::SetResponseContent(request.GetURL(),
86 &value, response.get());
87
88 return std::move(response);
89 }
90
91 std::unique_ptr<net::EmbeddedTestServer> https_server_;
92
93 DISALLOW_COPY_AND_ASSIGN(SameSiteDataRemoverBrowserTest);
94 };
95
IN_PROC_BROWSER_TEST_F(SameSiteDataRemoverBrowserTest,TestClearDataWithStorageRemoval)96 IN_PROC_BROWSER_TEST_F(SameSiteDataRemoverBrowserTest,
97 TestClearDataWithStorageRemoval) {
98 StoragePartition* storage_partition = GetStoragePartition();
99 CreateCookieForTest(
100 "TestCookie", "www.google.com", net::CookieSameSite::NO_RESTRICTION,
101 net::CookieOptions::SameSiteCookieContext(
102 net::CookieOptions::SameSiteCookieContext::ContextType::CROSS_SITE),
103 true /* is_cookie_secure */, GetBrowserContext());
104 browsing_data_browsertest_utils::AddServiceWorker(
105 "www.google.com", storage_partition, GetHttpsServer());
106
107 ClearData(/* clear_storage= */ true);
108
109 // Check that cookies were deleted.
110 const std::vector<net::CanonicalCookie>& cookies =
111 GetAllCookies(GetBrowserContext());
112 EXPECT_THAT(cookies, IsEmpty());
113
114 // Check that the service worker for the cookie domain was removed.
115 std::vector<StorageUsageInfo> service_workers =
116 browsing_data_browsertest_utils::GetServiceWorkers(storage_partition);
117 EXPECT_THAT(service_workers, IsEmpty());
118 }
119
IN_PROC_BROWSER_TEST_F(SameSiteDataRemoverBrowserTest,TestClearDataWithoutStorageRemoval)120 IN_PROC_BROWSER_TEST_F(SameSiteDataRemoverBrowserTest,
121 TestClearDataWithoutStorageRemoval) {
122 StoragePartition* storage_partition = GetStoragePartition();
123 CreateCookieForTest(
124 "TestCookie", "www.google.com", net::CookieSameSite::NO_RESTRICTION,
125 net::CookieOptions::SameSiteCookieContext(
126 net::CookieOptions::SameSiteCookieContext::ContextType::CROSS_SITE),
127 true /* is_cookie_secure */, GetBrowserContext());
128 browsing_data_browsertest_utils::AddServiceWorker(
129 "www.google.com", storage_partition, GetHttpsServer());
130
131 ClearData(/* clear_storage= */ false);
132
133 // Check that cookies were deleted.
134 const std::vector<net::CanonicalCookie>& cookies =
135 GetAllCookies(GetBrowserContext());
136 EXPECT_THAT(cookies, IsEmpty());
137
138 // Storage partition data should NOT have been cleared.
139 std::vector<StorageUsageInfo> service_workers =
140 browsing_data_browsertest_utils::GetServiceWorkers(storage_partition);
141 ASSERT_EQ(1u, service_workers.size());
142 EXPECT_EQ(service_workers[0].origin.GetURL(),
143 GetHttpsServer()->GetURL("www.google.com", "/"));
144 }
145
146 } // namespace content
147