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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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(id.name(), tooBig.getMessage());
  125. }
  126. ObjectStream in = ol.openStream();
  127. assertNotNull("have stream", in);
  128. assertEquals(type, in.getType());
  129. assertEquals(data.length, in.getSize());
  130. byte[] data2 = new byte[data.length];
  131. IO.readFully(in, data2, 0, data.length);
  132. assertTrue("same content", Arrays.equals(data2, data));
  133. assertEquals("stream at EOF", -1, in.read());
  134. in.close();
  135. }
  136. public void testStandardFormat_NegativeSize() throws Exception {
  137. ObjectId id = ObjectId.zeroId();
  138. byte[] data = rng.nextBytes(300);
  139. try {
  140. byte[] gz = compressStandardFormat("blob", "-1", data);
  141. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  142. fail("Did not throw CorruptObjectException");
  143. } catch (CorruptObjectException coe) {
  144. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  145. id.name(), JGitText.get().corruptObjectNegativeSize), coe
  146. .getMessage());
  147. }
  148. }
  149. public void testStandardFormat_InvalidType() throws Exception {
  150. ObjectId id = ObjectId.zeroId();
  151. byte[] data = rng.nextBytes(300);
  152. try {
  153. byte[] gz = compressStandardFormat("not.a.type", "1", data);
  154. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  155. fail("Did not throw CorruptObjectException");
  156. } catch (CorruptObjectException coe) {
  157. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  158. id.name(), JGitText.get().corruptObjectInvalidType), coe
  159. .getMessage());
  160. }
  161. }
  162. public void testStandardFormat_NoHeader() throws Exception {
  163. ObjectId id = ObjectId.zeroId();
  164. byte[] data = {};
  165. try {
  166. byte[] gz = compressStandardFormat("", "", data);
  167. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  168. fail("Did not throw CorruptObjectException");
  169. } catch (CorruptObjectException coe) {
  170. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  171. id.name(), JGitText.get().corruptObjectNoHeader), coe
  172. .getMessage());
  173. }
  174. }
  175. public void testStandardFormat_GarbageAfterSize() throws Exception {
  176. ObjectId id = ObjectId.zeroId();
  177. byte[] data = rng.nextBytes(300);
  178. try {
  179. byte[] gz = compressStandardFormat("blob", "1foo", data);
  180. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  181. fail("Did not throw CorruptObjectException");
  182. } catch (CorruptObjectException coe) {
  183. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  184. id.name(), JGitText.get().corruptObjectGarbageAfterSize),
  185. coe.getMessage());
  186. }
  187. }
  188. public void testStandardFormat_SmallObject_CorruptZLibStream()
  189. throws Exception {
  190. ObjectId id = ObjectId.zeroId();
  191. byte[] data = rng.nextBytes(300);
  192. try {
  193. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  194. for (int i = 5; i < gz.length; i++)
  195. gz[i] = 0;
  196. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  197. fail("Did not throw CorruptObjectException");
  198. } catch (CorruptObjectException coe) {
  199. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  200. id.name(), JGitText.get().corruptObjectBadStream), coe
  201. .getMessage());
  202. }
  203. }
  204. public void testStandardFormat_LargeObject_CorruptZLibStream()
  205. throws Exception {
  206. final int type = Constants.OBJ_BLOB;
  207. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  208. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  209. byte[] gz = compressStandardFormat(type, data);
  210. gz[gz.length - 1] = 0;
  211. gz[gz.length - 2] = 0;
  212. write(id, gz);
  213. ObjectLoader ol;
  214. {
  215. FileInputStream fs = new FileInputStream(path(id));
  216. try {
  217. ol = UnpackedObject.open(fs, path(id), id, wc);
  218. } finally {
  219. fs.close();
  220. }
  221. }
  222. try {
  223. byte[] tmp = new byte[data.length];
  224. InputStream in = ol.openStream();
  225. try {
  226. IO.readFully(in, tmp, 0, tmp.length);
  227. } finally {
  228. in.close();
  229. }
  230. fail("Did not throw CorruptObjectException");
  231. } catch (CorruptObjectException coe) {
  232. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  233. id.name(), JGitText.get().corruptObjectBadStream), coe
  234. .getMessage());
  235. }
  236. }
  237. public void testPackFormat_SmallObject() throws Exception {
  238. final int type = Constants.OBJ_BLOB;
  239. byte[] data = rng.nextBytes(300);
  240. byte[] gz = compressPackFormat(type, data);
  241. ObjectId id = ObjectId.zeroId();
  242. ObjectLoader ol = UnpackedObject.open(new ByteArrayInputStream(gz),
  243. path(id), id, wc);
  244. assertNotNull("created loader", ol);
  245. assertEquals(type, ol.getType());
  246. assertEquals(data.length, ol.getSize());
  247. assertFalse("is not large", ol.isLarge());
  248. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  249. ObjectStream in = ol.openStream();
  250. assertNotNull("have stream", in);
  251. assertEquals(type, in.getType());
  252. assertEquals(data.length, in.getSize());
  253. byte[] data2 = new byte[data.length];
  254. IO.readFully(in, data2, 0, data.length);
  255. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  256. in.close();
  257. }
  258. public void testPackFormat_LargeObject() throws Exception {
  259. final int type = Constants.OBJ_BLOB;
  260. byte[] data = rng.nextBytes(ObjectLoader.STREAM_THRESHOLD + 5);
  261. ObjectId id = new ObjectInserter.Formatter().idFor(type, data);
  262. write(id, compressPackFormat(type, data));
  263. ObjectLoader ol;
  264. {
  265. FileInputStream fs = new FileInputStream(path(id));
  266. try {
  267. ol = UnpackedObject.open(fs, path(id), id, wc);
  268. } finally {
  269. fs.close();
  270. }
  271. }
  272. assertNotNull("created loader", ol);
  273. assertEquals(type, ol.getType());
  274. assertEquals(data.length, ol.getSize());
  275. assertTrue("is large", ol.isLarge());
  276. try {
  277. ol.getCachedBytes();
  278. fail("Should have thrown LargeObjectException");
  279. } catch (LargeObjectException tooBig) {
  280. assertEquals(id.name(), tooBig.getMessage());
  281. }
  282. ObjectStream in = ol.openStream();
  283. assertNotNull("have stream", in);
  284. assertEquals(type, in.getType());
  285. assertEquals(data.length, in.getSize());
  286. byte[] data2 = new byte[data.length];
  287. IO.readFully(in, data2, 0, data.length);
  288. assertTrue("same content", Arrays.equals(data2, data));
  289. assertEquals("stream at EOF", -1, in.read());
  290. in.close();
  291. }
  292. public void testPackFormat_DeltaNotAllowed() throws Exception {
  293. ObjectId id = ObjectId.zeroId();
  294. byte[] data = rng.nextBytes(300);
  295. try {
  296. byte[] gz = compressPackFormat(Constants.OBJ_OFS_DELTA, data);
  297. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  298. fail("Did not throw CorruptObjectException");
  299. } catch (CorruptObjectException coe) {
  300. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  301. id.name(), JGitText.get().corruptObjectInvalidType), coe
  302. .getMessage());
  303. }
  304. try {
  305. byte[] gz = compressPackFormat(Constants.OBJ_REF_DELTA, data);
  306. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  307. fail("Did not throw CorruptObjectException");
  308. } catch (CorruptObjectException coe) {
  309. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  310. id.name(), JGitText.get().corruptObjectInvalidType), coe
  311. .getMessage());
  312. }
  313. try {
  314. byte[] gz = compressPackFormat(Constants.OBJ_TYPE_5, data);
  315. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  316. fail("Did not throw CorruptObjectException");
  317. } catch (CorruptObjectException coe) {
  318. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  319. id.name(), JGitText.get().corruptObjectInvalidType), coe
  320. .getMessage());
  321. }
  322. try {
  323. byte[] gz = compressPackFormat(Constants.OBJ_EXT, data);
  324. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  325. fail("Did not throw CorruptObjectException");
  326. } catch (CorruptObjectException coe) {
  327. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  328. id.name(), JGitText.get().corruptObjectInvalidType), coe
  329. .getMessage());
  330. }
  331. }
  332. private byte[] compressStandardFormat(int type, byte[] data)
  333. throws IOException {
  334. String typeString = Constants.typeString(type);
  335. String length = String.valueOf(data.length);
  336. return compressStandardFormat(typeString, length, data);
  337. }
  338. private byte[] compressStandardFormat(String type, String length,
  339. byte[] data) throws IOException {
  340. ByteArrayOutputStream out = new ByteArrayOutputStream();
  341. DeflaterOutputStream d = new DeflaterOutputStream(out);
  342. d.write(Constants.encodeASCII(type));
  343. d.write(' ');
  344. d.write(Constants.encodeASCII(length));
  345. d.write(0);
  346. d.write(data);
  347. d.finish();
  348. return out.toByteArray();
  349. }
  350. private byte[] compressPackFormat(int type, byte[] data) throws IOException {
  351. byte[] hdr = new byte[64];
  352. int rawLength = data.length;
  353. int nextLength = rawLength >>> 4;
  354. hdr[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  355. rawLength = nextLength;
  356. int n = 1;
  357. while (rawLength > 0) {
  358. nextLength >>>= 7;
  359. hdr[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  360. rawLength = nextLength;
  361. }
  362. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  363. out.write(hdr, 0, n);
  364. DeflaterOutputStream d = new DeflaterOutputStream(out);
  365. d.write(data);
  366. d.finish();
  367. return out.toByteArray();
  368. }
  369. private File path(ObjectId id) {
  370. return repo.getObjectDatabase().fileFor(id);
  371. }
  372. private void write(ObjectId id, byte[] data) throws IOException {
  373. File path = path(id);
  374. path.getParentFile().mkdirs();
  375. FileOutputStream out = new FileOutputStream(path);
  376. try {
  377. out.write(data);
  378. } finally {
  379. out.close();
  380. }
  381. }
  382. }