Add tests for PacketLineIn
[egit/graphgui.git] / org.spearce.jgit.test / tst / org / spearce / jgit / transport / PacketLineInTest.java
blobb9ab50ed5219cec89d2b8f39ab6dd47429c7c46a
1 /*
2 * Copyright (C) 2009, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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;
58 // readString
60 public void testReadString1() throws IOException {
61 init("0006a\n0007bc\n");
62 assertEquals("a", in.readString());
63 assertEquals("bc", in.readString());
64 assertEOF();
67 public void testReadString2() throws IOException {
68 init("0032want fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
69 final String act = in.readString();
70 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
71 assertEOF();
74 public void testReadString4() throws IOException {
75 init("0005a0006bc");
76 assertEquals("a", in.readString());
77 assertEquals("bc", in.readString());
78 assertEOF();
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());
85 assertEOF();
87 init("000fhi i am a s");
88 assertEquals("hi i am a s", in.readString());
89 assertEOF();
92 public void testReadString_LenHELO() {
93 init("HELO");
94 try {
95 in.readString();
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() {
103 init("0001");
104 try {
105 in.readString();
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() {
113 init("0002");
114 try {
115 in.readString();
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() {
123 init("0003");
124 try {
125 in.readString();
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 {
133 init("0004");
134 final String act = in.readString();
135 assertEquals("", act);
136 assertNotSame(PacketLineIn.END, act);
137 assertEOF();
140 public void testReadString_End() throws IOException {
141 init("0000");
142 assertSame(PacketLineIn.END, in.readString());
143 assertEOF();
146 // readStringNoLF
148 public void testReadStringRaw1() throws IOException {
149 init("0005a0006bc");
150 assertEquals("a", in.readStringRaw());
151 assertEquals("bc", in.readStringRaw());
152 assertEOF();
155 public void testReadStringRaw2() throws IOException {
156 init("0031want fcfcfb1fd94829c1a1704f894fc111d14770d34e");
157 final String act = in.readStringRaw();
158 assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
159 assertEOF();
162 public void testReadStringRaw3() throws IOException {
163 init("0004");
164 final String act = in.readStringRaw();
165 assertEquals("", act);
166 assertNotSame(PacketLineIn.END, act);
167 assertEOF();
170 public void testReadStringRaw_End() throws IOException {
171 init("0000");
172 assertSame(PacketLineIn.END, in.readStringRaw());
173 assertEOF();
176 public void testReadStringRaw4() {
177 init("HELO");
178 try {
179 in.readStringRaw();
180 fail("incorrectly accepted invalid packet header");
181 } catch (IOException e) {
182 assertEquals("Invalid packet line header: HELO", e.getMessage());
186 // readACK
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());
194 init("0008NAK\n");
195 assertSame(PacketLineIn.AckNackResult.NAK, in.readACK(actid));
196 assertTrue(actid.equals(expid));
197 assertEOF();
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));
208 assertEOF();
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));
219 assertEOF();
222 public void testReadACK_Invalid1() {
223 init("HELO");
224 try {
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() {
233 init("0009HELO\n");
234 try {
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() {
243 init("0000");
244 try {
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());
252 // test support
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());