Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the trusted-types tests run correctly in a vanilla wptrunner. #14400

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!DOCTYPE html>
<head>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
</head>
<body>
<script>
//HTML tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<!DOCTYPE html>
<head>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<meta http-equiv="Content-Security-Policy" content="trusted-types">
</head>
<body>
<script>
//No name given test
test(t => {
assert_throws(new TypeError(), _ => {
window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
});
}, "No name list given - policy creation throws");
run_tests_in_child_frame(
'<meta http-equiv="Content-Security-Policy" content="trusted-types">',
() => {
//No name given test
test(t => {
assert_throws(new TypeError(), _ => {
window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
});
}, "No name list given - policy creation throws");
});
</script>

Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<!DOCTYPE html>
<head>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<meta http-equiv="Content-Security-Policy" content="trusted-types *">
</head>
<body>
<script>
//No name given test
test(t => {
let policy = window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
assert_equals(policy.name, 'SomeName');
}, "Wildcard given - policy creation works");
run_tests_in_child_frame(
'<meta http-equiv="Content-Security-Policy" content="trusted-types *">',
() => {
//No name given test
test(t => {
let policy = window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
assert_equals(policy.name, 'SomeName');
}, "Wildcard given - policy creation works");
});
</script>

Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
<!DOCTYPE html>
<head>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>

<meta http-equiv="Content-Security-Policy" content="trusted-types SomeName JustOneMoreName">
</head>
<body>
<script>
// Whitelisted name test
test(t => {
let policy = window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
assert_equals(policy.name, 'SomeName');
}, "Whitelisted policy creation works.");
run_tests_in_child_frame(
'<meta http-equiv="Content-Security-Policy" content="trusted-types SomeName JustOneMoreName">',
() => {
// Whitelisted name test
test(t => {
let policy = window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
assert_equals(policy.name, 'SomeName');
}, "Whitelisted policy creation works.");

// Another whitelisted name test
test(t => {
let policy = window.TrustedTypes.createPolicy('JustOneMoreName', { createHTML: s => s } );
assert_equals(policy.name, 'JustOneMoreName');
}, "Another whitelisted policy creation works.");
// Another whitelisted name test
test(t => {
let policy = window.TrustedTypes.createPolicy('JustOneMoreName', { createHTML: s => s } );
assert_equals(policy.name, 'JustOneMoreName');
}, "Another whitelisted policy creation works.");

// Non-whitelisted names test
test(t => {
assert_throws(new TypeError(), _ => {
window.TrustedTypes.createPolicy('SomeOtherName', { createURL: s => s } );
});
}, "Non-whitelisted policy creation throws.");
// Non-whitelisted names test
test(t => {
assert_throws(new TypeError(), _ => {
window.TrustedTypes.createPolicy('SomeOtherName', { createURL: s => s } );
});
}, "Non-whitelisted policy creation throws.");
});
</script>
250 changes: 127 additions & 123 deletions trusted-types/TrustedTypePolicyFactory-isXXX.tentative.html
Original file line number Diff line number Diff line change
@@ -1,131 +1,135 @@
<!DOCTYPE html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>

<meta http-equiv="Content-Security-Policy" content="trusted-types *">
</head>
<body>
<script>
// Policy settings for all tests
const noopPolicy = {
'createHTML': (s) => s,
'createScriptURL': (s) => s,
'createURL': (s) => s,
'createScript': (s) => s,
};

// isHTML tests
test(t => {
const p = TrustedTypes.createPolicy('html', noopPolicy);
let html = p.createHTML(INPUTS.HTML);

assert_true(TrustedTypes.isHTML(html));
let html2 = Object.create(html);

// instanceof can pass, but we rely on isHTML
assert_true(html2 instanceof TrustedHTML);
assert_false(TrustedTypes.isHTML(html2));

let html3 = Object.assign({}, html, {toString: () => 'fake'});

assert_false(TrustedTypes.isHTML(html3));
}, 'TrustedTypePolicyFactory.isHTML requires the object to be created via policy.');

// isScript tests
test(t => {
const p = TrustedTypes.createPolicy('script', noopPolicy);
let script = p.createScript(INPUTS.SCRIPT);

assert_true(TrustedTypes.isScript(script));
let script2 = Object.create(script);

// instanceof can pass, but we rely on isScript
assert_true(script2 instanceof TrustedScript);
assert_false(TrustedTypes.isScript(script2));

let script3 = Object.assign({}, script, {toString: () => 'fake'});

assert_false(TrustedTypes.isScript(script3));
}, 'TrustedTypePolicyFactory.isScript requires the object to be created via policy.');

