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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
  45. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR;
  46. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
  47. import static org.eclipse.jgit.transport.SideBandOutputStream.HDR_SIZE;
  48. import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF;
  49. import static org.eclipse.jgit.transport.SideBandOutputStream.SMALL_BUF;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.IOException;
  52. import java.io.OutputStream;
  53. import junit.framework.TestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. // Note, test vectors created with:
  56. //
  57. // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
  58. public class SideBandOutputStreamTest extends TestCase {
  59. private ByteArrayOutputStream rawOut;
  60. protected void setUp() throws Exception {
  61. super.setUp();
  62. rawOut = new ByteArrayOutputStream();
  63. }
  64. public void testWrite_CH_DATA() throws IOException {
  65. final SideBandOutputStream out;
  66. out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut);
  67. out.write(new byte[] { 'a', 'b', 'c' });
  68. out.flush();
  69. assertBuffer("0008\001abc");
  70. }
  71. public void testWrite_CH_PROGRESS() throws IOException {
  72. final SideBandOutputStream out;
  73. out = new SideBandOutputStream(CH_PROGRESS, SMALL_BUF, rawOut);
  74. out.write(new byte[] { 'a', 'b', 'c' });
  75. out.flush();
  76. assertBuffer("0008\002abc");
  77. }
  78. public void testWrite_CH_ERROR() throws IOException {
  79. final SideBandOutputStream out;
  80. out = new SideBandOutputStream(CH_ERROR, SMALL_BUF, rawOut);
  81. out.write(new byte[] { 'a', 'b', 'c' });
  82. out.flush();
  83. assertBuffer("0008\003abc");
  84. }
  85. public void testWrite_Small() throws IOException {
  86. final SideBandOutputStream out;
  87. out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut);
  88. out.write('a');
  89. out.write('b');
  90. out.write('c');
  91. out.flush();
  92. assertBuffer("0008\001abc");
  93. }
  94. public void testWrite_SmallBlocks1() throws IOException {
  95. final SideBandOutputStream out;
  96. out = new SideBandOutputStream(CH_DATA, 6, rawOut);
  97. out.write('a');
  98. out.write('b');
  99. out.write('c');
  100. out.flush();
  101. assertBuffer("0006\001a0006\001b0006\001c");
  102. }
  103. public void testWrite_SmallBlocks2() throws IOException {
  104. final SideBandOutputStream out;
  105. out = new SideBandOutputStream(CH_DATA, 6, rawOut);
  106. out.write(new byte[] { 'a', 'b', 'c' });
  107. out.flush();
  108. assertBuffer("0006\001a0006\001b0006\001c");
  109. }
  110. public void testWrite_SmallBlocks3() throws IOException {
  111. final SideBandOutputStream out;
  112. out = new SideBandOutputStream(CH_DATA, 7, rawOut);
  113. out.write('a');
  114. out.write(new byte[] { 'b', 'c' });
  115. out.flush();
  116. assertBuffer("0007\001ab0006\001c");
  117. }
  118. public void testWrite_Large() throws IOException {
  119. final int buflen = MAX_BUF - HDR_SIZE;
  120. final byte[] buf = new byte[buflen];
  121. for (int i = 0; i < buf.length; i++) {
  122. buf[i] = (byte) i;
  123. }
  124. final SideBandOutputStream out;
  125. out = new SideBandOutputStream(CH_DATA, MAX_BUF, rawOut);
  126. out.write(buf);
  127. out.flush();
  128. final byte[] act = rawOut.toByteArray();
  129. final String explen = Integer.toString(buf.length + HDR_SIZE, 16);
  130. assertEquals(HDR_SIZE + buf.length, act.length);
  131. assertEquals(new String(act, 0, 4, "UTF-8"), explen);
  132. assertEquals(1, act[4]);
  133. for (int i = 0, j = HDR_SIZE; i < buf.length; i++, j++) {
  134. assertEquals(buf[i], act[j]);
  135. }
  136. }
  137. public void testFlush() throws IOException {
  138. final int[] flushCnt = new int[1];
  139. final OutputStream mockout = new OutputStream() {
  140. @Override
  141. public void write(int arg0) throws IOException {
  142. fail("should not write");
  143. }
  144. @Override
  145. public void flush() throws IOException {
  146. flushCnt[0]++;
  147. }
  148. };
  149. new SideBandOutputStream(CH_DATA, SMALL_BUF, mockout).flush();
  150. assertEquals(1, flushCnt[0]);
  151. }
  152. public void testConstructor_RejectsBadChannel() {
  153. try {
  154. new SideBandOutputStream(-1, MAX_BUF, rawOut);
  155. fail("Accepted -1 channel number");
  156. } catch (IllegalArgumentException e) {
  157. assertEquals("channel -1 must be in range [0, 255]", e.getMessage());
  158. }
  159. try {
  160. new SideBandOutputStream(0, MAX_BUF, rawOut);
  161. fail("Accepted 0 channel number");
  162. } catch (IllegalArgumentException e) {
  163. assertEquals("channel 0 must be in range [0, 255]", e.getMessage());
  164. }
  165. try {
  166. new SideBandOutputStream(256, MAX_BUF, rawOut);
  167. fail("Accepted 256 channel number");
  168. } catch (IllegalArgumentException e) {
  169. assertEquals("channel 256 must be in range [0, 255]", e
  170. .getMessage());
  171. }
  172. }
  173. public void testConstructor_RejectsBadBufferSize() {
  174. try {
  175. new SideBandOutputStream(CH_DATA, -1, rawOut);
  176. fail("Accepted -1 for buffer size");
  177. } catch (IllegalArgumentException e) {
  178. assertEquals("packet size -1 must be >= 5", e.getMessage());
  179. }
  180. try {
  181. new SideBandOutputStream(CH_DATA, 0, rawOut);
  182. fail("Accepted 0 for buffer size");
  183. } catch (IllegalArgumentException e) {
  184. assertEquals("packet size 0 must be >= 5", e.getMessage());
  185. }
  186. try {
  187. new SideBandOutputStream(CH_DATA, 1, rawOut);
  188. fail("Accepted 1 for buffer size");
  189. } catch (IllegalArgumentException e) {
  190. assertEquals("packet size 1 must be >= 5", e.getMessage());
  191. }
  192. try {
  193. new SideBandOutputStream(CH_DATA, Integer.MAX_VALUE, rawOut);
  194. fail("Accepted " + Integer.MAX_VALUE + " for buffer size");
  195. } catch (IllegalArgumentException e) {
  196. assertEquals("packet size " + Integer.MAX_VALUE
  197. + " must be <= 65520", e.getMessage());
  198. }
  199. }
  200. private void assertBuffer(final String exp) throws IOException {
  201. assertEquals(exp, new String(rawOut.toByteArray(),
  202. Constants.CHARACTER_ENCODING));
  203. }
  204. }