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