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 @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 }