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

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