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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_Len0001() {
  105. init("0001");
  106. try {
  107. in.readString();
  108. fail("incorrectly accepted invalid packet header");
  109. } catch (IOException e) {
  110. assertEquals("Invalid packet line header: 0001", e.getMessage());
  111. }
  112. }
  113. @Test
  114. public void testReadString_Len0002() {
  115. init("0002");
  116. try {
  117. in.readString();
  118. fail("incorrectly accepted invalid packet header");
  119. } catch (IOException e) {
  120. assertEquals("Invalid packet line header: 0002", e.getMessage());
  121. }
  122. }
  123. @Test
  124. public void testReadString_Len0003() {
  125. init("0003");
  126. try {
  127. in.readString();
  128. fail("incorrectly accepted invalid packet header");
  129. } catch (IOException e) {
  130. assertEquals("Invalid packet line header: 0003", e.getMessage());
  131. }
  132. }
  133. @Test
  134. public void testReadString_Len0004() throws IOException {
  135. init("0004");
  136. final String act = in.readString();
  137. assertEquals("", act);
  138. assertNotSame(PacketLineIn.END, act);
  139. assertEOF();
  140. }
  141. @Test
  142. public void testReadString_End() throws IOException {
  143. init("0000");
  144. assertSame(PacketLineIn.END, in.readString());
  145. assertEOF();
  146. }
  147. // readStringNoLF
  148. @Test
  149. public void testReadStringRaw1() throws IOException {
  150. init("0005a0006bc");
  151. assertEquals("a", in.readStringRaw());
  152. assertEquals("bc", in.readStringRaw());
  153. assertEOF();
  154. }
  155. @Test
  156. public void testReadStringRaw2() throws IOException {
  157. init("0031want fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  158. final String act = in.readStringRaw();
  159. assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
  160. assertEOF();
  161. }
  162. @Test
  163. public void testReadStringRaw3() throws IOException {
  164. init("0004");
  165. final String act = in.readStringRaw();
  166. assertEquals("", act);
  167. assertNotSame(PacketLineIn.END, act);
  168. assertEOF();
  169. }
  170. @Test
  171. public void testReadStringRaw_End() throws IOException {
  172. init("0000");
  173. assertSame(PacketLineIn.END, in.readStringRaw());
  174. assertEOF();
  175. }
  176. @Test
  177. public void testReadStringRaw4() {
  178. init("HELO");
  179. try {
  180. in.readStringRaw();
  181. fail("incorrectly accepted invalid packet header");
  182. } catch (IOException e) {
  183. assertEquals("Invalid packet line header: HELO", e.getMessage());
  184. }
  185. }
  186. // readACK
  187. @Test
  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());
  193. init("0008NAK\n");
  194. assertSame(PacketLineIn.AckNackResult.NAK, in.readACK(actid));
  195. assertEquals(expid, actid);
  196. assertEOF();
  197. }
  198. @Test
  199. public void testReadACK_ACK1() throws IOException {
  200. final ObjectId expid = ObjectId
  201. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  202. final MutableObjectId actid = new MutableObjectId();
  203. init("0031ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
  204. assertSame(PacketLineIn.AckNackResult.ACK, in.readACK(actid));
  205. assertEquals(expid, actid);
  206. assertEOF();
  207. }
  208. @Test
  209. public void testReadACK_ACKcontinue1() throws IOException {
  210. final ObjectId expid = ObjectId
  211. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  212. final MutableObjectId actid = new MutableObjectId();
  213. init("003aACK fcfcfb1fd94829c1a1704f894fc111d14770d34e continue\n");
  214. assertSame(PacketLineIn.AckNackResult.ACK_CONTINUE, in.readACK(actid));
  215. assertEquals(expid, actid);
  216. assertEOF();
  217. }
  218. @Test
  219. public void testReadACK_ACKcommon1() throws IOException {
  220. final ObjectId expid = ObjectId
  221. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  222. final MutableObjectId actid = new MutableObjectId();
  223. init("0038ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e common\n");
  224. assertSame(PacketLineIn.AckNackResult.ACK_COMMON, in.readACK(actid));
  225. assertEquals(expid, actid);
  226. assertEOF();
  227. }
  228. @Test
  229. public void testReadACK_ACKready1() throws IOException {
  230. final ObjectId expid = ObjectId
  231. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  232. final MutableObjectId actid = new MutableObjectId();
  233. init("0037ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e ready\n");
  234. assertSame(PacketLineIn.AckNackResult.ACK_READY, in.readACK(actid));
  235. assertEquals(expid, actid);
  236. assertEOF();
  237. }
  238. @Test
  239. public void testReadACK_Invalid1() {
  240. init("HELO");
  241. try {
  242. in.readACK(new MutableObjectId());
  243. fail("incorrectly accepted invalid packet header");
  244. } catch (IOException e) {
  245. assertEquals("Invalid packet line header: HELO", e.getMessage());
  246. }
  247. }
  248. @Test
  249. public void testReadACK_Invalid2() {
  250. init("0009HELO\n");
  251. try {
  252. in.readACK(new MutableObjectId());
  253. fail("incorrectly accepted invalid ACK/NAK");
  254. } catch (IOException e) {
  255. assertEquals("Expected ACK/NAK, got: HELO", e.getMessage());
  256. }
  257. }
  258. @Test
  259. public void testReadACK_Invalid3() {
  260. String s = "ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e neverhappen";
  261. init("003d" + s + "\n");
  262. try {
  263. in.readACK(new MutableObjectId());
  264. fail("incorrectly accepted unsupported ACK status");
  265. } catch (IOException e) {
  266. assertEquals("Expected ACK/NAK, got: " + s, e.getMessage());
  267. }
  268. }
  269. @Test
  270. public void testReadACK_Invalid4() {
  271. init("0000");
  272. try {
  273. in.readACK(new MutableObjectId());
  274. fail("incorrectly accepted no ACK/NAK");
  275. } catch (IOException e) {
  276. assertEquals("Expected ACK/NAK, found EOF", e.getMessage());
  277. }
  278. }
  279. @Test
  280. public void testReadACK_ERR() throws IOException {
  281. init("001aERR want is not valid\n");
  282. try {
  283. in.readACK(new MutableObjectId());
  284. fail("incorrectly accepted ERR");
  285. } catch (PackProtocolException e) {
  286. assertEquals("want is not valid", e.getMessage());
  287. }
  288. }
  289. // test support
  290. private void init(final String msg) {
  291. rawIn = new ByteArrayInputStream(Constants.encodeASCII(msg));
  292. in = new PacketLineIn(rawIn);
  293. }
  294. private void assertEOF() {
  295. assertEquals(-1, rawIn.read());
  296. }
  297. }