Bug 1493246 [wpt PR 13139] - isXXX methods added to TrustedTypePolicyFactory, a=testonly
authorMaja Kabus <kabusm@google.com>
Tue, 09 Oct 2018 04:12:24 +0000
changeset 495910 845c69a518c67269e41236be2cbf6419666352b9
parent 495909 cb5fd7c21843259c9568db7722f8a7b7ba00b1a2
child 495911 4187f201e08813d5a0a01fedf641465c0c6fb36c
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1493246, 13139, 739170, 1238433, 595527
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1493246 [wpt PR 13139] - isXXX methods added to TrustedTypePolicyFactory, a=testonly Automatic update from web-platform-testsisXXX methods added to TrustedTypePolicyFactory isHTML(), isScript(), isScriptURL() and isURL() added to TrustedTypePolicyFactory class as part of Trusted Types API update to current JS polyfill. The methods require additional code to be fully matched to polyfill expected behaviour. Added a helper private method GetWrapperTypeInfoFromScriptValue. Bug: 739170 Change-Id: I027e43ab6432405c686255a4d0ce24248c59a4dc Reviewed-on: https://chromium-review.googlesource.com/1238433 Commit-Queue: Daniel Vogelheim <vogelheim@chromium.org> Reviewed-by: Daniel Vogelheim <vogelheim@chromium.org> Cr-Commit-Position: refs/heads/master@{#595527} -- wpt-commits: 63641fb5fe9942efd3eb81a5c831ab7f247e40c2 wpt-pr: 13139
testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-createPolicy-createXYZTests.tentative.html
testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-isXXX.tentative.html
--- a/testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-createPolicy-createXYZTests.tentative.html
+++ b/testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-createPolicy-createXYZTests.tentative.html
@@ -2,18 +2,20 @@
 <script src="/resources/testharness.js" ></script>
 <script src="/resources/testharnessreport.js"></script>
 <script src="support/helper.sub.js"></script>
 <body>
 <script>
   //HTML tests
   function createHTMLTest(policyName, policy, expectedHTML, t) {
     let p = window.TrustedTypes.createPolicy(policyName, policy);
-    assert_true(p.createHTML('whatever') instanceof TrustedHTML);
-    assert_equals(p.createHTML('whatever') + "", expectedHTML);
+    let html = p.createHTML('whatever');
+    assert_true(html instanceof TrustedHTML);
+    assert_true(TrustedTypes.isHTML(html));
+    assert_equals(html + "", expectedHTML);
   }
 
   test(t => {
     createHTMLTest('TestPolicyHTML1', { createHTML: s => s }, 'whatever', t);
   }, "html = identity function");
 
   test(t => {
     createHTMLTest('TestPolicyHTML2', { createHTML: s => null }, "null", t);
@@ -72,18 +74,20 @@
     assert_throws(new TypeError(), _ => {
       p.createURL(INPUTS.URL);
     });
   }, "createHTML defined - calling undefined callbacks throws");
 
   //Script tests
   function createScriptTest(policyName, policy, expectedScript, t) {
     let p = window.TrustedTypes.createPolicy(policyName, policy);
-    assert_true(p.createScript('whatever') instanceof TrustedScript);
-    assert_equals(p.createScript('whatever') + "", expectedScript);
+    let script = p.createScript('whatever');
+    assert_true(script instanceof TrustedScript);
+    assert_true(TrustedTypes.isScript(script));
+    assert_equals(script + "", expectedScript);
   }
 
   test(t => {
     createScriptTest('TestPolicyScript1', { createScript: s => s }, 'whatever', t);
   }, "script = identity function");
 
   test(t => {
     createScriptTest('TestPolicyScript2', { createScript: s => null }, "null", t);
@@ -145,18 +149,20 @@
       p.createURL(INPUTS.URL);
     });
   }, "createScript defined - calling undefined callbacks throws");
 
 
   //ScriptURL tests
   function createScriptURLTest(policyName, policy, expectedScriptURL, t) {
     let p = window.TrustedTypes.createPolicy(policyName, policy);
-    assert_true(p.createScriptURL(INPUTS.SCRIPTURL) instanceof TrustedScriptURL);
-    assert_equals(p.createScriptURL(INPUTS.SCRIPTURL) + "", expectedScriptURL);
+    let scriptUrl = p.createScriptURL(INPUTS.SCRIPTURL);
+    assert_true(scriptUrl instanceof TrustedScriptURL);
+    assert_true(TrustedTypes.isScriptURL(scriptUrl));
+    assert_equals(scriptUrl + "", expectedScriptURL);
   }
 
   test(t => {
     createScriptURLTest('TestPolicyScriptURL1', { createScriptURL: s => s }, INPUTS.SCRIPTURL, t);
   }, "script_url = identity function");
 
   test(t => {
     createScriptURLTest('TestPolicyScriptURL2', { createScriptURL: s => null }, "", t);
@@ -218,18 +224,20 @@
       p.createURL(INPUTS.URL);
     });
   }, "createScriptURL defined - calling undefined callbacks throws");
 
 
   //URL tests
   function createURLTest(policyName, policy, expectedURL, t) {
     let p = window.TrustedTypes.createPolicy(policyName, policy);
-    assert_true(p.createURL(INPUTS.URL) instanceof TrustedURL);
-    assert_equals(p.createURL(INPUTS.URL) + "", expectedURL);
+    let url = p.createURL(INPUTS.URL);
+    assert_true(url instanceof TrustedURL);
+    assert_true(TrustedTypes.isURL(url));
+    assert_equals(url + "", expectedURL);
   }
 
   test(t => {
     createURLTest('TestPolicyURL1', { createURL: s => s }, INPUTS.URL, t);
   }, "url = identity function");
 
   test(t => {
     createURLTest('TestPolicyURL2', { createURL: s => null }, "", t);
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-isXXX.tentative.html
@@ -0,0 +1,118 @@
+<!DOCTYPE html>
+<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">
+<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
+  // TODO(vogelheim): Implement TrustedTypes (& policy objects) as 'frozen'.
+/*  test(t => {
+    assert_throws(new TypeError(), _ => {
+      TrustedTypes.isHTML = () => true;
+    });
+
+    assert_false(TrustedTypes.isHTML({}));
+  }, 'TrustedTypePolicyFactory.IsHTML cannot be redefined.');
+
+  test(t => {
+    assert_throws(new TypeError(), _ => {
+      TrustedTypes.isScript = () => true;
+    });
+
+    assert_false(TrustedTypes.isScript({}));
+  }, 'TrustedTypePolicyFactory.isScript cannot be redefined.');
+
+  test(t => {
+    assert_throws(new TypeError(), _ => {
+      TrustedTypes.isScriptURL = () => true;
+    });
+
+    assert_false(TrustedTypes.isScriptURL({}));
+  }, 'TrustedTypePolicyFactory.isScriptURL cannot be redefined.');
+
+  test(t => {
+    assert_throws(new TypeError(), _ => {
+      TrustedTypes.isURL = () => true;
+    });
+
+    assert_false(TrustedTypes.isURL({}));
+  }, 'TrustedTypePolicyFactory.isURL cannot be redefined.');*/
+</script>