Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LongObjectIdTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2015, Matthias Sohn <matthias.sohn@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 java.nio.charset.StandardCharsets.US_ASCII;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertFalse;
  15. import static org.junit.Assert.assertNotEquals;
  16. import static org.junit.Assert.assertTrue;
  17. import static org.junit.Assert.fail;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStreamWriter;
  21. import java.nio.ByteBuffer;
  22. import java.nio.charset.Charset;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.Locale;
  26. import org.eclipse.jgit.junit.JGitTestUtil;
  27. import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
  28. import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
  29. import org.eclipse.jgit.util.FileUtils;
  30. import org.junit.AfterClass;
  31. import org.junit.BeforeClass;
  32. import org.junit.Test;
  33. /*
  34. * Ported to SHA-256 from org.eclipse.jgit.lib.ObjectIdTest
  35. */
  36. public class LongObjectIdTest {
  37. private static Path tmp;
  38. @BeforeClass
  39. public static void setup() throws IOException {
  40. tmp = Files.createTempDirectory("jgit_test_");
  41. }
  42. @AfterClass
  43. public static void tearDown() throws IOException {
  44. FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
  45. }
  46. @Test
  47. public void test001_toString() {
  48. final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
  49. final LongObjectId oid = LongObjectId.fromString(x);
  50. assertEquals(x, oid.name());
  51. }
  52. @Test
  53. public void test002_toString() {
  54. final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
  55. final LongObjectId oid = LongObjectId.fromString(x);
  56. assertEquals(x, oid.name());
  57. }
  58. @Test
  59. public void test003_equals() {
  60. final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
  61. final LongObjectId a = LongObjectId.fromString(x);
  62. final LongObjectId b = LongObjectId.fromString(x);
  63. assertEquals(a.hashCode(), b.hashCode());
  64. assertEquals("a and b should be equal", b, a);
  65. }
  66. @Test
  67. public void test004_isId() {
  68. assertTrue("valid id", LongObjectId.isId(
  69. "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
  70. }
  71. @Test
  72. public void test005_notIsId() {
  73. assertFalse("bob is not an id", LongObjectId.isId("bob"));
  74. }
  75. @Test
  76. public void test006_notIsId() {
  77. assertFalse("63 digits is not an id", LongObjectId.isId(
  78. "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0"));
  79. }
  80. @Test
  81. public void test007_isId() {
  82. assertTrue("uppercase is accepted", LongObjectId.isId(
  83. "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2dEb7ab0A"));
  84. }
  85. @Test
  86. public void test008_notIsId() {
  87. assertFalse("g is not a valid hex digit", LongObjectId.isId(
  88. "g367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
  89. }
  90. @Test
  91. public void test009_toString() {
  92. final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
  93. final LongObjectId oid = LongObjectId.fromString(x);
  94. assertEquals(x, LongObjectId.toString(oid));
  95. }
  96. @Test
  97. public void test010_toString() {
  98. final String x = "0000000000000000000000000000000000000000000000000000000000000000";
  99. assertEquals(x, LongObjectId.toString(null));
  100. }
  101. @Test
  102. public void test011_toString() {
  103. final String x = "0123456789ABCDEFabcdef01234567890123456789ABCDEFabcdef0123456789";
  104. final LongObjectId oid = LongObjectId.fromString(x);
  105. assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
  106. }
  107. @Test
  108. public void testGetByte() {
  109. byte[] raw = new byte[32];
  110. for (int i = 0; i < 32; i++)
  111. raw[i] = (byte) (0xa0 + i);
  112. LongObjectId id = LongObjectId.fromRaw(raw);
  113. assertEquals(raw[0] & 0xff, id.getFirstByte());
  114. assertEquals(raw[0] & 0xff, id.getByte(0));
  115. assertEquals(raw[1] & 0xff, id.getByte(1));
  116. assertEquals(raw[1] & 0xff, id.getSecondByte());
  117. for (int i = 2; i < 32; i++) {
  118. assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
  119. }
  120. try {
  121. id.getByte(32);
  122. fail("LongObjectId has 32 byte only");
  123. } catch (ArrayIndexOutOfBoundsException e) {
  124. // expected
  125. }
  126. }
  127. @Test
  128. public void testSetByte() {
  129. byte[] exp = new byte[32];
  130. for (int i = 0; i < 32; i++) {
  131. exp[i] = (byte) (0xa0 + i);
  132. }
  133. MutableLongObjectId id = new MutableLongObjectId();
  134. id.fromRaw(exp);
  135. assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
  136. id.setByte(0, 0x10);
  137. assertEquals(0x10, id.getByte(0));
  138. exp[0] = 0x10;
  139. assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
  140. for (int p = 1; p < 32; p++) {
  141. id.setByte(p, 0x10 + p);
  142. assertEquals(0x10 + p, id.getByte(p));
  143. exp[p] = (byte) (0x10 + p);
  144. assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
  145. }
  146. for (int p = 0; p < 32; p++) {
  147. id.setByte(p, 0x80 + p);
  148. assertEquals(0x80 + p, id.getByte(p));
  149. exp[p] = (byte) (0x80 + p);
  150. assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
  151. }
  152. }
  153. @Test
  154. public void testZeroId() {
  155. AnyLongObjectId zero = new LongObjectId(0L, 0L, 0L, 0L);
  156. assertEquals(zero, LongObjectId.zeroId());
  157. assertEquals(
  158. "0000000000000000000000000000000000000000000000000000000000000000",
  159. LongObjectId.zeroId().name());
  160. }
  161. @Test
  162. public void testEquals() {
  163. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  164. assertTrue("id should equal itself", id1.equals(id1));
  165. AnyLongObjectId id2 = new LongObjectId(id1);
  166. assertEquals("objects should be equals", id1, id2);
  167. id2 = LongObjectIdTestUtils.hash("other");
  168. assertNotEquals("objects should be not equal", id1, id2);
  169. }
  170. @Test
  171. public void testCopyRawBytes() {
  172. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  173. AnyLongObjectId id2 = new LongObjectId(id1);
  174. byte[] buf = new byte[64];
  175. id1.copyRawTo(buf, 0);
  176. id2.copyRawTo(buf, 32);
  177. assertTrue("objects should be equals",
  178. LongObjectId.equals(buf, 0, buf, 32));
  179. }
  180. @Test
  181. public void testCopyRawLongs() {
  182. long[] a = new long[4];
  183. a[0] = 1L;
  184. a[1] = 2L;
  185. a[2] = 3L;
  186. a[3] = 4L;
  187. AnyLongObjectId id1 = new LongObjectId(a[0], a[1], a[2], a[3]);
  188. AnyLongObjectId id2 = LongObjectId.fromRaw(a);
  189. assertEquals("objects should be equals", id1, id2);
  190. }
  191. @Test
  192. public void testCopyFromStringInvalid() {
  193. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  194. try {
  195. LongObjectId.fromString(id1.name() + "01234");
  196. fail("expected InvalidLongObjectIdException");
  197. } catch (InvalidLongObjectIdException e) {
  198. assertEquals("Invalid id: " + id1.name() + "01234",
  199. e.getMessage());
  200. }
  201. }
  202. @Test
  203. public void testCopyFromStringByte() {
  204. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  205. byte[] buf = new byte[64];
  206. Charset cs = US_ASCII;
  207. cs.encode(id1.name()).get(buf);
  208. AnyLongObjectId id2 = LongObjectId.fromString(buf, 0);
  209. assertEquals("objects should be equals", id1, id2);
  210. }
  211. @Test
  212. public void testHashFile() throws IOException {
  213. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  214. Path f = tmp.resolve("test");
  215. JGitTestUtil.write(f.toFile(), "test");
  216. AnyLongObjectId id2 = LongObjectIdTestUtils.hash(f);
  217. assertEquals("objects should be equals", id1, id2);
  218. }
  219. @Test
  220. public void testCompareTo() {
  221. AnyLongObjectId id1 = LongObjectId.fromString(
  222. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
  223. assertEquals(0, id1.compareTo(LongObjectId.fromString(
  224. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
  225. AnyLongObjectId self = id1;
  226. assertEquals(0, id1.compareTo(self));
  227. assertEquals(-1, id1.compareTo(LongObjectId.fromString(
  228. "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
  229. assertEquals(-1, id1.compareTo(LongObjectId.fromString(
  230. "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef")));
  231. assertEquals(-1, id1.compareTo(LongObjectId.fromString(
  232. "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef")));
  233. assertEquals(-1, id1.compareTo(LongObjectId.fromString(
  234. "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef")));
  235. assertEquals(1, id1.compareTo(LongObjectId.fromString(
  236. "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
  237. assertEquals(1, id1.compareTo(LongObjectId.fromString(
  238. "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef")));
  239. assertEquals(1, id1.compareTo(LongObjectId.fromString(
  240. "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef")));
  241. assertEquals(1, id1.compareTo(LongObjectId.fromString(
  242. "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef")));
  243. }
  244. @Test
  245. public void testCompareToByte() {
  246. AnyLongObjectId id1 = LongObjectId.fromString(
  247. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
  248. byte[] buf = new byte[32];
  249. id1.copyRawTo(buf, 0);
  250. assertEquals(0, id1.compareTo(buf, 0));
  251. LongObjectId
  252. .fromString(
  253. "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  254. .copyRawTo(buf, 0);
  255. assertEquals(-1, id1.compareTo(buf, 0));
  256. LongObjectId
  257. .fromString(
  258. "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  259. .copyRawTo(buf, 0);
  260. assertEquals(1, id1.compareTo(buf, 0));
  261. }
  262. @Test
  263. public void testCompareToLong() {
  264. AnyLongObjectId id1 = new LongObjectId(1L, 2L, 3L, 4L);
  265. long[] buf = new long[4];
  266. id1.copyRawTo(buf, 0);
  267. assertEquals(0, id1.compareTo(buf, 0));
  268. new LongObjectId(2L, 2L, 3L, 4L).copyRawTo(buf, 0);
  269. assertEquals(-1, id1.compareTo(buf, 0));
  270. new LongObjectId(0L, 2L, 3L, 4L).copyRawTo(buf, 0);
  271. assertEquals(1, id1.compareTo(buf, 0));
  272. }
  273. @Test
  274. public void testCopyToByte() {
  275. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  276. byte[] buf = new byte[64];
  277. id1.copyTo(buf, 0);
  278. assertEquals(id1, LongObjectId.fromString(buf, 0));
  279. }
  280. @Test
  281. public void testCopyRawToByteBuffer() {
  282. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  283. ByteBuffer buf = ByteBuffer.allocate(32);
  284. id1.copyRawTo(buf);
  285. assertEquals(id1, LongObjectId.fromRaw(buf.array(), 0));
  286. }
  287. @Test
  288. public void testCopyToByteBuffer() {
  289. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  290. ByteBuffer buf = ByteBuffer.allocate(64);
  291. id1.copyTo(buf);
  292. assertEquals(id1, LongObjectId.fromString(buf.array(), 0));
  293. }
  294. @Test
  295. public void testCopyRawToOutputStream() throws IOException {
  296. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  297. ByteArrayOutputStream os = new ByteArrayOutputStream(32);
  298. id1.copyRawTo(os);
  299. assertEquals(id1, LongObjectId.fromRaw(os.toByteArray(), 0));
  300. }
  301. @Test
  302. public void testCopyToOutputStream() throws IOException {
  303. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  304. ByteArrayOutputStream os = new ByteArrayOutputStream(64);
  305. id1.copyTo(os);
  306. assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
  307. }
  308. @Test
  309. public void testCopyToWriter() throws IOException {
  310. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  311. ByteArrayOutputStream os = new ByteArrayOutputStream(64);
  312. try (OutputStreamWriter w = new OutputStreamWriter(os,
  313. UTF_8)) {
  314. id1.copyTo(w);
  315. }
  316. assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
  317. }
  318. @Test
  319. public void testCopyToWriterWithBuf() throws IOException {
  320. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  321. ByteArrayOutputStream os = new ByteArrayOutputStream(64);
  322. try (OutputStreamWriter w = new OutputStreamWriter(os,
  323. UTF_8)) {
  324. char[] buf = new char[64];
  325. id1.copyTo(buf, w);
  326. }
  327. assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
  328. }
  329. @Test
  330. public void testCopyToStringBuilder() {
  331. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  332. StringBuilder sb = new StringBuilder();
  333. char[] buf = new char[64];
  334. id1.copyTo(buf, sb);
  335. assertEquals(id1, LongObjectId.fromString(sb.toString()));
  336. }
  337. @Test
  338. public void testCopy() {
  339. AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
  340. assertEquals(id1.copy(), id1);
  341. MutableLongObjectId id2 = new MutableLongObjectId();
  342. id2.fromObjectId(id1);
  343. assertEquals(id1, id2.copy());
  344. }
  345. }