subunit2html dealing with incorrect data format

The patch encodes a data string which contains
non-ASCII characters to unicode type.

In python2 there are two text types str and unicode.
When we add two different text types, for example:
>>> "Hello " + u"World"
u'Hello World'

The string on the left is decoded by using the default
system encoding into a Unicode string.
If it would contain non-ASCII characters(as in our case)
this normally blow up with an UnicodeDecodeError because
the default encoding is set to ASCII.

Change-Id: I17e5aa6f45133d9d12aa38cec9cc42694913d4bd
Closes-Bug: #1857901
This commit is contained in:
lkuchlan 2019-12-30 16:52:41 +02:00
parent 8859449359
commit 2c07d4e638
2 changed files with 23 additions and 1 deletions

View File

@ -53,6 +53,7 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import codecs
import collections
import datetime
import io
@ -680,9 +681,16 @@ class HtmlOutput(testtools.TestResult):
tmpl = (has_output and TemplateData.REPORT_TEST_WITH_OUTPUT_TMPL or
TemplateData.REPORT_TEST_NO_OUTPUT_TMPL)
try:
output = saxutils.escape(o + e)
# We expect to get this exception in python2.
except UnicodeDecodeError:
e = codecs.decode(e, 'utf-8')
output = saxutils.escape(o + e)
script = TemplateData.REPORT_TEST_OUTPUT_TMPL % dict(
id=tid,
output=saxutils.escape(o + e),
output=output,
)
row = tmpl % dict(

View File

@ -76,3 +76,17 @@ class TestSubunit2html(base.TestCase):
'example.path.to.test8']
for i, r in enumerate(sorted_result):
self.assertEqual(expected_class_order[i], str(r[0]))
@data(RemotedTestCase, PlaceHolder)
def test_generate_report_with_no_ascii_characters(self, test_cls):
# The test examines a case where an error containing no ascii
# characters is received.
test = test_cls(u'example.path.to.test1.method')
try:
raise Exception('\xe2\x82\xa5')
except Exception:
err = sys.exc_info()
obj = subunit2html.HtmlOutput()
# Add failure that contains no ascii characters
obj.addFailure(test, err)
obj._generate_report()