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 	@Test
102 	public void testRecvWantsWithSessionID()
103 			throws PackProtocolException, IOException {
104 		PacketLineIn pckIn = formatAsPacketLine(String.join(" ", "want",
105 				"4624442d68ee402a94364191085b77137618633e", "thin-pack",
106 				"agent=JGit.test/0.0.1", "session-id=client-session-id", "\n"),
107 				"want f900c8326a43303685c46b279b9f70411bff1a4b\n",
108 				PacketLineIn.end());
109 		ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
110 		FetchV0Request request = parser.recvWants(pckIn);
111 		assertTrue(request.getClientCapabilities()
112 				.contains(GitProtocolConstants.OPTION_THIN_PACK));
113 		assertEquals(1, request.getClientCapabilities().size());
114 		assertEquals("client-session-id", request.getClientSID());
115 		assertThat(request.getWantIds(),
116 				hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
117 						"f900c8326a43303685c46b279b9f70411bff1a4b"));
118 	}
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 
139 	@Test
140 	public void testRecvWantsDeepen()
141 			throws PackProtocolException, IOException {
142 		PacketLineIn pckIn = formatAsPacketLine(
143 				"want 4624442d68ee402a94364191085b77137618633e\n",
144 				"want f900c8326a43303685c46b279b9f70411bff1a4b\n", "deepen 3\n",
145 				PacketLineIn.end());
146 		ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
147 		FetchV0Request request = parser.recvWants(pckIn);
148 		assertTrue(request.getClientCapabilities().isEmpty());
149 		assertEquals(3, request.getDepth());
150 		assertThat(request.getWantIds(),
151 				hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
152 						"f900c8326a43303685c46b279b9f70411bff1a4b"));
153 	}
154 
155 	@Test
156 	public void testRecvWantsDeepenSince()
157 			throws PackProtocolException, IOException {
158 		PacketLineIn pckIn = formatAsPacketLine(
159 				"want 4624442d68ee402a94364191085b77137618633e\n",
160 				"want f900c8326a43303685c46b279b9f70411bff1a4b\n",
161 				"deepen-since 1652773020\n",
162 				PacketLineIn.end());
163 		ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
164 		FetchV0Request request = parser.recvWants(pckIn);
165 		assertTrue(request.getClientCapabilities().isEmpty());
166 		assertEquals(1652773020, request.getDeepenSince());
167 		assertThat(request.getWantIds(),
168 				   hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
169 									"f900c8326a43303685c46b279b9f70411bff1a4b"));
170 	}
171 
172 	@Test
173 	public void testRecvWantsDeepenNots()
174 			throws PackProtocolException, IOException {
175 		PacketLineIn pckIn = formatAsPacketLine(
176 				"want 4624442d68ee402a94364191085b77137618633e\n",
177 				"want f900c8326a43303685c46b279b9f70411bff1a4b\n",
178 				"deepen-not 856d5138d7269a483efe276d4a6b5c25b4fbb1a4\n",
179 				"deepen-not heads/refs/test\n",
180 				PacketLineIn.end());
181 		ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
182 		FetchV0Request request = parser.recvWants(pckIn);
183 		assertTrue(request.getClientCapabilities().isEmpty());
184 		assertThat(request.getDeepenNots(), contains("856d5138d7269a483efe276d4a6b5c25b4fbb1a4",
185 													 "heads/refs/test"));
186 		assertThat(request.getWantIds(),
187 				   hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
188 									"f900c8326a43303685c46b279b9f70411bff1a4b"));
189 	}
190 
191 	@Test
192 	public void testRecvWantsShallow()
193 			throws PackProtocolException, IOException {
194 		PacketLineIn pckIn = formatAsPacketLine(
195 				"want 4624442d68ee402a94364191085b77137618633e\n",
196 				"want f900c8326a43303685c46b279b9f70411bff1a4b\n",
197 				"shallow 4b643d0ef739a1b494e7d6926d8d8ed80d35edf4\n",
198 				PacketLineIn.end());
199 		ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
200 		FetchV0Request request = parser.recvWants(pckIn);
201 		assertTrue(request.getClientCapabilities().isEmpty());
202 		assertThat(request.getWantIds(),
203 				hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
204 						"f900c8326a43303685c46b279b9f70411bff1a4b"));
205 		assertThat(request.getClientShallowCommits(),
206 				hasOnlyObjectIds("4b643d0ef739a1b494e7d6926d8d8ed80d35edf4"));
207 	}
208 
209 	@Test
210 	public void testRecvWantsFilter()
211 			throws PackProtocolException, IOException {
212 		PacketLineIn pckIn = formatAsPacketLine(
213 				"want 4624442d68ee402a94364191085b77137618633e\n",
214 				"want f900c8326a43303685c46b279b9f70411bff1a4b\n",
215 				"filter blob:limit=13000\n",
216 				PacketLineIn.end());
217 		ProtocolV0Parser parser = new ProtocolV0Parser(defaultConfig());
218 		FetchV0Request request = parser.recvWants(pckIn);
219 		assertTrue(request.getClientCapabilities().isEmpty());
220 		assertThat(request.getWantIds(),
221 				hasOnlyObjectIds("4624442d68ee402a94364191085b77137618633e",
222 						"f900c8326a43303685c46b279b9f70411bff1a4b"));
223 		assertTrue(request.getFilterSpec().allowsType(OBJ_BLOB));
224 		assertTrue(request.getFilterSpec().allowsType(OBJ_TREE));
225 		assertTrue(request.getFilterSpec().allowsType(OBJ_COMMIT));
226 		assertTrue(request.getFilterSpec().allowsType(OBJ_TAG));
227 		assertEquals(13000, request.getFilterSpec().getBlobLimit());
228 		assertEquals(-1, request.getFilterSpec().getTreeDepthLimit());
229 	}
230 
231 }