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  		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
81  				SMALL_BUF, rawOut)) {
82  			out.write(new byte[] { 'a', 'b', 'c' });
83  			out.flush();
84  		}
85  		assertBuffer("0008\001abc");
86  	}
87  
88  	@Test
89  	public void testWrite_CH_PROGRESS() throws IOException {
90  		try (SideBandOutputStream out = new SideBandOutputStream(CH_PROGRESS,
91  				SMALL_BUF, rawOut)) {
92  			out.write(new byte[] { 'a', 'b', 'c' });
93  			out.flush();
94  		}
95  		assertBuffer("0008\002abc");
96  	}
97  
98  	@Test
99  	public void testWrite_CH_ERROR() throws IOException {
100 		try (SideBandOutputStream out = new SideBandOutputStream(CH_ERROR,
101 				SMALL_BUF, rawOut)) {
102 			out.write(new byte[] { 'a', 'b', 'c' });
103 			out.flush();
104 		}
105 		assertBuffer("0008\003abc");
106 	}
107 
108 	@Test
109 	public void testWrite_Small() throws IOException {
110 		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
111 				SMALL_BUF, rawOut)) {
112 			out.write('a');
113 			out.write('b');
114 			out.write('c');
115 			out.flush();
116 		}
117 		assertBuffer("0008\001abc");
118 	}
119 
120 	@Test
121 	public void testWrite_SmallBlocks1() throws IOException {
122 		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
123 				rawOut)) {
124 			out.write('a');
125 			out.write('b');
126 			out.write('c');
127 			out.flush();
128 		}
129 		assertBuffer("0006\001a0006\001b0006\001c");
130 	}
131 
132 	@Test
133 	public void testWrite_SmallBlocks2() throws IOException {
134 		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
135 				rawOut)) {
136 			out.write(new byte[] { 'a', 'b', 'c' });
137 			out.flush();
138 		}
139 		assertBuffer("0006\001a0006\001b0006\001c");
140 	}
141 
142 	@Test
143 	public void testWrite_SmallBlocks3() throws IOException {
144 		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 7,
145 				rawOut)) {
146 			out.write('a');
147 			out.write(new byte[] { 'b', 'c' });
148 			out.flush();
149 		}
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 		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
162 				MAX_BUF, rawOut)) {
163 			out.write(buf);
164 			out.flush();
165 		}
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 	@Test
178 	public void testFlush() throws IOException {
179 		final int[] flushCnt = new int[1];
180 		final OutputStream mockout = new OutputStream() {
181 			@Override
182 			public void write(int arg0) throws IOException {
183 				fail("should not write");
184 			}
185 
186 			@Override
187 			public void flush() throws IOException {
188 				flushCnt[0]++;
189 			}
190 		};
191 
192 		try (SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
193 				SMALL_BUF, mockout)) {
194 			out.flush();
195 		}
196 		assertEquals(1, flushCnt[0]);
197 	}
198 
199 	private void createSideBandOutputStream(int chan, int sz, OutputStream os)
200 			throws Exception {
201 		try (SideBandOutputStream s = new SideBandOutputStream(chan, sz, os)) {
202 			// Unused
203 		}
204 	}
205 
206 	@Test
207 	public void testConstructor_RejectsBadChannel() throws Exception {
208 		try {
209 			createSideBandOutputStream(-1, MAX_BUF, rawOut);
210 			fail("Accepted -1 channel number");
211 		} catch (IllegalArgumentException e) {
212 			assertEquals("channel -1 must be in range [1, 255]", e.getMessage());
213 		}
214 
215 		try {
216 			createSideBandOutputStream(0, MAX_BUF, rawOut);
217 			fail("Accepted 0 channel number");
218 		} catch (IllegalArgumentException e) {
219 			assertEquals("channel 0 must be in range [1, 255]", e.getMessage());
220 		}
221 
222 		try {
223 			createSideBandOutputStream(256, MAX_BUF, rawOut);
224 			fail("Accepted 256 channel number");
225 		} catch (IllegalArgumentException e) {
226 			assertEquals("channel 256 must be in range [1, 255]", e
227 					.getMessage());
228 		}
229 	}
230 
231 	@Test
232 	public void testConstructor_RejectsBadBufferSize() throws Exception {
233 		try {
234 			createSideBandOutputStream(CH_DATA, -1, rawOut);
235 			fail("Accepted -1 for buffer size");
236 		} catch (IllegalArgumentException e) {
237 			assertEquals("packet size -1 must be >= 5", e.getMessage());
238 		}
239 
240 		try {
241 			createSideBandOutputStream(CH_DATA, 0, rawOut);
242 			fail("Accepted 0 for buffer size");
243 		} catch (IllegalArgumentException e) {
244 			assertEquals("packet size 0 must be >= 5", e.getMessage());
245 		}
246 
247 		try {
248 			createSideBandOutputStream(CH_DATA, 1, rawOut);
249 			fail("Accepted 1 for buffer size");
250 		} catch (IllegalArgumentException e) {
251 			assertEquals("packet size 1 must be >= 5", e.getMessage());
252 		}
253 
254 		try {
255 			createSideBandOutputStream(CH_DATA, Integer.MAX_VALUE, rawOut);
256 			fail("Accepted " + Integer.MAX_VALUE + " for buffer size");
257 		} catch (IllegalArgumentException e) {
258 			assertEquals(MessageFormat.format(
259 					JGitText.get().packetSizeMustBeAtMost,
260 					valueOf(Integer.MAX_VALUE), valueOf(65520)), e.getMessage());
261 		}
262 	}
263 
264 	private void assertBuffer(String exp) {
265 		assertEquals(exp, new String(rawOut.toByteArray(), UTF_8));
266 	}
267 }