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

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