Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 1 | // 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 | |
| 9 | namespace storage { |
| 10 | namespace test { |
| 11 | |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 12 | const std::vector<std::string> kStorageTypesForFrame{ |
| 13 | "Cookie", "LocalStorage", "FileSystem", "FileSystemAccess", |
| 14 | "SessionStorage", "IndexedDb", "WebSql", "CacheStorage", |
| 15 | "ServiceWorker", "CookieStore", "StorageFoundation"}; |
| 16 | |
| 17 | const std::vector<std::string> kStorageTypesForWorker{ |
| 18 | "WorkerFileSystemAccess", "WorkerCacheStorage", "WorkerIndexedDb", |
| 19 | "WorkerStorageFoundation"}; |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 20 | |
| 21 | const std::vector<std::string> kCrossTabCommunicationTypes{ |
| 22 | "SharedWorker", |
| 23 | "WebLock", |
| 24 | }; |
| 25 | |
| 26 | constexpr char kRequestStorageAccess[] = |
| 27 | "document.requestStorageAccess().then(" |
| 28 | " () => { window.domAutomationController.send(true); }," |
| 29 | " () => { window.domAutomationController.send(false); }," |
| 30 | ");"; |
| 31 | |
| 32 | constexpr char kHasStorageAccess[] = |
| 33 | "document.hasStorageAccess().then(" |
| 34 | " (result) => { window.domAutomationController.send(result); }," |
| 35 | " () => { window.domAutomationController.send(false); }," |
| 36 | ");"; |
| 37 | |
| 38 | void ExpectFrameContent(content::RenderFrameHost* frame, |
| 39 | const std::string& expected) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 40 | EXPECT_EQ(expected, content::EvalJs(frame, "document.body.textContent")); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void 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 | |
| 49 | void SetStorageForFrame(content::RenderFrameHost* frame) { |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 50 | for (const auto& data_type : kStorageTypesForFrame) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 51 | bool data = content::EvalJs(frame, "set" + data_type + "()", |
| 52 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 53 | .ExtractBool(); |
Ari Chivukula | 8e111da | 2021-10-08 23:57:00 | [diff] [blame] | 54 | 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 Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 65 | void SetStorageForWorker(content::RenderFrameHost* frame) { |
| 66 | for (const auto& data_type : kStorageTypesForWorker) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 67 | bool data = content::EvalJs(frame, "set" + data_type + "()", |
| 68 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 69 | .ExtractBool(); |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 70 | EXPECT_TRUE(data) << "SetStorageForWorker for " << data_type; |
| 71 | } |
| 72 | } |
| 73 | |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 74 | void ExpectStorageForFrame(content::RenderFrameHost* frame, bool expected) { |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 75 | for (const auto& data_type : kStorageTypesForFrame) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 76 | bool data = content::EvalJs(frame, "has" + data_type + "();", |
| 77 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 78 | .ExtractBool(); |
Ari Chivukula | 8e111da | 2021-10-08 23:57:00 | [diff] [blame] | 79 | 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 Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 90 | void ExpectStorageForWorker(content::RenderFrameHost* frame, bool expected) { |
| 91 | for (const auto& data_type : kStorageTypesForWorker) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 92 | bool data = content::EvalJs(frame, "has" + data_type + "();", |
| 93 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 94 | .ExtractBool(); |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 95 | EXPECT_EQ(expected, data) << "ExpectStorageForWorker for " << data_type; |
| 96 | } |
| 97 | } |
| 98 | |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 99 | void SetCrossTabInfoForFrame(content::RenderFrameHost* frame) { |
| 100 | for (const auto& data_type : kCrossTabCommunicationTypes) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 101 | bool data = content::EvalJs(frame, "set" + data_type + "()", |
| 102 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 103 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 104 | EXPECT_TRUE(data) << data_type; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void ExpectCrossTabInfoForFrame(content::RenderFrameHost* frame, |
| 109 | bool expected) { |
| 110 | for (const auto& data_type : kCrossTabCommunicationTypes) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 111 | bool data = content::EvalJs(frame, "has" + data_type + "();", |
| 112 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 113 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 114 | EXPECT_EQ(expected, data) << data_type; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void RequestStorageAccessForFrame(content::RenderFrameHost* frame, |
| 119 | bool should_resolve) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 120 | bool data = content::EvalJs(frame, kRequestStorageAccess, |
| 121 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 122 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 123 | EXPECT_EQ(should_resolve, data) << "document.requestStorageAccess()"; |
| 124 | } |
| 125 | |
| 126 | void CheckStorageAccessForFrame(content::RenderFrameHost* frame, |
| 127 | bool access_expected) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 128 | bool data = content::EvalJs(frame, kHasStorageAccess, |
| 129 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 130 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 131 | EXPECT_EQ(access_expected, data) << "document.hasStorageAccess()"; |
| 132 | } |
| 133 | |
| 134 | } // namespace test |
| 135 | } // namespace storage |