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_Len0001() {
120 init("0001");
121 try {
122 in.readString();
123 fail("incorrectly accepted invalid packet header");
124 } catch (IOException e) {
125 assertEquals("Invalid packet line header: 0001", e.getMessage());
126 }
127 }
128
129 @Test
130 public void testReadString_Len0002() {
131 init("0002");
132 try {
133 in.readString();
134 fail("incorrectly accepted invalid packet header");
135 } catch (IOException e) {
136 assertEquals("Invalid packet line header: 0002", e.getMessage());
137 }
138 }
139
140 @Test
141 public void testReadString_Len0003() {
142 init("0003");
143 try {
144 in.readString();
145 fail("incorrectly accepted invalid packet header");
146 } catch (IOException e) {
147 assertEquals("Invalid packet line header: 0003", e.getMessage());
148 }
149 }
150
151 @Test
152 public void testReadString_Len0004() throws IOException {
153 init("0004");
154 final String act = in.readString();
155 assertEquals("", act);
156 assertNotSame(PacketLineIn.END, act);
157 assertEOF();
158 }
159
160 @Test
161 public void testReadString_End() throws IOException {
162 init("0000");
163 assertSame(PacketLineIn.END, in.readString());
164 assertEOF();
165 }
166
167
168
169 @Test
170 public void testReadStringRaw1() throws IOException {
171 init("0005a0006bc");
172 assertEquals("a", in.readStringRaw());
173 assertEquals("bc", in.readStringRaw());
174 assertEOF();
175 }
176
177 @Test
178 public void testReadStringRaw2() throws IOException {
179 init("0031want fcfcfb1fd94829c1a1704f894fc111d14770d34e");
180 final String act = in.readStringRaw();
181 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
182 assertEOF();
183 }
184
185 @Test
186 public void testReadStringRaw3() throws IOException {
187 init("0004");
188 final String act = in.readStringRaw();
189 assertEquals("", act);
190 assertNotSame(PacketLineIn.END, act);
191 assertEOF();
192 }
193
194 @Test
195 public void testReadStringRaw_End() throws IOException {
196 init("0000");
197 assertSame(PacketLineIn.END, in.readStringRaw());
198 assertEOF();
199 }
200
201 @Test
202 public void testReadStringRaw4() {
203 init("HELO");
204 try {
205 in.readStringRaw();
206 fail("incorrectly accepted invalid packet header");
207 } catch (IOException e) {
208 assertEquals("Invalid packet line header: HELO", e.getMessage());
209 }
210 }
211
212
213
214 @Test
215 public void testReadACK_NAK() throws IOException {
216 final ObjectId expid = ObjectId
217 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
218 final MutableObjectId actid = new MutableObjectId();
219 actid.fromString(expid.name());
220
221 init("0008NAK\n");
222 assertSame(PacketLineIn.AckNackResult.NAK, in.readACK(actid));
223 assertEquals(expid, actid);
224 assertEOF();
225 }
226
227 @Test
228 public void testReadACK_ACK1() throws IOException {
229 final ObjectId expid = ObjectId
230 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
231 final MutableObjectId actid = new MutableObjectId();
232
233 init("0031ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
234 assertSame(PacketLineIn.AckNackResult.ACK, in.readACK(actid));
235 assertEquals(expid, actid);
236 assertEOF();
237 }
238
239 @Test
240 public void testReadACK_ACKcontinue1() throws IOException {
241 final ObjectId expid = ObjectId
242 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
243 final MutableObjectId actid = new MutableObjectId();
244
245 init("003aACK fcfcfb1fd94829c1a1704f894fc111d14770d34e continue\n");
246 assertSame(PacketLineIn.AckNackResult.ACK_CONTINUE, in.readACK(actid));
247 assertEquals(expid, actid);
248 assertEOF();
249 }
250
251 @Test
252 public void testReadACK_ACKcommon1() throws IOException {
253 final ObjectId expid = ObjectId
254 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
255 final MutableObjectId actid = new MutableObjectId();
256
257 init("0038ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e common\n");
258 assertSame(PacketLineIn.AckNackResult.ACK_COMMON, in.readACK(actid));
259 assertEquals(expid, actid);
260 assertEOF();
261 }
262
263 @Test
264 public void testReadACK_ACKready1() throws IOException {
265 final ObjectId expid = ObjectId
266 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
267 final MutableObjectId actid = new MutableObjectId();
268
269 init("0037ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e ready\n");
270 assertSame(PacketLineIn.AckNackResult.ACK_READY, in.readACK(actid));
271 assertEquals(expid, actid);
272 assertEOF();
273 }
274
275 @Test
276 public void testReadACK_Invalid1() {
277 init("HELO");
278 try {
279 in.readACK(new MutableObjectId());
280 fail("incorrectly accepted invalid packet header");
281 } catch (IOException e) {
282 assertEquals("Invalid packet line header: HELO", e.getMessage());
283 }
284 }
285
286 @Test
287 public void testReadACK_Invalid2() {
288 init("0009HELO\n");
289 try {
290 in.readACK(new MutableObjectId());
291 fail("incorrectly accepted invalid ACK/NAK");
292 } catch (IOException e) {
293 assertEquals("Expected ACK/NAK, got: HELO", e.getMessage());
294 }
295 }
296
297 @Test
298 public void testReadACK_Invalid3() {
299 String s = "ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e neverhappen";
300 init("003d" + s + "\n");
301 try {
302 in.readACK(new MutableObjectId());
303 fail("incorrectly accepted unsupported ACK status");
304 } catch (IOException e) {
305 assertEquals("Expected ACK/NAK, got: " + s, e.getMessage());
306 }
307 }
308
309 @Test
310 public void testReadACK_Invalid4() {
311 init("0000");
312 try {
313 in.readACK(new MutableObjectId());
314 fail("incorrectly accepted no ACK/NAK");
315 } catch (IOException e) {
316 assertEquals("Expected ACK/NAK, found EOF", e.getMessage());
317 }
318 }
319
320 @Test
321 public void testReadACK_ERR() throws IOException {
322 init("001aERR want is not valid\n");
323 try {
324 in.readACK(new MutableObjectId());
325 fail("incorrectly accepted ERR");
326 } catch (PackProtocolException e) {
327 assertEquals("want is not valid", e.getMessage());
328 }
329 }
330
331
332
333 private void init(final String msg) {
334 rawIn = new ByteArrayInputStream(Constants.encodeASCII(msg));
335 in = new PacketLineIn(rawIn);
336 }
337
338 private void assertEOF() {
339 assertEquals(-1, rawIn.read());
340 }
341 }