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