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.

ProtocolV0ParserTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2018, Google LLC.
  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.eclipse.jgit.transport.ObjectIdMatcher.hasOnlyObjectIds;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertThat;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.IOException;
  51. import org.eclipse.jgit.errors.PackProtocolException;
  52. import org.eclipse.jgit.lib.Config;
  53. import org.junit.Test;
  54. public class ProtocolV0ParserTest {
  55. /*
  56. * Convert the input lines to the PacketLine that the parser reads.
  57. */
  58. private static PacketLineIn formatAsPacketLine(String... inputLines)
  59. throws IOException {
  60. ByteArrayOutputStream send = new ByteArrayOutputStream();
  61. PacketLineOut pckOut = new PacketLineOut(send);
  62. for (String line : inputLines) {
  63. if (line == PacketLineIn.END) {
  64. pckOut.end();
  65. } else if (line == PacketLineIn.DELIM) {
  66. pckOut.writeDelim();
  67. } else {
  68. pckOut.writeString(line);
  69. }
  70. }
  71. return new PacketLineIn(new ByteArrayInputStream(send.toByteArray()));
  72. }
  73. private static TransferConfig defaultConfig() {
  74. Config rc = new Config();
  75. rc.setBoolean("uploadpack", null, "allowfilter", true);
  76. return new TransferConfig(rc);
  77. }
  78. @Test
  79. public void testRecvWantsWithCapabilities()
  80. throws PackProtocolException, IOException {
  81. PacketLineIn pckIn = formatAsPacketLine(
  82. String.join(" ", "want",
  83. "4624442d68ee402a94364191085b77137618633e", "thin-pack",
  84. "no-progress", "include-tag", "ofs-delta", "\n"),
  85. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  86. PacketLineIn.END);
  87. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  88. FetchV0Request request = parser.recvWants(pckIn);
  89. assertTrue(request.getClientCapabilities()
  90. .contains(GitProtocolConstants.OPTION_THIN_PACK));
  91. assertTrue(request.getClientCapabilities()
  92. .contains(GitProtocolConstants.OPTION_NO_PROGRESS));
  93. assertTrue(request.getClientCapabilities()
  94. .contains(GitProtocolConstants.OPTION_INCLUDE_TAG));
  95. assertTrue(request.getClientCapabilities()
  96. .contains(GitProtocolConstants.CAPABILITY_OFS_DELTA));
  97. assertThat(request.getWantIds(),
  98. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  99. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  100. }
  101. @Test
  102. public void testRecvWantsWithAgent()
  103. throws PackProtocolException, IOException {
  104. PacketLineIn pckIn = formatAsPacketLine(
  105. String.join(" ", "want",
  106. "4624442d68ee402a94364191085b77137618633e", "thin-pack",
  107. "agent=JGit.test/0.0.1", "\n"),
  108. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  109. PacketLineIn.END);
  110. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  111. FetchV0Request request = parser.recvWants(pckIn);
  112. assertTrue(request.getClientCapabilities()
  113. .contains(GitProtocolConstants.OPTION_THIN_PACK));
  114. assertEquals(1, request.getClientCapabilities().size());
  115. assertEquals("JGit.test/0.0.1", request.getAgent());
  116. assertThat(request.getWantIds(),
  117. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  118. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  119. }
  120. /*
  121. * First round of protocol v0 negotiation. Client send wants, no
  122. * capabilities.
  123. */
  124. @Test
  125. public void testRecvWantsWithoutCapabilities()
  126. throws PackProtocolException, IOException {
  127. PacketLineIn pckIn = formatAsPacketLine(
  128. "want 4624442d68ee402a94364191085b77137618633e\n",
  129. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  130. PacketLineIn.END);
  131. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  132. FetchV0Request request = parser.recvWants(pckIn);
  133. assertTrue(request.getClientCapabilities().isEmpty());
  134. assertThat(request.getWantIds(),
  135. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  136. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  137. }
  138. @Test
  139. public void testRecvWantsDeepen()
  140. throws PackProtocolException, IOException {
  141. PacketLineIn pckIn = formatAsPacketLine(
  142. "want 4624442d68ee402a94364191085b77137618633e\n",
  143. "want f900c8326a43303685c46b279b9f70411bff1a4b\n", "deepen 3\n",
  144. PacketLineIn.END);
  145. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  146. FetchV0Request request = parser.recvWants(pckIn);
  147. assertTrue(request.getClientCapabilities().isEmpty());
  148. assertEquals(3, request.getDepth());
  149. assertThat(request.getWantIds(),
  150. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  151. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  152. }
  153. @Test
  154. public void testRecvWantsShallow()
  155. throws PackProtocolException, IOException {
  156. PacketLineIn pckIn = formatAsPacketLine(
  157. "want 4624442d68ee402a94364191085b77137618633e\n",
  158. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  159. "shallow 4b643d0ef739a1b494e7d6926d8d8ed80d35edf4\n",
  160. PacketLineIn.END);
  161. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  162. FetchV0Request request = parser.recvWants(pckIn);
  163. assertTrue(request.getClientCapabilities().isEmpty());
  164. assertThat(request.getWantIds(),
  165. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  166. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  167. assertThat(request.getClientShallowCommits(),
  168. hasOnlyObjectIds("4b643d0ef739a1b494e7d6926d8d8ed80d35edf4"));
  169. }
  170. @Test
  171. public void testRecvWantsFilter()
  172. throws PackProtocolException, IOException {
  173. PacketLineIn pckIn = formatAsPacketLine(
  174. "want 4624442d68ee402a94364191085b77137618633e\n",
  175. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  176. "filter blob:limit=13000\n",
  177. PacketLineIn.END);
  178. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  179. FetchV0Request request = parser.recvWants(pckIn);
  180. assertTrue(request.getClientCapabilities().isEmpty());
  181. assertThat(request.getWantIds(),
  182. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  183. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  184. assertEquals(13000, request.getFilterSpec().getBlobLimit());
  185. }
  186. }