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.assertSame;
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.transport.resolver.RepositoryResolver;
75  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
76  import org.eclipse.jgit.util.NB;
77  import org.junit.Before;
78  import org.junit.Test;
79  
80  public class ProtocolErrorTest extends HttpTestCase {
81  	private Repository remoteRepository;
82  
83  	private URIish remoteURI;
84  
85  	private RevBlob a_blob;
86  
87  	@Before
88  	public void setUp() throws Exception {
89  		super.setUp();
90  
91  		final TestRepository<Repository> src = createTestRepository();
92  		final String srcName = src.getRepository().getDirectory().getName();
93  
94  		ServletContextHandler app = server.addContext("/git");
95  		GitServlet gs = new GitServlet();
96  		gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
97  			public Repository open(HttpServletRequest req, String name)
98  					throws RepositoryNotFoundException,
99  					ServiceNotEnabledException {
100 				if (!name.equals(srcName))
101 					throw new RepositoryNotFoundException(name);
102 
103 				final Repository db = src.getRepository();
104 				db.incrementOpen();
105 				return db;
106 			}
107 		});
108 		app.addServlet(new ServletHolder(gs), "/*");
109 
110 		server.setUp();
111 
112 		remoteRepository = src.getRepository();
113 		remoteURI = toURIish(app, srcName);
114 
115 		StoredConfig cfg = remoteRepository.getConfig();
116 		cfg.setBoolean("http", null, "receivepack", true);
117 		cfg.save();
118 
119 		a_blob = src.blob("a");
120 	}
121 
122 	@Test
123 	public void testPush_UnpackError_TruncatedPack() throws Exception {
124 		StringBuilder sb = new StringBuilder();
125 		sb.append(ObjectId.zeroId().name());
126 		sb.append(' ');
127 		sb.append(a_blob.name());
128 		sb.append(' ');
129 		sb.append("refs/objects/A");
130 		sb.append('\0');
131 		sb.append("report-status");
132 
133 		ByteArrayOutputStream reqbuf = new ByteArrayOutputStream();
134 		PacketLineOut reqpck = new PacketLineOut(reqbuf);
135 		reqpck.writeString(sb.toString());
136 		reqpck.end();
137 
138 		packHeader(reqbuf, 1);
139 
140 		byte[] reqbin = reqbuf.toByteArray();
141 
142 		URL u = new URL(remoteURI.toString() + "/git-receive-pack");
143 		HttpURLConnection c = (HttpURLConnection) u.openConnection();
144 		try {
145 			c.setRequestMethod("POST");
146 			c.setDoOutput(true);
147 			c.setRequestProperty("Content-Type",
148 					GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE);
149 			c.setFixedLengthStreamingMode(reqbin.length);
150 			OutputStream out = c.getOutputStream();
151 			try {
152 				out.write(reqbin);
153 			} finally {
154 				out.close();
155 			}
156 
157 			assertEquals(200, c.getResponseCode());
158 			assertEquals(GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE,
159 					c.getContentType());
160 
161 			InputStream rawin = c.getInputStream();
162 			try {
163 				PacketLineIn pckin = new PacketLineIn(rawin);
164 				assertEquals("unpack error "
165 						+ JGitText.get().packfileIsTruncatedNoParam,
166 						pckin.readString());
167 				assertEquals("ng refs/objects/A n/a (unpacker error)",
168 						pckin.readString());
169 				assertSame(PacketLineIn.END, pckin.readString());
170 			} finally {
171 				rawin.close();
172 			}
173 		} finally {
174 			c.disconnect();
175 		}
176 	}
177 
178 	private static void packHeader(ByteArrayOutputStream tinyPack, int cnt)
179 			throws IOException {
180 		final byte[] hdr = new byte[8];
181 		NB.encodeInt32(hdr, 0, 2);
182 		NB.encodeInt32(hdr, 4, cnt);
183 
184 		tinyPack.write(Constants.PACK_SIGNATURE);
185 		tinyPack.write(hdr, 0, 8);
186 	}
187 }