Migrated core and extension modules to Kotlin
(Except BitmessageContext and Bytes)
This commit is contained in:
		@@ -1,3 +1,19 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2017 Christian Basler
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package ch.dissem.bitmessage.security;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.InternalContext;
 | 
			
		||||
@@ -11,6 +27,7 @@ import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
 | 
			
		||||
import ch.dissem.bitmessage.utils.CallbackWaiter;
 | 
			
		||||
import ch.dissem.bitmessage.utils.Singleton;
 | 
			
		||||
import ch.dissem.bitmessage.utils.UnixTime;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.BeforeClass;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
@@ -31,12 +48,12 @@ import static org.mockito.Mockito.when;
 | 
			
		||||
public class CryptographyTest {
 | 
			
		||||
    public static final byte[] TEST_VALUE = "teststring".getBytes();
 | 
			
		||||
    public static final byte[] TEST_SHA1 = DatatypeConverter.parseHexBinary(""
 | 
			
		||||
            + "b8473b86d4c2072ca9b08bd28e373e8253e865c4");
 | 
			
		||||
        + "b8473b86d4c2072ca9b08bd28e373e8253e865c4");
 | 
			
		||||
    public static final byte[] TEST_SHA512 = DatatypeConverter.parseHexBinary(""
 | 
			
		||||
            + "6253b39071e5df8b5098f59202d414c37a17d6a38a875ef5f8c7d89b0212b028"
 | 
			
		||||
            + "692d3d2090ce03ae1de66c862fa8a561e57ed9eb7935ce627344f742c0931d72");
 | 
			
		||||
        + "6253b39071e5df8b5098f59202d414c37a17d6a38a875ef5f8c7d89b0212b028"
 | 
			
		||||
        + "692d3d2090ce03ae1de66c862fa8a561e57ed9eb7935ce627344f742c0931d72");
 | 
			
		||||
    public static final byte[] TEST_RIPEMD160 = DatatypeConverter.parseHexBinary(""
 | 
			
		||||
            + "cd566972b5e50104011a92b59fa8e0b1234851ae");
 | 
			
		||||
        + "cd566972b5e50104011a92b59fa8e0b1234851ae");
 | 
			
		||||
 | 
			
		||||
    private static SpongyCryptography crypto;
 | 
			
		||||
 | 
			
		||||
@@ -45,62 +62,62 @@ public class CryptographyTest {
 | 
			
		||||
        crypto = new SpongyCryptography();
 | 
			
		||||
        Singleton.initialize(crypto);
 | 
			
		||||
        InternalContext ctx = mock(InternalContext.class);
 | 
			
		||||
        InternalContext.Companion.setValue(null, null, ctx);
 | 
			
		||||
        when(ctx.getProofOfWorkEngine()).thenReturn(new MultiThreadedPOWEngine());
 | 
			
		||||
        crypto.setContext(ctx);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testRipemd160() {
 | 
			
		||||
        assertArrayEquals(TEST_RIPEMD160, crypto.ripemd160(TEST_VALUE));
 | 
			
		||||
        Assert.assertArrayEquals(TEST_RIPEMD160, crypto.ripemd160(TEST_VALUE));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSha1() {
 | 
			
		||||
        assertArrayEquals(TEST_SHA1, crypto.sha1(TEST_VALUE));
 | 
			
		||||
        Assert.assertArrayEquals(TEST_SHA1, crypto.sha1(TEST_VALUE));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSha512() {
 | 
			
		||||
        assertArrayEquals(TEST_SHA512, crypto.sha512(TEST_VALUE));
 | 
			
		||||
        Assert.assertArrayEquals(TEST_SHA512, crypto.sha512(TEST_VALUE));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testChaining() {
 | 
			
		||||
        assertArrayEquals(TEST_SHA512, crypto.sha512("test".getBytes(), "string".getBytes()));
 | 
			
		||||
        Assert.assertArrayEquals(TEST_SHA512, crypto.sha512("test".getBytes(), "string".getBytes()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureDoubleHashYieldsSameResultAsHashOfHash() {
 | 
			
		||||
        assertArrayEquals(crypto.sha512(TEST_SHA512), crypto.doubleSha512(TEST_VALUE));
 | 
			
		||||
        Assert.assertArrayEquals(crypto.sha512(TEST_SHA512), crypto.doubleSha512(TEST_VALUE));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test(expected = IOException.class)
 | 
			
		||||
    public void ensureExceptionForInsufficientProofOfWork() throws IOException {
 | 
			
		||||
        ObjectMessage objectMessage = new ObjectMessage.Builder()
 | 
			
		||||
                .nonce(new byte[8])
 | 
			
		||||
                .expiresTime(UnixTime.now(+28 * DAY))
 | 
			
		||||
                .objectType(0)
 | 
			
		||||
                .payload(GenericPayload.read(0, 1, new ByteArrayInputStream(new byte[0]), 0))
 | 
			
		||||
                .build();
 | 
			
		||||
            .nonce(new byte[8])
 | 
			
		||||
            .expiresTime(UnixTime.now() + 28 * DAY)
 | 
			
		||||
            .objectType(0)
 | 
			
		||||
            .payload(GenericPayload.Companion.read(0, 1, new ByteArrayInputStream(new byte[0]), 0))
 | 
			
		||||
            .build();
 | 
			
		||||
        crypto.checkProofOfWork(objectMessage, 1000, 1000);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testDoProofOfWork() throws Exception {
 | 
			
		||||
        ObjectMessage objectMessage = new ObjectMessage.Builder()
 | 
			
		||||
                .nonce(new byte[8])
 | 
			
		||||
                .expiresTime(UnixTime.now(+2 * MINUTE))
 | 
			
		||||
                .objectType(0)
 | 
			
		||||
                .payload(GenericPayload.read(0, 1, new ByteArrayInputStream(new byte[0]), 0))
 | 
			
		||||
                .build();
 | 
			
		||||
            .nonce(new byte[8])
 | 
			
		||||
            .expiresTime(UnixTime.now() + 2 * MINUTE)
 | 
			
		||||
            .objectType(0)
 | 
			
		||||
            .payload(GenericPayload.Companion.read(0, 1, new ByteArrayInputStream(new byte[0]), 0))
 | 
			
		||||
            .build();
 | 
			
		||||
        final CallbackWaiter<byte[]> waiter = new CallbackWaiter<>();
 | 
			
		||||
        crypto.doProofOfWork(objectMessage, 1000, 1000,
 | 
			
		||||
                new ProofOfWorkEngine.Callback() {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
 | 
			
		||||
                        waiter.setValue(nonce);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
            new ProofOfWorkEngine.Callback() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
 | 
			
		||||
                    waiter.setValue(nonce);
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        objectMessage.setNonce(waiter.waitForValue());
 | 
			
		||||
        try {
 | 
			
		||||
            crypto.checkProofOfWork(objectMessage, 1000, 1000);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user