1 17 18 19 20 package org.apache.fop; 21 22 import java.io.IOException ; 23 import java.io.StringReader ; 24 import java.security.NoSuchAlgorithmException ; 25 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParserFactory ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.fop.util.DigestFilter; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.XMLReader ; 35 36 40 public class DigestFilterTestCase extends TestCase { 41 42 private SAXParserFactory parserFactory; 43 44 45 protected void setUp() { 46 parserFactory = SAXParserFactory.newInstance(); 47 parserFactory.setNamespaceAware(true); 48 } 49 50 private boolean compareDigest(byte[] a, byte[] b) { 51 if (a.length != b.length) { 52 return false; 53 } 54 for (int i = 0; i < a.length; i++) { 55 if (a[i] != b[i]) { 56 return false; 57 } 58 } 59 return true; 60 } 61 62 private String digestToString(byte[] digest) { 63 StringBuffer buffer = new StringBuffer (2 * digest.length); 64 for (int i = 0; i < digest.length; i++) { 65 int val = digest[i]; 66 int hi = (val >> 4) & 0xF; 67 int lo = val & 0xF; 68 if (hi < 10) { 69 buffer.append((char) (hi + 0x30)); 70 } else { 71 buffer.append((char) (hi + 0x61 - 10)); 72 } 73 if (lo < 10) { 74 buffer.append((char) (lo + 0x30)); 75 } else { 76 buffer.append((char) (lo + 0x61 - 10)); 77 } 78 } 79 return buffer.toString(); 80 } 81 82 private byte[] runTest(String input) 83 throws 84 NoSuchAlgorithmException , 85 ParserConfigurationException , 86 SAXException , 87 IOException { 88 XMLReader parser = parserFactory.newSAXParser().getXMLReader(); 89 DigestFilter digestFilter = new DigestFilter("MD5"); 90 digestFilter.setParent(parser); 91 digestFilter.setFeature("http://xml.org/sax/features/namespaces", true); 92 parser.setContentHandler(digestFilter); 93 InputSource inputSource = new InputSource (new StringReader (input)); 94 parser.parse(inputSource); 95 return digestFilter.getDigestValue(); 96 } 97 98 public final void testLineFeed() 99 throws 100 NoSuchAlgorithmException , 101 ParserConfigurationException , 102 SAXException , 103 IOException { 104 byte[] lfDigest = runTest("<a>\n</a>"); 105 byte[] crlfDigest = runTest("<a>\r\n</a>"); 106 assertTrue( 107 "LF: " 108 + digestToString(lfDigest) 109 + " CRLF: " 110 + digestToString(crlfDigest), 111 compareDigest(lfDigest, crlfDigest)); 112 } 113 114 public final void testAttributeOrder() 115 throws 116 NoSuchAlgorithmException , 117 ParserConfigurationException , 118 SAXException , 119 IOException { 120 byte[] sortDigest = runTest("<a a1='1' a2='2' a3='3'/>"); 121 byte[] permutationDigest = runTest("<a a2='2' a3='3' a1='1'/>"); 122 assertTrue( 123 "Sort: " 124 + digestToString(sortDigest) 125 + " permuted: " 126 + digestToString(permutationDigest), 127 compareDigest(sortDigest, permutationDigest)); 128 byte[] reverseDigest = runTest("<a a3='3' a2='2' a1='1'/>"); 129 assertTrue( 130 "Sort: " 131 + digestToString(sortDigest) 132 + " permuted: " 133 + digestToString(reverseDigest), 134 compareDigest(sortDigest, reverseDigest)); 135 } 136 137 public final void testNamespacePrefix() 138 throws 139 NoSuchAlgorithmException , 140 ParserConfigurationException , 141 SAXException , 142 IOException { 143 byte[] prefix1Digest = runTest("<a:a xmlns:a='foo'/>"); 144 byte[] prefix2Digest = runTest("<b:a xmlns:b='foo'/>"); 145 assertTrue( 146 "prefix1: " 147 + digestToString(prefix1Digest) 148 + " prefix2: " 149 + digestToString(prefix2Digest), 150 compareDigest(prefix1Digest, prefix2Digest)); 151 } 152 153 } 154 | Popular Tags |