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