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.

UnpackedObjectTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.File;
  47. import java.io.FileInputStream;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.text.MessageFormat;
  52. import java.util.Arrays;
  53. import java.util.zip.DeflaterOutputStream;
  54. import org.eclipse.jgit.JGitText;
  55. import org.eclipse.jgit.errors.CorruptObjectException;
  56. import org.eclipse.jgit.errors.LargeObjectException;
  57. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  58. import org.eclipse.jgit.junit.TestRng;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.ObjectInserter;
  62. import org.eclipse.jgit.lib.ObjectLoader;
  63. import org.eclipse.jgit.lib.ObjectStream;
  64. import org.eclipse.jgit.util.IO;
  65. public class UnpackedObjectTest extends LocalDiskRepositoryTestCase {
  66. private TestRng rng;
  67. private FileRepository repo;
  68. private WindowCursor wc;
  69. protected void setUp() throws Exception {
  70. super.setUp();
  71. rng = new TestRng(getName());
  72. repo = createBareRepository();
  73. wc = (WindowCursor) repo.newObjectReader();
  74. }
  75. protected void tearDown() throws Exception {
  76. if (wc != null)
  77. wc.release();
  78. super.tearDown();
  79. }
  80. public void testStandardFormat_SmallObject() throws Exception {
  81. final int type = Constants.OBJ_BLOB;
  82. byte[] data = rng.nextBytes(300);
  83. byte[] gz = compressStandardFormat(type, data);
  84. ObjectId id = ObjectId.zeroId();
  85. ObjectLoader ol = UnpackedObject.open(new ByteArrayInputStream(gz),
  86. path(id), id, wc);
  87. assertNotNull("created loader", ol);
  88. assertEquals(type, ol.getType());
  89. assertEquals(data.length, ol.getSize());
  90. assertFalse("is not large", ol.isLarge());
  91. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  92. ObjectStream in = ol.openStream();
  93. assertNotNull("have stream", in);
  94. assertEquals(type, in.getType());
  95. assertEquals(data.length, in.getSize());
  96. byte[] data2 = new byte[data.length];
  97. IO.readFully(in, data2, 0, data.length);
  98. assertTrue("same content", Arrays.equals(data2, data));
  99. assertEquals("stream at EOF", -1, in.read());
  100. in.close();
  101. }
  102. public void testStandardFormat_LargeObject() throws Exception {
  103. final int type = Constants.OBJ_BLOB;
  104. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  105. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  106. write(id, compressStandardFormat(type, data));
  107. ObjectLoader ol;
  108. {
  109. FileInputStream fs = new FileInputStream(path(id));
  110. try {
  111. ol = UnpackedObject.open(fs, path(id), id, wc);
  112. } finally {
  113. fs.close();
  114. }
  115. }
  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 testStandardFormat_NegativeSize() throws Exception {
  139. ObjectId id = ObjectId.zeroId();
  140. byte[] data = rng.nextBytes(300);
  141. try {
  142. byte[] gz = compressStandardFormat("blob", "-1", data);
  143. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  144. fail("Did not throw CorruptObjectException");
  145. } catch (CorruptObjectException coe) {
  146. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  147. id.name(), JGitText.get().corruptObjectNegativeSize), coe
  148. .getMessage());
  149. }
  150. }
  151. public void testStandardFormat_InvalidType() throws Exception {
  152. ObjectId id = ObjectId.zeroId();
  153. byte[] data = rng.nextBytes(300);
  154. try {
  155. byte[] gz = compressStandardFormat("not.a.type", "1", data);
  156. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  157. fail("Did not throw CorruptObjectException");
  158. } catch (CorruptObjectException coe) {
  159. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  160. id.name(), JGitText.get().corruptObjectInvalidType), coe
  161. .getMessage());
  162. }
  163. }
  164. public void testStandardFormat_NoHeader() throws Exception {
  165. ObjectId id = ObjectId.zeroId();
  166. byte[] data = {};
  167. try {
  168. byte[] gz = compressStandardFormat("", "", data);
  169. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  170. fail("Did not throw CorruptObjectException");
  171. } catch (CorruptObjectException coe) {
  172. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  173. id.name(), JGitText.get().corruptObjectNoHeader), coe
  174. .getMessage());
  175. }
  176. }
  177. public void testStandardFormat_GarbageAfterSize() throws Exception {
  178. ObjectId id = ObjectId.zeroId();
  179. byte[] data = rng.nextBytes(300);
  180. try {
  181. byte[] gz = compressStandardFormat("blob", "1foo", data);
  182. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  183. fail("Did not throw CorruptObjectException");
  184. } catch (CorruptObjectException coe) {
  185. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  186. id.name(), JGitText.get().corruptObjectGarbageAfterSize),
  187. coe.getMessage());
  188. }
  189. }
  190. public void testStandardFormat_SmallObject_CorruptZLibStream()
  191. throws Exception {
  192. ObjectId id = ObjectId.zeroId();
  193. byte[] data = rng.nextBytes(300);
  194. try {
  195. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  196. for (int i = 5; i < gz.length; i++)
  197. gz[i] = 0;
  198. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  199. fail("Did not throw CorruptObjectException");
  200. } catch (CorruptObjectException coe) {
  201. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  202. id.name(), JGitText.get().corruptObjectBadStream), coe
  203. .getMessage());
  204. }
  205. }
  206. public void testStandardFormat_SmallObject_TruncatedZLibStream()
  207. throws Exception {
  208. ObjectId id = ObjectId.zeroId();
  209. byte[] data = rng.nextBytes(300);
  210. try {
  211. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  212. byte[] tr = new byte[gz.length - 1];
  213. System.arraycopy(gz, 0, tr, 0, tr.length);
  214. UnpackedObject.open(new ByteArrayInputStream(tr), path(id), id, wc);
  215. fail("Did not throw CorruptObjectException");
  216. } catch (CorruptObjectException coe) {
  217. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  218. id.name(), JGitText.get().corruptObjectBadStream), coe
  219. .getMessage());
  220. }
  221. }
  222. public void testStandardFormat_SmallObject_TrailingGarbage()
  223. throws Exception {
  224. ObjectId id = ObjectId.zeroId();
  225. byte[] data = rng.nextBytes(300);
  226. try {
  227. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  228. byte[] tr = new byte[gz.length + 1];
  229. System.arraycopy(gz, 0, tr, 0, gz.length);
  230. UnpackedObject.open(new ByteArrayInputStream(tr), path(id), id, wc);
  231. fail("Did not throw CorruptObjectException");
  232. } catch (CorruptObjectException coe) {
  233. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  234. id.name(), JGitText.get().corruptObjectBadStream), coe
  235. .getMessage());
  236. }
  237. }
  238. public void testStandardFormat_LargeObject_CorruptZLibStream()
  239. throws Exception {
  240. final int type = Constants.OBJ_BLOB;
  241. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  242. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  243. byte[] gz = compressStandardFormat(type, data);
  244. gz[gz.length - 1] = 0;
  245. gz[gz.length - 2] = 0;
  246. write(id, gz);
  247. ObjectLoader ol;
  248. {
  249. FileInputStream fs = new FileInputStream(path(id));
  250. try {
  251. ol = UnpackedObject.open(fs, path(id), id, wc);
  252. } finally {
  253. fs.close();
  254. }
  255. }
  256. try {
  257. byte[] tmp = new byte[data.length];
  258. InputStream in = ol.openStream();
  259. try {
  260. IO.readFully(in, tmp, 0, tmp.length);
  261. } finally {
  262. in.close();
  263. }
  264. fail("Did not throw CorruptObjectException");
  265. } catch (CorruptObjectException coe) {
  266. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  267. id.name(), JGitText.get().corruptObjectBadStream), coe
  268. .getMessage());
  269. }
  270. }
  271. public void testStandardFormat_LargeObject_TruncatedZLibStream()
  272. throws Exception {
  273. final int type = Constants.OBJ_BLOB;
  274. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  275. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  276. byte[] gz = compressStandardFormat(type, data);
  277. byte[] tr = new byte[gz.length - 1];
  278. System.arraycopy(gz, 0, tr, 0, tr.length);
  279. write(id, tr);
  280. ObjectLoader ol;
  281. {
  282. FileInputStream fs = new FileInputStream(path(id));
  283. try {
  284. ol = UnpackedObject.open(fs, path(id), id, wc);
  285. } finally {
  286. fs.close();
  287. }
  288. }
  289. byte[] tmp = new byte[data.length];
  290. InputStream in = ol.openStream();
  291. IO.readFully(in, tmp, 0, tmp.length);
  292. try {
  293. in.close();
  294. fail("close did not throw CorruptObjectException");
  295. } catch (CorruptObjectException coe) {
  296. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  297. id.name(), JGitText.get().corruptObjectBadStream), coe
  298. .getMessage());
  299. }
  300. }
  301. public void testStandardFormat_LargeObject_TrailingGarbage()
  302. throws Exception {
  303. final int type = Constants.OBJ_BLOB;
  304. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  305. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  306. byte[] gz = compressStandardFormat(type, data);
  307. byte[] tr = new byte[gz.length + 1];
  308. System.arraycopy(gz, 0, tr, 0, gz.length);
  309. write(id, tr);
  310. ObjectLoader ol;
  311. {
  312. FileInputStream fs = new FileInputStream(path(id));
  313. try {
  314. ol = UnpackedObject.open(fs, path(id), id, wc);
  315. } finally {
  316. fs.close();
  317. }
  318. }
  319. byte[] tmp = new byte[data.length];
  320. InputStream in = ol.openStream();
  321. IO.readFully(in, tmp, 0, tmp.length);
  322. try {
  323. in.close();
  324. fail("close did not throw CorruptObjectException");
  325. } catch (CorruptObjectException coe) {
  326. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  327. id.name(), JGitText.get().corruptObjectBadStream), coe
  328. .getMessage());
  329. }
  330. }
  331. public void testPackFormat_SmallObject() throws Exception {
  332. final int type = Constants.OBJ_BLOB;
  333. byte[] data = rng.nextBytes(300);
  334. byte[] gz = compressPackFormat(type, data);
  335. ObjectId id = ObjectId.zeroId();
  336. ObjectLoader ol = UnpackedObject.open(new ByteArrayInputStream(gz),
  337. path(id), id, wc);
  338. assertNotNull("created loader", ol);
  339. assertEquals(type, ol.getType());
  340. assertEquals(data.length, ol.getSize());
  341. assertFalse("is not large", ol.isLarge());
  342. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  343. ObjectStream in = ol.openStream();
  344. assertNotNull("have stream", in);
  345. assertEquals(type, in.getType());
  346. assertEquals(data.length, in.getSize());
  347. byte[] data2 = new byte[data.length];
  348. IO.readFully(in, data2, 0, data.length);
  349. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  350. in.close();
  351. }
  352. public void testPackFormat_LargeObject() throws Exception {
  353. final int type = Constants.OBJ_BLOB;
  354. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  355. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  356. write(id, compressPackFormat(type, data));
  357. ObjectLoader ol;
  358. {
  359. FileInputStream fs = new FileInputStream(path(id));
  360. try {
  361. ol = UnpackedObject.open(fs, path(id), id, wc);
  362. } finally {
  363. fs.close();
  364. }
  365. }
  366. assertNotNull("created loader", ol);
  367. assertEquals(type, ol.getType());
  368. assertEquals(data.length, ol.getSize());
  369. assertTrue("is large", ol.isLarge());
  370. try {
  371. ol.getCachedBytes();
  372. fail("Should have thrown LargeObjectException");
  373. } catch (LargeObjectException tooBig) {
  374. assertEquals(MessageFormat.format(
  375. JGitText.get().largeObjectException, id.name()), tooBig
  376. .getMessage());
  377. }
  378. ObjectStream in = ol.openStream();
  379. assertNotNull("have stream", in);
  380. assertEquals(type, in.getType());
  381. assertEquals(data.length, in.getSize());
  382. byte[] data2 = new byte[data.length];
  383. IO.readFully(in, data2, 0, data.length);
  384. assertTrue("same content", Arrays.equals(data2, data));
  385. assertEquals("stream at EOF", -1, in.read());
  386. in.close();
  387. }
  388. public void testPackFormat_DeltaNotAllowed() throws Exception {
  389. ObjectId id = ObjectId.zeroId();
  390. byte[] data = rng.nextBytes(300);
  391. try {
  392. byte[] gz = compressPackFormat(Constants.OBJ_OFS_DELTA, data);
  393. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  394. fail("Did not throw CorruptObjectException");
  395. } catch (CorruptObjectException coe) {
  396. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  397. id.name(), JGitText.get().corruptObjectInvalidType), coe
  398. .getMessage());
  399. }
  400. try {
  401. byte[] gz = compressPackFormat(Constants.OBJ_REF_DELTA, data);
  402. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  403. fail("Did not throw CorruptObjectException");
  404. } catch (CorruptObjectException coe) {
  405. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  406. id.name(), JGitText.get().corruptObjectInvalidType), coe
  407. .getMessage());
  408. }
  409. try {
  410. byte[] gz = compressPackFormat(Constants.OBJ_TYPE_5, data);
  411. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  412. fail("Did not throw CorruptObjectException");
  413. } catch (CorruptObjectException coe) {
  414. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  415. id.name(), JGitText.get().corruptObjectInvalidType), coe
  416. .getMessage());
  417. }
  418. try {
  419. byte[] gz = compressPackFormat(Constants.OBJ_EXT, data);
  420. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  421. fail("Did not throw CorruptObjectException");
  422. } catch (CorruptObjectException coe) {
  423. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  424. id.name(), JGitText.get().corruptObjectInvalidType), coe
  425. .getMessage());
  426. }
  427. }
  428. private byte[] compressStandardFormat(int type, byte[] data)
  429. throws IOException {
  430. String typeString = Constants.typeString(type);
  431. String length = String.valueOf(data.length);
  432. return compressStandardFormat(typeString, length, data);
  433. }
  434. private byte[] compressStandardFormat(String type, String length,
  435. byte[] data) throws IOException {
  436. ByteArrayOutputStream out = new ByteArrayOutputStream();
  437. DeflaterOutputStream d = new DeflaterOutputStream(out);
  438. d.write(Constants.encodeASCII(type));
  439. d.write(' ');
  440. d.write(Constants.encodeASCII(length));
  441. d.write(0);
  442. d.write(data);
  443. d.finish();
  444. return out.toByteArray();
  445. }
  446. private byte[] compressPackFormat(int type, byte[] data) throws IOException {
  447. byte[] hdr = new byte[64];
  448. int rawLength = data.length;
  449. int nextLength = rawLength >>> 4;
  450. hdr[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  451. rawLength = nextLength;
  452. int n = 1;
  453. while (rawLength > 0) {
  454. nextLength >>>= 7;
  455. hdr[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  456. rawLength = nextLength;
  457. }
  458. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  459. out.write(hdr, 0, n);
  460. DeflaterOutputStream d = new DeflaterOutputStream(out);
  461. d.write(data);
  462. d.finish();
  463. return out.toByteArray();
  464. }
  465. private File path(ObjectId id) {
  466. return repo.getObjectDatabase().fileFor(id);
  467. }
  468. private void write(ObjectId id, byte[] data) throws IOException {
  469. File path = path(id);
  470. path.getParentFile().mkdirs();
  471. FileOutputStream out = new FileOutputStream(path);
  472. try {
  473. out.write(data);
  474. } finally {
  475. out.close();
  476. }
  477. }
  478. }