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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.close();
  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. try (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. }
  222. @Test
  223. public void testDelta_FailsOver2GiB() throws Exception {
  224. try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) {
  225. byte[] base = new byte[] { 'a' };
  226. ObjectId idA = fmt.idFor(Constants.OBJ_BLOB, base);
  227. ObjectId idB = fmt.idFor(Constants.OBJ_BLOB, new byte[] { 'b' });
  228. PackedObjectInfo a = new PackedObjectInfo(idA);
  229. PackedObjectInfo b = new PackedObjectInfo(idB);
  230. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024);
  231. packHeader(pack, 2);
  232. a.setOffset(pack.length());
  233. objectHeader(pack, Constants.OBJ_BLOB, base.length);
  234. deflate(pack, base);
  235. ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  236. DeltaEncoder de = new DeltaEncoder(tmp, base.length, 3L << 30);
  237. de.copy(0, 1);
  238. byte[] delta = tmp.toByteArray();
  239. b.setOffset(pack.length());
  240. objectHeader(pack, Constants.OBJ_REF_DELTA, delta.length);
  241. idA.copyRawTo(pack);
  242. deflate(pack, delta);
  243. byte[] footer = digest(pack);
  244. File dir = new File(repo.getObjectDatabase().getDirectory(),
  245. "pack");
  246. File packName = new File(dir, idA.name() + ".pack");
  247. File idxName = new File(dir, idA.name() + ".idx");
  248. FileOutputStream f = new FileOutputStream(packName);
  249. try {
  250. f.write(pack.toByteArray());
  251. } finally {
  252. f.close();
  253. }
  254. f = new FileOutputStream(idxName);
  255. try {
  256. List<PackedObjectInfo> list = new ArrayList<PackedObjectInfo>();
  257. list.add(a);
  258. list.add(b);
  259. Collections.sort(list);
  260. new PackIndexWriterV1(f).write(list, footer);
  261. } finally {
  262. f.close();
  263. }
  264. PackFile packFile = new PackFile(packName, PackExt.INDEX.getBit());
  265. try {
  266. packFile.get(wc, b);
  267. fail("expected LargeObjectException.ExceedsByteArrayLimit");
  268. } catch (LargeObjectException.ExceedsByteArrayLimit bad) {
  269. assertNull(bad.getObjectId());
  270. } finally {
  271. packFile.close();
  272. }
  273. }
  274. }
  275. private static byte[] clone(int first, byte[] base) {
  276. byte[] r = new byte[base.length];
  277. System.arraycopy(base, 1, r, 1, r.length - 1);
  278. r[0] = (byte) first;
  279. return r;
  280. }
  281. private static byte[] delta(byte[] base, byte[] dest) throws IOException {
  282. ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  283. DeltaEncoder de = new DeltaEncoder(tmp, base.length, dest.length);
  284. de.insert(dest, 0, 1);
  285. de.copy(1, base.length - 1);
  286. return tmp.toByteArray();
  287. }
  288. private static void packHeader(TemporaryBuffer.Heap pack, int cnt)
  289. throws IOException {
  290. final byte[] hdr = new byte[8];
  291. NB.encodeInt32(hdr, 0, 2);
  292. NB.encodeInt32(hdr, 4, cnt);
  293. pack.write(Constants.PACK_SIGNATURE);
  294. pack.write(hdr, 0, 8);
  295. }
  296. private static void objectHeader(TemporaryBuffer.Heap pack, int type, int sz)
  297. throws IOException {
  298. byte[] buf = new byte[8];
  299. int nextLength = sz >>> 4;
  300. buf[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (sz & 0x0F));
  301. sz = nextLength;
  302. int n = 1;
  303. while (sz > 0) {
  304. nextLength >>>= 7;
  305. buf[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (sz & 0x7F));
  306. sz = nextLength;
  307. }
  308. pack.write(buf, 0, n);
  309. }
  310. private static void deflate(TemporaryBuffer.Heap pack, final byte[] content)
  311. throws IOException {
  312. final Deflater deflater = new Deflater();
  313. final byte[] buf = new byte[128];
  314. deflater.setInput(content, 0, content.length);
  315. deflater.finish();
  316. do {
  317. final int n = deflater.deflate(buf, 0, buf.length);
  318. if (n > 0)
  319. pack.write(buf, 0, n);
  320. } while (!deflater.finished());
  321. deflater.end();
  322. }
  323. private static byte[] digest(TemporaryBuffer.Heap buf)
  324. throws IOException {
  325. MessageDigest md = Constants.newMessageDigest();
  326. md.update(buf.toByteArray());
  327. byte[] footer = md.digest();
  328. buf.write(footer);
  329. return footer;
  330. }
  331. private ObjectInserter inserter;
  332. @After
  333. public void release() {
  334. if (inserter != null) {
  335. inserter.close();
  336. }
  337. }
  338. private PackParser index(byte[] raw) throws IOException {
  339. if (inserter == null)
  340. inserter = repo.newObjectInserter();
  341. return inserter.newPackParser(new ByteArrayInputStream(raw));
  342. }
  343. }