1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
68
69
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" )
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" )
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" )
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" )
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" )
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" )
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" )
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" )
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" )
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
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 }