Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpunit --log-junit generates invalid xml file when test output contains binary data #2974

Closed
SimonHeimberg opened this issue Jan 26, 2018 · 3 comments

Comments

@SimonHeimberg
Copy link
Contributor

SimonHeimberg commented Jan 26, 2018

Q A
PHPUnit version 6.5.5
PHP version 7.0/7.1
Installation Method Composer

Problem

running phpunit --log-junit /some/file_junit.xml generates an invalid xml file when a test produces output with binary data.

shortened example output:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="Project Test Suite" tests="552" assertions="548" errors="7" failures="3" skipped="208" time="156.086893">
    <testsuite name="AppBundle\Tests\Controller\PageLoadingTest" file="/home/me/src/proj/src/AppBundle/Tests/Controller/PageLoadingTest.php" tests="424" assertions="239" errors="6" failures="3" skipped="186" time="106.122545">
      <testsuite name="AppBundle\Tests\Controller\PageLoadingTest::testSimplePageLoading" tests="173" assertions="162" errors="3" failures="2" skipped="11" time="50.862405">
	    <testcase name="testSimplePageLoading with data set #91" class="AppBundle\Tests\Controller\PageLoadingTest" classname="AppBundle.Tests.Controller.PageLoadingTest" file="/home/me/src/proj/src/AppBundle/Tests/Controller/PageLoadingTest.php" line="26" assertions="1" time="0.325804">
          <system-out>PK���</system-out>
        </testcase>
    </testsuite>
  </testsuite>
</testsuites>

The illegal characters are after <system-out>PK, this element reads as
<system-out>PK\x03\x04\x14</system-out> (\xNN is hex encoded).

As file do download: illegal_xml_from_phpunit_junit.xml.txt

Notes

about allowed characters in xml: https://stackoverflow.com/a/28152666/4124767

I do not know what replacements does the xml writer of php (hopefully & and > and similar).

Somewhere (on stackoverflow?) I found the following code to replace illegal characters. I do not yet an Idea if this helps.

$r = '∎';
$validContent = preg_replace('/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', $r, $invalidContent);
@ryanaslett
Copy link

ryanaslett commented Jan 31, 2018

This is the code we've been using in drupalci to defend our xml against invalid chars:
$validContent = preg_replace('/[^\x{0009}\x{000A}\x{000D}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u', '�', $invalidContent);

@sebastianbergmann
Copy link
Owner

A pull request would be appreciated.

mkasberg added a commit to mkasberg/phpunit that referenced this issue Feb 16, 2018
JUnit XML (produced with --log-junit) is invalid (and won't be opened by
some programs) if it contains invalid XML characters in the <system-out>
node. Fix this by sanitizing text before adding it to the XML.
@mkasberg
Copy link
Contributor

I was able to reproduce it and found a simple fix for the problem.

This test case should help if you're trying to reproduce:

BadJunitXMLTest.php

<?php

use PHPUnit\Framework\TestCase;

class BadJUnitXMLTest extends TestCase {
    
    public function testBadXML() {
        print "Bad Chars: \u{03} \u{04} \u{14}";
        print "  Escaped: & < >";
        assertTrue(true);
    }

}

sebastianbergmann pushed a commit that referenced this issue Feb 16, 2018
JUnit XML (produced with --log-junit) is invalid (and won't be opened by
some programs) if it contains invalid XML characters in the <system-out>
node. Fix this by sanitizing text before adding it to the XML.
SimonHeimberg added a commit to SimonHeimberg/php-censor that referenced this issue Feb 27, 2018
Work around outup from phpunit versions before 6.5.7/7.0.2 (before sebastianbergmann/phpunit#2974 was fixed).
corpsee pushed a commit to php-censor/php-censor that referenced this issue May 1, 2018
Work around outup from phpunit versions before 6.5.7/7.0.2 (before sebastianbergmann/phpunit#2974 was fixed).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants