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.

BinaryHunkStreamTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util.io;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.util.Arrays;
  19. import org.junit.Test;
  20. /**
  21. * Tests for {@link BinaryHunkInputStream} and {@link BinaryHunkOutputStream}.
  22. */
  23. public class BinaryHunkStreamTest {
  24. @Test
  25. public void testRoundtripWholeBuffer() throws IOException {
  26. for (int length = 1; length < 520 + 52; length++) {
  27. byte[] data = new byte[length];
  28. for (int i = 0; i < data.length; i++) {
  29. data[i] = (byte) (255 - (i % 256));
  30. }
  31. try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
  32. BinaryHunkOutputStream out = new BinaryHunkOutputStream(
  33. bos)) {
  34. out.write(data);
  35. out.flush();
  36. byte[] encoded = bos.toByteArray();
  37. assertFalse(Arrays.equals(data, encoded));
  38. try (BinaryHunkInputStream in = new BinaryHunkInputStream(
  39. new ByteArrayInputStream(encoded))) {
  40. byte[] decoded = new byte[data.length];
  41. int newLength = in.read(decoded);
  42. assertEquals(newLength, decoded.length);
  43. assertEquals(-1, in.read());
  44. assertArrayEquals(data, decoded);
  45. }
  46. }
  47. }
  48. }
  49. @Test
  50. public void testRoundtripChunks() throws IOException {
  51. for (int length = 1; length < 520 + 52; length++) {
  52. byte[] data = new byte[length];
  53. for (int i = 0; i < data.length; i++) {
  54. data[i] = (byte) (255 - (i % 256));
  55. }
  56. try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
  57. BinaryHunkOutputStream out = new BinaryHunkOutputStream(
  58. bos)) {
  59. out.write(data, 0, data.length / 2);
  60. out.write(data, data.length / 2, data.length - data.length / 2);
  61. out.flush();
  62. byte[] encoded = bos.toByteArray();
  63. assertFalse(Arrays.equals(data, encoded));
  64. try (BinaryHunkInputStream in = new BinaryHunkInputStream(
  65. new ByteArrayInputStream(encoded))) {
  66. byte[] decoded = new byte[data.length];
  67. int p = 0;
  68. int n;
  69. while ((n = in.read(decoded, p,
  70. Math.min(decoded.length - p, 57))) >= 0) {
  71. p += n;
  72. if (p == decoded.length) {
  73. break;
  74. }
  75. }
  76. assertEquals(p, decoded.length);
  77. assertEquals(-1, in.read());
  78. assertArrayEquals(data, decoded);
  79. }
  80. }
  81. }
  82. }
  83. @Test
  84. public void testRoundtripBytes() throws IOException {
  85. for (int length = 1; length < 520 + 52; length++) {
  86. byte[] data = new byte[length];
  87. for (int i = 0; i < data.length; i++) {
  88. data[i] = (byte) (255 - (i % 256));
  89. }
  90. try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
  91. BinaryHunkOutputStream out = new BinaryHunkOutputStream(
  92. bos)) {
  93. for (int i = 0; i < data.length; i++) {
  94. out.write(data[i]);
  95. }
  96. out.flush();
  97. byte[] encoded = bos.toByteArray();
  98. assertFalse(Arrays.equals(data, encoded));
  99. try (BinaryHunkInputStream in = new BinaryHunkInputStream(
  100. new ByteArrayInputStream(encoded))) {
  101. byte[] decoded = new byte[data.length];
  102. for (int i = 0; i < decoded.length; i++) {
  103. int val = in.read();
  104. assertTrue(0 <= val && val <= 255);
  105. decoded[i] = (byte) val;
  106. }
  107. assertEquals(-1, in.read());
  108. assertArrayEquals(data, decoded);
  109. }
  110. }
  111. }
  112. }
  113. @Test
  114. public void testRoundtripWithClose() throws IOException {
  115. for (int length = 1; length < 520 + 52; length++) {
  116. byte[] data = new byte[length];
  117. for (int i = 0; i < data.length; i++) {
  118. data[i] = (byte) (255 - (i % 256));
  119. }
  120. try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
  121. try (BinaryHunkOutputStream out = new BinaryHunkOutputStream(
  122. bos)) {
  123. out.write(data);
  124. }
  125. byte[] encoded = bos.toByteArray();
  126. assertFalse(Arrays.equals(data, encoded));
  127. try (BinaryHunkInputStream in = new BinaryHunkInputStream(
  128. new ByteArrayInputStream(encoded))) {
  129. byte[] decoded = new byte[data.length];
  130. int newLength = in.read(decoded);
  131. assertEquals(newLength, decoded.length);
  132. assertEquals(-1, in.read());
  133. assertArrayEquals(data, decoded);
  134. }
  135. }
  136. }
  137. }
  138. }