Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Unified Diff: tests/html/safe_dom_test.dart

Issue 14839006: Adding tests for experimental Safe DOM creation technique. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/html/range_test.dart ('k') | tools/dom/templates/html/impl/impl_Range.darttemplate » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/html/safe_dom_test.dart
diff --git a/tests/html/safe_dom_test.dart b/tests/html/safe_dom_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5e7ae35c30b1f554e9bc773382039ecbae2333c4
--- /dev/null
+++ b/tests/html/safe_dom_test.dart
@@ -0,0 +1,98 @@
+library safe_dom_test;
+
+import 'dart:async';
+import 'dart:html';
+import '../../pkg/unittest/lib/unittest.dart';
+import '../../pkg/unittest/lib/html_config.dart';
+
+main() {
+ useHtmlConfiguration();
+
+ // Checks to see if any illegal properties were set via script.
+ var checkerScript = '''
+ window.addEventListener('message', function(e) {
+ if (e.data == 'check_unsafe') {
+ if (window.unsafe_value) {
+ window.postMessage('unsafe_check_failed', '*');
+ } else {
+ window.postMessage('unsafe_check_passed', '*');
+ }
+ //window.alert('checking!');
+ }
+ }, false);
+ ''';
+
+ var script = new ScriptElement();
+ script.text = checkerScript;
+ document.body.append(script);
+
+ var unsafeString =
+ '<img src="_.png" onerror="javascript:window.unsafe_value=1;" crap="1"/>';
+
+ test('Safe DOM', () {
+ var fragment = createContextualFragment(unsafeString);
+
+ expect(isSafe(), completion(true),
+ reason: 'Expected no unsafe code executed.');
+ });
+
+ // Make sure that scripts did get executed, so we know our detection works.
+ test('Unsafe Execution', () {
+ var div = new DivElement();
+ div.innerHtml = unsafeString;
+ // Crashing DRT ??
+ // var fragment = createContextualFragment(unsafeString);
+ // div.append(fragment);
+ // document.body.append(div)
+
+ expect(isSafe(), completion(false),
+ reason: 'Expected unsafe code was executed.');
+ });
+
+ test('Validity', () {
+ var fragment = createContextualFragment('<span>content</span>');
+ var div = new DivElement();
+ div.append(fragment);
+
+ expect(div.nodes.length, 1);
+ expect(div.nodes[0] is SpanElement, isTrue);
+ });
+}
+
+DocumentFragment createContextualFragment(String html, [String contextTag]) {
+ var doc = document.implementation.createHtmlDocument('');
+
+ var contextElement;
+ if (contextTag != null) {
+ contextElement = doc.$dom_createElement(contextTag);
+ } else {
+ contextElement = doc.body;
+ }
+
+ if (Range.supportsCreateContextualFragment) {
+ var range = doc.$dom_createRange();
+ range.selectNode(contextElement);
+ return range.createContextualFragment(html);
+ } else {
+ contextElement.innerHtml = html;
+ var fragment = new DocumentFragment();;
+ while (contextElement.$dom_firstChild != null) {
+ fragment.append(contextElement.$dom_firstChild);
+ }
+ return fragment;
+ }
+}
+
+// Delay to wait for the image load to fail.
+const Duration imageLoadDelay = const Duration(milliseconds: 500);
+
+Future<bool> isSafe() {
+ return new Future.delayed(imageLoadDelay).then((_) {
+ window.postMessage('check_unsafe', '*');
+ }).then((_) {
+ return window.onMessage.where(
+ (e) => e.data.startsWith('unsafe_check')).first;
+ }).then((e) {
+ return e.data == 'unsafe_check_passed';
+ });
+}
« no previous file with comments | « tests/html/range_test.dart ('k') | tools/dom/templates/html/impl/impl_Range.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698