blob: f361c7467f93c35a89ad04b46a2368122afb203d [file] [log] [blame]
Brandon Maslen6134c852020-05-18 21:45:211// Copyright (c) 2020 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 "chrome/browser/net/storage_test_utils.h"
6
7#include "content/public/test/browser_test_utils.h"
8
9namespace storage {
10namespace test {
11
Asami Doibcbe9af2021-03-24 04:50:5212const std::vector<std::string> kStorageTypesForFrame{
13 "Cookie", "LocalStorage", "FileSystem", "FileSystemAccess",
14 "SessionStorage", "IndexedDb", "WebSql", "CacheStorage",
15 "ServiceWorker", "CookieStore", "StorageFoundation"};
16
17const std::vector<std::string> kStorageTypesForWorker{
18 "WorkerFileSystemAccess", "WorkerCacheStorage", "WorkerIndexedDb",
19 "WorkerStorageFoundation"};
Brandon Maslen6134c852020-05-18 21:45:2120
21const std::vector<std::string> kCrossTabCommunicationTypes{
22 "SharedWorker",
23 "WebLock",
24};
25
26constexpr char kRequestStorageAccess[] =
27 "document.requestStorageAccess().then("
28 " () => { window.domAutomationController.send(true); },"
29 " () => { window.domAutomationController.send(false); },"
30 ");";
31
32constexpr char kHasStorageAccess[] =
33 "document.hasStorageAccess().then("
34 " (result) => { window.domAutomationController.send(result); },"
35 " () => { window.domAutomationController.send(false); },"
36 ");";
37
38void ExpectFrameContent(content::RenderFrameHost* frame,
39 const std::string& expected) {
Avi Drissman0f0e26e52021-04-29 03:13:3440 EXPECT_EQ(expected, content::EvalJs(frame, "document.body.textContent"));
Brandon Maslen6134c852020-05-18 21:45:2141}
42
43void ExpectCookiesOnHost(content::BrowserContext* context,
44 const GURL& host_url,
45 const std::string& expected) {
46 EXPECT_EQ(expected, content::GetCookies(context, host_url));
47}
48
49void SetStorageForFrame(content::RenderFrameHost* frame) {
Asami Doibcbe9af2021-03-24 04:50:5250 for (const auto& data_type : kStorageTypesForFrame) {
Avi Drissman0f0e26e52021-04-29 03:13:3451 bool data = content::EvalJs(frame, "set" + data_type + "()",
52 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
53 .ExtractBool();
Ari Chivukula8e111da2021-10-08 23:57:0054 if (frame->GetLastCommittedOrigin() !=
55 frame->GetMainFrame()->GetLastCommittedOrigin() &&
56 data_type == "WebSql") {
57 // Third-party context WebSQL is disabled as of M97.
58 EXPECT_FALSE(data) << "SetStorageForFrame for " << data_type;
59 } else {
60 EXPECT_TRUE(data) << "SetStorageForFrame for " << data_type;
61 }
Brandon Maslen6134c852020-05-18 21:45:2162 }
63}
64
Asami Doibcbe9af2021-03-24 04:50:5265void SetStorageForWorker(content::RenderFrameHost* frame) {
66 for (const auto& data_type : kStorageTypesForWorker) {
Avi Drissman0f0e26e52021-04-29 03:13:3467 bool data = content::EvalJs(frame, "set" + data_type + "()",
68 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
69 .ExtractBool();
Asami Doibcbe9af2021-03-24 04:50:5270 EXPECT_TRUE(data) << "SetStorageForWorker for " << data_type;
71 }
72}
73
Brandon Maslen6134c852020-05-18 21:45:2174void ExpectStorageForFrame(content::RenderFrameHost* frame, bool expected) {
Asami Doibcbe9af2021-03-24 04:50:5275 for (const auto& data_type : kStorageTypesForFrame) {
Avi Drissman0f0e26e52021-04-29 03:13:3476 bool data = content::EvalJs(frame, "has" + data_type + "();",
77 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
78 .ExtractBool();
Ari Chivukula8e111da2021-10-08 23:57:0079 if (frame->GetLastCommittedOrigin() !=
80 frame->GetMainFrame()->GetLastCommittedOrigin() &&
81 data_type == "WebSql") {
82 // Third-party context WebSQL is disabled as of M97.
83 EXPECT_FALSE(data) << "ExpectStorageForFrame for " << data_type;
84 } else {
85 EXPECT_EQ(expected, data) << "ExpectStorageForFrame for " << data_type;
86 }
Brandon Maslen6134c852020-05-18 21:45:2187 }
88}
89
Asami Doibcbe9af2021-03-24 04:50:5290void ExpectStorageForWorker(content::RenderFrameHost* frame, bool expected) {
91 for (const auto& data_type : kStorageTypesForWorker) {
Avi Drissman0f0e26e52021-04-29 03:13:3492 bool data = content::EvalJs(frame, "has" + data_type + "();",
93 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
94 .ExtractBool();
Asami Doibcbe9af2021-03-24 04:50:5295 EXPECT_EQ(expected, data) << "ExpectStorageForWorker for " << data_type;
96 }
97}
98
Brandon Maslen6134c852020-05-18 21:45:2199void SetCrossTabInfoForFrame(content::RenderFrameHost* frame) {
100 for (const auto& data_type : kCrossTabCommunicationTypes) {
Avi Drissman0f0e26e52021-04-29 03:13:34101 bool data = content::EvalJs(frame, "set" + data_type + "()",
102 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
103 .ExtractBool();
Brandon Maslen6134c852020-05-18 21:45:21104 EXPECT_TRUE(data) << data_type;
105 }
106}
107
108void ExpectCrossTabInfoForFrame(content::RenderFrameHost* frame,
109 bool expected) {
110 for (const auto& data_type : kCrossTabCommunicationTypes) {
Avi Drissman0f0e26e52021-04-29 03:13:34111 bool data = content::EvalJs(frame, "has" + data_type + "();",
112 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
113 .ExtractBool();
Brandon Maslen6134c852020-05-18 21:45:21114 EXPECT_EQ(expected, data) << data_type;
115 }
116}
117
118void RequestStorageAccessForFrame(content::RenderFrameHost* frame,
119 bool should_resolve) {
Avi Drissman0f0e26e52021-04-29 03:13:34120 bool data = content::EvalJs(frame, kRequestStorageAccess,
121 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
122 .ExtractBool();
Brandon Maslen6134c852020-05-18 21:45:21123 EXPECT_EQ(should_resolve, data) << "document.requestStorageAccess()";
124}
125
126void CheckStorageAccessForFrame(content::RenderFrameHost* frame,
127 bool access_expected) {
Avi Drissman0f0e26e52021-04-29 03:13:34128 bool data = content::EvalJs(frame, kHasStorageAccess,
129 content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
130 .ExtractBool();
Brandon Maslen6134c852020-05-18 21:45:21131 EXPECT_EQ(access_expected, data) << "document.hasStorageAccess()";
132}
133
134} // namespace test
135} // namespace storage