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 13KB

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