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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/storage_test_utils.h"
#include "content/public/test/browser_test_utils.h"
namespace storage::test {
const std::vector<std::string> kCookiesTypesForFrame{"Cookie", "CookieStore"};
const std::vector<std::string> kStorageTypesForFrame{
"LocalStorage", "FileSystem", "FileSystemAccess", "SessionStorage",
"IndexedDb", "CacheStorage", "ServiceWorker"};
const std::vector<std::string> kStorageTypesForWorker{
"WorkerFileSystemAccess", "WorkerCacheStorage", "WorkerIndexedDb"};
const std::vector<std::string> kCrossTabCommunicationTypes{
"SharedWorker",
"WebLock",
};
constexpr char kRequestStorageAccess[] =
"document.requestStorageAccess()"
" .then(() => document.hasStorageAccess())";
constexpr char kRequestStorageAccessBeyondCookies[] =
"document.requestStorageAccess({estimate: true}).then((handle) => "
"handle.estimate().then(() => true, () => false), () => false)";
constexpr char kRequestStorageAccessFor[] =
"document.requestStorageAccessFor($1)";
constexpr char kHasStorageAccess[] = "document.hasStorageAccess()";
std::vector<std::string> GetStorageTypesForFrame(bool include_cookies) {
std::vector<std::string> types(kStorageTypesForFrame);
if (include_cookies) {
types.insert(types.end(), kCookiesTypesForFrame.begin(),
kCookiesTypesForFrame.end());
}
return types;
}
std::string GetFrameContent(content::RenderFrameHost* frame) {
return content::EvalJs(frame, "document.body.textContent",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractString();
}
void SetStorageForFrame(content::RenderFrameHost* frame,
bool include_cookies,
bool expected_to_be_set,
const base::Location& location) {
base::flat_map<std::string, bool> actual;
base::flat_map<std::string, bool> expected;
for (const auto& data_type : GetStorageTypesForFrame(include_cookies)) {
actual[data_type] = content::EvalJs(frame, "set" + data_type + "()",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
expected[data_type] = expected_to_be_set;
}
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected))
<< "(expected at " << location.ToString() << ")";
}
void SetStorageForWorker(content::RenderFrameHost* frame,
const base::Location& location) {
base::flat_map<std::string, bool> actual;
base::flat_map<std::string, bool> expected;
for (const auto& data_type : kStorageTypesForWorker) {
actual[data_type] = content::EvalJs(frame, "set" + data_type + "()",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
expected[data_type] = true;
}
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected))
<< "(expected at " << location.ToString() << ")";
}
void ExpectStorageForFrame(content::RenderFrameHost* frame,
bool expected,
const base::Location& location) {
base::flat_map<std::string, bool> actual;
base::flat_map<std::string, bool> expected_elts;
for (const auto& data_type : GetStorageTypesForFrame(false)) {
actual[data_type] = content::EvalJs(frame, "has" + data_type + "();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
expected_elts[data_type] = expected;
}
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected_elts))
<< "(expected at " << location.ToString() << ")";
}
void ExpectStorageForWorker(content::RenderFrameHost* frame,
bool expected,
const base::Location& location) {
base::flat_map<std::string, bool> actual;
base::flat_map<std::string, bool> expected_elts;
for (const auto& data_type : kStorageTypesForWorker) {
actual[data_type] = content::EvalJs(frame, "has" + data_type + "();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
expected_elts[data_type] = expected;
}
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected_elts))
<< "(expected at " << location.ToString() << ")";
}
void SetCrossTabInfoForFrame(content::RenderFrameHost* frame,
const base::Location& location) {
base::flat_map<std::string, bool> actual;
base::flat_map<std::string, bool> expected;
for (const auto& data_type : kCrossTabCommunicationTypes) {
actual[data_type] = content::EvalJs(frame, "set" + data_type + "()",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
expected[data_type] = true;
}
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected))
<< "(expected at " << location.ToString() << ")";
}
void ExpectCrossTabInfoForFrame(content::RenderFrameHost* frame,
bool expected,
const base::Location& location) {
base::flat_map<std::string, bool> actual;
base::flat_map<std::string, bool> expected_elts;
for (const auto& data_type : kCrossTabCommunicationTypes) {
actual[data_type] = content::EvalJs(frame, "has" + data_type + "();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
expected_elts[data_type] = expected;
}
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected_elts))
<< "(expected at " << location.ToString() << ")";
}
bool RequestAndCheckStorageAccessForFrame(content::RenderFrameHost* frame,
bool omit_user_gesture) {
int options = content::EXECUTE_SCRIPT_DEFAULT_OPTIONS;
if (omit_user_gesture) {
options |= content::EXECUTE_SCRIPT_NO_USER_GESTURE;
}
return content::EvalJs(frame, kRequestStorageAccess, options).ExtractBool();
}
bool RequestAndCheckStorageAccessBeyondCookiesForFrame(
content::RenderFrameHost* frame) {
return content::EvalJs(frame, kRequestStorageAccessBeyondCookies)
.ExtractBool();
}
bool RequestStorageAccessForOrigin(content::RenderFrameHost* frame,
const std::string& origin,
bool omit_user_gesture) {
int options = content::EXECUTE_SCRIPT_DEFAULT_OPTIONS;
if (omit_user_gesture) {
options |= content::EXECUTE_SCRIPT_NO_USER_GESTURE;
}
return content::ExecJs(
frame, content::JsReplace(kRequestStorageAccessFor, origin), options);
}
bool HasStorageAccessForFrame(content::RenderFrameHost* frame) {
return content::EvalJs(frame, kHasStorageAccess,
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
}
std::string FetchWithCredentials(content::RenderFrameHost* frame,
const GURL& url,
const bool cors_enabled) {
constexpr char script[] = R"(
fetch($1, {method: 'GET', mode: $2, credentials: 'include'})
.then((result) => result.text());
)";
const std::string mode = cors_enabled ? "cors" : "no-cors";
return content::EvalJs(frame, content::JsReplace(script, url, mode),
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractString();
}
} // namespace storage::test
|