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.

PackParserTest.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Imran M Yousuf <imyousuf@smartitengineering.com>
  4. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.transport;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.File;
  50. import java.io.FileInputStream;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.security.MessageDigest;
  54. import java.util.zip.Deflater;
  55. import org.eclipse.jgit.junit.JGitTestUtil;
  56. import org.eclipse.jgit.junit.TestRepository;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.NullProgressMonitor;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.ObjectInserter;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.lib.RepositoryTestCase;
  63. import org.eclipse.jgit.revwalk.RevBlob;
  64. import org.eclipse.jgit.storage.file.ObjectDirectoryPackParser;
  65. import org.eclipse.jgit.storage.file.PackFile;
  66. import org.eclipse.jgit.util.NB;
  67. import org.eclipse.jgit.util.TemporaryBuffer;
  68. import org.junit.After;
  69. import org.junit.Test;
  70. /**
  71. * Test indexing of git packs. A pack is read from a stream, copied
  72. * to a new pack and an index is created. Then the packs are tested
  73. * to make sure they contain the expected objects (well we don't test
  74. * for all of them unless the packs are very small).
  75. */
  76. public class PackParserTest extends RepositoryTestCase {
  77. /**
  78. * Test indexing one of the test packs in the egit repo. It has deltas.
  79. *
  80. * @throws IOException
  81. */
  82. @Test
  83. public void test1() throws IOException {
  84. File packFile = JGitTestUtil.getTestResourceFile("pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack");
  85. final InputStream is = new FileInputStream(packFile);
  86. try {
  87. ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(is);
  88. p.parse(NullProgressMonitor.INSTANCE);
  89. PackFile file = p.getPackFile();
  90. assertTrue(file.hasObject(ObjectId.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904")));
  91. assertTrue(file.hasObject(ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab")));
  92. assertTrue(file.hasObject(ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259")));
  93. assertTrue(file.hasObject(ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3")));
  94. assertTrue(file.hasObject(ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7")));
  95. assertTrue(file.hasObject(ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327")));
  96. assertTrue(file.hasObject(ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035")));
  97. assertTrue(file.hasObject(ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799")));
  98. } finally {
  99. is.close();
  100. }
  101. }
  102. /**
  103. * This is just another pack. It so happens that we have two convenient pack to
  104. * test with in the repository.
  105. *
  106. * @throws IOException
  107. */
  108. @Test
  109. public void test2() throws IOException {
  110. File packFile = JGitTestUtil.getTestResourceFile("pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack");
  111. final InputStream is = new FileInputStream(packFile);
  112. try {
  113. ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(is);
  114. p.parse(NullProgressMonitor.INSTANCE);
  115. PackFile file = p.getPackFile();
  116. assertTrue(file.hasObject(ObjectId.fromString("02ba32d3649e510002c21651936b7077aa75ffa9")));
  117. assertTrue(file.hasObject(ObjectId.fromString("0966a434eb1a025db6b71485ab63a3bfbea520b6")));
  118. assertTrue(file.hasObject(ObjectId.fromString("09efc7e59a839528ac7bda9fa020dc9101278680")));
  119. assertTrue(file.hasObject(ObjectId.fromString("0a3d7772488b6b106fb62813c4d6d627918d9181")));
  120. assertTrue(file.hasObject(ObjectId.fromString("1004d0d7ac26fbf63050a234c9b88a46075719d3")));
  121. assertTrue(file.hasObject(ObjectId.fromString("10da5895682013006950e7da534b705252b03be6")));
  122. assertTrue(file.hasObject(ObjectId.fromString("1203b03dc816ccbb67773f28b3c19318654b0bc8")));
  123. assertTrue(file.hasObject(ObjectId.fromString("15fae9e651043de0fd1deef588aa3fbf5a7a41c6")));
  124. assertTrue(file.hasObject(ObjectId.fromString("16f9ec009e5568c435f473ba3a1df732d49ce8c3")));
  125. assertTrue(file.hasObject(ObjectId.fromString("1fd7d579fb6ae3fe942dc09c2c783443d04cf21e")));
  126. assertTrue(file.hasObject(ObjectId.fromString("20a8ade77639491ea0bd667bf95de8abf3a434c8")));
  127. assertTrue(file.hasObject(ObjectId.fromString("2675188fd86978d5bc4d7211698b2118ae3bf658")));
  128. // and lots more...
  129. } finally {
  130. is.close();
  131. }
  132. }
  133. @Test
  134. public void testTinyThinPack() throws Exception {
  135. TestRepository d = new TestRepository(db);
  136. RevBlob a = d.blob("a");
  137. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  138. packHeader(pack, 1);
  139. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  140. a.copyRawTo(pack);
  141. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  142. digest(pack);
  143. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  144. p.setAllowThin(true);
  145. p.parse(NullProgressMonitor.INSTANCE);
  146. }
  147. @Test
  148. public void testPackWithDuplicateBlob() throws Exception {
  149. final byte[] data = Constants.encode("0123456789abcdefg");
  150. TestRepository<Repository> d = new TestRepository<Repository>(db);
  151. assertTrue(db.hasObject(d.blob(data)));
  152. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  153. packHeader(pack, 1);
  154. pack.write((Constants.OBJ_BLOB) << 4 | 0x80 | 1);
  155. pack.write(1);
  156. deflate(pack, data);
  157. digest(pack);
  158. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  159. p.setAllowThin(false);
  160. p.parse(NullProgressMonitor.INSTANCE);
  161. }
  162. private void packHeader(TemporaryBuffer.Heap tinyPack, int cnt)
  163. throws IOException {
  164. final byte[] hdr = new byte[8];
  165. NB.encodeInt32(hdr, 0, 2);
  166. NB.encodeInt32(hdr, 4, cnt);
  167. tinyPack.write(Constants.PACK_SIGNATURE);
  168. tinyPack.write(hdr, 0, 8);
  169. }
  170. private void deflate(TemporaryBuffer.Heap tinyPack, final byte[] content)
  171. throws IOException {
  172. final Deflater deflater = new Deflater();
  173. final byte[] buf = new byte[128];
  174. deflater.setInput(content, 0, content.length);
  175. deflater.finish();
  176. do {
  177. final int n = deflater.deflate(buf, 0, buf.length);
  178. if (n > 0)
  179. tinyPack.write(buf, 0, n);
  180. } while (!deflater.finished());
  181. }
  182. private void digest(TemporaryBuffer.Heap buf) throws IOException {
  183. MessageDigest md = Constants.newMessageDigest();
  184. md.update(buf.toByteArray());
  185. buf.write(md.digest());
  186. }
  187. private ObjectInserter inserter;
  188. @After
  189. public void release() {
  190. if (inserter != null)
  191. inserter.release();
  192. }
  193. private PackParser index(InputStream in) throws IOException {
  194. if (inserter == null)
  195. inserter = db.newObjectInserter();
  196. return inserter.newPackParser(in);
  197. }
  198. }