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

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