// isScriptURL tests
test(t => {
const p = TrustedTypes.createPolicy('script_url', noopPolicy);
let script = p.createScriptURL(INPUTS.SCRIPTURL);

assert_true(TrustedTypes.isScriptURL(script));
let script2 = Object.create(script);

// instanceof can pass, but we rely on isScript
assert_true(script2 instanceof TrustedScriptURL);
assert_false(TrustedTypes.isScriptURL(script2));

let script3 = Object.assign({}, script, {toString: () => 'fake'});

assert_false(TrustedTypes.isScriptURL(script3));
}, 'TrustedTypePolicyFactory.isScriptURL requires the object to be created via policy.');

// isURL tests
test(t => {
const p = TrustedTypes.createPolicy('url', noopPolicy);
let url = p.createURL(INPUTS.URL);

assert_true(TrustedTypes.isURL(url));
let url2 = Object.create(url);

// instanceof can pass, but we rely on isScript
assert_true(url2 instanceof TrustedURL);
assert_false(TrustedTypes.isURL(url2));

let url3 = Object.assign({}, url, {toString: () => 'fake'});

assert_false(TrustedTypes.isURL(url3));
}, 'TrustedTypePolicyFactory.isURL requires the object to be created via policy.');

// Redefinition tests, assign to property.
// (Assignments will through in the polyfill (because the objects are frozen)
// but will be silently dropped in the native implementation (because that's
// what [Unforgeable] does. Hence, the tests use try {..} catch {} to cover
// both situationsm rather than expect_throws(...).)
test(t => {
try { TrustedTypes.isHTML = () => 'fake'; } catch { }
assert_false(TrustedTypes.isHTML({}));
}, 'TrustedTypePolicyFactory.IsHTML cannot be redefined.');

test(t => {
try { TrustedTypes.isScript = () => 'fake'; } catch { }
assert_false(TrustedTypes.isScript({}));
}, 'TrustedTypePolicyFactory.isScript cannot be redefined.');

test(t => {
try { TrustedTypes.isScriptURL = () => 'fake'; } catch { }
assert_false(TrustedTypes.isScriptURL({}));
}, 'TrustedTypePolicyFactory.isScriptURL cannot be redefined.');

test(t => {
try { TrustedTypes.isURL = () => 'fake'; } catch { }
assert_false(TrustedTypes.isURL({}));
}, 'TrustedTypePolicyFactory.isURL cannot be redefined.');

// Redefinition tests, via Object.defineProperty.
test(t => {
try { Object.defineProperty(TrustedTypes, 'isHTML', () => 'fake'); } catch { }
assert_false(TrustedTypes.isHTML({}));
}, 'TrustedTypePolicyFactory.IsHTML cannot be redefined via defineProperty.');

test(t => {
try { Object.defineProperty(TrustedTypes, 'isScript', () => 'fake'); } catch { }
assert_false(TrustedTypes.isScript({}));
}, 'TrustedTypePolicyFactory.isScript cannot be redefined via definePropert.');

test(t => {
try { Object.defineProperty(TrustedTypes, 'isScriptURL', () => 'fake'); } catch { }
assert_false(TrustedTypes.isScriptURL({}));
}, 'TrustedTypePolicyFactory.isScriptURL cannot be redefined via definePropert.');

