Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2018, Google LLC. 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.eclipse.jgit.transport.ObjectIdMatcher.hasOnlyObjectIds;
  12. import static org.hamcrest.MatcherAssert.assertThat;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import org.eclipse.jgit.errors.PackProtocolException;
  19. import org.eclipse.jgit.lib.Config;
  20. import org.junit.Test;
  21. public class ProtocolV0ParserTest {
  22. /*
  23. * Convert the input lines to the PacketLine that the parser reads.
  24. */
  25. private static PacketLineIn formatAsPacketLine(String... inputLines)
  26. throws IOException {
  27. ByteArrayOutputStream send = new ByteArrayOutputStream();
  28. PacketLineOut pckOut = new PacketLineOut(send);
  29. for (String line : inputLines) {
  30. if (PacketLineIn.isEnd(line)) {
  31. pckOut.end();
  32. } else if (PacketLineIn.isDelimiter(line)) {
  33. pckOut.writeDelim();
  34. } else {
  35. pckOut.writeString(line);
  36. }
  37. }
  38. return new PacketLineIn(new ByteArrayInputStream(send.toByteArray()));
  39. }
  40. private static TransferConfig defaultConfig() {
  41. Config rc = new Config();
  42. rc.setBoolean("uploadpack", null, "allowfilter", true);
  43. return new TransferConfig(rc);
  44. }
  45. @Test
  46. public void testRecvWantsWithCapabilities()
  47. throws PackProtocolException, IOException {
  48. PacketLineIn pckIn = formatAsPacketLine(
  49. String.join(" ", "want",
  50. "4624442d68ee402a94364191085b77137618633e", "thin-pack",
  51. "no-progress", "include-tag", "ofs-delta", "\n"),
  52. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  53. PacketLineIn.end());
  54. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  55. FetchV0Request request = parser.recvWants(pckIn);
  56. assertTrue(request.getClientCapabilities()
  57. .contains(GitProtocolConstants.OPTION_THIN_PACK));
  58. assertTrue(request.getClientCapabilities()
  59. .contains(GitProtocolConstants.OPTION_NO_PROGRESS));
  60. assertTrue(request.getClientCapabilities()
  61. .contains(GitProtocolConstants.OPTION_INCLUDE_TAG));
  62. assertTrue(request.getClientCapabilities()
  63. .contains(GitProtocolConstants.CAPABILITY_OFS_DELTA));
  64. assertThat(request.getWantIds(),
  65. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  66. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  67. }
  68. @Test
  69. public void testRecvWantsWithAgent()
  70. throws PackProtocolException, IOException {
  71. PacketLineIn pckIn = formatAsPacketLine(
  72. String.join(" ", "want",
  73. "4624442d68ee402a94364191085b77137618633e", "thin-pack",
  74. "agent=JGit.test/0.0.1", "\n"),
  75. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  76. PacketLineIn.end());
  77. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  78. FetchV0Request request = parser.recvWants(pckIn);
  79. assertTrue(request.getClientCapabilities()
  80. .contains(GitProtocolConstants.OPTION_THIN_PACK));
  81. assertEquals(1, request.getClientCapabilities().size());
  82. assertEquals("JGit.test/0.0.1", request.getAgent());
  83. assertThat(request.getWantIds(),
  84. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  85. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  86. }
  87. /*
  88. * First round of protocol v0 negotiation. Client send wants, no
  89. * capabilities.
  90. */
  91. @Test
  92. public void testRecvWantsWithoutCapabilities()
  93. throws PackProtocolException, IOException {
  94. PacketLineIn pckIn = formatAsPacketLine(
  95. "want 4624442d68ee402a94364191085b77137618633e\n",
  96. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  97. PacketLineIn.end());
  98. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  99. FetchV0Request request = parser.recvWants(pckIn);
  100. assertTrue(request.getClientCapabilities().isEmpty());
  101. assertThat(request.getWantIds(),
  102. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  103. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  104. }
  105. @Test
  106. public void testRecvWantsDeepen()
  107. throws PackProtocolException, IOException {
  108. PacketLineIn pckIn = formatAsPacketLine(
  109. "want 4624442d68ee402a94364191085b77137618633e\n",
  110. "want f900c8326a43303685c46b279b9f70411bff1a4b\n", "deepen 3\n",
  111. PacketLineIn.end());
  112. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  113. FetchV0Request request = parser.recvWants(pckIn);
  114. assertTrue(request.getClientCapabilities().isEmpty());
  115. assertEquals(3, request.getDepth());
  116. assertThat(request.getWantIds(),
  117. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  118. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  119. }
  120. @Test
  121. public void testRecvWantsShallow()
  122. throws PackProtocolException, IOException {
  123. PacketLineIn pckIn = formatAsPacketLine(
  124. "want 4624442d68ee402a94364191085b77137618633e\n",
  125. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  126. "shallow 4b643d0ef739a1b494e7d6926d8d8ed80d35edf4\n",
  127. PacketLineIn.end());
  128. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  129. FetchV0Request request = parser.recvWants(pckIn);
  130. assertTrue(request.getClientCapabilities().isEmpty());
  131. assertThat(request.getWantIds(),
  132. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  133. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  134. assertThat(request.getClientShallowCommits(),
  135. hasOnlyObjectIds("4b643d0ef739a1b494e7d6926d8d8ed80d35edf4"));
  136. }
  137. @Test
  138. public void testRecvWantsFilter()
  139. throws PackProtocolException, IOException {
  140. PacketLineIn pckIn = formatAsPacketLine(
  141. "want 4624442d68ee402a94364191085b77137618633e\n",
  142. "want f900c8326a43303685c46b279b9f70411bff1a4b\n",
  143. "filter blob:limit=13000\n",
  144. PacketLineIn.end());
  145. ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
  146. FetchV0Request request = parser.recvWants(pckIn);
  147. assertTrue(request.getClientCapabilities().isEmpty());
  148. assertThat(request.getWantIds(),
  149. hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
  150. "f900c8326a43303685c46b279b9f70411bff1a4b"));
  151. assertEquals(13000, request.getFilterSpec().getBlobLimit());
  152. assertEquals(-1, request.getFilterSpec().getTreeDepthLimit());
  153. }
  154. }