Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PackParserTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.assertEquals;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertTrue;
  50. import static org.junit.Assert.fail;
  51. import java.io.ByteArrayInputStream;
  52. import java.io.File;
  53. import java.io.FileInputStream;
  54. import java.io.IOException;
  55. import java.io.InputStream;
  56. import java.security.MessageDigest;
  57. import java.text.MessageFormat;
  58. import java.util.zip.Deflater;
  59. import org.eclipse.jgit.errors.TooLargeObjectInPackException;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.internal.storage.file.ObjectDirectoryPackParser;
  62. import org.eclipse.jgit.internal.storage.file.PackFile;
  63. import org.eclipse.jgit.junit.JGitTestUtil;
  64. import org.eclipse.jgit.junit.RepositoryTestCase;
  65. import org.eclipse.jgit.junit.TestRepository;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.NullProgressMonitor;
  68. import org.eclipse.jgit.lib.ObjectId;
  69. import org.eclipse.jgit.lib.ObjectInserter;
  70. import org.eclipse.jgit.lib.Repository;
  71. import org.eclipse.jgit.revwalk.RevBlob;
  72. import org.eclipse.jgit.util.NB;
  73. import org.eclipse.jgit.util.TemporaryBuffer;
  74. import org.eclipse.jgit.util.io.UnionInputStream;
  75. import org.junit.After;
  76. import org.junit.Test;
  77. /**
  78. * Test indexing of git packs. A pack is read from a stream, copied
  79. * to a new pack and an index is created. Then the packs are tested
  80. * to make sure they contain the expected objects (well we don't test
  81. * for all of them unless the packs are very small).
  82. */
  83. public class PackParserTest extends RepositoryTestCase {
  84. /**
  85. * Test indexing one of the test packs in the egit repo. It has deltas.
  86. *
  87. * @throws IOException
  88. */
  89. @Test
  90. public void test1() throws IOException {
  91. File packFile = JGitTestUtil.getTestResourceFile("pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack");
  92. final InputStream is = new FileInputStream(packFile);
  93. try {
  94. ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(is);
  95. p.parse(NullProgressMonitor.INSTANCE);
  96. PackFile file = p.getPackFile();
  97. assertTrue(file.hasObject(ObjectId.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904")));
  98. assertTrue(file.hasObject(ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab")));
  99. assertTrue(file.hasObject(ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259")));
  100. assertTrue(file.hasObject(ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3")));
  101. assertTrue(file.hasObject(ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7")));
  102. assertTrue(file.hasObject(ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327")));
  103. assertTrue(file.hasObject(ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035")));
  104. assertTrue(file.hasObject(ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799")));
  105. } finally {
  106. is.close();
  107. }
  108. }
  109. /**
  110. * This is just another pack. It so happens that we have two convenient pack to
  111. * test with in the repository.
  112. *
  113. * @throws IOException
  114. */
  115. @Test
  116. public void test2() throws IOException {
  117. File packFile = JGitTestUtil.getTestResourceFile("pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack");
  118. final InputStream is = new FileInputStream(packFile);
  119. try {
  120. ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(is);
  121. p.parse(NullProgressMonitor.INSTANCE);
  122. PackFile file = p.getPackFile();
  123. assertTrue(file.hasObject(ObjectId.fromString("02ba32d3649e510002c21651936b7077aa75ffa9")));
  124. assertTrue(file.hasObject(ObjectId.fromString("0966a434eb1a025db6b71485ab63a3bfbea520b6")));
  125. assertTrue(file.hasObject(ObjectId.fromString("09efc7e59a839528ac7bda9fa020dc9101278680")));
  126. assertTrue(file.hasObject(ObjectId.fromString("0a3d7772488b6b106fb62813c4d6d627918d9181")));
  127. assertTrue(file.hasObject(ObjectId.fromString("1004d0d7ac26fbf63050a234c9b88a46075719d3")));
  128. assertTrue(file.hasObject(ObjectId.fromString("10da5895682013006950e7da534b705252b03be6")));
  129. assertTrue(file.hasObject(ObjectId.fromString("1203b03dc816ccbb67773f28b3c19318654b0bc8")));
  130. assertTrue(file.hasObject(ObjectId.fromString("15fae9e651043de0fd1deef588aa3fbf5a7a41c6")));
  131. assertTrue(file.hasObject(ObjectId.fromString("16f9ec009e5568c435f473ba3a1df732d49ce8c3")));
  132. assertTrue(file.hasObject(ObjectId.fromString("1fd7d579fb6ae3fe942dc09c2c783443d04cf21e")));
  133. assertTrue(file.hasObject(ObjectId.fromString("20a8ade77639491ea0bd667bf95de8abf3a434c8")));
  134. assertTrue(file.hasObject(ObjectId.fromString("2675188fd86978d5bc4d7211698b2118ae3bf658")));
  135. // and lots more...
  136. } finally {
  137. is.close();
  138. }
  139. }
  140. @Test
  141. public void testTinyThinPack() throws Exception {
  142. TestRepository d = new TestRepository<Repository>(db);
  143. RevBlob a = d.blob("a");
  144. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  145. packHeader(pack, 1);
  146. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  147. a.copyRawTo(pack);
  148. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  149. digest(pack);
  150. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  151. p.setAllowThin(true);
  152. p.parse(NullProgressMonitor.INSTANCE);
  153. }
  154. @Test
  155. public void testPackWithDuplicateBlob() throws Exception {
  156. final byte[] data = Constants.encode("0123456789abcdefg");
  157. TestRepository<Repository> d = new TestRepository<Repository>(db);
  158. assertTrue(db.hasObject(d.blob(data)));
  159. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  160. packHeader(pack, 1);
  161. pack.write((Constants.OBJ_BLOB) << 4 | 0x80 | 1);
  162. pack.write(1);
  163. deflate(pack, data);
  164. digest(pack);
  165. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  166. p.setAllowThin(false);
  167. p.parse(NullProgressMonitor.INSTANCE);
  168. }
  169. @Test
  170. public void testPackWithTrailingGarbage() throws Exception {
  171. TestRepository d = new TestRepository<Repository>(db);
  172. RevBlob a = d.blob("a");
  173. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  174. packHeader(pack, 1);
  175. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  176. a.copyRawTo(pack);
  177. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  178. digest(pack);
  179. PackParser p = index(new UnionInputStream(
  180. new ByteArrayInputStream(pack.toByteArray()),
  181. new ByteArrayInputStream(new byte[] { 0x7e })));
  182. p.setAllowThin(true);
  183. p.setCheckEofAfterPackFooter(true);
  184. try {
  185. p.parse(NullProgressMonitor.INSTANCE);
  186. fail("Pack with trailing garbage was accepted");
  187. } catch (IOException err) {
  188. assertEquals(
  189. MessageFormat.format(JGitText.get().expectedEOFReceived, "\\x7e"),
  190. err.getMessage());
  191. }
  192. }
  193. @Test
  194. public void testMaxObjectSizeFullBlob() throws Exception {
  195. TestRepository d = new TestRepository<Repository>(db);
  196. final byte[] data = Constants.encode("0123456789");
  197. d.blob(data);
  198. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  199. packHeader(pack, 1);
  200. pack.write((Constants.OBJ_BLOB) << 4 | 10);
  201. deflate(pack, data);
  202. digest(pack);
  203. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  204. p.setMaxObjectSizeLimit(11);
  205. p.parse(NullProgressMonitor.INSTANCE);
  206. p = index(new ByteArrayInputStream(pack.toByteArray()));
  207. p.setMaxObjectSizeLimit(10);
  208. p.parse(NullProgressMonitor.INSTANCE);
  209. p = index(new ByteArrayInputStream(pack.toByteArray()));
  210. p.setMaxObjectSizeLimit(9);
  211. try {
  212. p.parse(NullProgressMonitor.INSTANCE);
  213. fail("PackParser should have failed");
  214. } catch (TooLargeObjectInPackException e) {
  215. assertTrue(e.getMessage().contains("10")); // obj size
  216. assertTrue(e.getMessage().contains("9")); // max obj size
  217. }
  218. }
  219. @Test
  220. public void testMaxObjectSizeDeltaBlock() throws Exception {
  221. TestRepository d = new TestRepository<Repository>(db);
  222. RevBlob a = d.blob("a");
  223. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  224. packHeader(pack, 1);
  225. pack.write((Constants.OBJ_REF_DELTA) << 4 | 14);
  226. a.copyRawTo(pack);
  227. deflate(pack, new byte[] { 1, 11, 11, 'a', '0', '1', '2', '3', '4',
  228. '5', '6', '7', '8', '9' });
  229. digest(pack);
  230. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  231. p.setAllowThin(true);
  232. p.setMaxObjectSizeLimit(14);
  233. p.parse(NullProgressMonitor.INSTANCE);
  234. p = index(new ByteArrayInputStream(pack.toByteArray()));
  235. p.setAllowThin(true);
  236. p.setMaxObjectSizeLimit(13);
  237. try {
  238. p.parse(NullProgressMonitor.INSTANCE);
  239. fail("PackParser should have failed");
  240. } catch (TooLargeObjectInPackException e) {
  241. assertTrue(e.getMessage().contains("13")); // max obj size
  242. assertFalse(e.getMessage().contains("14")); // no delta size
  243. }
  244. }
  245. @Test
  246. public void testMaxObjectSizeDeltaResultSize() throws Exception {
  247. TestRepository d = new TestRepository<Repository>(db);
  248. RevBlob a = d.blob("0123456789");
  249. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  250. packHeader(pack, 1);
  251. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  252. a.copyRawTo(pack);
  253. deflate(pack, new byte[] { 10, 11, 1, 'a' });
  254. digest(pack);
  255. PackParser p = index(new ByteArrayInputStream(pack.toByteArray()));
  256. p.setAllowThin(true);
  257. p.setMaxObjectSizeLimit(11);
  258. p.parse(NullProgressMonitor.INSTANCE);
  259. p = index(new ByteArrayInputStream(pack.toByteArray()));
  260. p.setAllowThin(true);
  261. p.setMaxObjectSizeLimit(10);
  262. try {
  263. p.parse(NullProgressMonitor.INSTANCE);
  264. fail("PackParser should have failed");
  265. } catch (TooLargeObjectInPackException e) {
  266. assertTrue(e.getMessage().contains("11")); // result obj size
  267. assertTrue(e.getMessage().contains("10")); // max obj size
  268. }
  269. }
  270. @Test
  271. public void testNonMarkingInputStream() throws Exception {
  272. TestRepository d = new TestRepository<Repository>(db);
  273. RevBlob a = d.blob("a");
  274. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  275. packHeader(pack, 1);
  276. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  277. a.copyRawTo(pack);
  278. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  279. digest(pack);
  280. InputStream in = new ByteArrayInputStream(pack.toByteArray()) {
  281. @Override
  282. public boolean markSupported() {
  283. return false;
  284. }
  285. @Override
  286. public void mark(int maxlength) {
  287. fail("Mark should not be called");
  288. }
  289. };
  290. PackParser p = index(in);
  291. p.setAllowThin(true);
  292. p.setCheckEofAfterPackFooter(false);
  293. p.setExpectDataAfterPackFooter(true);
  294. try {
  295. p.parse(NullProgressMonitor.INSTANCE);
  296. fail("PackParser should have failed");
  297. } catch (IOException e) {
  298. assertEquals(e.getMessage(),
  299. JGitText.get().inputStreamMustSupportMark);
  300. }
  301. }
  302. @Test
  303. public void testDataAfterPackFooterSingleRead() throws Exception {
  304. TestRepository d = new TestRepository<Repository>(db);
  305. RevBlob a = d.blob("a");
  306. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(32*1024);
  307. packHeader(pack, 1);
  308. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  309. a.copyRawTo(pack);
  310. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  311. digest(pack);
  312. byte packData[] = pack.toByteArray();
  313. byte streamData[] = new byte[packData.length + 1];
  314. System.arraycopy(packData, 0, streamData, 0, packData.length);
  315. streamData[packData.length] = 0x7e;
  316. InputStream in = new ByteArrayInputStream(streamData);
  317. PackParser p = index(in);
  318. p.setAllowThin(true);
  319. p.setCheckEofAfterPackFooter(false);
  320. p.setExpectDataAfterPackFooter(true);
  321. p.parse(NullProgressMonitor.INSTANCE);
  322. assertEquals(0x7e, in.read());
  323. }
  324. @Test
  325. public void testDataAfterPackFooterSplitObjectRead() throws Exception {
  326. final byte[] data = Constants.encode("0123456789");
  327. // Build a pack ~17k
  328. int objects = 900;
  329. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(32 * 1024);
  330. packHeader(pack, objects);
  331. for (int i = 0; i < objects; i++) {
  332. pack.write((Constants.OBJ_BLOB) << 4 | 10);
  333. deflate(pack, data);
  334. }
  335. digest(pack);
  336. byte packData[] = pack.toByteArray();
  337. byte streamData[] = new byte[packData.length + 1];
  338. System.arraycopy(packData, 0, streamData, 0, packData.length);
  339. streamData[packData.length] = 0x7e;
  340. InputStream in = new ByteArrayInputStream(streamData);
  341. PackParser p = index(in);
  342. p.setAllowThin(true);
  343. p.setCheckEofAfterPackFooter(false);
  344. p.setExpectDataAfterPackFooter(true);
  345. p.parse(NullProgressMonitor.INSTANCE);
  346. assertEquals(0x7e, in.read());
  347. }
  348. @Test
  349. public void testDataAfterPackFooterSplitHeaderRead() throws Exception {
  350. TestRepository d = new TestRepository<Repository>(db);
  351. final byte[] data = Constants.encode("a");
  352. RevBlob b = d.blob(data);
  353. int objects = 248;
  354. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(32 * 1024);
  355. packHeader(pack, objects + 1);
  356. int offset = 13;
  357. StringBuilder sb = new StringBuilder();
  358. for (int i = 0; i < offset; i++)
  359. sb.append(i);
  360. offset = sb.toString().length();
  361. int lenByte = (Constants.OBJ_BLOB) << 4 | (offset & 0x0F);
  362. offset >>= 4;
  363. if (offset > 0)
  364. lenByte |= 1 << 7;
  365. pack.write(lenByte);
  366. while (offset > 0) {
  367. lenByte = offset & 0x7F;
  368. offset >>= 6;
  369. if (offset > 0)
  370. lenByte |= 1 << 7;
  371. pack.write(lenByte);
  372. }
  373. deflate(pack, Constants.encode(sb.toString()));
  374. for (int i = 0; i < objects; i++) {
  375. // The last pack header written falls across the 8192 byte boundary
  376. // between [8189:8210]
  377. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  378. b.copyRawTo(pack);
  379. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  380. }
  381. digest(pack);
  382. byte packData[] = pack.toByteArray();
  383. byte streamData[] = new byte[packData.length + 1];
  384. System.arraycopy(packData, 0, streamData, 0, packData.length);
  385. streamData[packData.length] = 0x7e;
  386. InputStream in = new ByteArrayInputStream(streamData);
  387. PackParser p = index(in);
  388. p.setAllowThin(true);
  389. p.setCheckEofAfterPackFooter(false);
  390. p.setExpectDataAfterPackFooter(true);
  391. p.parse(NullProgressMonitor.INSTANCE);
  392. assertEquals(0x7e, in.read());
  393. }
  394. private static void packHeader(TemporaryBuffer.Heap tinyPack, int cnt)
  395. throws IOException {
  396. final byte[] hdr = new byte[8];
  397. NB.encodeInt32(hdr, 0, 2);
  398. NB.encodeInt32(hdr, 4, cnt);
  399. tinyPack.write(Constants.PACK_SIGNATURE);
  400. tinyPack.write(hdr, 0, 8);
  401. }
  402. private static void deflate(TemporaryBuffer.Heap tinyPack,
  403. final byte[] content)
  404. throws IOException {
  405. final Deflater deflater = new Deflater();
  406. final byte[] buf = new byte[128];
  407. deflater.setInput(content, 0, content.length);
  408. deflater.finish();
  409. do {
  410. final int n = deflater.deflate(buf, 0, buf.length);
  411. if (n > 0)
  412. tinyPack.write(buf, 0, n);
  413. } while (!deflater.finished());
  414. }
  415. private static void digest(TemporaryBuffer.Heap buf) throws IOException {
  416. MessageDigest md = Constants.newMessageDigest();
  417. md.update(buf.toByteArray());
  418. buf.write(md.digest());
  419. }
  420. private ObjectInserter inserter;
  421. @After
  422. public void release() {
  423. if (inserter != null) {
  424. inserter.close();
  425. }
  426. }
  427. private PackParser index(InputStream in) throws IOException {
  428. if (inserter == null)
  429. inserter = db.newObjectInserter();
  430. return inserter.newPackParser(in);
  431. }
  432. }