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.internal.storage.file;
45
46 import static org.junit.Assert.assertArrayEquals;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertNull;
51 import static org.junit.Assert.assertTrue;
52 import static org.junit.Assert.fail;
53
54 import java.io.ByteArrayInputStream;
55 import java.io.ByteArrayOutputStream;
56 import java.io.File;
57 import java.io.FileOutputStream;
58 import java.io.IOException;
59 import java.security.MessageDigest;
60 import java.text.MessageFormat;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.Collections;
64 import java.util.List;
65 import java.util.zip.Deflater;
66
67 import org.eclipse.jgit.errors.LargeObjectException;
68 import org.eclipse.jgit.internal.JGitText;
69 import org.eclipse.jgit.internal.storage.pack.DeltaEncoder;
70 import org.eclipse.jgit.internal.storage.pack.PackExt;
71 import org.eclipse.jgit.junit.JGitTestUtil;
72 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
73 import org.eclipse.jgit.junit.TestRepository;
74 import org.eclipse.jgit.junit.TestRng;
75 import org.eclipse.jgit.lib.Constants;
76 import org.eclipse.jgit.lib.NullProgressMonitor;
77 import org.eclipse.jgit.lib.ObjectId;
78 import org.eclipse.jgit.lib.ObjectInserter;
79 import org.eclipse.jgit.lib.ObjectLoader;
80 import org.eclipse.jgit.lib.ObjectStream;
81 import org.eclipse.jgit.lib.Repository;
82 import org.eclipse.jgit.revwalk.RevBlob;
83 import org.eclipse.jgit.storage.file.WindowCacheConfig;
84 import org.eclipse.jgit.transport.PackParser;
85 import org.eclipse.jgit.transport.PackedObjectInfo;
86 import org.eclipse.jgit.util.IO;
87 import org.eclipse.jgit.util.NB;
88 import org.eclipse.jgit.util.TemporaryBuffer;
89 import org.junit.After;
90 import org.junit.Before;
91 import org.junit.Test;
92
93 public class PackFileTest extends LocalDiskRepositoryTestCase {
94 private int streamThreshold = 16 * 1024;
95
96 private TestRng rng;
97
98 private FileRepository repo;
99
100 private TestRepository<Repository> tr;
101
102 private WindowCursor wc;
103
104 private TestRng getRng() {
105 if (rng == null)
106 rng = new TestRng(JGitTestUtil.getName());
107 return rng;
108 }
109
110 @Before
111 public void setUp() throws Exception {
112 super.setUp();
113
114 WindowCacheConfig cfg = new WindowCacheConfig();
115 cfg.setStreamFileThreshold(streamThreshold);
116 cfg.install();
117
118 repo = createBareRepository();
119 tr = new TestRepository<Repository>(repo);
120 wc = (WindowCursor) repo.newObjectReader();
121 }
122
123 @After
124 public void tearDown() throws Exception {
125 if (wc != null)
126 wc.close();
127 new WindowCacheConfig().install();
128 super.tearDown();
129 }
130
131 @Test
132 public void testWhole_SmallObject() throws Exception {
133 final int type = Constants.OBJ_BLOB;
134 byte[] data = getRng().nextBytes(300);
135 RevBlob id = tr.blob(data);
136 tr.branch("master").commit().add("A", id).create();
137 tr.packAndPrune();
138 assertTrue("has blob", wc.has(id));
139
140 ObjectLoader ol = wc.open(id);
141 assertNotNull("created loader", ol);
142 assertEquals(type, ol.getType());
143 assertEquals(data.length, ol.getSize());
144 assertFalse("is not large", ol.isLarge());
145 assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
146
147 ObjectStream in = ol.openStream();
148 assertNotNull("have stream", in);
149 assertEquals(type, in.getType());
150 assertEquals(data.length, in.getSize());
151 byte[] data2 = new byte[data.length];
152 IO.readFully(in, data2, 0, data.length);
153 assertTrue("same content", Arrays.equals(data2, data));
154 assertEquals("stream at EOF", -1, in.read());
155 in.close();
156 }
157
158 @Test
159 public void testWhole_LargeObject() throws Exception {
160 final int type = Constants.OBJ_BLOB;
161 byte[] data = getRng().nextBytes(streamThreshold + 5);
162 RevBlob id = tr.blob(data);
163 tr.branch("master").commit().add("A", id).create();
164 tr.packAndPrune();
165 assertTrue("has blob", wc.has(id));
166
167 ObjectLoader ol = wc.open(id);
168 assertNotNull("created loader", ol);
169 assertEquals(type, ol.getType());
170 assertEquals(data.length, ol.getSize());
171 assertTrue("is large", ol.isLarge());
172 try {
173 ol.getCachedBytes();
174 fail("Should have thrown LargeObjectException");
175 } catch (LargeObjectException tooBig) {
176 assertEquals(MessageFormat.format(
177 JGitText.get().largeObjectException, id.name()), tooBig
178 .getMessage());
179 }
180
181 ObjectStream in = ol.openStream();
182 assertNotNull("have stream", in);
183 assertEquals(type, in.getType());
184 assertEquals(data.length, in.getSize());
185 byte[] data2 = new byte[data.length];
186 IO.readFully(in, data2, 0, data.length);
187 assertTrue("same content", Arrays.equals(data2, data));
188 assertEquals("stream at EOF", -1, in.read());
189 in.close();
190 }
191
192 @Test
193 public void testDelta_SmallObjectChain() throws Exception {
194 try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) {
195 byte[] data0 = new byte[512];
196 Arrays.fill(data0, (byte) 0xf3);
197 ObjectId id0 = fmt.idFor(Constants.OBJ_BLOB, data0);
198
199 TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024);
200 packHeader(pack, 4);
201 objectHeader(pack, Constants.OBJ_BLOB, data0.length);
202 deflate(pack, data0);
203
204 byte[] data1 = clone(0x01, data0);
205 byte[] delta1 = delta(data0, data1);
206 ObjectId id1 = fmt.idFor(Constants.OBJ_BLOB, data1);
207 objectHeader(pack, Constants.OBJ_REF_DELTA, delta1.length);
208 id0.copyRawTo(pack);
209 deflate(pack, delta1);
210
211 byte[] data2 = clone(0x02, data1);
212 byte[] delta2 = delta(data1, data2);
213 ObjectId id2 = fmt.idFor(Constants.OBJ_BLOB, data2);
214 objectHeader(pack, Constants.OBJ_REF_DELTA, delta2.length);
215 id1.copyRawTo(pack);
216 deflate(pack, delta2);
217
218 byte[] data3 = clone(0x03, data2);
219 byte[] delta3 = delta(data2, data3);
220 ObjectId id3 = fmt.idFor(Constants.OBJ_BLOB, data3);
221 objectHeader(pack, Constants.OBJ_REF_DELTA, delta3.length);
222 id2.copyRawTo(pack);
223 deflate(pack, delta3);
224
225 digest(pack);
226 PackParser ip = index(pack.toByteArray());
227 ip.setAllowThin(true);
228 ip.parse(NullProgressMonitor.INSTANCE);
229
230 assertTrue("has blob", wc.has(id3));
231
232 ObjectLoader ol = wc.open(id3);
233 assertNotNull("created loader", ol);
234 assertEquals(Constants.OBJ_BLOB, ol.getType());
235 assertEquals(data3.length, ol.getSize());
236 assertFalse("is large", ol.isLarge());
237 assertNotNull(ol.getCachedBytes());
238 assertArrayEquals(data3, ol.getCachedBytes());
239
240 ObjectStream in = ol.openStream();
241 assertNotNull("have stream", in);
242 assertEquals(Constants.OBJ_BLOB, in.getType());
243 assertEquals(data3.length, in.getSize());
244 byte[] act = new byte[data3.length];
245 IO.readFully(in, act, 0, data3.length);
246 assertTrue("same content", Arrays.equals(act, data3));
247 assertEquals("stream at EOF", -1, in.read());
248 in.close();
249 }
250 }
251
252 @Test
253 public void testDelta_FailsOver2GiB() throws Exception {
254 try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) {
255 byte[] base = new byte[] { 'a' };
256 ObjectId idA = fmt.idFor(Constants.OBJ_BLOB, base);
257 ObjectId idB = fmt.idFor(Constants.OBJ_BLOB, new byte[] { 'b' });
258
259 PackedObjectInfo a = new PackedObjectInfo(idA);
260 PackedObjectInfo b = new PackedObjectInfo(idB);
261
262 TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024);
263 packHeader(pack, 2);
264 a.setOffset(pack.length());
265 objectHeader(pack, Constants.OBJ_BLOB, base.length);
266 deflate(pack, base);
267
268 ByteArrayOutputStream tmp = new ByteArrayOutputStream();
269 DeltaEncoder de = new DeltaEncoder(tmp, base.length, 3L << 30);
270 de.copy(0, 1);
271 byte[] delta = tmp.toByteArray();
272 b.setOffset(pack.length());
273 objectHeader(pack, Constants.OBJ_REF_DELTA, delta.length);
274 idA.copyRawTo(pack);
275 deflate(pack, delta);
276 byte[] footer = digest(pack);
277
278 File dir = new File(repo.getObjectDatabase().getDirectory(),
279 "pack");
280 File packName = new File(dir, idA.name() + ".pack");
281 File idxName = new File(dir, idA.name() + ".idx");
282
283 FileOutputStream f = new FileOutputStream(packName);
284 try {
285 f.write(pack.toByteArray());
286 } finally {
287 f.close();
288 }
289
290 f = new FileOutputStream(idxName);
291 try {
292 List<PackedObjectInfo> list = new ArrayList<PackedObjectInfo>();
293 list.add(a);
294 list.add(b);
295 Collections.sort(list);
296 new PackIndexWriterV1(f).write(list, footer);
297 } finally {
298 f.close();
299 }
300
301 PackFile packFile = new PackFile(packName, PackExt.INDEX.getBit());
302 try {
303 packFile.get(wc, b);
304 fail("expected LargeObjectException.ExceedsByteArrayLimit");
305 } catch (LargeObjectException.ExceedsByteArrayLimit bad) {
306 assertNull(bad.getObjectId());
307 } finally {
308 packFile.close();
309 }
310 }
311 }
312
313 @Test
314 public void testConfigurableStreamFileThreshold() throws Exception {
315 byte[] data = getRng().nextBytes(300);
316 RevBlob id = tr.blob(data);
317 tr.branch("master").commit().add("A", id).create();
318 tr.packAndPrune();
319 assertTrue("has blob", wc.has(id));
320
321 ObjectLoader ol = wc.open(id);
322 ObjectStream in = ol.openStream();
323 assertTrue(in instanceof ObjectStream.SmallStream);
324 assertEquals(300, in.available());
325 in.close();
326
327 wc.setStreamFileThreshold(299);
328 ol = wc.open(id);
329 in = ol.openStream();
330 assertTrue(in instanceof ObjectStream.Filter);
331 assertEquals(1, in.available());
332 }
333
334 private static byte[] clone(int first, byte[] base) {
335 byte[] r = new byte[base.length];
336 System.arraycopy(base, 1, r, 1, r.length - 1);
337 r[0] = (byte) first;
338 return r;
339 }
340
341 private static byte[] delta(byte[] base, byte[] dest) throws IOException {
342 ByteArrayOutputStream tmp = new ByteArrayOutputStream();
343 DeltaEncoder de = new DeltaEncoder(tmp, base.length, dest.length);
344 de.insert(dest, 0, 1);
345 de.copy(1, base.length - 1);
346 return tmp.toByteArray();
347 }
348
349 private static void packHeader(TemporaryBuffer.Heap pack, int cnt)
350 throws IOException {
351 final byte[] hdr = new byte[8];
352 NB.encodeInt32(hdr, 0, 2);
353 NB.encodeInt32(hdr, 4, cnt);
354 pack.write(Constants.PACK_SIGNATURE);
355 pack.write(hdr, 0, 8);
356 }
357
358 private static void objectHeader(TemporaryBuffer.Heap pack, int type, int sz)
359 throws IOException {
360 byte[] buf = new byte[8];
361 int nextLength = sz >>> 4;
362 buf[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (sz & 0x0F));
363 sz = nextLength;
364 int n = 1;
365 while (sz > 0) {
366 nextLength >>>= 7;
367 buf[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (sz & 0x7F));
368 sz = nextLength;
369 }
370 pack.write(buf, 0, n);
371 }
372
373 private static void deflate(TemporaryBuffer.Heap pack, final byte[] content)
374 throws IOException {
375 final Deflater deflater = new Deflater();
376 final byte[] buf = new byte[128];
377 deflater.setInput(content, 0, content.length);
378 deflater.finish();
379 do {
380 final int n = deflater.deflate(buf, 0, buf.length);
381 if (n > 0)
382 pack.write(buf, 0, n);
383 } while (!deflater.finished());
384 deflater.end();
385 }
386
387 private static byte[] digest(TemporaryBuffer.Heap buf)
388 throws IOException {
389 MessageDigest md = Constants.newMessageDigest();
390 md.update(buf.toByteArray());
391 byte[] footer = md.digest();
392 buf.write(footer);
393 return footer;
394 }
395
396 private ObjectInserter inserter;
397
398 @After
399 public void release() {
400 if (inserter != null) {
401 inserter.close();
402 }
403 }
404
405 private PackParser index(byte[] raw) throws IOException {
406 if (inserter == null)
407 inserter = repo.newObjectInserter();
408 return inserter.newPackParser(new ByteArrayInputStream(raw));
409 }
410 }