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