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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <tuple>
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "content/public/test/browser_test.h"
#include "extensions/common/extension_features.h"
#include "extensions/test/test_extension_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
#if BUILDFLAG(IS_ANDROID)
constexpr char kBackgroundJS_SabDisallowed[] = R"(
chrome.test.runTests([
function sendSharedArrayBufferToWorker() {
try {
let sab = new SharedArrayBuffer(16);
chrome.test.fail('SAB construction succeeded unexpectedly')
} catch (e) {
chrome.test.succeed();
}
}
]);
)";
#else
constexpr char kWorkerJS[] = R"(
function verifyData(data) {
if (data.byteLength != 16)
return `Improper byteLength: ${data.byteLength}`;
const bufView = new Uint8Array(data);
for (let i = 0; i < 16; i++) {
if (bufView[i] != i % 2) {
return `Data mismatch at index ${i}: Expected: ${i % 2}, got: ${
bufView[i]}`;
}
}
return 'PASS';
}
self.addEventListener('message', e => {
try {
postMessage(verifyData(e.data));
} catch (e) {
postMessage(e.message);
}
});
)";
constexpr char kBackgroundJS_SabAllowed[] = R"(
chrome.test.runTests([
function sendSharedArrayBufferToWorker() {
let sab = new SharedArrayBuffer(16);
let bufView = new Uint8Array(sab);
for (let i = 0; i < 16; i++)
bufView[i] = (i % 2);
const workerUrl = chrome.runtime.getURL('worker.js');
let worker = new Worker(workerUrl);
worker.onmessage = e => {
chrome.test.assertEq('PASS', e.data);
chrome.test.succeed();
};
worker.postMessage(sab);
chrome.test.assertEq(16, sab.byteLength);
// The worker will ack on receiving the SharedArrayBuffer causing the test
// to terminate.
}
]);
)";
#endif
// Parameterized on tuple of
// <is_sab_allowed_unconditionally, is_cross_origin_isolated, is_platform_app>.
class SharedArrayBufferTest
: public ExtensionApiTest,
public ::testing::WithParamInterface<std::tuple<bool, bool>> {
public:
TestExtensionDir& test_dir() { return test_dir_; }
private:
TestExtensionDir test_dir_;
};
IN_PROC_BROWSER_TEST_P(SharedArrayBufferTest, TransferToWorker) {
ASSERT_TRUE(StartEmbeddedTestServer());
bool is_cross_origin_isolated;
bool is_platform_app;
std::tie(is_cross_origin_isolated, is_platform_app) = GetParam();
auto builder = base::Value::Dict()
.Set("manifest_version", 2)
.Set("name", "SharedArrayBuffer")
.Set("version", "1.1");
if (is_cross_origin_isolated) {
builder.Set("cross_origin_opener_policy",
base::Value::Dict().Set("value", "same-origin"));
builder.Set("cross_origin_embedder_policy",
base::Value::Dict().Set("value", "require-corp"));
}
base::Value::Dict background_builder;
background_builder.Set("scripts",
base::Value::List().Append("background.js"));
if (is_platform_app) {
builder.Set("app", base::Value::Dict().Set("background",
std::move(background_builder)));
} else {
builder.Set("background", std::move(background_builder));
}
test_dir().WriteManifest(builder);
#if BUILDFLAG(IS_ANDROID)
test_dir().WriteFile(FILE_PATH_LITERAL("background.js"),
kBackgroundJS_SabDisallowed);
#else
test_dir().WriteFile(FILE_PATH_LITERAL("background.js"),
kBackgroundJS_SabAllowed);
test_dir().WriteFile(FILE_PATH_LITERAL("worker.js"), kWorkerJS);
#endif
ASSERT_TRUE(RunExtensionTest(test_dir().Pack(),
{.launch_as_platform_app = is_platform_app},
{} /* load_options */))
<< message_;
}
INSTANTIATE_TEST_SUITE_P(
,
SharedArrayBufferTest,
::testing::Combine(::testing::Bool(), ::testing::Bool()),
[](const testing::TestParamInfo<std::tuple<bool, bool>>& info) {
bool is_cross_origin_isolated;
bool is_platform_app;
std::tie(is_cross_origin_isolated, is_platform_app) = info.param;
return base::StringPrintf("%s_%s",
is_cross_origin_isolated ? "COI" : "NonCOI",
is_platform_app ? "App" : "Extension");
});
} // namespace extensions
|