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