You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PacketLineInTest.java 9.2KB

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