2 * Copyright (C) 2009, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.transport
;
40 import java
.io
.ByteArrayInputStream
;
41 import java
.io
.IOException
;
43 import junit
.framework
.TestCase
;
45 import org
.spearce
.jgit
.lib
.Constants
;
46 import org
.spearce
.jgit
.lib
.MutableObjectId
;
47 import org
.spearce
.jgit
.lib
.ObjectId
;
49 // Note, test vectors created with:
51 // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
53 public class PacketLineInTest
extends TestCase
{
54 private ByteArrayInputStream rawIn
;
56 private PacketLineIn in
;
60 public void testReadString1() throws IOException
{
61 init("0006a\n0007bc\n");
62 assertEquals("a", in
.readString());
63 assertEquals("bc", in
.readString());
67 public void testReadString2() throws IOException
{
68 init("0032want fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
69 final String act
= in
.readString();
70 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act
);
74 public void testReadString4() throws IOException
{
76 assertEquals("a", in
.readString());
77 assertEquals("bc", in
.readString());
81 public void testReadString5() throws IOException
{
82 // accept both upper and lower case
83 init("000Fhi i am a s");
84 assertEquals("hi i am a s", in
.readString());
87 init("000fhi i am a s");
88 assertEquals("hi i am a s", in
.readString());
92 public void testReadString_LenHELO() {
96 fail("incorrectly accepted invalid packet header");
97 } catch (IOException e
) {
98 assertEquals("Invalid packet line header: HELO", e
.getMessage());
102 public void testReadString_Len0001() {
106 fail("incorrectly accepted invalid packet header");
107 } catch (IOException e
) {
108 assertEquals("Invalid packet line header: 0001", e
.getMessage());
112 public void testReadString_Len0002() {
116 fail("incorrectly accepted invalid packet header");
117 } catch (IOException e
) {
118 assertEquals("Invalid packet line header: 0002", e
.getMessage());
122 public void testReadString_Len0003() {
126 fail("incorrectly accepted invalid packet header");
127 } catch (IOException e
) {
128 assertEquals("Invalid packet line header: 0003", e
.getMessage());
132 public void testReadString_Len0004() throws IOException
{
134 final String act
= in
.readString();
135 assertEquals("", act
);
136 assertNotSame(PacketLineIn
.END
, act
);
140 public void testReadString_End() throws IOException
{
142 assertSame(PacketLineIn
.END
, in
.readString());
148 public void testReadStringRaw1() throws IOException
{
150 assertEquals("a", in
.readStringRaw());
151 assertEquals("bc", in
.readStringRaw());
155 public void testReadStringRaw2() throws IOException
{
156 init("0031want fcfcfb1fd94829c1a1704f894fc111d14770d34e");
157 final String act
= in
.readStringRaw();
158 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act
);
162 public void testReadStringRaw3() throws IOException
{
164 final String act
= in
.readStringRaw();
165 assertEquals("", act
);
166 assertNotSame(PacketLineIn
.END
, act
);
170 public void testReadStringRaw_End() throws IOException
{
172 assertSame(PacketLineIn
.END
, in
.readStringRaw());
176 public void testReadStringRaw4() {
180 fail("incorrectly accepted invalid packet header");
181 } catch (IOException e
) {
182 assertEquals("Invalid packet line header: HELO", e
.getMessage());
188 public void testReadACK_NAK() throws IOException
{
189 final ObjectId expid
= ObjectId
190 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
191 final MutableObjectId actid
= new MutableObjectId();
192 actid
.fromString(expid
.name());
195 assertSame(PacketLineIn
.AckNackResult
.NAK
, in
.readACK(actid
));
196 assertTrue(actid
.equals(expid
));
200 public void testReadACK_ACK1() throws IOException
{
201 final ObjectId expid
= ObjectId
202 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
203 final MutableObjectId actid
= new MutableObjectId();
205 init("0031ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
206 assertSame(PacketLineIn
.AckNackResult
.ACK
, in
.readACK(actid
));
207 assertTrue(actid
.equals(expid
));
211 public void testReadACK_ACKcontinue1() throws IOException
{
212 final ObjectId expid
= ObjectId
213 .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
214 final MutableObjectId actid
= new MutableObjectId();
216 init("003aACK fcfcfb1fd94829c1a1704f894fc111d14770d34e continue\n");
217 assertSame(PacketLineIn
.AckNackResult
.ACK_CONTINUE
, in
.readACK(actid
));
218 assertTrue(actid
.equals(expid
));
222 public void testReadACK_Invalid1() {
225 in
.readACK(new MutableObjectId());
226 fail("incorrectly accepted invalid packet header");
227 } catch (IOException e
) {
228 assertEquals("Invalid packet line header: HELO", e
.getMessage());
232 public void testReadACK_Invalid2() {
235 in
.readACK(new MutableObjectId());
236 fail("incorrectly accepted invalid ACK/NAK");
237 } catch (IOException e
) {
238 assertEquals("Expected ACK/NAK, got: HELO", e
.getMessage());
242 public void testReadACK_Invalid3() {
245 in
.readACK(new MutableObjectId());
246 fail("incorrectly accepted no ACK/NAK");
247 } catch (IOException e
) {
248 assertEquals("Expected ACK/NAK, found EOF", e
.getMessage());
254 private void init(final String msg
) {
255 rawIn
= new ByteArrayInputStream(Constants
.encodeASCII(msg
));
256 in
= new PacketLineIn(rawIn
);
259 private void assertEOF() {
260 assertEquals(-1, rawIn
.read());