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

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