Hexlify strings in TestJavascriptEscape.

This commit is contained in:
Florian Bruhin 2015-08-04 08:49:42 +02:00
parent 0ce9ae070c
commit 0a16f29bd1
2 changed files with 120 additions and 8 deletions

View File

@ -25,10 +25,12 @@ from unittest import mock
import collections.abc import collections.abc
import operator import operator
import itertools import itertools
import binascii
import os.path
import hypothesis import hypothesis
import hypothesis.strategies import hypothesis.strategies
from PyQt5.QtCore import QRect, QPoint from PyQt5.QtCore import PYQT_VERSION, QRect, QPoint
from PyQt5.QtWebKit import QWebElement from PyQt5.QtWebKit import QWebElement
import pytest import pytest
@ -525,21 +527,63 @@ class TestJavascriptEscape:
"""Test javascript escaping with some expected outcomes.""" """Test javascript escaping with some expected outcomes."""
assert webelem.javascript_escape(before) == after assert webelem.javascript_escape(before) == after
def _test_escape(self, text, webframe): def _test_escape(self, text, qtbot, webframe):
"""Helper function for test_real_escape*.""" """Helper function for test_real_escape*."""
try:
self._test_escape_simple(text, webframe)
except AssertionError:
# Try another method if the simple method failed.
#
# See _test_escape_hexlified documentation on why this is
# necessary.
self._test_escape_hexlified(text, qtbot, webframe)
def _test_escape_hexlified(self, text, qtbot, webframe):
"""Test conversion by hexlifying in javascript.
Since the conversion of QStrings to Python strings is broken in some
older PyQt versions in some corner cases, we load a HTML file which
generates an MD5 of the escaped text and use that for comparisons.
"""
escaped = webelem.javascript_escape(text) escaped = webelem.javascript_escape(text)
webframe.evaluateJavaScript('window.foo = "{}";'.format(escaped)) path = os.path.join(os.path.dirname(__file__),
assert webframe.evaluateJavaScript('window.foo') == text 'test_webelem_jsescape.html')
with open(path, encoding='utf-8') as f:
html_source = f.read().replace('%INPUT%', escaped)
with qtbot.waitSignal(webframe.loadFinished, raising=True):
webframe.setHtml(html_source)
result = webframe.evaluateJavaScript('window.qute_test_result')
assert result is not None
assert '|' in result
result_md5, result_text = result.split('|', maxsplit=1)
text_md5 = binascii.hexlify(text.encode('utf-8')).decode('ascii')
assert result_md5 == text_md5, result_text
def _test_escape_simple(self, text, webframe):
"""Test conversion by using evaluateJavaScript."""
escaped = webelem.javascript_escape(text)
result = webframe.evaluateJavaScript('"{}";'.format(escaped))
assert result == text
@pytest.mark.parametrize('text', TESTS) @pytest.mark.parametrize('text', TESTS)
def test_real_escape(self, webframe, text): def test_real_escape(self, webframe, qtbot, text):
"""Test javascript escaping with a real QWebPage.""" """Test javascript escaping with a real QWebPage."""
self._test_escape(text, webframe) self._test_escape(text, qtbot, webframe)
@hypothesis.given(hypothesis.strategies.text()) @hypothesis.given(hypothesis.strategies.text())
def test_real_escape_hypothesis(self, webframe, text): def test_real_escape_hypothesis(self, webframe, qtbot, text):
"""Test javascript escaping with a real QWebPage and hypothesis.""" """Test javascript escaping with a real QWebPage and hypothesis."""
self._test_escape(text, webframe) # We can't simply use self._test_escape because of this:
# https://github.com/pytest-dev/pytest-qt/issues/69
# self._test_escape(text, qtbot, webframe)
try:
self._test_escape_simple(text, webframe)
except AssertionError:
if PYQT_VERSION >= 0x050300:
self._test_escape_hexlified(text, qtbot, webframe)
class TestGetChildFrames: class TestGetChildFrames:

View File

@ -0,0 +1,68 @@
<!--
Helper file for test_javascript_escape() in test_webelem.py.
Since the conversion from QStrings to Python strings is broken in some corner
cases in PyQt < 5.4 we hexlify the string we got in javascript here and test
that in the test.
-->
<html>
<head>
<script type="text/javascript">
//<![CDATA[
/*
* hexlify() and str2rstr_utf8() are based on:
*
* JavaScript MD5 1.0.1
* https://github.com/blueimp/JavaScript-MD5
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*
* Based on
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
'use strict';
function hexlify(input) {
var hex_tab = '0123456789abcdef';
var output = '';
var x;
var i;
for (i = 0; i < input.length; i += 1) {
x = input.charCodeAt(i);
output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
}
return output;
}
function encode_utf8(input) {
return unescape(encodeURIComponent(input));
}
function set_text() {
var elems = document.getElementsByTagName("p");
var hexlified = hexlify(encode_utf8("%INPUT%"));
var result = hexlified + "|" + "%INPUT%";
elems[0].innerHTML = result
window.qute_test_result = result;
}
//]]>
</script>
</head>
<body onload="set_text()">
<p>set_text() not called...</p>
</html>