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

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