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 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
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 }