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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2009, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertSame;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.IOException;
  18. import org.eclipse.jgit.errors.PackProtocolException;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.lib.MutableObjectId;
  21. import org.eclipse.jgit.lib.ObjectId;
  22. import org.junit.Test;
  23. // Note, test vectors created with:
  24. //
  25. // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
  26. public class PacketLineInTest {
  27. private ByteArrayInputStream rawIn;
  28. private PacketLineIn in;
  29. // readString
  30. @Test
  31. public void testReadString1() throws IOException {
  32. init("0006a\n0007bc\n");
  33. assertEquals("a", in.readString());
  34. assertEquals("bc", in.readString());
  35. assertEOF();
  36. }
  37. @Test
  38. public void testReadString2() throws IOException {
  39. init("0032want fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
  40. final String act = in.readString();
  41. assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
  42. assertEOF();
  43. }
  44. @Test
  45. public void testReadString4() throws IOException {
  46. init("0005a0006bc");
  47. assertEquals("a", in.readString());
  48. assertEquals("bc", in.readString());
  49. assertEOF();
  50. }
  51. @Test
  52. public void testReadString5() throws IOException {
  53. // accept both upper and lower case
  54. init("000Fhi i am a s");
  55. assertEquals("hi i am a s", in.readString());
  56. assertEOF();
  57. init("000fhi i am a s");
  58. assertEquals("hi i am a s", in.readString());
  59. assertEOF();
  60. }
  61. @Test
  62. public void testReadString_LenHELO() {
  63. init("HELO");
  64. try {
  65. in.readString();
  66. fail("incorrectly accepted invalid packet header");
  67. } catch (IOException e) {
  68. assertEquals("Invalid packet line header: HELO", e.getMessage());
  69. }
  70. }
  71. @Test
  72. public void testReadString_Len0002() {
  73. init("0002");
  74. try {
  75. in.readString();
  76. fail("incorrectly accepted invalid packet header");
  77. } catch (IOException e) {
  78. assertEquals("Invalid packet line header: 0002", e.getMessage());
  79. }
  80. }
  81. @Test
  82. public void testReadString_Len0003() {
  83. init("0003");
  84. try {
  85. in.readString();
  86. fail("incorrectly accepted invalid packet header");
  87. } catch (IOException e) {
  88. assertEquals("Invalid packet line header: 0003", e.getMessage());
  89. }
  90. }
  91. @Test
  92. public void testReadString_Len0004() throws IOException {
  93. init("0004");
  94. final String act = in.readString();
  95. assertEquals("", act);
  96. assertFalse(PacketLineIn.isEnd(act));
  97. assertFalse(PacketLineIn.isDelimiter(act));
  98. assertEOF();
  99. }
  100. @Test
  101. public void testReadString_End() throws IOException {
  102. init("0000");
  103. String act = in.readString();
  104. assertTrue(PacketLineIn.isEnd(act));
  105. assertFalse(PacketLineIn.isDelimiter(act));
  106. assertEOF();
  107. }
  108. @Test
  109. public void testReadString_Delim() throws IOException {
  110. init("0001");
  111. String act = in.readString();
  112. assertTrue(PacketLineIn.isDelimiter(act));
  113. assertFalse(PacketLineIn.isEnd(act));
  114. assertEOF();
  115. }
  116. // readStringNoLF
  117. @Test
  118. public void testReadStringRaw1() throws IOException {
  119. init("0005a0006bc");
  120. assertEquals("a", in.readStringRaw());
  121. assertEquals("bc", in.readStringRaw());
  122. assertEOF();
  123. }
  124. @Test
  125. public void testReadStringRaw2() throws IOException {
  126. init("0031want fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  127. final String act = in.readStringRaw();
  128. assertEquals("want fcfcfb1fd94829c1a1704f894fc111d14770d34e", act);
  129. assertEOF();
  130. }
  131. @Test
  132. public void testReadStringRaw3() throws IOException {
  133. init("0004");
  134. final String act = in.readStringRaw();
  135. assertEquals("", act);
  136. assertFalse(PacketLineIn.isEnd(act));
  137. assertEOF();
  138. }
  139. @Test
  140. public void testReadStringRaw_End() throws IOException {
  141. init("0000");
  142. assertTrue(PacketLineIn.isEnd(in.readString()));
  143. assertEOF();
  144. }
  145. @Test
  146. public void testReadStringRaw4() {
  147. init("HELO");
  148. try {
  149. in.readStringRaw();
  150. fail("incorrectly accepted invalid packet header");
  151. } catch (IOException e) {
  152. assertEquals("Invalid packet line header: HELO", e.getMessage());
  153. }
  154. }
  155. // readACK
  156. @Test
  157. public void testReadACK_NAK() throws IOException {
  158. final ObjectId expid = ObjectId
  159. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  160. final MutableObjectId actid = new MutableObjectId();
  161. actid.fromString(expid.name());
  162. init("0008NAK\n");
  163. assertSame(PacketLineIn.AckNackResult.NAK, in.readACK(actid));
  164. assertEquals(expid, actid);
  165. assertEOF();
  166. }
  167. @Test
  168. public void testReadACK_ACK1() throws IOException {
  169. final ObjectId expid = ObjectId
  170. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  171. final MutableObjectId actid = new MutableObjectId();
  172. init("0031ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e\n");
  173. assertSame(PacketLineIn.AckNackResult.ACK, in.readACK(actid));
  174. assertEquals(expid, actid);
  175. assertEOF();
  176. }
  177. @Test
  178. public void testReadACK_ACKcontinue1() throws IOException {
  179. final ObjectId expid = ObjectId
  180. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  181. final MutableObjectId actid = new MutableObjectId();
  182. init("003aACK fcfcfb1fd94829c1a1704f894fc111d14770d34e continue\n");
  183. assertSame(PacketLineIn.AckNackResult.ACK_CONTINUE, in.readACK(actid));
  184. assertEquals(expid, actid);
  185. assertEOF();
  186. }
  187. @Test
  188. public void testReadACK_ACKcommon1() throws IOException {
  189. final ObjectId expid = ObjectId
  190. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  191. final MutableObjectId actid = new MutableObjectId();
  192. init("0038ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e common\n");
  193. assertSame(PacketLineIn.AckNackResult.ACK_COMMON, in.readACK(actid));
  194. assertEquals(expid, actid);
  195. assertEOF();
  196. }
  197. @Test
  198. public void testReadACK_ACKready1() throws IOException {
  199. final ObjectId expid = ObjectId
  200. .fromString("fcfcfb1fd94829c1a1704f894fc111d14770d34e");
  201. final MutableObjectId actid = new MutableObjectId();
  202. init("0037ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e ready\n");
  203. assertSame(PacketLineIn.AckNackResult.ACK_READY, in.readACK(actid));
  204. assertEquals(expid, actid);
  205. assertEOF();
  206. }
  207. @Test
  208. public void testReadACK_Invalid1() {
  209. init("HELO");
  210. try {
  211. in.readACK(new MutableObjectId());
  212. fail("incorrectly accepted invalid packet header");
  213. } catch (IOException e) {
  214. assertEquals("Invalid packet line header: HELO", e.getMessage());
  215. }
  216. }
  217. @Test
  218. public void testReadACK_Invalid2() {
  219. init("0009HELO\n");
  220. try {
  221. in.readACK(new MutableObjectId());
  222. fail("incorrectly accepted invalid ACK/NAK");
  223. } catch (IOException e) {
  224. assertEquals("Expected ACK/NAK, got: HELO", e.getMessage());
  225. }
  226. }
  227. @Test
  228. public void testReadACK_Invalid3() {
  229. String s = "ACK fcfcfb1fd94829c1a1704f894fc111d14770d34e neverhappen";
  230. init("003d" + s + "\n");
  231. try {
  232. in.readACK(new MutableObjectId());
  233. fail("incorrectly accepted unsupported ACK status");
  234. } catch (IOException e) {
  235. assertEquals("Expected ACK/NAK, got: " + s, e.getMessage());
  236. }
  237. }
  238. @Test
  239. public void testReadACK_Invalid4() {
  240. init("0000");
  241. try {
  242. in.readACK(new MutableObjectId());
  243. fail("incorrectly accepted no ACK/NAK");
  244. } catch (IOException e) {
  245. assertEquals("Expected ACK/NAK, found EOF", e.getMessage());
  246. }
  247. }
  248. @Test
  249. public void testReadACK_ERR() throws IOException {
  250. init("001aERR want is not valid\n");
  251. try {
  252. in.readACK(new MutableObjectId());
  253. fail("incorrectly accepted ERR");
  254. } catch (PackProtocolException e) {
  255. assertEquals("want is not valid", e.getMessage());
  256. }
  257. }
  258. // test support
  259. private void init(String msg) {
  260. rawIn = new ByteArrayInputStream(Constants.encodeASCII(msg));
  261. in = new PacketLineIn(rawIn);
  262. }
  263. private void assertEOF() {
  264. assertEquals(-1, rawIn.read());
  265. }
  266. }