You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PackFileTest.java 12KB

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