View Javadoc
1   /*
2    * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.lfs.lib;
12  
13  import static java.nio.charset.StandardCharsets.UTF_8;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertNotEquals;
17  import static org.junit.Assert.assertNull;
18  import static org.junit.Assert.assertThrows;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  
25  import org.eclipse.jgit.lfs.LfsPointer;
26  import org.junit.Test;
27  
28  /*
29   * Test LfsPointer file abstraction
30   */
31  public class LFSPointerTest {
32  
33  	private static final String TEST_SHA256 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
34  
35  	@Test
36  	public void testEncoding() throws IOException {
37  		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
38  		LfsPointer ptr = new LfsPointer(id, 4);
39  		try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
40  			ptr.encode(baos);
41  			assertEquals(
42  					"version https://git-lfs.github.com/spec/v1\noid sha256:"
43  							+ TEST_SHA256 + "\nsize 4\n",
44  					baos.toString(UTF_8.name()));
45  		}
46  	}
47  
48  	@Test
49  	public void testReadValidLfsPointer() throws Exception {
50  		String ptr = "version https://git-lfs.github.com/spec/v1\n"
51  				+ "oid sha256:" + TEST_SHA256 + '\n'
52  				+ "size 4\n";
53  		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
54  		LfsPointer lfs = new LfsPointer(id, 4);
55  		try (ByteArrayInputStream in = new ByteArrayInputStream(
56  				ptr.getBytes(UTF_8))) {
57  			assertEquals(lfs, LfsPointer.parseLfsPointer(in));
58  		}
59  	}
60  
61  	@Test
62  	public void testReadValidLfsPointerUnordered() throws Exception {
63  		// This is actually not allowed per the spec, but JGit accepts it
64  		// anyway.
65  		String ptr = "version https://git-lfs.github.com/spec/v1\n"
66  				+ "size 4\n"
67  				+ "oid sha256:" + TEST_SHA256 + '\n';
68  		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
69  		LfsPointer lfs = new LfsPointer(id, 4);
70  		try (ByteArrayInputStream in = new ByteArrayInputStream(
71  				ptr.getBytes(UTF_8))) {
72  			assertEquals(lfs, LfsPointer.parseLfsPointer(in));
73  		}
74  	}
75  
76  	@Test
77  	public void testReadValidLfsPointerVersionNotFirst() throws Exception {
78  		// This is actually not allowed per the spec, but JGit accepts it
79  		// anyway.
80  		String ptr = "oid sha256:" + TEST_SHA256 + '\n'
81  				+ "size 4\n"
82  				+ "version https://git-lfs.github.com/spec/v1\n";
83  		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
84  		LfsPointer lfs = new LfsPointer(id, 4);
85  		try (ByteArrayInputStream in = new ByteArrayInputStream(
86  				ptr.getBytes(UTF_8))) {
87  			assertEquals(lfs, LfsPointer.parseLfsPointer(in));
88  		}
89  	}
90  
91  	@Test
92  	public void testReadInvalidLfsPointer() throws Exception {
93  		String cSource = "size_t someFunction(void *ptr); // Fake C source\n";
94  		try (ByteArrayInputStream in = new ByteArrayInputStream(
95  				cSource.getBytes(UTF_8))) {
96  			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
97  		}
98  	}
99  
100 	@Test
101 	public void testReadInvalidLfsPointer2() throws Exception {
102 		String cSource = "size_t\nsomeFunction(void *ptr);\n// Fake C source\n";
103 		try (ByteArrayInputStream in = new ByteArrayInputStream(
104 				cSource.getBytes(UTF_8))) {
105 			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
106 		}
107 	}
108 
109 	@Test
110 	public void testReadInValidLfsPointerVersionWrong() throws Exception {
111 		String ptr = "version https://git-lfs.example.org/spec/v1\n"
112 				+ "oid sha256:" + TEST_SHA256 + '\n'
113 				+ "size 4\n";
114 		try (ByteArrayInputStream in = new ByteArrayInputStream(
115 				ptr.getBytes(UTF_8))) {
116 			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
117 		}
118 	}
119 
120 	@Test
121 	public void testReadInValidLfsPointerVersionTwice() throws Exception {
122 		String ptr = "version https://git-lfs.github.com/spec/v1\n"
123 				+ "version https://git-lfs.github.com/spec/v1\n"
124 				+ "oid sha256:" + TEST_SHA256 + '\n'
125 				+ "size 4\n";
126 		try (ByteArrayInputStream in = new ByteArrayInputStream(
127 				ptr.getBytes(UTF_8))) {
128 			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
129 		}
130 	}
131 
132 	@Test
133 	public void testReadInValidLfsPointerVersionTwice2() throws Exception {
134 		String ptr = "version https://git-lfs.github.com/spec/v1\n"
135 				+ "oid sha256:" + TEST_SHA256 + '\n'
136 				+ "version https://git-lfs.github.com/spec/v1\n"
137 				+ "size 4\n";
138 		try (ByteArrayInputStream in = new ByteArrayInputStream(
139 				ptr.getBytes(UTF_8))) {
140 			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
141 		}
142 	}
143 
144 	@Test
145 	public void testReadInValidLfsPointerOidTwice() throws Exception {
146 		String ptr = "version https://git-lfs.github.com/spec/v1\n"
147 				+ "oid sha256:" + TEST_SHA256 + '\n'
148 				+ "oid sha256:" + TEST_SHA256 + '\n'
149 				+ "size 4\n";
150 		try (ByteArrayInputStream in = new ByteArrayInputStream(
151 				ptr.getBytes(UTF_8))) {
152 			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
153 		}
154 	}
155 
156 	@Test
157 	public void testReadInValidLfsPointerSizeTwice() throws Exception {
158 		String ptr = "version https://git-lfs.github.com/spec/v1\n"
159 				+ "size 4\n"
160 				+ "size 4\n"
161 				+ "oid sha256:" + TEST_SHA256 + '\n';
162 		try (ByteArrayInputStream in = new ByteArrayInputStream(
163 				ptr.getBytes(UTF_8))) {
164 			assertNull("Is not a LFS pointer", LfsPointer.parseLfsPointer(in));
165 		}
166 	}
167 
168 	@Test
169 	public void testRoundtrip() throws Exception {
170 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
171 		LfsPointer ptr = new LfsPointer(id, 4);
172 		try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
173 			ptr.encode(baos);
174 			try (ByteArrayInputStream in = new ByteArrayInputStream(
175 					baos.toByteArray())) {
176 				assertEquals(ptr, LfsPointer.parseLfsPointer(in));
177 			}
178 		}
179 	}
180 
181 	@Test
182 	public void testEquals() throws Exception {
183 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
184 		LfsPointer lfs = new LfsPointer(id, 4);
185 		AnyLongObjectId id2 = LongObjectId.fromString(TEST_SHA256);
186 		LfsPointer lfs2 = new LfsPointer(id2, 4);
187 		assertTrue(lfs.equals(lfs2));
188 		assertTrue(lfs2.equals(lfs));
189 	}
190 
191 	@Test
192 	public void testEqualsNull() throws Exception {
193 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
194 		LfsPointer lfs = new LfsPointer(id, 4);
195 		assertFalse(lfs.equals(null));
196 	}
197 
198 	@Test
199 	public void testEqualsSame() throws Exception {
200 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
201 		LfsPointer lfs = new LfsPointer(id, 4);
202 		assertTrue(lfs.equals(lfs));
203 	}
204 
205 	@Test
206 	public void testEqualsOther() throws Exception {
207 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
208 		LfsPointer lfs = new LfsPointer(id, 4);
209 		assertFalse(lfs.equals(new Object()));
210 	}
211 
212 	@Test
213 	public void testNotEqualsOid() throws Exception {
214 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
215 		LfsPointer lfs = new LfsPointer(id, 4);
216 		AnyLongObjectId id2 = LongObjectId
217 				.fromString(TEST_SHA256.replace('7', '5'));
218 		LfsPointer lfs2 = new LfsPointer(id2, 4);
219 		assertFalse(lfs.equals(lfs2));
220 		assertFalse(lfs2.equals(lfs));
221 	}
222 
223 	@Test
224 	public void testNotEqualsSize() throws Exception {
225 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
226 		LfsPointer lfs = new LfsPointer(id, 4);
227 		AnyLongObjectId id2 = LongObjectId.fromString(TEST_SHA256);
228 		LfsPointer lfs2 = new LfsPointer(id2, 5);
229 		assertFalse(lfs.equals(lfs2));
230 		assertFalse(lfs2.equals(lfs));
231 	}
232 
233 	@Test
234 	public void testCompareToEquals() throws Exception {
235 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
236 		LfsPointer lfs = new LfsPointer(id, 4);
237 		AnyLongObjectId id2 = LongObjectId.fromString(TEST_SHA256);
238 		LfsPointer lfs2 = new LfsPointer(id2, 4);
239 		assertEquals(0, lfs.compareTo(lfs2));
240 		assertEquals(0, lfs2.compareTo(lfs));
241 	}
242 
243 	@Test
244 	@SuppressWarnings("SelfComparison")
245 	public void testCompareToSame() throws Exception {
246 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
247 		LfsPointer lfs = new LfsPointer(id, 4);
248 		assertEquals(0, lfs.compareTo(lfs));
249 	}
250 
251 	@Test
252 	public void testCompareToNotEqualsOid() throws Exception {
253 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
254 		LfsPointer lfs = new LfsPointer(id, 4);
255 		AnyLongObjectId id2 = LongObjectId
256 				.fromString(TEST_SHA256.replace('7', '5'));
257 		LfsPointer lfs2 = new LfsPointer(id2, 4);
258 		assertNotEquals(0, lfs.compareTo(lfs2));
259 		assertNotEquals(0, lfs2.compareTo(lfs));
260 	}
261 
262 	@Test
263 	public void testCompareToNotEqualsSize() throws Exception {
264 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
265 		LfsPointer lfs = new LfsPointer(id, 4);
266 		AnyLongObjectId id2 = LongObjectId.fromString(TEST_SHA256);
267 		LfsPointer lfs2 = new LfsPointer(id2, 5);
268 		assertNotEquals(0, lfs.compareTo(lfs2));
269 		assertNotEquals(0, lfs2.compareTo(lfs));
270 	}
271 
272 	@Test
273 	public void testCompareToNull() throws Exception {
274 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
275 		LfsPointer lfs = new LfsPointer(id, 4);
276 		assertThrows(NullPointerException.class, () -> lfs.compareTo(null));
277 	}
278 
279 	@Test
280 	public void testHashcodeEquals() throws Exception {
281 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
282 		LfsPointer lfs = new LfsPointer(id, 4);
283 		AnyLongObjectId id2 = LongObjectId.fromString(TEST_SHA256);
284 		LfsPointer lfs2 = new LfsPointer(id2, 4);
285 		assertEquals(lfs.hashCode(), lfs2.hashCode());
286 	}
287 
288 	@Test
289 	public void testHashcodeSame() throws Exception {
290 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
291 		LfsPointer lfs = new LfsPointer(id, 4);
292 		assertEquals(lfs.hashCode(), lfs.hashCode());
293 	}
294 
295 	@Test
296 	public void testHashcodeNotEquals() throws Exception {
297 		AnyLongObjectId id = LongObjectId.fromString(TEST_SHA256);
298 		LfsPointer lfs = new LfsPointer(id, 4);
299 		AnyLongObjectId id2 = LongObjectId.fromString(TEST_SHA256);
300 		LfsPointer lfs2 = new LfsPointer(id2, 5);
301 		assertNotEquals(lfs.hashCode(), lfs2.hashCode());
302 	}
303 }