View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  // Note, test vectors created with:
62  //
63  // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
64  
65  public class PacketLineInTest {
66  	private ByteArrayInputStream rawIn;
67  
68  	private PacketLineIn in;
69  
70  	// readString
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  		// accept both upper and lower case
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 	// readStringNoLF
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 	// readACK
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 	// test support
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 }