package org.jivesoftware.openfire.nio; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; /** * This unit tests verifies the correct detection of numeric character references that have invalid numeric values. * * From the XML 1.0 specificaton: <quote><b>Character Reference</b> <code><pre>CharRef ::= '&#' [0-9]+ ';' * | '&#x' [0-9a-fA-F]+ ';' [WFC: Legal Character]</pre></code> * <p/> * <b>Well-formedness constraint: Legal Character<b> Characters referred to using character references must match the * production for Char. * * (...) * <code><pre>Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]</pre></code> any * Unicode character, excluding the surrogate blocks, FFFE, and FFFF. <quote> * * This test is based on three large sets of values. * <ol> * <li>A set containing only illegal numeric character values only;</li> * <li>A set containing legal numeric character reference values only;</li> * <li>A set containing values that are no character references at all (but do resemble them)</li> * </ol> * * The first and second set consist of lines in which each line contains both the decimal as well as the hexidecimal * representation of the same numeric character reference. The remainder of the line is filled with zero-padded copies * of the same values. The various values (on each new line) are picked from the edges of each of the ranges that make * up the complete set of valid characters. * * @author Guus der Kinderen, guus.der.kinderen@gmail.com * * @see http://www.w3.org/TR/2008/REC-xml-20081126/#dt-charref */ public class XmlNumericCharacterReferenceTest { final String[] illegalNumericCharacterReferences = new String[] { "�" , "�", "�" , "�", "�" , "�", "�" , "�", "�" , "�", "�" , "�", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", "" , "", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "￾", "￾", "￾", "￾", "￾", "￾", "￾", "￾", "￾", "￾", "￿", "￿", "￿", "￿", "￿", "￿", "￿", "￿", "￿", "￿", "�", "�", "�", "�", "�", "�", "�", "�" }; final String[] legalNumericCharacterReferences = new String[] { " " , " ", " " , " ", " " , " ", " " , " ", " " , " ", " " , " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "!", "!", "!", "!", "!", "!", "!", "!", "!", "!", "!", "!", "퟾", "퟾", "퟾", "퟾", "퟾", "퟾", "퟾", "퟾", "퟾", "퟾", "퟿", "퟿", "퟿", "퟿", "퟿", "퟿", "퟿", "퟿", "퟿", "퟿", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "𐀀", "𐀀", "𐀀", "𐀀", "𐀀", "𐀀", "𐀀", "𐀀", "𐀁", "𐀁", "𐀁", "𐀁", "𐀁", "𐀁", "𐀁", "𐀁", "􏿾", "􏿾", "􏿾", "􏿾", "􏿾", "􏿾", "􏿾", "􏿾", "􏿿", "􏿿", "􏿿", "􏿿", "􏿿", "􏿿", "􏿿", "􏿿" }; final String[] notNumericCharacterRefrences = new String[] { "&", " ", " ", "#9;", "#x9;", "", "", "#1;", "#x1;", "&#9;", "&#x9;", "&#1;", "&#x1;" }; /** * Iterates of the collection of legal numeric character references and asserts that * {@link XMLLightweightParser#hasIllegalCharacterReferences(String)} does not find an illegal reference when each * of these values are passed as an argument to this method. */ @Test public void testLegalNumericCharacterReferences() throws Exception { for(final String reference : legalNumericCharacterReferences) { // do magic final boolean result = XMLLightweightParser.hasIllegalCharacterReferences(reference); // verify assertFalse("Value \""+reference+"\" is reported to contain an illegal numeric character reference, even though this hard-coded test value should not contain one.", result); } } /** * Iterates of the collection of illegal numeric character references and asserts that * {@link XMLLightweightParser#hasIllegalCharacterReferences(String)} finds an illegal reference for each of these * values passed as an argument to this method. */ @Test public void testIllegalNumericCharacterReferences() throws Exception { for(final String reference : illegalNumericCharacterReferences) { // do magic final boolean result = XMLLightweightParser.hasIllegalCharacterReferences(reference); // verify assertTrue("No illegal numeric character reference was found in value \""+reference+"\", even though this hard-coded test value should contain one.", result); } } /** * Iterates of the collection of values that are not numeric character references and asserts that * {@link XMLLightweightParser#hasIllegalCharacterReferences(String)} does not find an illegal reference when each * of these values are passed as an argument to this method. */ @Test public void testnotNumericCharacterReferences() throws Exception { for(final String reference : legalNumericCharacterReferences) { // do magic final boolean result = XMLLightweightParser.hasIllegalCharacterReferences(reference); // verify assertFalse("Value \""+reference+"\" is reported to contain an illegal numeric character reference, even though this hard-coded test value should not contain a numeric character reference at all.", result); } } /** * Checks if {@link XMLLightweightParser#hasIllegalCharacterReferences(String)} correctly skips over a legal numeric * reference if it is embedded in a snippet of text. */ @Test public void testTextWithLegalNumericCharacterReferences() throws Exception { // setup final String text = "The value is a legal numeric character reference."; // do magic final boolean result = XMLLightweightParser.hasIllegalCharacterReferences(text); // verify assertFalse(result); } /** * Checks if {@link XMLLightweightParser#hasIllegalCharacterReferences(String)} correctly identifies an illegal * numeric reference if it is embedded in a snippet of text. */ @Test public void testTextWithIllegalNumericCharacterReferences() throws Exception { // setup final String text = "The value  is an illegal numeric character reference."; // do magic final boolean result = XMLLightweightParser.hasIllegalCharacterReferences(text); // verify assertTrue(result); } /** * Checks if {@link XMLLightweightParser#hasIllegalCharacterReferences(String)} correctly identifies an illegal * numeric reference if it is embedded in a snippet of text that also contains a legal reference. */ @Test public void testTextWithBothLegalAndIllegalNumericCharacterReferences() throws Exception { // setup final String text = "The value is a legal numeric character reference, but the value  is not."; // do magic final boolean result = XMLLightweightParser.hasIllegalCharacterReferences(text); // verify assertTrue(result); } }