Commit ae3cfc7d authored by Yifan Luo's avatar Yifan Luo Committed by Commit Bot

[Sanitizer API] WPT tests clean up.

Bug: 1116418
Change-Id: I417bd15835f2e9cf367c369253c1d1a2bfb6edc9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2416695
Commit-Queue: Yifan Luo <lyf@chromium.org>
Reviewed-by: default avatarDaniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814759}
parent 955e5183
......@@ -3,12 +3,18 @@
[
Exposed=Window
] interface Sanitizer {
[RaisesException] constructor(optional SanitizerConfig sanitizerConfig = {});
[CallWith=ScriptState, RaisesException] DocumentFragment sanitize(DOMString input);
[CallWith=ScriptState, RaisesException] DOMString sanitizeToString(DOMString input);
constructor(optional SanitizerConfig sanitizerConfig = {});
DocumentFragment sanitize(DOMString input);
DOMString sanitizeToString(DOMString input);
readonly attribute SanitizerConfig creationOptions;
};
dictionary SanitizerConfig {
sequence<DOMString>? dropElements;
sequence<DOMString>? dropAttributes;
sequence<DOMString> allowElements;
sequence<DOMString> blockElements;
sequence<DOMString> dropElements;
sequence<DOMString> allowAttributes;
sequence<DOMString> blockAttributes;
sequence<DOMString> dropAttributes;
};
This is a testharness.js-based test.
FAIL SanitizerAPI creator without config. assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI creator with empty config. assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI creator with null as config. assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI creator with undefined as config. assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI creator with config ignore unknown values. assert_object_equals: expected property "dropElements" missing
PASS SanitizerAPI config is not editable.
PASS SanitizerAPI creator with config {dropElements: []}.
FAIL SanitizerAPI creator with config {dropElements: null}. Failed to construct 'Sanitizer': The provided value cannot be converted to a sequence.
FAIL SanitizerAPI creator with config {dropElements: undefined}. assert_object_equals: expected property "dropElements" missing
PASS SanitizerAPI creator with config {dropElements: 123}.
PASS SanitizerAPI creator with config {dropElements: div}.
Harness: the test ran to completion.
......@@ -3,85 +3,35 @@
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/testcases.sub.js"></script>
</head>
<body>
<script>
const default_option ={dropElements: null};
test(t => {
let s = new Sanitizer();
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator without config.");
test(t => {
let s = new Sanitizer({});
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with empty config.");
test(t => {
let s = new Sanitizer(null);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with null as config.");
test(t => {
let s = new Sanitizer(undefined);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with undefined as config.");
test(t => {
let s = new Sanitizer({testConfig: [1,2,3], attr: ["test", "i", "am"]});
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config ignore unknown values.");
test(t => {
let options = {dropElements: ["div"]};
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
options.dropElements.push("test");
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
s.creationOptions = {dropElements: ["test", "t"]};
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
s.creationOptions['dropElements'] = [1,2,3];
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
}, "SanitizerAPI config is not editable.");
test(t => {
let s = new Sanitizer({dropElements: []});
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config {dropElements: []}.")
test(t => {
let s = new Sanitizer({dropElements: null});
assert_true(s instanceof Sanitizer);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config {dropElements: null}.")
test(t => {
let s = new Sanitizer({dropElements: undefined});
assert_true(s instanceof Sanitizer);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config {dropElements: undefined}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: 123})});
}, "SanitizerAPI creator with config {dropElements: 123}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: "div"})});
}, "SanitizerAPI creator with config {dropElements: div}.");
const config_names = ["dropElements", "blockElements", "allowElements", "dropAttributes", "blockAttributes", "allowAttributes"];
config_names.forEach(cname => {
let options = {};
options[cname] = [];
test(t => {
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config " + JSON.stringify(options) + ".");
});
</script>
</body>
......
This is a testharness.js-based test.
FAIL SanitizerAPI sanitize function for string assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for html fragment assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for broken html assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for empty object assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for number assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for zeros assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for arithmetic assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for empty string assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for undefined assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for null assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for document assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for html without close tag assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for scripts assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for onclick scripts assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitize function for invalid config_input assert_object_equals: expected property "dropElements" missing
PASS SanitizerAPI sanitize function for empty dropElements list
PASS SanitizerAPI sanitize function for test html without close tag with dropElements list ['div']
PASS SanitizerAPI sanitize function for test script with ["script"] as dropElements list
PASS SanitizerAPI sanitize function for dropElements list ["test", "i"]}
PASS SanitizerAPI sanitize function for dropElements list ["I", "AM"]}
PASS SanitizerAPI sanitize function for dropElements list ["am", "p"]}
PASS SanitizerAPI sanitize function for dropElements list with invalid values}
Harness: the test ran to completion.
......@@ -16,8 +16,6 @@
testcases.forEach(c => test(t => {
let s = new Sanitizer(c.config_input);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, c.config_value);
fragment = s.sanitize(c.value);
assert_true(fragment instanceof DocumentFragment);
......
This is a testharness.js-based test.
FAIL SanitizerAPI sanitizeToString function for string assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for html fragment assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for broken html assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for empty object assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for number assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for zeros assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for arithmetic assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for empty string assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for undefined assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for null assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for document assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for html without close tag assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for scripts assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for onclick scripts assert_object_equals: expected property "dropElements" missing
FAIL SanitizerAPI sanitizeToString function for invalid config_input assert_object_equals: expected property "dropElements" missing
PASS SanitizerAPI sanitizeToString function for empty dropElements list
PASS SanitizerAPI sanitizeToString function for test html without close tag with dropElements list ['div']
PASS SanitizerAPI sanitizeToString function for test script with ["script"] as dropElements list
PASS SanitizerAPI sanitizeToString function for dropElements list ["test", "i"]}
PASS SanitizerAPI sanitizeToString function for dropElements list ["I", "AM"]}
PASS SanitizerAPI sanitizeToString function for dropElements list ["am", "p"]}
PASS SanitizerAPI sanitizeToString function for dropElements list with invalid values}
Harness: the test ran to completion.
......@@ -10,8 +10,6 @@
<script>
testcases.forEach(c => test(t => {
let s = new Sanitizer(c.config_input);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, c.config_value);
assert_equals(s.sanitizeToString(c.value), c.result);
}, "SanitizerAPI sanitizeToString function for " + c.message));
</script>
......
const testcases = [
{config_input: {}, config_value: {dropElements: null}, value: "test", result: "test", message: "string"},
{config_input: {}, config_value: {dropElements: null}, value: "<b>bla</b>", result: "<b>bla</b>", message: "html fragment"},
{config_input: {}, config_value: {dropElements: null}, value: "<a<embla", result: "", message: "broken html"},
{config_input: {}, config_value: {dropElements: null}, value: {}, result: "[object Object]", message: "empty object"},
{config_input: {}, config_value: {dropElements: null}, value: 1, result: "1", message: "number"},
{config_input: {}, config_value: {dropElements: null}, value: 000, result: "0", message: "zeros"},
{config_input: {}, config_value: {dropElements: null}, value: 1+2, result: "3", message: "arithmetic"},
{config_input: {}, config_value: {dropElements: null}, value: "", result: "", message: "empty string"},
{config_input: {}, config_value: {dropElements: null}, value: undefined, result: "undefined", message: "undefined"},
{config_input: {}, config_value: {dropElements: null}, value: null, result: "null", message: "null"},
{config_input: {}, config_value: {dropElements: null}, value: "<html><head></head><body>test</body></html>", result: "test", message: "document"},
{config_input: {}, config_value: {dropElements: null}, value: "<div>test", result: "<div>test</div>", message: "html without close tag"},
{config_input: {}, config_value: {dropElements: null}, value: "<script>alert('i am a test')<\/script>", result: "", message: "scripts"},
{config_input: {}, config_value: {dropElements: null}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "onclick scripts"},
{config_input: {test: 123}, config_value: {dropElements: null}, value: "test", result: "test", message: "invalid config_input"},
{config_input: {dropElements: []}, config_value: {dropElements:[]}, value: "test", result: "test", message: "empty dropElements list"},
{config_input: {dropElements: ["div"]}, config_value: {dropElements:["DIV"]}, value: "<div>test</div><c>bla", result: "<c>bla</c>", message: "test html without close tag with dropElements list ['div']"},
{config_input: {dropElements: ["script"]}, config_value: {dropElements:["SCRIPT"]}, value: "<script>alert('i am a test')<\/script>", result: "", message: "test script with [\"script\"] as dropElements list"},
{config_input: {dropElements: ["test", "i"]}, config_value: {dropElements:["TEST","I"]}, value: "<div>balabala<i>test</i></div><test>t</test>", result: "<div>balabala</div>", message: "dropElements list [\"test\", \"i\"]}"},
{config_input: {dropElements: ["I", "AM"]}, config_value: {dropElements:["I", "AM"]}, value: "<div>balabala<am>test</am></div>", result: "<div>balabala</div>", message: "dropElements list [\"I\", \"AM\"]}"},
{config_input: {dropElements: ["am", "p"]}, config_value: {dropElements:["AM","P"]}, value: "<div>balabala<i>i</i><p>t</p><test>a</test></div>", result: "<div>balabala<i>i</i><test>a</test></div>", message: "dropElements list [\"am\", \"p\"]}"},
{config_input: {dropElements: [123, [], "test", "i"]}, config_value: {dropElements:["123","","TEST","I"]}, value: "<div>balabala<i>test</i></div><test>t</test>", result: "<div>balabala</div>", message: "dropElements list with invalid values}"}
{config_input: {}, value: "test", result: "test", message: "string"},
{config_input: {}, value: "<b>bla</b>", result: "<b>bla</b>", message: "html fragment"},
{config_input: {}, value: "<a<embla", result: "", message: "broken html"},
{config_input: {}, value: {}, result: "[object Object]", message: "empty object"},
{config_input: {}, value: 1, result: "1", message: "number"},
{config_input: {}, value: 000, result: "0", message: "zeros"},
{config_input: {}, value: 1+2, result: "3", message: "arithmetic"},
{config_input: {}, value: "", result: "", message: "empty string"},
{config_input: {}, value: "<html><head></head><body>test</body></html>", result: "test", message: "document"},
{config_input: {}, value: "<div>test", result: "<div>test</div>", message: "html without close tag"},
];
......@@ -3,7 +3,6 @@
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/testcases.sub.js"></script>
</head>
<body>
......@@ -39,88 +38,57 @@
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config ignore unknown values.");
// TODO(lyf): Lump following two tests together into a shared function.
test(t => {
let options = {dropElements: ["div"]};
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
options.dropElements.push("test");
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
s.creationOptions = {dropElements: ["test", "t"]};
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
s.creationOptions['dropElements'] = [1,2,3];
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
}, "SanitizerAPI config dropElements is not editable.");
test(t => {
let options = {dropAttributes: ["onclick"]};
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {dropAttributes: ["onclick"]});
options.dropAttributes.push("test");
assert_object_equals(s.creationOptions, {dropAttributes: ["onclick"]});
s.creationOptions = {dropAttributes: ["test", "t"]};
assert_object_equals(s.creationOptions, {dropAttributes: ["onclick"]});
s.creationOptions['dropAttributes'] = [1,2,3];
assert_object_equals(s.creationOptions, {dropAttributes: ["onclick"]});
}, "SanitizerAPI config dropAttributes is not editable.");
test(t => {
let s = new Sanitizer({dropElements: []});
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config {dropElements: []}.")
test(t => {
let s = new Sanitizer({dropElements: undefined});
assert_true(s instanceof Sanitizer);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config {dropElements: undefined}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: 123})});
}, "SanitizerAPI creator with config {dropElements: 123}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: "div"})});
}, "SanitizerAPI creator with config {dropElements: div}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: null})});
}, "SanitizerAPI creator with config {dropElements: null}.")
test(t => {
let s = new Sanitizer({dropAttributes: []});
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config {dropAttributes: []}.")
test(t => {
let s = new Sanitizer({dropAttributes: undefined});
assert_true(s instanceof Sanitizer);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config {dropAttributes: undefined}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropAttributes: 123})});
}, "SanitizerAPI creator with config {dropAttributes: 123}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropAttributes: "div"})});
}, "SanitizerAPI creator with config {dropAttributes: div}.");
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropAttributes: null})});
}, "SanitizerAPI creator with config {dropAttributes: null}.")
const config_names = ["dropElements", "dropAttributes"];
config_names.forEach(cname => {
test(t => {
let options = {};
options[cname] = cname.endsWith("Elements") ? ["div"] : ["script"];
let result = {};
result[cname] = cname.endsWith("Elements") ? ["DIV"] : ["script"];
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, result);
options[cname].push("test");
assert_object_equals(s.creationOptions, result);
let new_options = {};
new_options[cname] = ["test", "t"];
s.creationOptions = new_options;
assert_object_equals(s.creationOptions, result);
s.creationOptions[cname] = [1,2,3];
assert_object_equals(s.creationOptions, result);
}, "SanitizerAPI config " + cname + " is not editable.");
let options = {};
options[cname] = [];
test(t => {
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer)
assert_object_equals(s.creationOptions, options);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config " + JSON.stringify(options) + ".");
options = {};
options[cname] = undefined;
test(t => {
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer)
assert_object_equals(s.creationOptions, default_option);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config " + JSON.stringify(options, function(k,v){return v===undefined?"::undefined::":v}).replace(new RegExp("\"::undefined::\"", 'g'), "undefined") + ".");
let testcases = [null, 123, "div"];
testcases.forEach(c => {
options = {};
options[cname] = c;
test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer(options)});
}, "SanitizerAPI creator with config " + JSON.stringify(options) + ".");
});
});
</script>
</body>
......
......@@ -13,6 +13,7 @@ PASS SanitizerAPI with config: null, sanitize function for null
PASS SanitizerAPI with config: document, sanitize function for document
PASS SanitizerAPI with config: html without close tag, sanitize function for html without close tag
FAIL SanitizerAPI with config: scripts for default configs, sanitize function for scripts for default configs assert_equals: expected "" but got "<script>alert('i am a test')</script>"
FAIL SanitizerAPI with config: onclick scripts, sanitize function for onclick scripts assert_equals: expected "<p>Click.</p>" but got "<p onclick=\"a= 123\">Click.</p>"
PASS SanitizerAPI with config: invalid config_input, sanitize function for invalid config_input
PASS SanitizerAPI with config: empty dropElements list, sanitize function for empty dropElements list
PASS SanitizerAPI with config: test html without close tag with dropElements list ['div'], sanitize function for test html without close tag with dropElements list ['div']
......
......@@ -13,6 +13,7 @@ PASS SanitizerAPI config: null, sanitizeToString function for null
PASS SanitizerAPI config: document, sanitizeToString function for document
PASS SanitizerAPI config: html without close tag, sanitizeToString function for html without close tag
FAIL SanitizerAPI config: scripts for default configs, sanitizeToString function for scripts for default configs assert_equals: expected "" but got "<script>alert('i am a test')</script>"
FAIL SanitizerAPI config: onclick scripts, sanitizeToString function for onclick scripts assert_equals: expected "<p>Click.</p>" but got "<p onclick=\"a= 123\">Click.</p>"
PASS SanitizerAPI config: invalid config_input, sanitizeToString function for invalid config_input
PASS SanitizerAPI config: empty dropElements list, sanitizeToString function for empty dropElements list
PASS SanitizerAPI config: test html without close tag with dropElements list ['div'], sanitizeToString function for test html without close tag with dropElements list ['div']
......
......@@ -12,6 +12,7 @@ const testcases = [
{config_input: {}, config_value: {}, value: "<html><head></head><body>test</body></html>", result: "test", message: "document"},
{config_input: {}, config_value: {}, value: "<div>test", result: "<div>test</div>", message: "html without close tag"},
{config_input: {}, config_value: {}, value: "<script>alert('i am a test')<\/script>", result: "", message: "scripts for default configs"},
{config_input: {}, config_value: {}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "onclick scripts"},
{config_input: {test: 123}, config_value: {}, value: "test", result: "test", message: "invalid config_input"},
{config_input: {dropElements: []}, config_value: {dropElements:[]}, value: "test", result: "test", message: "empty dropElements list"},
{config_input: {dropElements: ["div"]}, config_value: {dropElements:["DIV"]}, value: "<div>test</div><c>bla", result: "<c>bla</c>", message: "test html without close tag with dropElements list ['div']"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment