From 25a4caec2a65dda9c947799450bce505a855c421 Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Wed, 14 Dec 2016 13:14:47 -0200 Subject: [PATCH] Add Unit tests for CryptoComponent#hash() --- .../briarproject/bramble/crypto/HashTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bramble-core/src/test/java/org/briarproject/bramble/crypto/HashTest.java diff --git a/bramble-core/src/test/java/org/briarproject/bramble/crypto/HashTest.java b/bramble-core/src/test/java/org/briarproject/bramble/crypto/HashTest.java new file mode 100644 index 0000000000..3b5ba056f2 --- /dev/null +++ b/bramble-core/src/test/java/org/briarproject/bramble/crypto/HashTest.java @@ -0,0 +1,50 @@ +package org.briarproject.bramble.crypto; + +import org.briarproject.bramble.BrambleTestCase; +import org.briarproject.bramble.TestSeedProvider; +import org.briarproject.bramble.TestUtils; +import org.briarproject.bramble.api.crypto.CryptoComponent; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; + +public class HashTest extends BrambleTestCase { + + private final CryptoComponent crypto; + + private final String label = TestUtils.getRandomString(42); + private final byte[] inputBytes = TestUtils.getRandomBytes(123); + private final byte[] inputBytes1 = TestUtils.getRandomBytes(234); + private final byte[] inputBytes2 = new byte[0]; + + public HashTest() { + crypto = new CryptoComponentImpl(new TestSeedProvider()); + } + + @Test + public void testIdenticalInputsProduceIdenticalHashes() { + byte[] hash1 = crypto.hash(label, inputBytes, inputBytes1, inputBytes2); + byte[] hash2 = crypto.hash(label, inputBytes, inputBytes1, inputBytes2); + assertArrayEquals(hash1, hash2); + } + + @Test + public void testDifferentInputsProduceDifferentHashes() { + byte[] hash1 = crypto.hash(label, inputBytes, inputBytes1, inputBytes2); + byte[] hash2 = crypto.hash(label, inputBytes2, inputBytes1, inputBytes); + assertFalse(Arrays.equals(hash1, hash2)); + } + + @Test + public void testDifferentLabelsProduceDifferentHashes() { + String label2 = TestUtils.getRandomString(42); + byte[] hash1 = crypto.hash(label, inputBytes, inputBytes1, inputBytes2); + byte[] hash2 = + crypto.hash(label2, inputBytes, inputBytes1, inputBytes2); + assertFalse(Arrays.equals(hash1, hash2)); + } + +} -- GitLab