test(t => {
try { Object.defineProperty(TrustedTypes, 'isURL', () => 'fake'); } catch { }
assert_false(TrustedTypes.isURL({}));
}, 'TrustedTypePolicyFactory.isURL cannot be redefined via definePropert.');
run_tests_in_child_frame(
'<meta http-equiv="Content-Security-Policy" content="trusted-types *">',
() => {

// Policy settings for all tests
const noopPolicy = {
'createHTML': (s) => s,
'createScriptURL': (s) => s,
'createURL': (s) => s,
'createScript': (s) => s,
};

// isHTML tests
test(t => {
const p = TrustedTypes.createPolicy('html', noopPolicy);
let html = p.createHTML(INPUTS.HTML);

assert_true(TrustedTypes.isHTML(html));
let html2 = Object.create(html);

// instanceof can pass, but we rely on isHTML
assert_true(html2 instanceof TrustedHTML);
assert_false(TrustedTypes.isHTML(html2));

let html3 = Object.assign({}, html, {toString: () => 'fake'});

assert_false(TrustedTypes.isHTML(html3));
}, 'TrustedTypePolicyFactory.isHTML requires the object to be created via policy.');

// isScript tests
test(t => {
const p = TrustedTypes.createPolicy('script', noopPolicy);
let script = p.createScript(INPUTS.SCRIPT);

assert_true(TrustedTypes.isScript(script));
let script2 = Object.create(script);

// instanceof can pass, but we rely on isScript
assert_true(script2 instanceof TrustedScript);
assert_false(TrustedTypes.isScript(script2));

let script3 = Object.assign({}, script, {toString: () => 'fake'});

assert_false(TrustedTypes.isScript(script3));
}, 'TrustedTypePolicyFactory.isScript requires the object to be created via policy.');

// isScriptURL tests
test(t => {
const p = TrustedTypes.createPolicy('script_url', noopPolicy);
let script = p.createScriptURL(INPUTS.SCRIPTURL);

assert_true(TrustedTypes.isScriptURL(script));
let script2 = Object.create(script);

// instanceof can pass, but we rely on isScript
assert_true(script2 instanceof TrustedScriptURL);
assert_false(TrustedTypes.isScriptURL(script2));

let script3 = Object.assign({}, script, {toString: () => 'fake'});

assert_false(TrustedTypes.isScriptURL(script3));
}, 'TrustedTypePolicyFactory.isScriptURL requires the object to be created via policy.');

// isURL tests
test(t => {
const p = TrustedTypes.createPolicy('url', noopPolicy);
let url = p.createURL(INPUTS.URL);

assert_true(TrustedTypes.isURL(url));
let url2 = Object.create(url);

// instanceof can pass, but we rely on isScript
assert_true(url2 instanceof TrustedURL);
assert_false(TrustedTypes.isURL(url2));

let url3 = Object.assign({}, url, {toString: () => 'fake'});

assert_false(TrustedTypes.isURL(url3));
}, 'TrustedTypePolicyFactory.isURL requires the object to be created via policy.');

// Redefinition tests, assign to property.
// (Assignments will through in the polyfill (because the objects are frozen)
// but will be silently dropped in the native implementation (because that's
// what [Unforgeable] does. Hence, the tests use try {..} catch {} to cover
// both situationsm rather than expect_throws(...).)
test(t => {
try { TrustedTypes.isHTML = () => 'fake'; } catch { }
assert_false(TrustedTypes.isHTML({}));
}, 'TrustedTypePolicyFactory.IsHTML cannot be redefined.');

test(t => {
try { TrustedTypes.isScript = () => 'fake'; } catch { }
assert_false(TrustedTypes.isScript({}));
}, 'TrustedTypePolicyFactory.isScript cannot be redefined.');

test(t => {
try { TrustedTypes.isScriptURL = () => 'fake'; } catch { }
assert_false(TrustedTypes.isScriptURL({}));
}, 'TrustedTypePolicyFactory.isScriptURL cannot be redefined.');

test(t => {
try { TrustedTypes.isURL = () => 'fake'; } catch { }
assert_false(TrustedTypes.isURL({}));
}, 'TrustedTypePolicyFactory.isURL cannot be redefined.');

// Redefinition tests, via Object.defineProperty.
test(t => {
try { Object.defineProperty(TrustedTypes, 'isHTML', () => 'fake'); } catch { }
assert_false(TrustedTypes.isHTML({}));
}, 'TrustedTypePolicyFactory.IsHTML cannot be redefined via defineProperty.');

test(t => {
try { Object.defineProperty(TrustedTypes, 'isScript', () => 'fake'); } catch { }
assert_false(TrustedTypes.isScript({}));
}, 'TrustedTypePolicyFactory.isScript cannot be redefined via definePropert.');

test(t => {
try { Object.defineProperty(TrustedTypes, 'isScriptURL', () => 'fake'); } catch { }
assert_false(TrustedTypes.isScriptURL({}));
}, 'TrustedTypePolicyFactory.isScriptURL cannot be redefined via definePropert.');

test(t => {
try { Object.defineProperty(TrustedTypes, 'isURL', () => 'fake'); } catch { }
assert_false(TrustedTypes.isURL({}));
}, 'TrustedTypePolicyFactory.isURL cannot be redefined via definePropert.');
});
</script>
Loading