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
junitlauncher - Fix an issue in LegacyXmlResultFormatter with ]]> in stacktraces #175
junitlauncher - Fix an issue in LegacyXmlResultFormatter with ]]> in stacktraces #175
Conversation
writer.writeCData(StringUtils.getStackTrace(t)); | ||
// write out the stacktrace, replacing any CDATA endings by splitting them into multiple CDATA segments | ||
// See https://bz.apache.org/bugzilla/show_bug.cgi?id=65833 | ||
writer.writeCData(StringUtils.getStackTrace(t).replaceAll("]]>", "]]]]><![CDATA[>")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently there is encode
method which seems to be intended for handling special character. So this logic probably would fit there better.
PS:
I'm not a project maintainer and ant
expert, just sharing my ideas since I've been recently exploring this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The encode
method currently only handles special characters (generic XML). This handles a special sequence for a specific data section. ]]>
.
From [1], """Within a CDATA section, only the CDEnd string is recognized as markup, so that left angle brackets and ampersands may occur in their literal form; they need not (and cannot) be escaped using " <
" and " &
". CDATA sections cannot nest.""" (CDEnd === ]]>
)
This means that replacing >
with >
would not work, as CDATA parsers shouldn't treat > === >
. They should treat it as a literal >
(since the & is literal), which can break many usages of the stacktrace. Sure, people could have regexes to fix the stack trace, but that is duplicate work for many people.
With all that said, writeCData
should properly handle text with ]]>
in it, but since people have had to work around it, it may no longer be possible to fix the issue in the actual implementing libraries without a lot of work. How would it know if you were working around this issue, and not escape the cdata sequence again? (]]>
-> ]]]]><!CDATA[>
-> ]]]]]]><!CDATA[><!CDATA[>
).
Example pitfall: A test failure outputs XML with a CDATA section, which is then written to CDATA in LegacyXmlResultFormatter. We want to keep the XML output as is, which means escaping the interior CDATA endings. If this then happens again in the writeCData
method, then the xml output shown to the user will most likely be wrong. From the above example, instead of ]]>
, the user would see ]]]]><!CDATA[>
unless the CDATA parser recursively parses the data (unlikely IMO).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for the details! Originally I was not clear why splitting CDATA section is better than escaping. But looks like it is a common practice for nesting CDATA sections [1]. Moreover, junit team fixed such an issue in the same way [2]. The fix looks good to me!
[1] https://en.wikipedia.org/wiki/CDATA#Nesting
[2] junit-team/junit5#1225
787ece4
to
b1b5dbf
Compare
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java
Show resolved
Hide resolved
This PR looks fine to me. Thank you for fixing this. Given that we have had odd issues in this area with XML content, do you think it's possible to add a test case to reproduce this issue and verify the fix? If so, would you be willing to add one in this PR? |
I was looking into this yesterday. I'll say this much, figuring out how the tests were run and verified took me a bit of time, and the output from EDIT: The |
Bugzilla Report 65833 This occurs when the stacktrace message contains ]]>, which is the CDATA end code. There is no escape, so it must be replaced with `]]` + `]]>` + `<![CDATA[` + `>`, which means that the CDATA section is split. Signed-off-by: Taylor Smock <tsmock@fb.com>
b1b5dbf
to
3af5a01
Compare
Non-regression test for Bugzilla Report 65833 This also updates JUnit 4 to 4.13.2, JUnit 5 to 5.8.2, and JUnit Platform to 1.8.2. This also adds junit-platform-testkit as a test dependency. Note: At time of this commit, tests for junitlauncher must be run from the pom.xml for junitlauncher. Attempting to modify build.xml to run the tests will result in a failure due to the presence of JUnit 3.8.2 on the classpath. Signed-off-by: Taylor Smock <tsmock@fb.com>
@tsmock [1] https://lists.apache.org/thread/2v8l95fwh0sgz0vlgqcf1rpd88swf3pv |
It did not help me. See https://github.com/apache/ant/blob/master/build.xml#L1989 . Specifically, when I commented that line out, the tests failed due to having JUnit 3.8.2 on the class path (see https://github.com/apache/ant/tree/master/lib/optional ). I'm actually surprised that JUnit 3 is still supported in ant -- it looks like the last release for it was in 2006 or 2007. Junit 4.0 was released around that time.) I was, however, able to run the tests with maven (I checked to make certain that all the tests will fail if the expected output was not there, as I changed a significant part of the logic in the test). |
Do I need to do anything else for this PR? |
Hello Taylor, I just merged the initial commit that is part of this PR. I left out the tests for now since it even had changed some junit versions. I'll look into the testing part separately once I get some free time. As for testing this locally, what Aleksei pointed to should have worked (that's how we test locally), but I haven't been able to look more into your failures. Thank you for the patience on this one. As part of the merge, I've added your name to the list of contributors to this project. |
I ended up updating the JUnit5 versions to get some of their newer testing methods for testing JUnit extensions. In any case, from what I know, there shouldn't be any effect on JUnit4 4.13.1 -> 4.13.2 should have no effect, minus the bug fixes
Also, thank you for letting me know that you didn't take everything from the branch -- I usually delete my branches once they get merged, but I won't delete this one for awhile. I will go ahead and rebase onto HEAD though. |
Bugzilla Report 65833
This occurs when the stacktrace message contains ]]>, which is the CDATA
end code. There is no escape, so it must be replaced with
]]
+]]>
+<![CDATA[
+>
, which means that the CDATA section is split.