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.

LfsPointerFilterTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2015, Dariusz Luksza <dariusz@luksza.org>
  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.lfs.lib;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  50. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  51. import org.eclipse.jgit.junit.TestRepository;
  52. import org.eclipse.jgit.revwalk.ObjectWalk;
  53. import org.eclipse.jgit.revwalk.RevCommit;
  54. import org.eclipse.jgit.revwalk.RevTree;
  55. import org.eclipse.jgit.treewalk.TreeWalk;
  56. import org.junit.Test;
  57. public class LfsPointerFilterTest {
  58. private static final int SIZE = 12345;
  59. private static final String OID = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393";
  60. private static final String[] NOT_VALID_LFS_FILES = { "", // empty file
  61. // simulate java file
  62. "package org.eclipse.jgit;",
  63. // invalid LFS pointer, no oid and version
  64. "version https://hawser.github.com/spec/v1\n",
  65. // invalid LFS pointer, no version
  66. "version https://hawser.github.com/spec/v1\n"
  67. + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n",
  68. // invalid LFS pointer, no id
  69. "version https://hawser.github.com/spec/v1\n" + "size 12345\n",
  70. // invalid LFS pointer, wrong order of oid and size
  71. "version https://hawser.github.com/spec/v1\n" + "size 12345\n"
  72. + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n" };
  73. private static final String[] LFS_VERSION_DOMAINS = {
  74. "hawser", "git-lfs"
  75. };
  76. private static final String[] VALID_LFS_FILES = {
  77. // valid LFS pointer
  78. "version https://%s.github.com/spec/v1\n"
  79. + "oid sha256:" + OID + "\n"
  80. + "size " + SIZE + "\n",
  81. // valid LFS pointer with "custom" key
  82. "version https://%s.github.com/spec/v1\n"
  83. + "custom key with value\n"
  84. + "oid sha256:" + OID + "\n"
  85. + "size " + SIZE + "\n",
  86. // valid LFS pointer with key with "."
  87. "version https://%s.github.com/spec/v1\n"
  88. + "oid sha256:" + OID + "\n"
  89. + "r.key key with .\n"
  90. + "size " + SIZE + "\n",
  91. // valid LFS pointer with key with "-"
  92. "version https://%s.github.com/spec/v1\n"
  93. + "oid sha256:" + OID + "\n"
  94. + "size " + SIZE + "\n"
  95. + "valid-name another valid key\n" };
  96. @Test
  97. public void testRegularFilesInRepositoryRoot() throws Exception {
  98. for (String file : NOT_VALID_LFS_FILES) {
  99. assertLfs("file.bin", file).withRecursive(false).shouldBe(false);
  100. }
  101. }
  102. @Test
  103. public void testNestedRegularFiles() throws Exception {
  104. for (String file : NOT_VALID_LFS_FILES) {
  105. assertLfs("a/file.bin", file).withRecursive(true).shouldBe(false);
  106. }
  107. }
  108. @Test
  109. public void testValidPointersInRepositoryRoot() throws Exception {
  110. for (String domain : LFS_VERSION_DOMAINS) {
  111. for (String file : VALID_LFS_FILES) {
  112. assertLfs("file.bin", String.format(file, domain))
  113. .withRecursive(true).shouldBe(true)
  114. .check();
  115. }
  116. }
  117. }
  118. @Test
  119. public void testValidNestedPointers() throws Exception {
  120. for (String domain : LFS_VERSION_DOMAINS) {
  121. for (String file : VALID_LFS_FILES) {
  122. assertLfs("a/file.bin", String.format(file, domain))
  123. .withRecursive(true).shouldBe(true).check();
  124. }
  125. }
  126. }
  127. @Test
  128. public void testValidNestedPointersWithoutRecurrence() throws Exception {
  129. for (String domain : LFS_VERSION_DOMAINS) {
  130. for (String file : VALID_LFS_FILES) {
  131. assertLfs("file.bin", String.format(file, domain))
  132. .withRecursive(false).shouldBe(true).check();
  133. assertLfs("a/file.bin", String.format(file, domain))
  134. .withRecursive(false).shouldBe(false).check();
  135. }
  136. }
  137. }
  138. private static LfsTreeWalk assertLfs(String path, String content) {
  139. return new LfsTreeWalk(path, content);
  140. }
  141. private static class LfsTreeWalk {
  142. private final String path;
  143. private final String content;
  144. private boolean state;
  145. private boolean recursive;
  146. private TestRepository<InMemoryRepository> tr;
  147. LfsTreeWalk(String path, String content) {
  148. this.path = path;
  149. this.content = content;
  150. }
  151. LfsTreeWalk withRecursive(boolean shouldBeRecursive) {
  152. this.recursive = shouldBeRecursive;
  153. return this;
  154. }
  155. LfsTreeWalk shouldBe(boolean shouldBeValid) {
  156. this.state = shouldBeValid;
  157. return this;
  158. }
  159. void check() throws Exception {
  160. tr = new TestRepository<>(new InMemoryRepository(
  161. new DfsRepositoryDescription("test")));
  162. RevCommit commit = tr.branch("master").commit().add(path, content)
  163. .message("initial commit").create();
  164. RevTree tree = parseCommit(commit);
  165. LfsPointerFilter filter = new LfsPointerFilter();
  166. try (TreeWalk treeWalk = new TreeWalk(tr.getRepository())) {
  167. treeWalk.addTree(tree);
  168. treeWalk.setRecursive(recursive);
  169. treeWalk.setFilter(filter);
  170. if (state) {
  171. assertTrue(treeWalk.next());
  172. assertEquals(path, treeWalk.getPathString());
  173. assertNotNull(filter.getPointer());
  174. assertEquals(SIZE, filter.getPointer().getSize());
  175. assertEquals(OID, filter.getPointer().getOid().name());
  176. } else {
  177. assertFalse(treeWalk.next());
  178. assertNull(filter.getPointer());
  179. }
  180. }
  181. }
  182. private RevTree parseCommit(RevCommit commit) throws Exception {
  183. try (ObjectWalk ow = new ObjectWalk(tr.getRepository())) {
  184. return ow.parseCommit(commit).getTree();
  185. }
  186. }
  187. }
  188. }