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.

LongObjectIdTest.java 14KB

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