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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 (byte val : digest) {
  56. int hi = (val >> 4) & 0xF;
  57. int lo = val & 0xF;
  58. if (hi < 10) {
  59. buffer.append((char) (hi + 0x30));
  60. } else {
  61. buffer.append((char) (hi + 0x61 - 10));
  62. }
  63. if (lo < 10) {
  64. buffer.append((char) (lo + 0x30));
  65. } else {
  66. buffer.append((char) (lo + 0x61 - 10));
  67. }
  68. }
  69. return buffer.toString();
  70. }
  71. private byte[] runTest(String input)
  72. throws
  73. NoSuchAlgorithmException,
  74. ParserConfigurationException,
  75. SAXException,
  76. IOException {
  77. XMLReader parser = parserFactory.newSAXParser().getXMLReader();
  78. DigestFilter digestFilter = new DigestFilter("MD5");
  79. digestFilter.setParent(parser);
  80. digestFilter.setFeature("http://xml.org/sax/features/namespaces", true);
  81. parser.setContentHandler(digestFilter);
  82. InputSource inputSource = new InputSource(new StringReader(input));
  83. parser.parse(inputSource);
  84. return digestFilter.getDigestValue();
  85. }
  86. @Test
  87. public final void testLineFeed()
  88. throws
  89. NoSuchAlgorithmException,
  90. ParserConfigurationException,
  91. SAXException,
  92. IOException {
  93. byte[] lfDigest = runTest("<a>\n</a>");
  94. byte[] crlfDigest = runTest("<a>\r\n</a>");
  95. assertTrue(
  96. "LF: "
  97. + digestToString(lfDigest)
  98. + " CRLF: "
  99. + digestToString(crlfDigest),
  100. compareDigest(lfDigest, crlfDigest));
  101. }
  102. @Test
  103. public final void testAttributeOrder()
  104. throws
  105. NoSuchAlgorithmException,
  106. ParserConfigurationException,
  107. SAXException,
  108. IOException {
  109. byte[] sortDigest = runTest("<a a1='1' a2='2' a3='3'/>");
  110. byte[] permutationDigest = runTest("<a a2='2' a3='3' a1='1'/>");
  111. assertTrue(
  112. "Sort: "
  113. + digestToString(sortDigest)
  114. + " permuted: "
  115. + digestToString(permutationDigest),
  116. compareDigest(sortDigest, permutationDigest));
  117. byte[] reverseDigest = runTest("<a a3='3' a2='2' a1='1'/>");
  118. assertTrue(
  119. "Sort: "
  120. + digestToString(sortDigest)
  121. + " permuted: "
  122. + digestToString(reverseDigest),
  123. compareDigest(sortDigest, reverseDigest));
  124. }
  125. @Test
  126. public final void testNamespacePrefix()
  127. throws
  128. NoSuchAlgorithmException,
  129. ParserConfigurationException,
  130. SAXException,
  131. IOException {
  132. byte[] prefix1Digest = runTest("<a:a xmlns:a='foo'/>");
  133. byte[] prefix2Digest = runTest("<b:a xmlns:b='foo'/>");
  134. assertTrue(
  135. "prefix1: "
  136. + digestToString(prefix1Digest)
  137. + " prefix2: "
  138. + digestToString(prefix2Digest),
  139. compareDigest(prefix1Digest, prefix2Digest));
  140. }
  141. }