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.

LFSPointerTest.java 9.8KB

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