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.

CanonicalTreeParserTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (C) 2008-2009, 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.treewalk;
  44. import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
  45. import static org.eclipse.jgit.lib.FileMode.SYMLINK;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertSame;
  49. import static org.junit.Assert.assertTrue;
  50. import java.io.ByteArrayOutputStream;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.TreeFormatter;
  56. import org.eclipse.jgit.util.RawParseUtils;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class CanonicalTreeParserTest {
  60. private final CanonicalTreeParser ctp = new CanonicalTreeParser();
  61. private final FileMode m644 = FileMode.REGULAR_FILE;
  62. private final FileMode mt = FileMode.TREE;
  63. private final ObjectId hash_a = ObjectId
  64. .fromString("6b9c715d21d5486e59083fb6071566aa6ecd4d42");
  65. private final ObjectId hash_foo = ObjectId
  66. .fromString("a213e8e25bb2442326e86cbfb9ef56319f482869");
  67. private final ObjectId hash_sometree = ObjectId
  68. .fromString("daf4bdb0d7bb24319810fe0e73aa317663448c93");
  69. private byte[] tree1;
  70. private byte[] tree2;
  71. private byte[] tree3;
  72. @Before
  73. public void setUp() throws Exception {
  74. tree1 = mktree(entry(m644, "a", hash_a));
  75. tree2 = mktree(entry(m644, "a", hash_a), entry(m644, "foo", hash_foo));
  76. tree3 = mktree(entry(m644, "a", hash_a), entry(mt, "b_sometree",
  77. hash_sometree), entry(m644, "foo", hash_foo));
  78. }
  79. private static byte[] mktree(byte[]... data) throws Exception {
  80. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  81. for (byte[] e : data)
  82. out.write(e);
  83. return out.toByteArray();
  84. }
  85. private static byte[] entry(final FileMode mode, final String name,
  86. final ObjectId id) throws Exception {
  87. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  88. mode.copyTo(out);
  89. out.write(' ');
  90. out.write(Constants.encode(name));
  91. out.write(0);
  92. id.copyRawTo(out);
  93. return out.toByteArray();
  94. }
  95. private String path() {
  96. return RawParseUtils.decode(Constants.CHARSET, ctp.path,
  97. ctp.pathOffset, ctp.pathLen);
  98. }
  99. @Test
  100. public void testEmptyTree_AtEOF() throws Exception {
  101. ctp.reset(new byte[0]);
  102. assertTrue(ctp.eof());
  103. }
  104. @Test
  105. public void testOneEntry_Forward() throws Exception {
  106. ctp.reset(tree1);
  107. assertTrue(ctp.first());
  108. assertFalse(ctp.eof());
  109. assertEquals(m644.getBits(), ctp.mode);
  110. assertEquals("a", path());
  111. assertEquals(hash_a, ctp.getEntryObjectId());
  112. ctp.next(1);
  113. assertFalse(ctp.first());
  114. assertTrue(ctp.eof());
  115. }
  116. @Test
  117. public void testTwoEntries_ForwardOneAtATime() throws Exception {
  118. ctp.reset(tree2);
  119. assertTrue(ctp.first());
  120. assertFalse(ctp.eof());
  121. assertEquals(m644.getBits(), ctp.mode);
  122. assertEquals("a", path());
  123. assertEquals(hash_a, ctp.getEntryObjectId());
  124. ctp.next(1);
  125. assertFalse(ctp.eof());
  126. assertEquals(m644.getBits(), ctp.mode);
  127. assertEquals("foo", path());
  128. assertEquals(hash_foo, ctp.getEntryObjectId());
  129. ctp.next(1);
  130. assertFalse(ctp.first());
  131. assertTrue(ctp.eof());
  132. }
  133. @Test
  134. public void testOneEntry_Seek1IsEOF() throws Exception {
  135. ctp.reset(tree1);
  136. ctp.next(1);
  137. assertTrue(ctp.eof());
  138. }
  139. @Test
  140. public void testTwoEntries_Seek2IsEOF() throws Exception {
  141. ctp.reset(tree2);
  142. ctp.next(2);
  143. assertTrue(ctp.eof());
  144. }
  145. @Test
  146. public void testThreeEntries_Seek3IsEOF() throws Exception {
  147. ctp.reset(tree3);
  148. ctp.next(3);
  149. assertTrue(ctp.eof());
  150. }
  151. @Test
  152. public void testThreeEntries_Seek2() throws Exception {
  153. ctp.reset(tree3);
  154. ctp.next(2);
  155. assertFalse(ctp.eof());
  156. assertFalse(ctp.eof());
  157. assertEquals(m644.getBits(), ctp.mode);
  158. assertEquals("foo", path());
  159. assertEquals(hash_foo, ctp.getEntryObjectId());
  160. ctp.next(1);
  161. assertTrue(ctp.eof());
  162. }
  163. @Test
  164. public void testOneEntry_Backwards() throws Exception {
  165. ctp.reset(tree1);
  166. ctp.next(1);
  167. assertFalse(ctp.first());
  168. assertTrue(ctp.eof());
  169. ctp.back(1);
  170. assertTrue(ctp.first());
  171. assertFalse(ctp.eof());
  172. assertEquals(m644.getBits(), ctp.mode);
  173. assertEquals("a", path());
  174. assertEquals(hash_a, ctp.getEntryObjectId());
  175. }
  176. @Test
  177. public void testTwoEntries_BackwardsOneAtATime() throws Exception {
  178. ctp.reset(tree2);
  179. ctp.next(2);
  180. assertTrue(ctp.eof());
  181. ctp.back(1);
  182. assertFalse(ctp.eof());
  183. assertEquals(m644.getBits(), ctp.mode);
  184. assertEquals("foo", path());
  185. assertEquals(hash_foo, ctp.getEntryObjectId());
  186. ctp.back(1);
  187. assertFalse(ctp.eof());
  188. assertEquals(m644.getBits(), ctp.mode);
  189. assertEquals("a", path());
  190. assertEquals(hash_a, ctp.getEntryObjectId());
  191. }
  192. @Test
  193. public void testTwoEntries_BackwardsTwo() throws Exception {
  194. ctp.reset(tree2);
  195. ctp.next(2);
  196. assertTrue(ctp.eof());
  197. ctp.back(2);
  198. assertFalse(ctp.eof());
  199. assertEquals(m644.getBits(), ctp.mode);
  200. assertEquals("a", path());
  201. assertEquals(hash_a, ctp.getEntryObjectId());
  202. ctp.next(1);
  203. assertFalse(ctp.eof());
  204. assertEquals(m644.getBits(), ctp.mode);
  205. assertEquals("foo", path());
  206. assertEquals(hash_foo, ctp.getEntryObjectId());
  207. ctp.next(1);
  208. assertTrue(ctp.eof());
  209. }
  210. @Test
  211. public void testThreeEntries_BackwardsTwo() throws Exception {
  212. ctp.reset(tree3);
  213. ctp.next(3);
  214. assertTrue(ctp.eof());
  215. ctp.back(2);
  216. assertFalse(ctp.eof());
  217. assertEquals(mt.getBits(), ctp.mode);
  218. assertEquals("b_sometree", path());
  219. assertEquals(hash_sometree, ctp.getEntryObjectId());
  220. ctp.next(1);
  221. assertFalse(ctp.eof());
  222. assertEquals(m644.getBits(), ctp.mode);
  223. assertEquals("foo", path());
  224. assertEquals(hash_foo, ctp.getEntryObjectId());
  225. ctp.next(1);
  226. assertTrue(ctp.eof());
  227. }
  228. @Test
  229. public void testBackwards_ConfusingPathName() throws Exception {
  230. final String aVeryConfusingName = "confusing 644 entry 755 and others";
  231. ctp.reset(mktree(entry(m644, "a", hash_a), entry(mt, aVeryConfusingName,
  232. hash_sometree), entry(m644, "foo", hash_foo)));
  233. ctp.next(3);
  234. assertTrue(ctp.eof());
  235. ctp.back(2);
  236. assertFalse(ctp.eof());
  237. assertEquals(mt.getBits(), ctp.mode);
  238. assertEquals(aVeryConfusingName, path());
  239. assertEquals(hash_sometree, ctp.getEntryObjectId());
  240. ctp.back(1);
  241. assertFalse(ctp.eof());
  242. assertEquals(m644.getBits(), ctp.mode);
  243. assertEquals("a", path());
  244. assertEquals(hash_a, ctp.getEntryObjectId());
  245. }
  246. @Test
  247. public void testBackwords_Prebuilts1() throws Exception {
  248. // What is interesting about this test is the ObjectId for the
  249. // "darwin-x86" path entry ends in an octal digit (37 == '7').
  250. // Thus when scanning backwards we could over scan and consume
  251. // part of the SHA-1, and miss the path terminator.
  252. //
  253. final ObjectId common = ObjectId
  254. .fromString("af7bf97cb9bce3f60f1d651a0ef862e9447dd8bc");
  255. final ObjectId darwinx86 = ObjectId
  256. .fromString("e927f7398240f78face99e1a738dac54ef738e37");
  257. final ObjectId linuxx86 = ObjectId
  258. .fromString("ac08dd97120c7cb7d06e98cd5b152011183baf21");
  259. final ObjectId windows = ObjectId
  260. .fromString("6c4c64c221a022bb973165192cca4812033479df");
  261. ctp.reset(mktree(entry(mt, "common", common), entry(mt, "darwin-x86",
  262. darwinx86), entry(mt, "linux-x86", linuxx86), entry(mt,
  263. "windows", windows)));
  264. ctp.next(3);
  265. assertEquals("windows", ctp.getEntryPathString());
  266. assertSame(mt, ctp.getEntryFileMode());
  267. assertEquals(windows, ctp.getEntryObjectId());
  268. ctp.back(1);
  269. assertEquals("linux-x86", ctp.getEntryPathString());
  270. assertSame(mt, ctp.getEntryFileMode());
  271. assertEquals(linuxx86, ctp.getEntryObjectId());
  272. ctp.next(1);
  273. assertEquals("windows", ctp.getEntryPathString());
  274. assertSame(mt, ctp.getEntryFileMode());
  275. assertEquals(windows, ctp.getEntryObjectId());
  276. }
  277. @Test
  278. public void testBackwords_Prebuilts2() throws Exception {
  279. // What is interesting about this test is the ObjectId for the
  280. // "darwin-x86" path entry ends in an octal digit (37 == '7').
  281. // Thus when scanning backwards we could over scan and consume
  282. // part of the SHA-1, and miss the path terminator.
  283. //
  284. final ObjectId common = ObjectId
  285. .fromString("af7bf97cb9bce3f60f1d651a0ef862e9447dd8bc");
  286. final ObjectId darwinx86 = ObjectId
  287. .fromString("0000000000000000000000000000000000000037");
  288. final ObjectId linuxx86 = ObjectId
  289. .fromString("ac08dd97120c7cb7d06e98cd5b152011183baf21");
  290. final ObjectId windows = ObjectId
  291. .fromString("6c4c64c221a022bb973165192cca4812033479df");
  292. ctp.reset(mktree(entry(mt, "common", common), entry(mt, "darwin-x86",
  293. darwinx86), entry(mt, "linux-x86", linuxx86), entry(mt,
  294. "windows", windows)));
  295. ctp.next(3);
  296. assertEquals("windows", ctp.getEntryPathString());
  297. assertSame(mt, ctp.getEntryFileMode());
  298. assertEquals(windows, ctp.getEntryObjectId());
  299. ctp.back(1);
  300. assertEquals("linux-x86", ctp.getEntryPathString());
  301. assertSame(mt, ctp.getEntryFileMode());
  302. assertEquals(linuxx86, ctp.getEntryObjectId());
  303. ctp.next(1);
  304. assertEquals("windows", ctp.getEntryPathString());
  305. assertSame(mt, ctp.getEntryFileMode());
  306. assertEquals(windows, ctp.getEntryObjectId());
  307. }
  308. @Test
  309. public void testFreakingHugePathName() throws Exception {
  310. final int n = AbstractTreeIterator.DEFAULT_PATH_SIZE * 4;
  311. final StringBuilder b = new StringBuilder(n);
  312. for (int i = 0; i < n; i++)
  313. b.append('q');
  314. final String name = b.toString();
  315. ctp.reset(entry(m644, name, hash_a));
  316. assertFalse(ctp.eof());
  317. assertEquals(name, RawParseUtils.decode(Constants.CHARSET, ctp.path,
  318. ctp.pathOffset, ctp.pathLen));
  319. }
  320. @Test
  321. public void testFindAttributesWhenFirst() throws CorruptObjectException {
  322. TreeFormatter tree = new TreeFormatter();
  323. tree.append(".gitattributes", REGULAR_FILE, hash_a);
  324. ctp.reset(tree.toByteArray());
  325. assertTrue(ctp.findFile(".gitattributes"));
  326. assertEquals(REGULAR_FILE.getBits(), ctp.getEntryRawMode());
  327. assertEquals(".gitattributes", ctp.getEntryPathString());
  328. assertEquals(hash_a, ctp.getEntryObjectId());
  329. }
  330. @Test
  331. public void testFindAttributesWhenSecond() throws CorruptObjectException {
  332. TreeFormatter tree = new TreeFormatter();
  333. tree.append(".config", SYMLINK, hash_a);
  334. tree.append(".gitattributes", REGULAR_FILE, hash_foo);
  335. ctp.reset(tree.toByteArray());
  336. assertTrue(ctp.findFile(".gitattributes"));
  337. assertEquals(REGULAR_FILE.getBits(), ctp.getEntryRawMode());
  338. assertEquals(".gitattributes", ctp.getEntryPathString());
  339. assertEquals(hash_foo, ctp.getEntryObjectId());
  340. }
  341. @Test
  342. public void testFindAttributesWhenMissing() throws CorruptObjectException {
  343. TreeFormatter tree = new TreeFormatter();
  344. tree.append("src", REGULAR_FILE, hash_a);
  345. tree.append("zoo", REGULAR_FILE, hash_foo);
  346. ctp.reset(tree.toByteArray());
  347. assertFalse(ctp.findFile(".gitattributes"));
  348. assertEquals(11, ctp.idOffset()); // Did not walk the entire tree.
  349. assertEquals("src", ctp.getEntryPathString());
  350. }
  351. }