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.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
67
68
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" )
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" )
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" )
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" )
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" )
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" )
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" )
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" )
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" )
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
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 }