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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 java.text.MessageFormat;
  54. import junit.framework.TestCase;
  55. import org.eclipse.jgit.JGitText;
  56. import org.eclipse.jgit.lib.Constants;
  57. // Note, test vectors created with:
  58. //
  59. // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
  60. public class SideBandOutputStreamTest extends TestCase {
  61. private ByteArrayOutputStream rawOut;
  62. protected void setUp() throws Exception {
  63. super.setUp();
  64. rawOut = new ByteArrayOutputStream();
  65. }
  66. public void testWrite_CH_DATA() throws IOException {
  67. final SideBandOutputStream out;
  68. out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut);
  69. out.write(new byte[] { 'a', 'b', 'c' });
  70. out.flush();
  71. assertBuffer("0008\001abc");
  72. }
  73. public void testWrite_CH_PROGRESS() throws IOException {
  74. final SideBandOutputStream out;
  75. out = new SideBandOutputStream(CH_PROGRESS, SMALL_BUF, rawOut);
  76. out.write(new byte[] { 'a', 'b', 'c' });
  77. out.flush();
  78. assertBuffer("0008\002abc");
  79. }
  80. public void testWrite_CH_ERROR() throws IOException {
  81. final SideBandOutputStream out;
  82. out = new SideBandOutputStream(CH_ERROR, SMALL_BUF, rawOut);
  83. out.write(new byte[] { 'a', 'b', 'c' });
  84. out.flush();
  85. assertBuffer("0008\003abc");
  86. }
  87. public void testWrite_Small() throws IOException {
  88. final SideBandOutputStream out;
  89. out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut);
  90. out.write('a');
  91. out.write('b');
  92. out.write('c');
  93. out.flush();
  94. assertBuffer("0008\001abc");
  95. }
  96. public void testWrite_SmallBlocks1() throws IOException {
  97. final SideBandOutputStream out;
  98. out = new SideBandOutputStream(CH_DATA, 6, rawOut);
  99. out.write('a');
  100. out.write('b');
  101. out.write('c');
  102. out.flush();
  103. assertBuffer("0006\001a0006\001b0006\001c");
  104. }
  105. public void testWrite_SmallBlocks2() throws IOException {
  106. final SideBandOutputStream out;
  107. out = new SideBandOutputStream(CH_DATA, 6, rawOut);
  108. out.write(new byte[] { 'a', 'b', 'c' });
  109. out.flush();
  110. assertBuffer("0006\001a0006\001b0006\001c");
  111. }
  112. public void testWrite_SmallBlocks3() throws IOException {
  113. final SideBandOutputStream out;
  114. out = new SideBandOutputStream(CH_DATA, 7, rawOut);
  115. out.write('a');
  116. out.write(new byte[] { 'b', 'c' });
  117. out.flush();
  118. assertBuffer("0007\001ab0006\001c");
  119. }
  120. public void testWrite_Large() throws IOException {
  121. final int buflen = MAX_BUF - HDR_SIZE;
  122. final byte[] buf = new byte[buflen];
  123. for (int i = 0; i < buf.length; i++) {
  124. buf[i] = (byte) i;
  125. }
  126. final SideBandOutputStream out;
  127. out = new SideBandOutputStream(CH_DATA, MAX_BUF, rawOut);
  128. out.write(buf);
  129. out.flush();
  130. final byte[] act = rawOut.toByteArray();
  131. final String explen = Integer.toString(buf.length + HDR_SIZE, 16);
  132. assertEquals(HDR_SIZE + buf.length, act.length);
  133. assertEquals(new String(act, 0, 4, "UTF-8"), explen);
  134. assertEquals(1, act[4]);
  135. for (int i = 0, j = HDR_SIZE; i < buf.length; i++, j++) {
  136. assertEquals(buf[i], act[j]);
  137. }
  138. }
  139. public void testFlush() throws IOException {
  140. final int[] flushCnt = new int[1];
  141. final OutputStream mockout = new OutputStream() {
  142. @Override
  143. public void write(int arg0) throws IOException {
  144. fail("should not write");
  145. }
  146. @Override
  147. public void flush() throws IOException {
  148. flushCnt[0]++;
  149. }
  150. };
  151. new SideBandOutputStream(CH_DATA, SMALL_BUF, mockout).flush();
  152. assertEquals(1, flushCnt[0]);
  153. }
  154. public void testConstructor_RejectsBadChannel() {
  155. try {
  156. new SideBandOutputStream(-1, MAX_BUF, rawOut);
  157. fail("Accepted -1 channel number");
  158. } catch (IllegalArgumentException e) {
  159. assertEquals("channel -1 must be in range [0, 255]", e.getMessage());
  160. }
  161. try {
  162. new SideBandOutputStream(0, MAX_BUF, rawOut);
  163. fail("Accepted 0 channel number");
  164. } catch (IllegalArgumentException e) {
  165. assertEquals("channel 0 must be in range [0, 255]", e.getMessage());
  166. }
  167. try {
  168. new SideBandOutputStream(256, MAX_BUF, rawOut);
  169. fail("Accepted 256 channel number");
  170. } catch (IllegalArgumentException e) {
  171. assertEquals("channel 256 must be in range [0, 255]", e
  172. .getMessage());
  173. }
  174. }
  175. public void testConstructor_RejectsBadBufferSize() {
  176. try {
  177. new SideBandOutputStream(CH_DATA, -1, rawOut);
  178. fail("Accepted -1 for buffer size");
  179. } catch (IllegalArgumentException e) {
  180. assertEquals("packet size -1 must be >= 5", e.getMessage());
  181. }
  182. try {
  183. new SideBandOutputStream(CH_DATA, 0, rawOut);
  184. fail("Accepted 0 for buffer size");
  185. } catch (IllegalArgumentException e) {
  186. assertEquals("packet size 0 must be >= 5", e.getMessage());
  187. }
  188. try {
  189. new SideBandOutputStream(CH_DATA, 1, rawOut);
  190. fail("Accepted 1 for buffer size");
  191. } catch (IllegalArgumentException e) {
  192. assertEquals("packet size 1 must be >= 5", e.getMessage());
  193. }
  194. try {
  195. new SideBandOutputStream(CH_DATA, Integer.MAX_VALUE, rawOut);
  196. fail("Accepted " + Integer.MAX_VALUE + " for buffer size");
  197. } catch (IllegalArgumentException e) {
  198. assertEquals(MessageFormat.format(JGitText.get().packetSizeMustBeAtMost, Integer.MAX_VALUE, 65520), e.getMessage());
  199. }
  200. }
  201. private void assertBuffer(final String exp) throws IOException {
  202. assertEquals(exp, new String(rawOut.toByteArray(),
  203. Constants.CHARACTER_ENCODING));
  204. }
  205. }