1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 }