View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.http.test;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.IOException;
51  import java.io.InputStream;
52  import java.io.OutputStream;
53  import java.net.HttpURLConnection;
54  import java.net.URL;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  import org.eclipse.jetty.servlet.ServletContextHandler;
59  import org.eclipse.jetty.servlet.ServletHolder;
60  import org.eclipse.jgit.errors.RepositoryNotFoundException;
61  import org.eclipse.jgit.http.server.GitServlet;
62  import org.eclipse.jgit.http.server.GitSmartHttpTools;
63  import org.eclipse.jgit.internal.JGitText;
64  import org.eclipse.jgit.junit.TestRepository;
65  import org.eclipse.jgit.junit.http.HttpTestCase;
66  import org.eclipse.jgit.lib.Constants;
67  import org.eclipse.jgit.lib.ObjectId;
68  import org.eclipse.jgit.lib.Repository;
69  import org.eclipse.jgit.lib.StoredConfig;
70  import org.eclipse.jgit.revwalk.RevBlob;
71  import org.eclipse.jgit.transport.PacketLineIn;
72  import org.eclipse.jgit.transport.PacketLineOut;
73  import org.eclipse.jgit.transport.URIish;
74  import org.eclipse.jgit.util.NB;
75  import org.junit.Before;
76  import org.junit.Test;
77  
78  public class ProtocolErrorTest extends HttpTestCase {
79  	private Repository remoteRepository;
80  
81  	private URIish remoteURI;
82  
83  	private RevBlob a_blob;
84  
85  	@Override
86  	@Before
87  	public void setUp() throws Exception {
88  		super.setUp();
89  
90  		final TestRepository<Repository> src = createTestRepository();
91  		final String srcName = src.getRepository().getDirectory().getName();
92  
93  		ServletContextHandler app = server.addContext("/git");
94  		GitServlet gs = new GitServlet();
95  		gs.setRepositoryResolver((HttpServletRequest req, String name) -> {
96  			if (!name.equals(srcName)) {
97  				throw new RepositoryNotFoundException(name);
98  			}
99  			final Repository db = src.getRepository();
100 			db.incrementOpen();
101 			return db;
102 		});
103 		app.addServlet(new ServletHolder(gs), "/*");
104 
105 		server.setUp();
106 
107 		remoteRepository = src.getRepository();
108 		remoteURI = toURIish(app, srcName);
109 
110 		StoredConfig cfg = remoteRepository.getConfig();
111 		cfg.setBoolean("http", null, "receivepack", true);
112 		cfg.save();
113 
114 		a_blob = src.blob("a");
115 	}
116 
117 	@Test
118 	public void testPush_UnpackError_TruncatedPack() throws Exception {
119 		StringBuilder sb = new StringBuilder();
120 		sb.append(ObjectId.zeroId().name());
121 		sb.append(' ');
122 		sb.append(a_blob.name());
123 		sb.append(' ');
124 		sb.append("refs/objects/A");
125 		sb.append('\0');
126 		sb.append("report-status");
127 
128 		ByteArrayOutputStream reqbuf = new ByteArrayOutputStream();
129 		PacketLineOut reqpck = new PacketLineOut(reqbuf);
130 		reqpck.writeString(sb.toString());
131 		reqpck.end();
132 
133 		packHeader(reqbuf, 1);
134 
135 		byte[] reqbin = reqbuf.toByteArray();
136 
137 		URL u = new URL(remoteURI.toString() + "/git-receive-pack");
138 		HttpURLConnection c = (HttpURLConnection) u.openConnection();
139 		try {
140 			c.setRequestMethod("POST");
141 			c.setDoOutput(true);
142 			c.setRequestProperty("Content-Type",
143 					GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE);
144 			c.setFixedLengthStreamingMode(reqbin.length);
145 			try (OutputStream out = c.getOutputStream()) {
146 				out.write(reqbin);
147 			}
148 
149 			assertEquals(200, c.getResponseCode());
150 			assertEquals(GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE,
151 					c.getContentType());
152 
153 			try (InputStream rawin = c.getInputStream()) {
154 				PacketLineIn pckin = new PacketLineIn(rawin);
155 				assertEquals("unpack error "
156 						+ JGitText.get().packfileIsTruncatedNoParam,
157 						pckin.readString());
158 				assertEquals("ng refs/objects/A n/a (unpacker error)",
159 						pckin.readString());
160 				assertTrue(PacketLineIn.isEnd(pckin.readString()));
161 			}
162 		} finally {
163 			c.disconnect();
164 		}
165 	}
166 
167 	private static void packHeader(ByteArrayOutputStream tinyPack, int cnt)
168 			throws IOException {
169 		final byte[] hdr = new byte[8];
170 		NB.encodeInt32(hdr, 0, 2);
171 		NB.encodeInt32(hdr, 4, cnt);
172 
173 		tinyPack.write(Constants.PACK_SIGNATURE);
174 		tinyPack.write(hdr, 0, 8);
175 	}
176 }