You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DigestFilterTestCase.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop;
  19. import java.io.IOException;
  20. import java.io.StringReader;
  21. import java.security.NoSuchAlgorithmException;
  22. import javax.xml.parsers.ParserConfigurationException;
  23. import javax.xml.parsers.SAXParserFactory;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.xml.sax.InputSource;
  27. import org.xml.sax.SAXException;
  28. import org.xml.sax.XMLReader;
  29. import static org.junit.Assert.assertTrue;
  30. import org.apache.fop.util.DigestFilter;
  31. /**
  32. * Test case for digesting SAX filter.
  33. *
  34. */
  35. public class DigestFilterTestCase {
  36. private SAXParserFactory parserFactory;
  37. @Before
  38. public void setUp() {
  39. parserFactory = SAXParserFactory.newInstance();
  40. parserFactory.setNamespaceAware(true);
  41. }
  42. private boolean compareDigest(byte[] a, byte[] b) {
  43. if (a.length != b.length) {
  44. return false;
  45. }
  46. for (int i = 0; i < a.length; i++) {
  47. if (a[i] != b[i]) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. private String digestToString(byte[] digest) {
  54. StringBuffer buffer = new StringBuffer(2 * digest.length);
  55. for (int i = 0; i < digest.length; i++) {
  56. int val = digest[i];
  57. int hi = (val >> 4) & 0xF;
  58. int lo = val & 0xF;
  59. if (hi < 10) {
  60. buffer.append((char) (hi + 0x30));
  61. } else {
  62. buffer.append((char) (hi + 0x61 - 10));
  63. }
  64. if (lo < 10) {
  65. buffer.append((char) (lo + 0x30));
  66. } else {
  67. buffer.append((char) (lo + 0x61 - 10));
  68. }
  69. }
  70. return buffer.toString();
  71. }
  72. private byte[] runTest(String input)
  73. throws
  74. NoSuchAlgorithmException,
  75. ParserConfigurationException,
  76. SAXException,
  77. IOException {
  78. XMLReader parser = parserFactory.newSAXParser().getXMLReader();
  79. DigestFilter digestFilter = new DigestFilter("MD5");
  80. digestFilter.setParent(parser);
  81. digestFilter.setFeature("http://xml.org/sax/features/namespaces", true);
  82. parser.setContentHandler(digestFilter);
  83. InputSource inputSource = new InputSource(new StringReader(input));
  84. parser.parse(inputSource);
  85. return digestFilter.getDigestValue();
  86. }
  87. @Test
  88. public final void testLineFeed()
  89. throws
  90. NoSuchAlgorithmException,
  91. ParserConfigurationException,
  92. SAXException,
  93. IOException {
  94. byte[] lfDigest = runTest("<a>\n</a>");
  95. byte[] crlfDigest = runTest("<a>\r\n</a>");
  96. assertTrue(
  97. "LF: "
  98. + digestToString(lfDigest)
  99. + " CRLF: "
  100. + digestToString(crlfDigest),
  101. compareDigest(lfDigest, crlfDigest));
  102. }
  103. @Test
  104. public final void testAttributeOrder()
  105. throws
  106. NoSuchAlgorithmException,
  107. ParserConfigurationException,
  108. SAXException,
  109. IOException {
  110. byte[] sortDigest = runTest("<a a1='1' a2='2' a3='3'/>");
  111. byte[] permutationDigest = runTest("<a a2='2' a3='3' a1='1'/>");
  112. assertTrue(
  113. "Sort: "
  114. + digestToString(sortDigest)
  115. + " permuted: "
  116. + digestToString(permutationDigest),
  117. compareDigest(sortDigest, permutationDigest));
  118. byte[] reverseDigest = runTest("<a a3='3' a2='2' a1='1'/>");
  119. assertTrue(
  120. "Sort: "
  121. + digestToString(sortDigest)
  122. + " permuted: "
  123. + digestToString(reverseDigest),
  124. compareDigest(sortDigest, reverseDigest));
  125. }
  126. @Test
  127. public final void testNamespacePrefix()
  128. throws
  129. NoSuchAlgorithmException,
  130. ParserConfigurationException,
  131. SAXException,
  132. IOException {
  133. byte[] prefix1Digest = runTest("<a:a xmlns:a='foo'/>");
  134. byte[] prefix2Digest = runTest("<b:a xmlns:b='foo'/>");
  135. assertTrue(
  136. "prefix1: "
  137. + digestToString(prefix1Digest)
  138. + " prefix2: "
  139. + digestToString(prefix2Digest),
  140. compareDigest(prefix1Digest, prefix2Digest));
  141. }
  142. }