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.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  // Note, test vectors created with:
61  //
62  // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
63  
64  public class PacketLineInTest {
65  	private ByteArrayInputStream rawIn;
66  
67  	private PacketLineIn in;
68  
69  	// readString
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  		// accept both upper and lower case
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 	// readStringNoLF
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 	// readACK
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 	// test support
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 }