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