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.

SimpleMultiPartInputStreamTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.vaadin.tests.server;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import org.junit.Test;
  7. import com.vaadin.server.communication.FileUploadHandler.SimpleMultiPartInputStream;
  8. public class SimpleMultiPartInputStreamTest {
  9. /**
  10. * Check that the output for a given stream until boundary is as expected.
  11. *
  12. * @param input
  13. * @param boundary
  14. * @param expected
  15. * @throws Exception
  16. */
  17. protected void checkBoundaryDetection(byte[] input, String boundary,
  18. byte[] expected) throws Exception {
  19. ByteArrayInputStream bais = new ByteArrayInputStream(input);
  20. try (SimpleMultiPartInputStream smpis = new SimpleMultiPartInputStream(
  21. bais, boundary)) {
  22. ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
  23. int outbyte;
  24. try {
  25. while ((outbyte = smpis.read()) != -1) {
  26. resultStream.write(outbyte);
  27. }
  28. } catch (IOException e) {
  29. throw new IOException(
  30. e.getMessage() + "; expected " + new String(expected)
  31. + " but got " + resultStream.toString());
  32. }
  33. if (!Arrays.equals(expected, resultStream.toByteArray())) {
  34. throw new Exception("Mismatch: expected " + new String(expected)
  35. + " but got " + resultStream.toString());
  36. }
  37. }
  38. }
  39. protected void checkBoundaryDetection(String input, String boundary,
  40. String expected) throws Exception {
  41. checkBoundaryDetection(input.getBytes(), boundary, expected.getBytes());
  42. }
  43. @Test
  44. public void testSingleByteBoundaryAtEnd() throws Exception {
  45. checkBoundaryDetection("xyz123" + getFullBoundary("a"), "a", "xyz123");
  46. }
  47. @Test
  48. public void testSingleByteBoundaryInMiddle() throws Exception {
  49. checkBoundaryDetection("xyz" + getFullBoundary("a") + "123", "a",
  50. "xyz");
  51. }
  52. @Test
  53. public void testCorrectBoundaryAtEnd() throws Exception {
  54. checkBoundaryDetection("xyz123" + getFullBoundary("abc"), "abc",
  55. "xyz123");
  56. }
  57. @Test
  58. public void testCorrectBoundaryNearEnd() throws Exception {
  59. checkBoundaryDetection("xyz123" + getFullBoundary("abc") + "de", "abc",
  60. "xyz123");
  61. }
  62. @Test
  63. public void testCorrectBoundaryAtBeginning() throws Exception {
  64. checkBoundaryDetection(getFullBoundary("abc") + "xyz123", "abc", "");
  65. }
  66. @Test
  67. public void testRepeatingCharacterBoundary() throws Exception {
  68. checkBoundaryDetection(getFullBoundary("aa") + "xyz123", "aa", "");
  69. checkBoundaryDetection("axyz" + getFullBoundary("aa") + "123", "aa",
  70. "axyz");
  71. checkBoundaryDetection("xyz123" + getFullBoundary("aa"), "aa",
  72. "xyz123");
  73. }
  74. /**
  75. * Note, the boundary in this test is invalid. Boundary strings don't
  76. * contain CR/LF.
  77. *
  78. */
  79. // public void testRepeatingNewlineBoundary() throws Exception {
  80. // checkBoundaryDetection("1234567890" + getFullBoundary("\n\n")
  81. // + "1234567890", "\n\n", "");
  82. // }
  83. @Test
  84. public void testRepeatingStringBoundary() throws Exception {
  85. checkBoundaryDetection(getFullBoundary("abab") + "xyz123", "abab", "");
  86. checkBoundaryDetection("abaxyz" + getFullBoundary("abab") + "123",
  87. "abab", "abaxyz");
  88. checkBoundaryDetection("xyz123" + getFullBoundary("abab"), "abab",
  89. "xyz123");
  90. }
  91. @Test
  92. public void testOverlappingBoundary() throws Exception {
  93. checkBoundaryDetection("abc" + getFullBoundary("abcabd") + "xyz123",
  94. "abcabd", "abc");
  95. checkBoundaryDetection("xyzabc" + getFullBoundary("abcabd") + "123",
  96. "abcabd", "xyzabc");
  97. checkBoundaryDetection("xyz123abc" + getFullBoundary("abcabd"),
  98. "abcabd", "xyz123abc");
  99. }
  100. /*
  101. * TODO fix these tests, they don't do what their method name says.
  102. */
  103. // public void testNoBoundaryInInput() throws Exception {
  104. // try {
  105. // checkBoundaryDetection("xyz123", "abc", "xyz123");
  106. // fail();
  107. // } catch (IOException e) {
  108. // }
  109. // }
  110. //
  111. // public void testPartialBoundaryAtInputEnd() throws Exception {
  112. // try {
  113. // // This should lead to IOException (stream end), not AIOOBE
  114. // checkBoundaryDetection("xyz123ab", "abc", "xyz123ab");
  115. // fail();
  116. // } catch (IOException e) {
  117. // }
  118. // }
  119. //
  120. // public void testPartialBoundaryAtInputBeginning() throws Exception {
  121. // try {
  122. // checkBoundaryDetection("abxyz123", "abc", "abxyz123");
  123. // fail();
  124. // } catch (IOException e) {
  125. // }
  126. // }
  127. public static String getFullBoundary(String str) {
  128. return "\r\n--" + str + "--";
  129. }
  130. }