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.

TestEvilUnclosedBRFixingInputStream.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.util;
  16. import static org.junit.Assert.assertArrayEquals;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.nio.charset.Charset;
  21. import org.apache.poi.util.IOUtils;
  22. import org.apache.poi.util.ReplacingInputStream;
  23. import org.junit.Test;
  24. public final class TestEvilUnclosedBRFixingInputStream {
  25. static class EvilUnclosedBRFixingInputStream extends ReplacingInputStream {
  26. public EvilUnclosedBRFixingInputStream(byte[] source) {
  27. super(new ByteArrayInputStream(source), "<br>", "<br/>");
  28. }
  29. }
  30. @Test
  31. public void testOK() throws IOException {
  32. byte[] ok = getBytes("<p><div>Hello There!</div> <div>Tags!</div></p>");
  33. EvilUnclosedBRFixingInputStream inp = new EvilUnclosedBRFixingInputStream(ok);
  34. assertArrayEquals(ok, IOUtils.toByteArray(inp));
  35. inp.close();
  36. }
  37. @Test
  38. public void testProblem() throws IOException {
  39. byte[] orig = getBytes("<p><div>Hello<br>There!</div> <div>Tags!</div></p>");
  40. byte[] fixed = getBytes("<p><div>Hello<br/>There!</div> <div>Tags!</div></p>");
  41. EvilUnclosedBRFixingInputStream inp = new EvilUnclosedBRFixingInputStream(orig);
  42. assertArrayEquals(fixed, IOUtils.toByteArray(inp));
  43. inp.close();
  44. }
  45. /**
  46. * Checks that we can copy with br tags around the buffer boundaries
  47. */
  48. @Test
  49. public void testBufferSize() throws IOException {
  50. byte[] orig = getBytes("<p><div>Hello<br> <br>There!</div> <div>Tags!<br><br></div></p>");
  51. byte[] fixed = getBytes("<p><div>Hello<br/> <br/>There!</div> <div>Tags!<br/><br/></div></p>");
  52. // Vary the buffer size, so that we can end up with the br in the
  53. // overflow or only part in the buffer
  54. for(int i=5; i<orig.length; i++) {
  55. EvilUnclosedBRFixingInputStream inp = new EvilUnclosedBRFixingInputStream(orig);
  56. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  57. boolean going = true;
  58. while(going) {
  59. byte[] b = new byte[i];
  60. int r = inp.read(b);
  61. if(r > 0) {
  62. bout.write(b, 0, r);
  63. } else {
  64. going = false;
  65. }
  66. }
  67. byte[] result = bout.toByteArray();
  68. assertArrayEquals(fixed, result);
  69. inp.close();
  70. }
  71. }
  72. private static byte[] getBytes(String str) {
  73. return str.getBytes(Charset.forName("UTF-8"));
  74. }
  75. }