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.

SideBandOutputStreamTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc. 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.transport;
  11. import static java.lang.Integer.valueOf;
  12. import static java.nio.charset.StandardCharsets.UTF_8;
  13. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
  14. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR;
  15. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
  16. import static org.eclipse.jgit.transport.SideBandOutputStream.HDR_SIZE;
  17. import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF;
  18. import static org.eclipse.jgit.transport.SideBandOutputStream.SMALL_BUF;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.fail;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.text.MessageFormat;
  25. import org.eclipse.jgit.internal.JGitText;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. // Note, test vectors created with:
  29. //
  30. // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
  31. public class SideBandOutputStreamTest {
  32. private ByteArrayOutputStream rawOut;
  33. @Before
  34. public void setUp() throws Exception {
  35. rawOut = new ByteArrayOutputStream();
  36. }
  37. @Test
  38. public void testWrite_CH_DATA() throws IOException {
  39. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
  40. SMALL_BUF, rawOut)) {
  41. out.write(new byte[] { 'a', 'b', 'c' });
  42. out.flush();
  43. }
  44. assertBuffer("0008\001abc");
  45. }
  46. @Test
  47. public void testWrite_CH_PROGRESS() throws IOException {
  48. try (SideBandOutputStream out = new SideBandOutputStream(CH_PROGRESS,
  49. SMALL_BUF, rawOut)) {
  50. out.write(new byte[] { 'a', 'b', 'c' });
  51. out.flush();
  52. }
  53. assertBuffer("0008\002abc");
  54. }
  55. @Test
  56. public void testWrite_CH_ERROR() throws IOException {
  57. try (SideBandOutputStream out = new SideBandOutputStream(CH_ERROR,
  58. SMALL_BUF, rawOut)) {
  59. out.write(new byte[] { 'a', 'b', 'c' });
  60. out.flush();
  61. }
  62. assertBuffer("0008\003abc");
  63. }
  64. @Test
  65. public void testWrite_Small() throws IOException {
  66. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
  67. SMALL_BUF, rawOut)) {
  68. out.write('a');
  69. out.write('b');
  70. out.write('c');
  71. out.flush();
  72. }
  73. assertBuffer("0008\001abc");
  74. }
  75. @Test
  76. public void testWrite_SmallBlocks1() throws IOException {
  77. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
  78. rawOut)) {
  79. out.write('a');
  80. out.write('b');
  81. out.write('c');
  82. out.flush();
  83. }
  84. assertBuffer("0006\001a0006\001b0006\001c");
  85. }
  86. @Test
  87. public void testWrite_SmallBlocks2() throws IOException {
  88. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
  89. rawOut)) {
  90. out.write(new byte[] { 'a', 'b', 'c' });
  91. out.flush();
  92. }
  93. assertBuffer("0006\001a0006\001b0006\001c");
  94. }
  95. @Test
  96. public void testWrite_SmallBlocks3() throws IOException {
  97. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 7,
  98. rawOut)) {
  99. out.write('a');
  100. out.write(new byte[] { 'b', 'c' });
  101. out.flush();
  102. }
  103. assertBuffer("0007\001ab0006\001c");
  104. }
  105. @Test
  106. public void testWrite_Large() throws IOException {
  107. final int buflen = MAX_BUF - HDR_SIZE;
  108. final byte[] buf = new byte[buflen];
  109. for (int i = 0; i < buf.length; i++) {
  110. buf[i] = (byte) i;
  111. }
  112. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
  113. MAX_BUF, rawOut)) {
  114. out.write(buf);
  115. out.flush();
  116. }
  117. final byte[] act = rawOut.toByteArray();
  118. final String explen = Integer.toString(buf.length + HDR_SIZE, 16);
  119. assertEquals(HDR_SIZE + buf.length, act.length);
  120. assertEquals(new String(act, 0, 4, "UTF-8"), explen);
  121. assertEquals(1, act[4]);
  122. for (int i = 0, j = HDR_SIZE; i < buf.length; i++, j++) {
  123. assertEquals(buf[i], act[j]);
  124. }
  125. }
  126. @Test
  127. public void testFlush() throws IOException {
  128. final int[] flushCnt = new int[1];
  129. final OutputStream mockout = new OutputStream() {
  130. @Override
  131. public void write(int arg0) throws IOException {
  132. fail("should not write");
  133. }
  134. @Override
  135. public void flush() throws IOException {
  136. flushCnt[0]++;
  137. }
  138. };
  139. try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
  140. SMALL_BUF, mockout)) {
  141. out.flush();
  142. }
  143. assertEquals(1, flushCnt[0]);
  144. }
  145. private void createSideBandOutputStream(int chan, int sz, OutputStream os)
  146. throws Exception {
  147. try (SideBandOutputStream s = new SideBandOutputStream(chan, sz, os)) {
  148. // Unused
  149. }
  150. }
  151. @Test
  152. public void testConstructor_RejectsBadChannel() throws Exception {
  153. try {
  154. createSideBandOutputStream(-1, MAX_BUF, rawOut);
  155. fail("Accepted -1 channel number");
  156. } catch (IllegalArgumentException e) {
  157. assertEquals("channel -1 must be in range [1, 255]", e.getMessage());
  158. }
  159. try {
  160. createSideBandOutputStream(0, MAX_BUF, rawOut);
  161. fail("Accepted 0 channel number");
  162. } catch (IllegalArgumentException e) {
  163. assertEquals("channel 0 must be in range [1, 255]", e.getMessage());
  164. }
  165. try {
  166. createSideBandOutputStream(256, MAX_BUF, rawOut);
  167. fail("Accepted 256 channel number");
  168. } catch (IllegalArgumentException e) {
  169. assertEquals("channel 256 must be in range [1, 255]", e
  170. .getMessage());
  171. }
  172. }
  173. @Test
  174. public void testConstructor_RejectsBadBufferSize() throws Exception {
  175. try {
  176. createSideBandOutputStream(CH_DATA, -1, rawOut);
  177. fail("Accepted -1 for buffer size");
  178. } catch (IllegalArgumentException e) {
  179. assertEquals("packet size -1 must be >= 5", e.getMessage());
  180. }
  181. try {
  182. createSideBandOutputStream(CH_DATA, 0, rawOut);
  183. fail("Accepted 0 for buffer size");
  184. } catch (IllegalArgumentException e) {
  185. assertEquals("packet size 0 must be >= 5", e.getMessage());
  186. }
  187. try {
  188. createSideBandOutputStream(CH_DATA, 1, rawOut);
  189. fail("Accepted 1 for buffer size");
  190. } catch (IllegalArgumentException e) {
  191. assertEquals("packet size 1 must be >= 5", e.getMessage());
  192. }
  193. try {
  194. createSideBandOutputStream(CH_DATA, Integer.MAX_VALUE, rawOut);
  195. fail("Accepted " + Integer.MAX_VALUE + " for buffer size");
  196. } catch (IllegalArgumentException e) {
  197. assertEquals(MessageFormat.format(
  198. JGitText.get().packetSizeMustBeAtMost,
  199. valueOf(Integer.MAX_VALUE), valueOf(65520)), e.getMessage());
  200. }
  201. }
  202. private void assertBuffer(String exp) {
  203. assertEquals(exp, new String(rawOut.toByteArray(), UTF_8));
  204. }
  205. }