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 org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNotSame;
48 import static org.junit.Assert.assertSame;
49 import static org.junit.Assert.fail;
50
51 import java.io.ByteArrayInputStream;
52 import java.io.IOException;
53
54 import org.eclipse.jgit.errors.PackProtocolException;
55 import org.eclipse.jgit.lib.Constants;
56 import org.eclipse.jgit.lib.MutableObjectId;
57 import org.eclipse.jgit.lib.ObjectId;
58 import org.junit.Test;
59
60
61
62
63
64 public class PacketLineInTest {
65 private ByteArrayInputStream rawIn;
66
67 private PacketLineIn in;
68
69
70
71 @Test
72 public void testReadString1() throws IOException {
73 init("0006a\n0007bc\n");
74 assertEquals("a", in.readString());
75 assertEquals("bc", in.readString());
76 assertEOF();
77 }
78
79 @Test
80 public void testReadString2() throws IOException {
81 init("0032want fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
82 final String act = in.readString();
83 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
84 assertEOF();
85 }
86
87 @Test
88 public void testReadString4() throws IOException {
89 init("0005a0006bc");
90 assertEquals("a", in.readString());
91 assertEquals("bc", in.readString());
92 assertEOF();
93 }
94
95 @Test
96 public void testReadString5() throws IOException {
97
98 init("000Fhi i am a s");
99 assertEquals("hi i am a s", in.readString());
100 assertEOF();
101
102 init("000fhi i am a s");
103 assertEquals("hi i am a s", in.readString());
104 assertEOF();
105 }
106
107 @Test
108 public void testReadString_LenHELO() {
109 init("HELO");
110 try {
111 in.readString();
112 fail("incorrectly accepted invalid packet header");
113 } catch (IOException e) {
114 assertEquals("Invalid packet line header: HELO", e.getMessage());
115 }
116 }
117
118 @Test
119 public void testReadString_Len0002() {
120 init("0002");
121 try {
122 in.readString();
123 fail("incorrectly accepted invalid packet header");
124 } catch (IOException e) {
125 assertEquals("Invalid packet line header: 0002", e.getMessage());
126 }
127 }
128
129 @Test
130 public void testReadString_Len0003() {
131 init("0003");
132 try {
133 in.readString();
134 fail("incorrectly accepted invalid packet header");
135 } catch (IOException e) {
136 assertEquals("Invalid packet line header: 0003", e.getMessage());
137 }
138 }
139
140 @Test
141 public void testReadString_Len0004() throws IOException {
142 init("0004");
143 final String act = in.readString();
144 assertEquals("", act);
145 assertNotSame(PacketLineIn.END, act);
146 assertEOF();
147 }
148
149 @Test
150 public void testReadString_End() throws IOException {
151 init("0000");
152 assertSame(PacketLineIn.END, in.readString());
153 assertEOF();
154 }
155
156 @Test
157 public void testReadString_Delim() throws IOException {
158 init("0001");
159 assertSame(PacketLineIn.DELIM, in.readString());
160 assertEOF();
161 }
162
163
164
165 @Test
166 public void testReadStringRaw1() throws IOException {
167 init("0005a0006bc");
168 assertEquals("a", in.readStringRaw());
169 assertEquals("bc", in.readStringRaw());
170 assertEOF();
171 }
172
173 @Test
174 public void testReadStringRaw2() throws IOException {
175 init("0031want fcfcfb1fd94829c1a1704f894fc111d14770d34e");
176 final String act = in.readStringRaw();
177 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
178 assertEOF();
179 }
180
181 @Test
182 public void testReadStringRaw3() throws IOException {
183 init("0004");
184 final String act = in.readStringRaw();
185 assertEquals("", act);
186 assertNotSame(PacketLineIn.END, act);
187 assertEOF();
188 }
189
190 @Test
191 public void testReadStringRaw_End() throws IOException {
192 init("0000");
193 assertSame(PacketLineIn.END, in.readStringRaw());
194 assertEOF();
195 }
196
197 @Test
198 public void testReadStringRaw4() {
199 init("HELO");
200 try {
201 in.readStringRaw();
202 fail("incorrectly accepted invalid packet header");
203 } catch (IOException e) {
204 assertEquals("Invalid packet line header: HELO", e.getMessage());
205 }
206 }
207
208
209
210 @Test
211 public void testReadACK_NAK() throws IOException {
212 final ObjectId expid = ObjectId
213 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
214 final MutableObjectId actid = new MutableObjectId();
215 actid.fromString(expid.name());
216
217 init("0008NAK\n");
218 assertSame(PacketLineIn.AckNackResult.NAK, in.readACK(actid));
219 assertEquals(expid, actid);
220 assertEOF();
221 }
222
223 @Test
224 public void testReadACK_ACK1() throws IOException {
225 final ObjectId expid = ObjectId
226 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
227 final MutableObjectId actid = new MutableObjectId();
228
229 init("0031ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
230 assertSame(PacketLineIn.AckNackResult.ACK, in.readACK(actid));
231 assertEquals(expid, actid);
232 assertEOF();
233 }
234
235 @Test
236 public void testReadACK_ACKcontinue1() throws IOException {
237 final ObjectId expid = ObjectId
238 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
239 final MutableObjectId actid = new MutableObjectId();
240
241 init("003aACK fcfcfb1fd94829c1a1704f894fc111d14770d34e continue\n");
242 assertSame(PacketLineIn.AckNackResult.ACK_CONTINUE, in.readACK(actid));
243 assertEquals(expid, actid);
244 assertEOF();
245 }
246
247 @Test
248 public void testReadACK_ACKcommon1() throws IOException {
249 final ObjectId expid = ObjectId
250 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
251 final MutableObjectId actid = new MutableObjectId();
252
253 init("0038ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e common\n");
254 assertSame(PacketLineIn.AckNackResult.ACK_COMMON, in.readACK(actid));
255 assertEquals(expid, actid);
256 assertEOF();
257 }
258
259 @Test
260 public void testReadACK_ACKready1() throws IOException {
261 final ObjectId expid = ObjectId
262 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
263 final MutableObjectId actid = new MutableObjectId();
264
265 init("0037ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e ready\n");
266 assertSame(PacketLineIn.AckNackResult.ACK_READY, in.readACK(actid));
267 assertEquals(expid, actid);
268 assertEOF();
269 }
270
271 @Test
272 public void testReadACK_Invalid1() {
273 init("HELO");
274 try {
275 in.readACK(new MutableObjectId());
276 fail("incorrectly accepted invalid packet header");
277 } catch (IOException e) {
278 assertEquals("Invalid packet line header: HELO", e.getMessage());
279 }
280 }
281
282 @Test
283 public void testReadACK_Invalid2() {
284 init("0009HELO\n");
285 try {
286 in.readACK(new MutableObjectId());
287 fail("incorrectly accepted invalid ACK/NAK");
288 } catch (IOException e) {
289 assertEquals("Expected ACK/NAK, got: HELO", e.getMessage());
290 }
291 }
292
293 @Test
294 public void testReadACK_Invalid3() {
295 String s = "ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e neverhappen";
296 init("003d" + s + "\n");
297 try {
298 in.readACK(new MutableObjectId());
299 fail("incorrectly accepted unsupported ACK status");
300 } catch (IOException e) {
301 assertEquals("Expected ACK/NAK, got: " + s, e.getMessage());
302 }
303 }
304
305 @Test
306 public void testReadACK_Invalid4() {
307 init("0000");
308 try {
309 in.readACK(new MutableObjectId());
310 fail("incorrectly accepted no ACK/NAK");
311 } catch (IOException e) {
312 assertEquals("Expected ACK/NAK, found EOF", e.getMessage());
313 }
314 }
315
316 @Test
317 public void testReadACK_ERR() throws IOException {
318 init("001aERR want is not valid\n");
319 try {
320 in.readACK(new MutableObjectId());
321 fail("incorrectly accepted ERR");
322 } catch (PackProtocolException e) {
323 assertEquals("want is not valid", e.getMessage());
324 }
325 }
326
327
328
329 private void init(String msg) {
330 rawIn = new ByteArrayInputStream(Constants.encodeASCII(msg));
331 in = new PacketLineIn(rawIn);
332 }
333
334 private void assertEOF() {
335 assertEquals(-1, rawIn.read());
336 }
337 }