diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2015-07-17 16:22:56 +0200 |
---|---|---|
committer | Saša Živkov <sasa.zivkov@sap.com> | 2016-02-04 17:49:42 +0100 |
commit | 536db18cc62afab2d38aac5b2af73f575f40d8b6 (patch) | |
tree | 94dccd9b5a11010044efd27f126bd21a94d5a665 /org.eclipse.jgit.lfs.test/tst/org | |
parent | 2de33d7678ffe865e44fc5007ee5fc59afa03404 (diff) | |
download | jgit-536db18cc62afab2d38aac5b2af73f575f40d8b6.tar.gz jgit-536db18cc62afab2d38aac5b2af73f575f40d8b6.zip |
Implement SHA-256 abstraction
The Large File Storage extension specified by GitHub [1] uses SHA-256 to
compute the ID of large files stored by the extension. Hence implement a
SHA-256 abstraction similar to the SHA-1 abstraction used by JGit.
[1] https://git-lfs.github.com/
Bug: 470333
Change-Id: I3a95954543c8570d73929e55f4a884b55dbf1b7a
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.lfs.test/tst/org')
3 files changed, 1115 insertions, 0 deletions
diff --git a/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/AbbreviatedLongObjectIdTest.java b/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/AbbreviatedLongObjectIdTest.java new file mode 100644 index 0000000000..8938a7875d --- /dev/null +++ b/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/AbbreviatedLongObjectIdTest.java @@ -0,0 +1,596 @@ +/* + * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.lfs.lib; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils; +import org.junit.Test; + +/* + * Ported to SHA-256 from org.eclipse.jgit.lib.AbbreviatedObjectIdTest + */ +public class AbbreviatedLongObjectIdTest { + @Test + public void testEmpty_FromByteArray() { + final AbbreviatedLongObjectId i; + i = AbbreviatedLongObjectId.fromString(new byte[] {}, 0, 0); + assertNotNull(i); + assertEquals(0, i.length()); + assertFalse(i.isComplete()); + assertEquals("", i.name()); + } + + @Test + public void testEmpty_FromString() { + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId + .fromString(""); + assertNotNull(i); + assertEquals(0, i.length()); + assertFalse(i.isComplete()); + assertEquals("", i.name()); + } + + @Test + public void testFull_FromByteArray() { + final String s = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final byte[] b = org.eclipse.jgit.lib.Constants.encodeASCII(s); + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(b, + 0, b.length); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertTrue(i.isComplete()); + assertEquals(s, i.name()); + + final LongObjectId f = i.toLongObjectId(); + assertNotNull(f); + assertEquals(LongObjectId.fromString(s), f); + assertEquals(f.hashCode(), i.hashCode()); + } + + @Test + public void testFull_FromString() { + final String s = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertTrue(i.isComplete()); + assertEquals(s, i.name()); + + final LongObjectId f = i.toLongObjectId(); + assertNotNull(f); + assertEquals(LongObjectId.fromString(s), f); + assertEquals(f.hashCode(), i.hashCode()); + } + + @Test + public void test1_FromString() { + final String s = "2"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test2_FromString() { + final String s = "27"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test3_FromString() { + final String s = "27e"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test4_FromString() { + final String s = "27e1"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test5_FromString() { + final String s = "27e15"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test6_FromString() { + final String s = "27e15b"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test7_FromString() { + final String s = "27e15b7"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test8_FromString() { + final String s = "27e15b72"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test9_FromString() { + final String s = "27e15b729"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test15_FromString() { + final String s = "27e15b72937fc8f"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test16_FromString() { + final String s = "27e15b72937fc8f5"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test17_FromString() { + final String s = "27e15b72937fc8f55"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void test33_FromString() { + final String s = "27e15b72937fc8f558da24ac3d50ec203"; + final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s); + assertNotNull(i); + assertEquals(s.length(), i.length()); + assertFalse(i.isComplete()); + assertEquals(s, i.name()); + assertNull(i.toLongObjectId()); + } + + @Test + public void testEquals_Short() { + final String s = "27e15b72"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId.fromString(s); + final AbbreviatedLongObjectId b = AbbreviatedLongObjectId.fromString(s); + assertNotSame(a, b); + assertTrue(a.hashCode() == b.hashCode()); + assertEquals(b, a); + assertEquals(a, b); + } + + @Test + public void testEquals_Full() { + final String s = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId.fromString(s); + final AbbreviatedLongObjectId b = AbbreviatedLongObjectId.fromString(s); + assertNotSame(a, b); + assertTrue(a.hashCode() == b.hashCode()); + assertEquals(b, a); + assertEquals(a, b); + } + + @Test + public void testNotEquals_SameLength() { + final String sa = "27e15b72"; + final String sb = "27e15b7f"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + final AbbreviatedLongObjectId b = AbbreviatedLongObjectId + .fromString(sb); + assertFalse(a.equals(b)); + assertFalse(b.equals(a)); + } + + @Test + public void testNotEquals_DiffLength() { + final String sa = "27e15b72abcd"; + final String sb = "27e15b72"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + final AbbreviatedLongObjectId b = AbbreviatedLongObjectId + .fromString(sb); + assertFalse(a.equals(b)); + assertFalse(b.equals(a)); + } + + @Test + public void testPrefixCompare_Full() { + final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(s1); + final LongObjectId i1 = LongObjectId.fromString(s1); + assertEquals(0, a.prefixCompare(i1)); + assertTrue(i1.startsWith(a)); + + final String s2 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b11"; + final LongObjectId i2 = LongObjectId.fromString(s2); + assertTrue(a.prefixCompare(i2) < 0); + assertFalse(i2.startsWith(a)); + + final String s3 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b0f"; + final LongObjectId i3 = LongObjectId.fromString(s3); + assertTrue(a.prefixCompare(i3) > 0); + assertFalse(i3.startsWith(a)); + } + + @Test + public void testPrefixCompare_1() { + final String sa = "2"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + + final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i1 = LongObjectId.fromString(s1); + assertEquals(0, a.prefixCompare(i1)); + assertTrue(i1.startsWith(a)); + + final String s2 = "37e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i2 = LongObjectId.fromString(s2); + assertTrue(a.prefixCompare(i2) < 0); + assertFalse(i2.startsWith(a)); + + final String s3 = "17e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i3 = LongObjectId.fromString(s3); + assertTrue(a.prefixCompare(i3) > 0); + assertFalse(i3.startsWith(a)); + } + + @Test + public void testPrefixCompare_15() { + final String sa = "27e15b72937fc8f"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + + final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i1 = LongObjectId.fromString(s1); + assertEquals(0, a.prefixCompare(i1)); + assertTrue(i1.startsWith(a)); + + final String s2 = "27e15b72937fc90558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i2 = LongObjectId.fromString(s2); + assertTrue(a.prefixCompare(i2) < 0); + assertFalse(i2.startsWith(a)); + + final String s3 = "27e15b72937fc8e558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i3 = LongObjectId.fromString(s3); + assertTrue(a.prefixCompare(i3) > 0); + assertFalse(i3.startsWith(a)); + } + + @Test + public void testPrefixCompare_16() { + final String sa = "27e15b72937fc8f5"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + + final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i1 = LongObjectId.fromString(s1); + assertEquals(0, a.prefixCompare(i1)); + assertTrue(i1.startsWith(a)); + + final String s2 = "27e15b72937fc8f658da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i2 = LongObjectId.fromString(s2); + assertTrue(a.prefixCompare(i2) < 0); + assertFalse(i2.startsWith(a)); + + final String s3 = "27e15b72937fc8f458da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i3 = LongObjectId.fromString(s3); + assertTrue(a.prefixCompare(i3) > 0); + assertFalse(i3.startsWith(a)); + } + + @Test + public void testPrefixCompare_17() { + final String sa = "27e15b72937fc8f55"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + + final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i1 = LongObjectId.fromString(s1); + assertEquals(0, a.prefixCompare(i1)); + assertTrue(i1.startsWith(a)); + + final String s2 = "27e15b72937fc8f568da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i2 = LongObjectId.fromString(s2); + assertTrue(a.prefixCompare(i2) < 0); + assertFalse(i2.startsWith(a)); + + final String s3 = "27e15b72937fc8f548da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i3 = LongObjectId.fromString(s3); + assertTrue(a.prefixCompare(i3) > 0); + assertFalse(i3.startsWith(a)); + } + + @Test + public void testPrefixCompare_33() { + final String sa = "27e15b72937fc8f558da24ac3d50ec203"; + final AbbreviatedLongObjectId a = AbbreviatedLongObjectId + .fromString(sa); + + final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i1 = LongObjectId.fromString(s1); + assertEquals(0, a.prefixCompare(i1)); + assertTrue(i1.startsWith(a)); + + final String s2 = "27e15b72937fc8f558da24ac3d50ec20402a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i2 = LongObjectId.fromString(s2); + assertTrue(a.prefixCompare(i2) < 0); + assertFalse(i2.startsWith(a)); + + final String s3 = "27e15b72937fc8f558da24ac3d50ec20202a4cf21e33b87ae8e4ce90e89c4b10"; + final LongObjectId i3 = LongObjectId.fromString(s3); + assertTrue(a.prefixCompare(i3) > 0); + assertFalse(i3.startsWith(a)); + } + + @Test + public void testIsId() { + // These are all too short. + assertFalse(AbbreviatedLongObjectId.isId("")); + assertFalse(AbbreviatedLongObjectId.isId("a")); + + // These are too long. + assertFalse(AbbreviatedLongObjectId.isId(LongObjectId + .fromString( + "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10") + .name() + "0")); + assertFalse(AbbreviatedLongObjectId.isId(LongObjectId + .fromString( + "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10") + .name() + "c0ffee")); + + // These contain non-hex characters. + assertFalse(AbbreviatedLongObjectId.isId("01notahexstring")); + + // These should all work. + assertTrue(AbbreviatedLongObjectId.isId("ab")); + assertTrue(AbbreviatedLongObjectId.isId("abc")); + assertTrue(AbbreviatedLongObjectId.isId("abcd")); + assertTrue(AbbreviatedLongObjectId.isId("abcd0")); + assertTrue(AbbreviatedLongObjectId.isId("abcd09")); + assertTrue(AbbreviatedLongObjectId.isId(LongObjectId + .fromString( + "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10") + .name())); + } + + @Test + public void testAbbreviate() { + AnyLongObjectId id = LongObjectIdTestUtils.hash("test"); + assertEquals( + "abbreviated id should match the id it was abbreviated from", 0, + id.abbreviate(10).prefixCompare(id)); + } + + @Test + public void testFromStringByteWrongLength() { + byte[] buf = new byte[65]; + try { + AbbreviatedLongObjectId.fromString(buf, 0, 65); + fail("expected IllegalArgumentException for too long AbbreviatedLongObjectId"); + } catch (IllegalArgumentException e) { + // expected + } + } + + @Test + public void testFromStringWrongLength() { + AnyLongObjectId id = LongObjectIdTestUtils.hash("test"); + try { + AbbreviatedLongObjectId.fromString(id.name() + "c0ffee"); + fail("expected IllegalArgumentException for too long AbbreviatedLongObjectId"); + } catch (IllegalArgumentException e) { + // expected + } + } + + @Test + public void testFromLongObjectId() { + AnyLongObjectId id = LongObjectIdTestUtils.hash("test"); + assertEquals(0, + AbbreviatedLongObjectId.fromLongObjectId(id).prefixCompare(id)); + } + + @Test + public void testPrefixCompareByte() { + AnyLongObjectId id = LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"); + byte[] buf = new byte[32]; + id.copyRawTo(buf, 0); + + AbbreviatedLongObjectId a = id.abbreviate(62); + assertEquals(0, a.prefixCompare(buf, 0)); + + a = LongObjectId + .fromString( + "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") + .abbreviate(16); + assertEquals(-1, a.prefixCompare(buf, 0)); + a = LongObjectId + .fromString( + "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef") + .abbreviate(32); + assertEquals(-1, a.prefixCompare(buf, 0)); + a = LongObjectId + .fromString( + "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef") + .abbreviate(48); + assertEquals(-1, a.prefixCompare(buf, 0)); + a = LongObjectId + .fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef") + .abbreviate(64); + assertEquals(-1, a.prefixCompare(buf, 0)); + + a = LongObjectId + .fromString( + "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") + .abbreviate(16); + assertEquals(1, a.prefixCompare(buf, 0)); + a = LongObjectId + .fromString( + "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef") + .abbreviate(32); + assertEquals(1, a.prefixCompare(buf, 0)); + a = LongObjectId + .fromString( + "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef") + .abbreviate(48); + assertEquals(1, a.prefixCompare(buf, 0)); + a = LongObjectId + .fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef") + .abbreviate(64); + assertEquals(1, a.prefixCompare(buf, 0)); + } + + @Test + public void testPrefixCompareLong() { + AnyLongObjectId id = new LongObjectId(1L, 2L, 3L, 4L); + long[] buf = new long[4]; + id.copyRawTo(buf, 0); + + AbbreviatedLongObjectId a = id.abbreviate(62); + assertEquals(0, a.prefixCompare(buf, 0)); + + a = new LongObjectId(0L, 2L, 3L, 4L).abbreviate(16); + assertEquals(-1, a.prefixCompare(buf, 0)); + a = new LongObjectId(1L, 1L, 3L, 4L).abbreviate(32); + assertEquals(-1, a.prefixCompare(buf, 0)); + a = new LongObjectId(1L, 2L, 2L, 4L).abbreviate(48); + assertEquals(-1, a.prefixCompare(buf, 0)); + a = new LongObjectId(1L, 2L, 3L, 3L).abbreviate(64); + assertEquals(-1, a.prefixCompare(buf, 0)); + + a = new LongObjectId(2L, 2L, 3L, 4L).abbreviate(16); + assertEquals(1, a.prefixCompare(buf, 0)); + a = new LongObjectId(1L, 3L, 3L, 4L).abbreviate(32); + assertEquals(1, a.prefixCompare(buf, 0)); + a = new LongObjectId(1L, 2L, 4L, 4L).abbreviate(48); + assertEquals(1, a.prefixCompare(buf, 0)); + a = new LongObjectId(1L, 2L, 3L, 5L).abbreviate(64); + assertEquals(1, a.prefixCompare(buf, 0)); + } + + @Test + public void testGetFirstByte() { + AnyLongObjectId id = LongObjectId.fromString( + "f423456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef"); + AbbreviatedLongObjectId a = id.abbreviate(10); + assertEquals(0xf4, a.getFirstByte()); + assertEquals(id.getFirstByte(), a.getFirstByte()); + } + + @Test + public void testNotEquals() { + AbbreviatedLongObjectId a = new LongObjectId(1L, 2L, 3L, 4L) + .abbreviate(10); + assertFalse(a.equals("different")); + } +} diff --git a/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/LongObjectIdTest.java b/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/LongObjectIdTest.java new file mode 100644 index 0000000000..435a2a3c39 --- /dev/null +++ b/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/LongObjectIdTest.java @@ -0,0 +1,427 @@ +/* + * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.lfs.lib; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.jgit.junit.JGitTestUtil; +import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException; +import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.util.FileUtils; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/* + * Ported to SHA-256 from org.eclipse.jgit.lib.ObjectIdTest + */ +public class LongObjectIdTest { + private static Path tmp; + + @BeforeClass + public static void setup() throws IOException { + tmp = Files.createTempDirectory("jgit_test_"); + } + + @AfterClass + public static void tearDown() throws IOException { + FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY); + } + + @Test + public void test001_toString() { + final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"; + final LongObjectId oid = LongObjectId.fromString(x); + assertEquals(x, oid.name()); + } + + @Test + public void test002_toString() { + final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf"; + final LongObjectId oid = LongObjectId.fromString(x); + assertEquals(x, oid.name()); + } + + @Test + public void test003_equals() { + final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"; + final LongObjectId a = LongObjectId.fromString(x); + final LongObjectId b = LongObjectId.fromString(x); + assertEquals(a.hashCode(), b.hashCode()); + assertEquals("a and b should be equal", b, a); + } + + @Test + public void test004_isId() { + assertTrue("valid id", LongObjectId.isId( + "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a")); + } + + @Test + public void test005_notIsId() { + assertFalse("bob is not an id", LongObjectId.isId("bob")); + } + + @Test + public void test006_notIsId() { + assertFalse("63 digits is not an id", LongObjectId.isId( + "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0")); + } + + @Test + public void test007_isId() { + assertTrue("uppercase is accepted", LongObjectId.isId( + "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2dEb7ab0A")); + } + + @Test + public void test008_notIsId() { + assertFalse("g is not a valid hex digit", LongObjectId.isId( + "g367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a")); + } + + @Test + public void test009_toString() { + final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf"; + final LongObjectId oid = LongObjectId.fromString(x); + assertEquals(x, LongObjectId.toString(oid)); + } + + @Test + public void test010_toString() { + final String x = "0000000000000000000000000000000000000000000000000000000000000000"; + assertEquals(x, LongObjectId.toString(null)); + } + + @Test + public void test011_toString() { + final String x = "0123456789ABCDEFabcdef01234567890123456789ABCDEFabcdef0123456789"; + final LongObjectId oid = LongObjectId.fromString(x); + assertEquals(x.toLowerCase(), oid.name()); + } + + @Test + public void testGetByte() { + byte[] raw = new byte[32]; + for (int i = 0; i < 32; i++) + raw[i] = (byte) (0xa0 + i); + LongObjectId id = LongObjectId.fromRaw(raw); + + assertEquals(raw[0] & 0xff, id.getFirstByte()); + assertEquals(raw[0] & 0xff, id.getByte(0)); + assertEquals(raw[1] & 0xff, id.getByte(1)); + assertEquals(raw[1] & 0xff, id.getSecondByte()); + + for (int i = 2; i < 32; i++) { + assertEquals("index " + i, raw[i] & 0xff, id.getByte(i)); + } + try { + id.getByte(32); + fail("LongObjectId has 32 byte only"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + @Test + public void testSetByte() { + byte[] exp = new byte[32]; + for (int i = 0; i < 32; i++) { + exp[i] = (byte) (0xa0 + i); + } + + MutableLongObjectId id = new MutableLongObjectId(); + id.fromRaw(exp); + assertEquals(LongObjectId.fromRaw(exp).name(), id.name()); + + id.setByte(0, 0x10); + assertEquals(0x10, id.getByte(0)); + exp[0] = 0x10; + assertEquals(LongObjectId.fromRaw(exp).name(), id.name()); + + for (int p = 1; p < 32; p++) { + id.setByte(p, 0x10 + p); + assertEquals(0x10 + p, id.getByte(p)); + exp[p] = (byte) (0x10 + p); + assertEquals(LongObjectId.fromRaw(exp).name(), id.name()); + } + + for (int p = 0; p < 32; p++) { + id.setByte(p, 0x80 + p); + assertEquals(0x80 + p, id.getByte(p)); + exp[p] = (byte) (0x80 + p); + assertEquals(LongObjectId.fromRaw(exp).name(), id.name()); + } + } + + @Test + public void testZeroId() { + AnyLongObjectId zero = new LongObjectId(0L, 0L, 0L, 0L); + assertEquals(zero, LongObjectId.zeroId()); + assertEquals( + "0000000000000000000000000000000000000000000000000000000000000000", + LongObjectId.zeroId().name()); + } + + @Test + public void testEquals() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + assertTrue("id should equal itself", id1.equals(id1)); + AnyLongObjectId id2 = new LongObjectId(id1); + assertEquals("objects should be equals", id1, id2); + + id2 = LongObjectIdTestUtils.hash("other"); + assertNotEquals("objects should be not equal", id1, id2); + } + + @Test + public void testCopyRawBytes() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + AnyLongObjectId id2 = new LongObjectId(id1); + + byte[] buf = new byte[64]; + id1.copyRawTo(buf, 0); + id2.copyRawTo(buf, 32); + assertTrue("objects should be equals", + LongObjectId.equals(buf, 0, buf, 32)); + } + + @Test + public void testCopyRawLongs() { + long[] a = new long[4]; + a[0] = 1L; + a[1] = 2L; + a[2] = 3L; + a[3] = 4L; + AnyLongObjectId id1 = new LongObjectId(a[0], a[1], a[2], a[3]); + AnyLongObjectId id2 = LongObjectId.fromRaw(a); + assertEquals("objects should be equals", id1, id2); + } + + @Test + public void testCopyFromStringInvalid() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + try { + LongObjectId.fromString(id1.name() + "01234"); + fail("expected InvalidLongObjectIdException"); + } catch (InvalidLongObjectIdException e) { + assertEquals("Invalid id: " + id1.name() + "01234", + e.getMessage()); + } + } + + @Test + public void testCopyFromStringByte() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + byte[] buf = new byte[64]; + Charset cs = StandardCharsets.US_ASCII; + cs.encode(id1.name()).get(buf); + AnyLongObjectId id2 = LongObjectId.fromString(buf, 0); + assertEquals("objects should be equals", id1, id2); + } + + @Test + public void testHashFile() throws IOException { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + Path f = tmp.resolve("test"); + JGitTestUtil.write(f.toFile(), "test"); + AnyLongObjectId id2 = LongObjectIdTestUtils.hash(f); + assertEquals("objects should be equals", id1, id2); + } + + @Test + public void testCompareTo() { + AnyLongObjectId id1 = LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"); + assertEquals(0, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"))); + assertEquals(0, id1.compareTo(id1)); + + assertEquals(-1, id1.compareTo(LongObjectId.fromString( + "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"))); + assertEquals(-1, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef"))); + assertEquals(-1, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef"))); + assertEquals(-1, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef"))); + + assertEquals(1, id1.compareTo(LongObjectId.fromString( + "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"))); + assertEquals(1, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef"))); + assertEquals(1, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef"))); + assertEquals(1, id1.compareTo(LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef"))); + } + + @Test + public void testCompareToByte() { + AnyLongObjectId id1 = LongObjectId.fromString( + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"); + byte[] buf = new byte[32]; + id1.copyRawTo(buf, 0); + assertEquals(0, id1.compareTo(buf, 0)); + + LongObjectId + .fromString( + "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") + .copyRawTo(buf, 0); + assertEquals(-1, id1.compareTo(buf, 0)); + + LongObjectId + .fromString( + "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") + .copyRawTo(buf, 0); + assertEquals(1, id1.compareTo(buf, 0)); + } + + @Test + public void testCompareToLong() { + AnyLongObjectId id1 = new LongObjectId(1L, 2L, 3L, 4L); + long[] buf = new long[4]; + id1.copyRawTo(buf, 0); + assertEquals(0, id1.compareTo(buf, 0)); + + new LongObjectId(2L, 2L, 3L, 4L).copyRawTo(buf, 0); + assertEquals(-1, id1.compareTo(buf, 0)); + + new LongObjectId(0L, 2L, 3L, 4L).copyRawTo(buf, 0); + assertEquals(1, id1.compareTo(buf, 0)); + } + + @Test + public void testCopyToByte() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + byte[] buf = new byte[64]; + id1.copyTo(buf, 0); + assertEquals(id1, LongObjectId.fromString(buf, 0)); + } + + @Test + public void testCopyRawToByteBuffer() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + ByteBuffer buf = ByteBuffer.allocate(32); + id1.copyRawTo(buf); + assertEquals(id1, LongObjectId.fromRaw(buf.array(), 0)); + } + + @Test + public void testCopyToByteBuffer() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + ByteBuffer buf = ByteBuffer.allocate(64); + id1.copyTo(buf); + assertEquals(id1, LongObjectId.fromString(buf.array(), 0)); + } + + @Test + public void testCopyRawToOutputStream() throws IOException { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + ByteArrayOutputStream os = new ByteArrayOutputStream(32); + id1.copyRawTo(os); + assertEquals(id1, LongObjectId.fromRaw(os.toByteArray(), 0)); + } + + @Test + public void testCopyToOutputStream() throws IOException { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + ByteArrayOutputStream os = new ByteArrayOutputStream(64); + id1.copyTo(os); + assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0)); + } + + @Test + public void testCopyToWriter() throws IOException { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + ByteArrayOutputStream os = new ByteArrayOutputStream(64); + OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET); + id1.copyTo(w); + w.close(); + assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0)); + } + + @Test + public void testCopyToWriterWithBuf() throws IOException { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + ByteArrayOutputStream os = new ByteArrayOutputStream(64); + OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET); + char[] buf = new char[64]; + id1.copyTo(buf, w); + w.close(); + assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0)); + } + + @Test + public void testCopyToStringBuilder() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + StringBuilder sb = new StringBuilder(); + char[] buf = new char[64]; + id1.copyTo(buf, sb); + assertEquals(id1, LongObjectId.fromString(sb.toString())); + } + + @Test + public void testCopy() { + AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test"); + assertEquals(id1.copy(), id1); + MutableLongObjectId id2 = new MutableLongObjectId(); + id2.fromObjectId(id1); + assertEquals(id1, id2.copy()); + } +} diff --git a/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/MutableLongObjectIdTest.java b/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/MutableLongObjectIdTest.java new file mode 100644 index 0000000000..9bdc3ab0c0 --- /dev/null +++ b/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/lib/MutableLongObjectIdTest.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.lfs.lib; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/* + * Ported to SHA-256 from org.eclipse.jgit.lib.MutableObjectIdTest + */ +public class MutableLongObjectIdTest { + + @Test + public void testFromRawLong() { + MutableLongObjectId m = new MutableLongObjectId(); + m.fromRaw(new long[] { 1L, 2L, 3L, 4L }); + assertEquals(new LongObjectId(1L, 2L, 3L, 4L), m); + } + + @Test + public void testFromString() { + AnyLongObjectId id = new LongObjectId(1L, 2L, 3L, 4L); + MutableLongObjectId m = new MutableLongObjectId(); + m.fromString(id.name()); + assertEquals(id, m); + } + + @Test + public void testFromStringByte() { + AnyLongObjectId id = new LongObjectId(1L, 2L, 3L, 4L); + MutableLongObjectId m = new MutableLongObjectId(); + byte[] buf = new byte[64]; + id.copyTo(buf, 0); + m.fromString(buf, 0); + assertEquals(id, m); + } + + @Test + public void testCopy() { + MutableLongObjectId m = new MutableLongObjectId(); + m.fromRaw(new long[] { 1L, 2L, 3L, 4L }); + assertEquals(m, new MutableLongObjectId(m)); + } + + @Test + public void testToObjectId() { + MutableLongObjectId m = new MutableLongObjectId(); + m.fromRaw(new long[] { 1L, 2L, 3L, 4L }); + assertEquals(m, m.toObjectId()); + } +} |