選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ProtocolV0ParserTest.java 6.6KB

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