Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CanonicalTreeParserTest.java 11KB

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