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