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