Renamed module 'domain' to 'core' to make its purpose more clear
This commit is contained in:
		
							
								
								
									
										55
									
								
								core/src/test/java/ch/dissem/bitmessage/DecryptionTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								core/src/test/java/ch/dissem/bitmessage/DecryptionTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
 | 
			
		||||
import ch.dissem.bitmessage.entity.ObjectMessage;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.V4Broadcast;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.V5Broadcast;
 | 
			
		||||
import ch.dissem.bitmessage.exception.DecryptionFailedException;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestBase;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestUtils;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
import static org.junit.Assert.assertTrue;
 | 
			
		||||
 | 
			
		||||
public class DecryptionTest extends TestBase {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV4BroadcastIsDecryptedCorrectly() throws IOException, DecryptionFailedException {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ");
 | 
			
		||||
        TestUtils.loadPubkey(address);
 | 
			
		||||
        ObjectMessage objectMessage = TestUtils.loadObjectMessage(5, "V4Broadcast.payload");
 | 
			
		||||
        V4Broadcast broadcast = (V4Broadcast) objectMessage.getPayload();
 | 
			
		||||
        broadcast.decrypt(address);
 | 
			
		||||
        assertEquals("Test-Broadcast", broadcast.getPlaintext().getSubject());
 | 
			
		||||
        assertTrue(objectMessage.isSignatureValid(address.getPubkey()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV5BroadcastIsDecryptedCorrectly() throws IOException, DecryptionFailedException {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-2cXxfcSetKnbHJX2Y85rSkaVpsdNUZ5q9h");
 | 
			
		||||
        TestUtils.loadPubkey(address);
 | 
			
		||||
        ObjectMessage objectMessage = TestUtils.loadObjectMessage(5, "V5Broadcast.payload");
 | 
			
		||||
        V5Broadcast broadcast = (V5Broadcast) objectMessage.getPayload();
 | 
			
		||||
        broadcast.decrypt(address);
 | 
			
		||||
        assertEquals("Test-Broadcast", broadcast.getPlaintext().getSubject());
 | 
			
		||||
        assertTrue(objectMessage.isSignatureValid(address.getPubkey()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										64
									
								
								core/src/test/java/ch/dissem/bitmessage/EncryptionTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								core/src/test/java/ch/dissem/bitmessage/EncryptionTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
 | 
			
		||||
import ch.dissem.bitmessage.entity.ObjectMessage;
 | 
			
		||||
import ch.dissem.bitmessage.entity.Plaintext;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.CryptoBox;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.GenericPayload;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.Msg;
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
 | 
			
		||||
import ch.dissem.bitmessage.exception.DecryptionFailedException;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestBase;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestUtils;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import static ch.dissem.bitmessage.utils.Singleton.security;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
import static org.junit.Assert.assertNotNull;
 | 
			
		||||
 | 
			
		||||
public class EncryptionTest extends TestBase {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureDecryptedDataIsSameAsBeforeEncryption() throws IOException, DecryptionFailedException {
 | 
			
		||||
        GenericPayload before = new GenericPayload(0, 1, security().randomBytes(100));
 | 
			
		||||
 | 
			
		||||
        PrivateKey privateKey = new PrivateKey(false, 1, 1000, 1000);
 | 
			
		||||
        CryptoBox cryptoBox = new CryptoBox(before, privateKey.getPubkey().getEncryptionKey());
 | 
			
		||||
 | 
			
		||||
        GenericPayload after = GenericPayload.read(0, cryptoBox.decrypt(privateKey.getPrivateEncryptionKey()), 1, 100);
 | 
			
		||||
 | 
			
		||||
        assertEquals(before, after);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureMessageCanBeDecrypted() throws IOException, DecryptionFailedException {
 | 
			
		||||
        PrivateKey privateKey = PrivateKey.read(TestUtils.getResource("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8.privkey"));
 | 
			
		||||
        BitmessageAddress identity = new BitmessageAddress(privateKey);
 | 
			
		||||
        assertEquals("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8", identity.getAddress());
 | 
			
		||||
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(3, "V1Msg.payload");
 | 
			
		||||
        Msg msg = (Msg) object.getPayload();
 | 
			
		||||
        msg.decrypt(privateKey.getPrivateEncryptionKey());
 | 
			
		||||
        Plaintext plaintext = msg.getPlaintext();
 | 
			
		||||
        assertNotNull(plaintext);
 | 
			
		||||
        assertEquals("Test", plaintext.getSubject());
 | 
			
		||||
        assertEquals("Hallo, das ist ein Test von der v4-Adresse", plaintext.getText());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										71
									
								
								core/src/test/java/ch/dissem/bitmessage/SignatureTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								core/src/test/java/ch/dissem/bitmessage/SignatureTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,71 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
 | 
			
		||||
import ch.dissem.bitmessage.entity.ObjectMessage;
 | 
			
		||||
import ch.dissem.bitmessage.entity.Plaintext;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.Msg;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.ObjectType;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.Pubkey;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.V4Pubkey;
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
 | 
			
		||||
import ch.dissem.bitmessage.exception.DecryptionFailedException;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestBase;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestUtils;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
 | 
			
		||||
public class SignatureTest extends TestBase {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureValidationWorks() throws IOException {
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(3, "V3Pubkey.payload");
 | 
			
		||||
        Pubkey pubkey = (Pubkey) object.getPayload();
 | 
			
		||||
        assertTrue(object.isSignatureValid(pubkey));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureSigningWorks() throws IOException {
 | 
			
		||||
        PrivateKey privateKey = new PrivateKey(false, 1, 1000, 1000);
 | 
			
		||||
 | 
			
		||||
        ObjectMessage objectMessage = new ObjectMessage.Builder()
 | 
			
		||||
                .objectType(ObjectType.PUBKEY)
 | 
			
		||||
                .stream(1)
 | 
			
		||||
                .payload(privateKey.getPubkey())
 | 
			
		||||
                .build();
 | 
			
		||||
        objectMessage.sign(privateKey);
 | 
			
		||||
 | 
			
		||||
        assertTrue(objectMessage.isSignatureValid(privateKey.getPubkey()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureMessageIsProperlySigned() throws IOException, DecryptionFailedException {
 | 
			
		||||
        BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8");
 | 
			
		||||
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(3, "V1Msg.payload");
 | 
			
		||||
        Msg msg = (Msg) object.getPayload();
 | 
			
		||||
        msg.decrypt(identity.getPrivateKey().getPrivateEncryptionKey());
 | 
			
		||||
        Plaintext plaintext = msg.getPlaintext();
 | 
			
		||||
        assertEquals(TestUtils.loadContact().getPubkey(), plaintext.getFrom().getPubkey());
 | 
			
		||||
        assertNotNull(plaintext);
 | 
			
		||||
        assertTrue(object.isSignatureValid(plaintext.getFrom().getPubkey()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,143 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.entity;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.Pubkey;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.V4Pubkey;
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
 | 
			
		||||
import ch.dissem.bitmessage.exception.DecryptionFailedException;
 | 
			
		||||
import ch.dissem.bitmessage.utils.*;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
 | 
			
		||||
import static ch.dissem.bitmessage.entity.payload.Pubkey.Feature.DOES_ACK;
 | 
			
		||||
import static ch.dissem.bitmessage.utils.Singleton.security;
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
 | 
			
		||||
public class BitmessageAddressTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureBase58DecodesCorrectly() {
 | 
			
		||||
        assertHexEquals("800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D",
 | 
			
		||||
                Base58.decode("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureAddressStaysSame() {
 | 
			
		||||
        String address = "BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ";
 | 
			
		||||
        assertEquals(address, new BitmessageAddress(address).toString());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureStreamAndVersionAreParsed() {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ");
 | 
			
		||||
        assertEquals(1, address.getStream());
 | 
			
		||||
        assertEquals(3, address.getVersion());
 | 
			
		||||
 | 
			
		||||
        address = new BitmessageAddress("BM-87hJ99tPAXxtetvnje7Z491YSvbEtBJVc5e");
 | 
			
		||||
        assertEquals(1, address.getStream());
 | 
			
		||||
        assertEquals(4, address.getVersion());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateAddress() {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress(new PrivateKey(false, 1, 1000, 1000, DOES_ACK));
 | 
			
		||||
        assertNotNull(address.getPubkey());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testV2PubkeyImport() throws IOException {
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(2, "V2Pubkey.payload");
 | 
			
		||||
        Pubkey pubkey = (Pubkey) object.getPayload();
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT");
 | 
			
		||||
        address.setPubkey(pubkey);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testV3PubkeyImport() throws IOException {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ");
 | 
			
		||||
        assertArrayEquals(Bytes.fromHex("007402be6e76c3cb87caa946d0c003a3d4d8e1d5"), address.getRipe());
 | 
			
		||||
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(3, "V3Pubkey.payload");
 | 
			
		||||
        Pubkey pubkey = (Pubkey) object.getPayload();
 | 
			
		||||
        assertTrue(object.isSignatureValid(pubkey));
 | 
			
		||||
        address.setPubkey(pubkey);
 | 
			
		||||
 | 
			
		||||
        assertArrayEquals(Bytes.fromHex("007402be6e76c3cb87caa946d0c003a3d4d8e1d5"), pubkey.getRipe());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testV4PubkeyImport() throws IOException, DecryptionFailedException {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-2cXxfcSetKnbHJX2Y85rSkaVpsdNUZ5q9h");
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(4, "V4Pubkey.payload");
 | 
			
		||||
        object.decrypt(address.getPublicDecryptionKey());
 | 
			
		||||
        V4Pubkey pubkey = (V4Pubkey) object.getPayload();
 | 
			
		||||
        assertTrue(object.isSignatureValid(pubkey));
 | 
			
		||||
        address.setPubkey(pubkey);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testV3AddressImport() throws IOException {
 | 
			
		||||
        String address_string = "BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn";
 | 
			
		||||
        assertEquals(3, new BitmessageAddress(address_string).getVersion());
 | 
			
		||||
        assertEquals(1, new BitmessageAddress(address_string).getStream());
 | 
			
		||||
 | 
			
		||||
        byte[] privsigningkey = getSecret("5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9");
 | 
			
		||||
        byte[] privencryptionkey = getSecret("5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck");
 | 
			
		||||
 | 
			
		||||
        System.out.println("\n\n" + Strings.hex(privsigningkey) + "\n\n");
 | 
			
		||||
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress(new PrivateKey(privsigningkey, privencryptionkey,
 | 
			
		||||
                security().createPubkey(3, 1, privsigningkey, privencryptionkey, 320, 14000)));
 | 
			
		||||
        assertEquals(address_string, address.getAddress());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testGetSecret() throws IOException {
 | 
			
		||||
        assertHexEquals("0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D",
 | 
			
		||||
                getSecret("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private byte[] getSecret(String walletImportFormat) throws IOException {
 | 
			
		||||
        byte[] bytes = Base58.decode(walletImportFormat);
 | 
			
		||||
        if (bytes[0] != (byte) 0x80)
 | 
			
		||||
            throw new IOException("Unknown format: 0x80 expected as first byte, but secret " + walletImportFormat + " was " + bytes[0]);
 | 
			
		||||
        if (bytes.length != 37)
 | 
			
		||||
            throw new IOException("Unknown format: 37 bytes expected, but secret " + walletImportFormat + " was " + bytes.length + " long");
 | 
			
		||||
 | 
			
		||||
        byte[] hash = security().doubleSha256(bytes, 33);
 | 
			
		||||
        for (int i = 0; i < 4; i++) {
 | 
			
		||||
            if (hash[i] != bytes[33 + i]) throw new IOException("Hash check failed for secret " + walletImportFormat);
 | 
			
		||||
        }
 | 
			
		||||
        return Arrays.copyOfRange(bytes, 1, 33);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testV4AddressImport() throws IOException {
 | 
			
		||||
        assertEquals(4, new BitmessageAddress("BM-2cV5f9EpzaYARxtoruSpa6pDoucSf9ZNke").getVersion());
 | 
			
		||||
        byte[] privsigningkey = getSecret("5KMWqfCyJZGFgW6QrnPJ6L9Gatz25B51y7ErgqNr1nXUVbtZbdU");
 | 
			
		||||
        byte[] privencryptionkey = getSecret("5JXXWEuhHQEPk414SzEZk1PHDRi8kCuZd895J7EnKeQSahJPxGz");
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress(new PrivateKey(privsigningkey, privencryptionkey,
 | 
			
		||||
                security().createPubkey(4, 1, privsigningkey, privencryptionkey, 320, 14000)));
 | 
			
		||||
        assertEquals("BM-2cV5f9EpzaYARxtoruSpa6pDoucSf9ZNke", address.getAddress());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void assertHexEquals(String hex, byte[] bytes) {
 | 
			
		||||
        assertEquals(hex.toLowerCase(), Strings.hex(bytes).toString().toLowerCase());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,125 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.entity;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.*;
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.Label;
 | 
			
		||||
import ch.dissem.bitmessage.factory.Factory;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestBase;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestUtils;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
 | 
			
		||||
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
 | 
			
		||||
public class SerializationTest extends TestBase {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureGetPubkeyIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V2GetPubkey.payload", 2, GetPubkey.class);
 | 
			
		||||
        doTest("V3GetPubkey.payload", 2, GetPubkey.class);
 | 
			
		||||
        doTest("V4GetPubkey.payload", 2, GetPubkey.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV2PubkeyIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V2Pubkey.payload", 2, V2Pubkey.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV3PubkeyIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V3Pubkey.payload", 3, V3Pubkey.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV4PubkeyIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V4Pubkey.payload", 4, V4Pubkey.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV1MsgIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V1Msg.payload", 1, Msg.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV4BroadcastIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V4Broadcast.payload", 4, V4Broadcast.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureV5BroadcastIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V5Broadcast.payload", 5, V5Broadcast.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureUnknownDataIsDeserializedAndSerializedCorrectly() throws IOException {
 | 
			
		||||
        doTest("V1MsgStrangeData.payload", 1, GenericPayload.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensurePlaintextIsSerializedAndDeserializedCorrectly() throws Exception {
 | 
			
		||||
        Plaintext p1 = new Plaintext.Builder(MSG)
 | 
			
		||||
                .from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
 | 
			
		||||
                .to(TestUtils.loadContact())
 | 
			
		||||
                .message("Subject", "Message")
 | 
			
		||||
                .ack("ack".getBytes())
 | 
			
		||||
                .signature(new byte[0])
 | 
			
		||||
                .build();
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
        p1.write(out);
 | 
			
		||||
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
 | 
			
		||||
        Plaintext p2 = Plaintext.read(MSG, in);
 | 
			
		||||
 | 
			
		||||
        // Received is automatically set on deserialization, so we'll need to set it to 0
 | 
			
		||||
        Field received = Plaintext.class.getDeclaredField("received");
 | 
			
		||||
        received.setAccessible(true);
 | 
			
		||||
        received.set(p2, 0L);
 | 
			
		||||
 | 
			
		||||
        assertEquals(p1, p2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void doTest(String resourceName, int version, Class<?> expectedPayloadType) throws IOException {
 | 
			
		||||
        byte[] data = TestUtils.getBytes(resourceName);
 | 
			
		||||
        InputStream in = new ByteArrayInputStream(data);
 | 
			
		||||
        ObjectMessage object = Factory.getObjectMessage(version, in, data.length);
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
        assertNotNull(object);
 | 
			
		||||
        object.write(out);
 | 
			
		||||
        assertArrayEquals(data, out.toByteArray());
 | 
			
		||||
        assertEquals(expectedPayloadType.getCanonicalName(), object.getPayload().getClass().getCanonicalName());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureSystemSerializationWorks() throws Exception {
 | 
			
		||||
        Plaintext plaintext = new Plaintext.Builder(MSG)
 | 
			
		||||
                .from(TestUtils.loadContact())
 | 
			
		||||
                .to(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
 | 
			
		||||
                .labels(Collections.singletonList(new Label("Test", Label.Type.INBOX, 0)))
 | 
			
		||||
                .message("Test", "Test Test.\nTest")
 | 
			
		||||
                .build();
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
        ObjectOutputStream oos = new ObjectOutputStream(out);
 | 
			
		||||
        oos.writeObject(plaintext);
 | 
			
		||||
 | 
			
		||||
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
 | 
			
		||||
        ObjectInputStream ois = new ObjectInputStream(in);
 | 
			
		||||
        assertEquals(plaintext, ois.readObject());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,72 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.ports;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.utils.Bytes;
 | 
			
		||||
import ch.dissem.bitmessage.utils.CallbackWaiter;
 | 
			
		||||
import ch.dissem.bitmessage.utils.TestBase;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import static ch.dissem.bitmessage.utils.Singleton.security;
 | 
			
		||||
import static org.junit.Assert.assertTrue;
 | 
			
		||||
 | 
			
		||||
public class ProofOfWorkEngineTest extends TestBase {
 | 
			
		||||
    @Test(timeout = 90_000)
 | 
			
		||||
    public void testSimplePOWEngine() throws InterruptedException {
 | 
			
		||||
        testPOW(new SimplePOWEngine());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test(timeout = 90_000)
 | 
			
		||||
    public void testThreadedPOWEngine() throws InterruptedException {
 | 
			
		||||
        testPOW(new MultiThreadedPOWEngine());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void testPOW(ProofOfWorkEngine engine) throws InterruptedException {
 | 
			
		||||
        byte[] initialHash = security().sha512(new byte[]{1, 3, 6, 4});
 | 
			
		||||
        byte[] target = {0, 0, 0, -1, -1, -1, -1, -1};
 | 
			
		||||
 | 
			
		||||
        final CallbackWaiter<byte[]> waiter1 = new CallbackWaiter<>();
 | 
			
		||||
        engine.calculateNonce(initialHash, target,
 | 
			
		||||
                new ProofOfWorkEngine.Callback() {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
 | 
			
		||||
                        waiter1.setValue(nonce);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
        byte[] nonce = waiter1.waitForValue();
 | 
			
		||||
        System.out.println("Calculating nonce took " + waiter1.getTime() + "ms");
 | 
			
		||||
        assertTrue(Bytes.lt(security().doubleSha512(nonce, initialHash), target, 8));
 | 
			
		||||
 | 
			
		||||
        // Let's add a second (shorter) run to find possible multi threading issues
 | 
			
		||||
        byte[] initialHash2 = security().sha512(new byte[]{1, 3, 6, 5});
 | 
			
		||||
        byte[] target2 = {0, 0, -1, -1, -1, -1, -1, -1};
 | 
			
		||||
 | 
			
		||||
        final CallbackWaiter<byte[]> waiter2 = new CallbackWaiter<>();
 | 
			
		||||
        engine.calculateNonce(initialHash2, target2,
 | 
			
		||||
                new ProofOfWorkEngine.Callback() {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
 | 
			
		||||
                        waiter2.setValue(nonce);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
        byte[] nonce2 = waiter2.waitForValue();
 | 
			
		||||
        System.out.println("Calculating nonce took " + waiter2.getTime() + "ms");
 | 
			
		||||
        assertTrue(Bytes.lt(security().doubleSha512(nonce2, initialHash2), target2, 8));
 | 
			
		||||
        assertTrue("Second nonce must be quicker to find", waiter1.getTime() > waiter2.getTime());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										97
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/BytesTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/BytesTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,97 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.math.BigInteger;
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertArrayEquals;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
public class BytesTest {
 | 
			
		||||
    public static final Random rnd = new Random();
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureExpandsCorrectly() {
 | 
			
		||||
        byte[] source = {1};
 | 
			
		||||
        byte[] expected = {0, 1};
 | 
			
		||||
        assertArrayEquals(expected, Bytes.expand(source, 2));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureIncrementCarryWorks() throws IOException {
 | 
			
		||||
        byte[] bytes = {0, -1};
 | 
			
		||||
        Bytes.inc(bytes);
 | 
			
		||||
        assertArrayEquals(TestUtils.int16(256), bytes);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testIncrementByValue() throws IOException {
 | 
			
		||||
        for (int v = 0; v < 256; v++) {
 | 
			
		||||
            for (int i = 1; i < 256; i++) {
 | 
			
		||||
                byte[] bytes = {0, (byte) v};
 | 
			
		||||
                Bytes.inc(bytes, (byte) i);
 | 
			
		||||
                assertArrayEquals("value = " + v + "; inc = " + i + "; expected = " + (v + i), TestUtils.int16(v + i), bytes);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This test is used to compare different implementations of the single byte lt comparison. It an safely be ignored.
 | 
			
		||||
     */
 | 
			
		||||
    @Test
 | 
			
		||||
    @Ignore
 | 
			
		||||
    public void testLowerThanSingleByte() {
 | 
			
		||||
        byte[] a = new byte[1];
 | 
			
		||||
        byte[] b = new byte[1];
 | 
			
		||||
        for (int i = 0; i < 255; i++) {
 | 
			
		||||
            for (int j = 0; j < 255; j++) {
 | 
			
		||||
                System.out.println("a = " + i + "\tb = " + j);
 | 
			
		||||
                a[0] = (byte) i;
 | 
			
		||||
                b[0] = (byte) j;
 | 
			
		||||
                assertEquals(i < j, Bytes.lt(a, b));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testLowerThan() {
 | 
			
		||||
        for (int i = 0; i < 1000; i++) {
 | 
			
		||||
            BigInteger a = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
 | 
			
		||||
            BigInteger b = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
 | 
			
		||||
            System.out.println("a = " + a.toString(16) + "\tb = " + b.toString(16));
 | 
			
		||||
            assertEquals(a.compareTo(b) == -1, Bytes.lt(a.toByteArray(), b.toByteArray()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testLowerThanBounded() {
 | 
			
		||||
        for (int i = 0; i < 1000; i++) {
 | 
			
		||||
            BigInteger a = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
 | 
			
		||||
            BigInteger b = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
 | 
			
		||||
            System.out.println("a = " + a.toString(16) + "\tb = " + b.toString(16));
 | 
			
		||||
            assertEquals(a.compareTo(b) == -1, Bytes.lt(
 | 
			
		||||
                    Bytes.expand(a.toByteArray(), 100),
 | 
			
		||||
                    Bytes.expand(b.toByteArray(), 100),
 | 
			
		||||
                    100));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,35 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.LinkedList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
public class CollectionsTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureSelectRandomReturnsMaximumPossibleItems() throws Exception {
 | 
			
		||||
        List<Integer> list = new LinkedList<>();
 | 
			
		||||
        for (int i = 0; i < 10; i++) {
 | 
			
		||||
            list.add(i);
 | 
			
		||||
        }
 | 
			
		||||
        assertEquals(9, Collections.selectRandom(9, list).size());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,40 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.*;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
public class DecodeTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void ensureDecodingWorks() throws Exception {
 | 
			
		||||
        // This should test all relevant cases for var_int and therefore also uint_16, uint_32 and int_64
 | 
			
		||||
        testCodec(0);
 | 
			
		||||
        for (long i = 1; i > 0; i = 3 * i + 7) {
 | 
			
		||||
            testCodec(i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void testCodec(long number) throws IOException {
 | 
			
		||||
        ByteArrayOutputStream is = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(number, is);
 | 
			
		||||
        assertEquals(number, Decode.varInt(new ByteArrayInputStream(is.toByteArray())));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										124
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/EncodeTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/EncodeTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,124 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.io.ByteArrayOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
public class EncodeTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testUint8() throws IOException {
 | 
			
		||||
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int8(0, stream);
 | 
			
		||||
        checkBytes(stream, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int8(255, stream);
 | 
			
		||||
        checkBytes(stream, 255);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testUint16() throws IOException {
 | 
			
		||||
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int16(0, stream);
 | 
			
		||||
        checkBytes(stream, 0, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int16(513, stream);
 | 
			
		||||
        checkBytes(stream, 2, 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testUint32() throws IOException {
 | 
			
		||||
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int32(0, stream);
 | 
			
		||||
        checkBytes(stream, 0, 0, 0, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int32(67305985, stream);
 | 
			
		||||
        checkBytes(stream, 4, 3, 2, 1);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int32(3355443201l, stream);
 | 
			
		||||
        checkBytes(stream, 200, 0, 0, 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testUint64() throws IOException {
 | 
			
		||||
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int64(0, stream);
 | 
			
		||||
        checkBytes(stream, 0, 0, 0, 0, 0, 0, 0, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int64(578437695752307201L, stream);
 | 
			
		||||
        checkBytes(stream, 8, 7, 6, 5, 4, 3, 2, 1);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        // 200 * 72057594037927936L + 1
 | 
			
		||||
        Encode.int64(0xc800000000000001L, stream);
 | 
			
		||||
        checkBytes(stream, 200, 0, 0, 0, 0, 0, 0, 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testVarInt() throws IOException {
 | 
			
		||||
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(0, stream);
 | 
			
		||||
        checkBytes(stream, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(252, stream);
 | 
			
		||||
        checkBytes(stream, 252);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(253, stream);
 | 
			
		||||
        checkBytes(stream, 253, 0, 253);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(65535, stream);
 | 
			
		||||
        checkBytes(stream, 253, 255, 255);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(65536, stream);
 | 
			
		||||
        checkBytes(stream, 254, 0, 1, 0, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(4294967295L, stream);
 | 
			
		||||
        checkBytes(stream, 254, 255, 255, 255, 255);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(4294967296L, stream);
 | 
			
		||||
        checkBytes(stream, 255, 0, 0, 0, 1, 0, 0, 0, 0);
 | 
			
		||||
 | 
			
		||||
        stream = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.varInt(-1L, stream);
 | 
			
		||||
        checkBytes(stream, 255, 255, 255, 255, 255, 255, 255, 255, 255);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void checkBytes(ByteArrayOutputStream stream, int... bytes) {
 | 
			
		||||
        assertEquals(bytes.length, stream.size());
 | 
			
		||||
        byte[] streamBytes = stream.toByteArray();
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < bytes.length; i++) {
 | 
			
		||||
            assertEquals((byte) bytes[i], streamBytes[i]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,29 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
public class StringsTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testHexString() {
 | 
			
		||||
        assertEquals("48656c6c6f21", Strings.hex("Hello!".getBytes()).toString());
 | 
			
		||||
        assertEquals("0001", Strings.hex(new byte[]{0, 1}).toString());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/TestBase.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/TestBase.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by chris on 20.07.15.
 | 
			
		||||
 */
 | 
			
		||||
public class TestBase {
 | 
			
		||||
    static {
 | 
			
		||||
        Singleton.initialize(new BouncyCryptography());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										86
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/TestUtils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								core/src/test/java/ch/dissem/bitmessage/utils/TestUtils.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,86 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2015 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.utils;
 | 
			
		||||
 | 
			
		||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
 | 
			
		||||
import ch.dissem.bitmessage.entity.ObjectMessage;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.Pubkey;
 | 
			
		||||
import ch.dissem.bitmessage.entity.payload.V4Pubkey;
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
 | 
			
		||||
import ch.dissem.bitmessage.exception.DecryptionFailedException;
 | 
			
		||||
import ch.dissem.bitmessage.factory.Factory;
 | 
			
		||||
 | 
			
		||||
import java.io.ByteArrayInputStream;
 | 
			
		||||
import java.io.ByteArrayOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * If there's ever a need for this in production code, it should be rewritten to be more efficient.
 | 
			
		||||
 */
 | 
			
		||||
public class TestUtils {
 | 
			
		||||
    public static byte[] int16(int number) throws IOException {
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
        Encode.int16(number, out);
 | 
			
		||||
        return out.toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static ObjectMessage loadObjectMessage(int version, String resourceName) throws IOException {
 | 
			
		||||
        byte[] data = getBytes(resourceName);
 | 
			
		||||
        InputStream in = new ByteArrayInputStream(data);
 | 
			
		||||
        return Factory.getObjectMessage(version, in, data.length);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static byte[] getBytes(String resourceName) throws IOException {
 | 
			
		||||
        InputStream in = TestUtils.class.getClassLoader().getResourceAsStream(resourceName);
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
        byte[] buffer = new byte[1024];
 | 
			
		||||
        int len = in.read(buffer);
 | 
			
		||||
        while (len != -1) {
 | 
			
		||||
            out.write(buffer, 0, len);
 | 
			
		||||
            len = in.read(buffer);
 | 
			
		||||
        }
 | 
			
		||||
        return out.toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static InputStream getResource(String resourceName) {
 | 
			
		||||
        return TestUtils.class.getClassLoader().getResourceAsStream(resourceName);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static BitmessageAddress loadIdentity(String address) throws IOException {
 | 
			
		||||
        PrivateKey privateKey = PrivateKey.read(TestUtils.getResource(address + ".privkey"));
 | 
			
		||||
        BitmessageAddress identity = new BitmessageAddress(privateKey);
 | 
			
		||||
        assertEquals(address, identity.getAddress());
 | 
			
		||||
        return identity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static BitmessageAddress loadContact() throws IOException, DecryptionFailedException {
 | 
			
		||||
        BitmessageAddress address = new BitmessageAddress("BM-2cXxfcSetKnbHJX2Y85rSkaVpsdNUZ5q9h");
 | 
			
		||||
        ObjectMessage object = TestUtils.loadObjectMessage(4, "V4Pubkey.payload");
 | 
			
		||||
        object.decrypt(address.getPublicDecryptionKey());
 | 
			
		||||
        address.setPubkey((V4Pubkey) object.getPayload());
 | 
			
		||||
        return address;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void loadPubkey(BitmessageAddress address) throws IOException {
 | 
			
		||||
        byte[] bytes = getBytes(address.getAddress() + ".pubkey");
 | 
			
		||||
        Pubkey pubkey = Factory.readPubkey(address.getVersion(), address.getStream(), new ByteArrayInputStream(bytes), bytes.length, false);
 | 
			
		||||
        address.setPubkey(pubkey);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V1Msg.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V1Msg.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V1MsgStrangeData.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V1MsgStrangeData.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V2GetPubkey.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V2GetPubkey.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V2Pubkey.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V2Pubkey.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V3GetPubkey.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V3GetPubkey.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V3Pubkey.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V3Pubkey.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V4Broadcast.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V4Broadcast.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V4GetPubkey.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V4GetPubkey.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V4Pubkey.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V4Pubkey.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								core/src/test/resources/V5Broadcast.payload
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/src/test/resources/V5Broadcast.payload
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user