Added tests and code improvements

This commit is contained in:
Christian Basler 2016-04-08 19:22:40 +02:00
parent f70c15da38
commit 4f7f80c12a
46 changed files with 163 additions and 43 deletions

View File

@ -36,6 +36,8 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.*; import java.util.concurrent.*;
import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES;
import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE;
import static ch.dissem.bitmessage.entity.Plaintext.Status.*; import static ch.dissem.bitmessage.entity.Plaintext.Status.*;
import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST; import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST;
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
@ -99,8 +101,8 @@ public class BitmessageContext {
final BitmessageAddress identity = new BitmessageAddress(new PrivateKey( final BitmessageAddress identity = new BitmessageAddress(new PrivateKey(
shorter, shorter,
ctx.getStreams()[0], ctx.getStreams()[0],
ctx.getNetworkNonceTrialsPerByte(), NETWORK_NONCE_TRIALS_PER_BYTE,
ctx.getNetworkExtraBytes(), NETWORK_EXTRA_BYTES,
features features
)); ));
ctx.getAddressRepository().save(identity); ctx.getAddressRepository().save(identity);
@ -130,12 +132,12 @@ public class BitmessageContext {
} }
public List<BitmessageAddress> createDeterministicAddresses( public List<BitmessageAddress> createDeterministicAddresses(
String passphrase, int numberOfAddresses, int version, int stream, boolean shorter) { String passphrase, int numberOfAddresses, long version, long stream, boolean shorter) {
List<BitmessageAddress> result = BitmessageAddress.deterministic( List<BitmessageAddress> result = BitmessageAddress.deterministic(
passphrase, numberOfAddresses, version, stream, shorter); passphrase, numberOfAddresses, version, stream, shorter);
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
BitmessageAddress address = result.get(i); BitmessageAddress address = result.get(i);
address.setAlias(passphrase + " (" + (i + 1) + ")"); address.setAlias("deterministic (" + (i + 1) + ")");
ctx.getAddressRepository().save(address); ctx.getAddressRepository().save(address);
} }
return result; return result;

View File

@ -43,6 +43,9 @@ import java.util.TreeSet;
public class InternalContext { public class InternalContext {
private final static Logger LOG = LoggerFactory.getLogger(InternalContext.class); private final static Logger LOG = LoggerFactory.getLogger(InternalContext.class);
public final static long NETWORK_NONCE_TRIALS_PER_BYTE = 1000;
public final static long NETWORK_EXTRA_BYTES = 1000;
private final Cryptography cryptography; private final Cryptography cryptography;
private final Inventory inventory; private final Inventory inventory;
private final NodeRegistry nodeRegistry; private final NodeRegistry nodeRegistry;
@ -58,8 +61,6 @@ public class InternalContext {
private final TreeSet<Long> streams = new TreeSet<>(); private final TreeSet<Long> streams = new TreeSet<>();
private final int port; private final int port;
private final long clientNonce; private final long clientNonce;
private final long networkNonceTrialsPerByte = 1000;
private final long networkExtraBytes = 1000;
private long connectionTTL; private long connectionTTL;
private int connectionLimit; private int connectionLimit;
@ -158,14 +159,6 @@ public class InternalContext {
return port; return port;
} }
public long getNetworkNonceTrialsPerByte() {
return networkNonceTrialsPerByte;
}
public long getNetworkExtraBytes() {
return networkExtraBytes;
}
public void send(final BitmessageAddress from, BitmessageAddress to, final ObjectPayload payload, public void send(final BitmessageAddress from, BitmessageAddress to, final ObjectPayload payload,
final long timeToLive) { final long timeToLive) {
try { try {

View File

@ -14,6 +14,8 @@ import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES;
import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE;
import static ch.dissem.bitmessage.utils.Singleton.security; import static ch.dissem.bitmessage.utils.Singleton.security;
/** /**
@ -45,8 +47,8 @@ public class ProofOfWorkService implements ProofOfWorkEngine.Callback, InternalC
public void doProofOfWork(BitmessageAddress recipient, ObjectMessage object) { public void doProofOfWork(BitmessageAddress recipient, ObjectMessage object) {
Pubkey pubkey = recipient == null ? null : recipient.getPubkey(); Pubkey pubkey = recipient == null ? null : recipient.getPubkey();
long nonceTrialsPerByte = pubkey == null ? ctx.getNetworkNonceTrialsPerByte() : pubkey.getNonceTrialsPerByte(); long nonceTrialsPerByte = pubkey == null ? NETWORK_NONCE_TRIALS_PER_BYTE : pubkey.getNonceTrialsPerByte();
long extraBytes = pubkey == null ? ctx.getNetworkExtraBytes() : pubkey.getExtraBytes(); long extraBytes = pubkey == null ? NETWORK_EXTRA_BYTES : pubkey.getExtraBytes();
powRepo.putObject(object, nonceTrialsPerByte, extraBytes); powRepo.putObject(object, nonceTrialsPerByte, extraBytes);
if (object.getPayload() instanceof PlaintextHolder) { if (object.getPayload() instanceof PlaintextHolder) {

View File

@ -29,6 +29,8 @@ import java.util.List;
* The 'addr' command holds a list of known active Bitmessage nodes. * The 'addr' command holds a list of known active Bitmessage nodes.
*/ */
public class Addr implements MessagePayload { public class Addr implements MessagePayload {
private static final long serialVersionUID = -5117688017050138720L;
private final List<NetworkAddress> addresses; private final List<NetworkAddress> addresses;
private Addr(Builder builder) { private Addr(Builder builder) {
@ -53,7 +55,7 @@ public class Addr implements MessagePayload {
} }
public static final class Builder { public static final class Builder {
private List<NetworkAddress> addresses = new ArrayList<NetworkAddress>(); private List<NetworkAddress> addresses = new ArrayList<>();
public Builder addresses(Collection<NetworkAddress> addresses){ public Builder addresses(Collection<NetworkAddress> addresses){
this.addresses.addAll(addresses); this.addresses.addAll(addresses);

View File

@ -101,6 +101,7 @@ public class BitmessageAddress implements Serializable {
public BitmessageAddress(String address, String passphrase) { public BitmessageAddress(String address, String passphrase) {
this(address); this(address);
this.privateKey = new PrivateKey(this, passphrase); this.privateKey = new PrivateKey(this, passphrase);
this.pubkey = this.privateKey.getPubkey();
if (!Arrays.equals(ripe, privateKey.getPubkey().getRipe())) { if (!Arrays.equals(ripe, privateKey.getPubkey().getRipe())) {
throw new IllegalArgumentException("Wrong address or passphrase"); throw new IllegalArgumentException("Wrong address or passphrase");
} }
@ -120,7 +121,7 @@ public class BitmessageAddress implements Serializable {
} }
public static List<BitmessageAddress> deterministic(String passphrase, int numberOfAddresses, public static List<BitmessageAddress> deterministic(String passphrase, int numberOfAddresses,
int version, int stream, boolean shorter) { long version, long stream, boolean shorter) {
List<BitmessageAddress> result = new ArrayList<>(numberOfAddresses); List<BitmessageAddress> result = new ArrayList<>(numberOfAddresses);
List<PrivateKey> privateKeys = PrivateKey.deterministic(passphrase, numberOfAddresses, version, stream, shorter); List<PrivateKey> privateKeys = PrivateKey.deterministic(passphrase, numberOfAddresses, version, stream, shorter);
for (PrivateKey pk : privateKeys) { for (PrivateKey pk : privateKeys) {

View File

@ -29,6 +29,8 @@ import static ch.dissem.bitmessage.utils.Decode.varString;
* @author Christian Basler * @author Christian Basler
*/ */
public class CustomMessage implements MessagePayload { public class CustomMessage implements MessagePayload {
private static final long serialVersionUID = -8932056829480326011L;
public static final String COMMAND_ERROR = "ERROR"; public static final String COMMAND_ERROR = "ERROR";
private final String command; private final String command;

View File

@ -28,6 +28,8 @@ import java.util.List;
* The 'getdata' command is used to request objects from a node. * The 'getdata' command is used to request objects from a node.
*/ */
public class GetData implements MessagePayload { public class GetData implements MessagePayload {
private static final long serialVersionUID = 1433878785969631061L;
public static final int MAX_INVENTORY_SIZE = 50_000; public static final int MAX_INVENTORY_SIZE = 50_000;
List<InventoryVector> inventory; List<InventoryVector> inventory;

View File

@ -28,6 +28,8 @@ import java.util.List;
* The 'inv' command holds up to 50000 inventory vectors, i.e. hashes of inventory items. * The 'inv' command holds up to 50000 inventory vectors, i.e. hashes of inventory items.
*/ */
public class Inv implements MessagePayload { public class Inv implements MessagePayload {
private static final long serialVersionUID = 3662992522956947145L;
private List<InventoryVector> inventory; private List<InventoryVector> inventory;
private Inv(Builder builder) { private Inv(Builder builder) {

View File

@ -33,6 +33,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
* A network message is exchanged between two nodes. * A network message is exchanged between two nodes.
*/ */
public class NetworkMessage implements Streamable { public class NetworkMessage implements Streamable {
private static final long serialVersionUID = 702708857104464809L;
/** /**
* Magic value indicating message origin network, and used to seek to next message when stream state is unknown * Magic value indicating message origin network, and used to seek to next message when stream state is unknown
*/ */

View File

@ -36,6 +36,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
* The 'object' command sends an object that is shared throughout the network. * The 'object' command sends an object that is shared throughout the network.
*/ */
public class ObjectMessage implements MessagePayload { public class ObjectMessage implements MessagePayload {
private static final long serialVersionUID = 2495752480120659139L;
private byte[] nonce; private byte[] nonce;
private long expiresTime; private long expiresTime;
private long objectType; private long objectType;

View File

@ -31,6 +31,8 @@ import java.util.*;
* The unencrypted message to be sent by 'msg' or 'broadcast'. * The unencrypted message to be sent by 'msg' or 'broadcast'.
*/ */
public class Plaintext implements Streamable { public class Plaintext implements Streamable {
private static final long serialVersionUID = -5325729856394951079L;
private final Type type; private final Type type;
private final BitmessageAddress from; private final BitmessageAddress from;
private final long encoding; private final long encoding;

View File

@ -23,6 +23,8 @@ import java.io.OutputStream;
* The 'verack' command answers a 'version' command, accepting the other node's version. * The 'verack' command answers a 'version' command, accepting the other node's version.
*/ */
public class VerAck implements MessagePayload { public class VerAck implements MessagePayload {
private static final long serialVersionUID = -4302074845199181687L;
@Override @Override
public Command getCommand() { public Command getCommand() {
return Command.VERACK; return Command.VERACK;

View File

@ -29,6 +29,8 @@ import java.util.Random;
* The 'version' command advertises this node's latest supported protocol version upon initiation. * The 'version' command advertises this node's latest supported protocol version upon initiation.
*/ */
public class Version implements MessagePayload { public class Version implements MessagePayload {
private static final long serialVersionUID = 7219240857343176567L;
/** /**
* Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's * Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's
* version is lower but continue with the connection if it is higher. * version is lower but continue with the connection if it is higher.

View File

@ -32,6 +32,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
* Broadcasts are version 4 or 5. * Broadcasts are version 4 or 5.
*/ */
public abstract class Broadcast extends ObjectPayload implements Encrypted, PlaintextHolder { public abstract class Broadcast extends ObjectPayload implements Encrypted, PlaintextHolder {
private static final long serialVersionUID = 4064521827582239069L;
protected final long stream; protected final long stream;
protected CryptoBox encrypted; protected CryptoBox encrypted;
protected Plaintext plaintext; protected Plaintext plaintext;

View File

@ -31,6 +31,7 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
public class CryptoBox implements Streamable { public class CryptoBox implements Streamable {
private static final long serialVersionUID = 7217659539975573852L;
private static final Logger LOG = LoggerFactory.getLogger(CryptoBox.class); private static final Logger LOG = LoggerFactory.getLogger(CryptoBox.class);
private final byte[] initializationVector; private final byte[] initializationVector;

View File

@ -28,6 +28,8 @@ import java.util.Arrays;
* have to know what it is. * have to know what it is.
*/ */
public class GenericPayload extends ObjectPayload { public class GenericPayload extends ObjectPayload {
private static final long serialVersionUID = -912314085064185940L;
private long stream; private long stream;
private byte[] data; private byte[] data;

View File

@ -27,6 +27,8 @@ import java.io.OutputStream;
* Request for a public key. * Request for a public key.
*/ */
public class GetPubkey extends ObjectPayload { public class GetPubkey extends ObjectPayload {
private static final long serialVersionUID = -3634516646972610180L;
private long stream; private long stream;
private byte[] ripeTag; private byte[] ripeTag;

View File

@ -31,6 +31,8 @@ import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
* Used for person-to-person messages. * Used for person-to-person messages.
*/ */
public class Msg extends ObjectPayload implements Encrypted, PlaintextHolder { public class Msg extends ObjectPayload implements Encrypted, PlaintextHolder {
private static final long serialVersionUID = 4327495048296365733L;
private long stream; private long stream;
private CryptoBox encrypted; private CryptoBox encrypted;
private Plaintext plaintext; private Plaintext plaintext;

View File

@ -21,12 +21,13 @@ import ch.dissem.bitmessage.entity.Streamable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable;
/** /**
* The payload of an 'object' command. This is shared by the network. * The payload of an 'object' command. This is shared by the network.
*/ */
public abstract class ObjectPayload implements Streamable { public abstract class ObjectPayload implements Streamable {
private static final long serialVersionUID = -5034977402902364482L;
private final long version; private final long version;
protected ObjectPayload(long version) { protected ObjectPayload(long version) {

View File

@ -26,6 +26,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
* Public keys for signing and encryption, the answer to a 'getpubkey' request. * Public keys for signing and encryption, the answer to a 'getpubkey' request.
*/ */
public abstract class Pubkey extends ObjectPayload { public abstract class Pubkey extends ObjectPayload {
private static final long serialVersionUID = -6634533361454999619L;
public final static long LATEST_VERSION = 4; public final static long LATEST_VERSION = 4;
protected Pubkey(long version) { protected Pubkey(long version) {

View File

@ -27,6 +27,8 @@ import java.io.OutputStream;
* A version 2 public key. * A version 2 public key.
*/ */
public class V2Pubkey extends Pubkey { public class V2Pubkey extends Pubkey {
private static final long serialVersionUID = -257598690676510460L;
protected long stream; protected long stream;
protected int behaviorBitfield; protected int behaviorBitfield;
protected byte[] publicSigningKey; // 64 Bytes protected byte[] publicSigningKey; // 64 Bytes

View File

@ -29,6 +29,8 @@ import java.util.Objects;
* A version 3 public key. * A version 3 public key.
*/ */
public class V3Pubkey extends V2Pubkey { public class V3Pubkey extends V2Pubkey {
private static final long serialVersionUID = 6958853116648528319L;
long nonceTrialsPerByte; long nonceTrialsPerByte;
long extraBytes; long extraBytes;
byte[] signature; byte[] signature;

View File

@ -28,6 +28,8 @@ import java.io.OutputStream;
* Broadcasts are version 4 or 5. * Broadcasts are version 4 or 5.
*/ */
public class V4Broadcast extends Broadcast { public class V4Broadcast extends Broadcast {
private static final long serialVersionUID = 195663108282762711L;
protected V4Broadcast(long version, long stream, CryptoBox encrypted, Plaintext plaintext) { protected V4Broadcast(long version, long stream, CryptoBox encrypted, Plaintext plaintext) {
super(version, stream, encrypted, plaintext); super(version, stream, encrypted, plaintext);
} }

View File

@ -33,6 +33,8 @@ import java.util.Arrays;
* to create messages to be used in spam or in flooding attacks. * to create messages to be used in spam or in flooding attacks.
*/ */
public class V4Pubkey extends Pubkey implements Encrypted { public class V4Pubkey extends Pubkey implements Encrypted {
private static final long serialVersionUID = 1556710353694033093L;
private long stream; private long stream;
private byte[] tag; private byte[] tag;
private CryptoBox encrypted; private CryptoBox encrypted;

View File

@ -28,6 +28,8 @@ import java.io.OutputStream;
* Users who are subscribed to the sending address will see the message appear in their inbox. * Users who are subscribed to the sending address will see the message appear in their inbox.
*/ */
public class V5Broadcast extends V4Broadcast { public class V5Broadcast extends V4Broadcast {
private static final long serialVersionUID = 920649721626968644L;
private byte[] tag; private byte[] tag;
private V5Broadcast(long stream, byte[] tag, CryptoBox encrypted) { private V5Broadcast(long stream, byte[] tag, CryptoBox encrypted) {

View File

@ -16,6 +16,7 @@
package ch.dissem.bitmessage.entity.valueobject; package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.InternalContext;
import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Streamable; import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.payload.Pubkey; import ch.dissem.bitmessage.entity.payload.Pubkey;
@ -81,7 +82,8 @@ public class PrivateKey implements Streamable {
private PrivateKey(Builder builder) { private PrivateKey(Builder builder) {
this.privateSigningKey = builder.privSK; this.privateSigningKey = builder.privSK;
this.privateEncryptionKey = builder.privEK; this.privateEncryptionKey = builder.privEK;
this.pubkey = Factory.createPubkey(builder.version, builder.stream, builder.pubSK, builder.pubEK, 0, 0); this.pubkey = Factory.createPubkey(builder.version, builder.stream, builder.pubSK, builder.pubEK,
InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE, InternalContext.NETWORK_EXTRA_BYTES);
} }
private static class Builder { private static class Builder {

View File

@ -20,6 +20,8 @@ package ch.dissem.bitmessage.exception;
* Indicates an illegal Bitmessage address * Indicates an illegal Bitmessage address
*/ */
public class AddressFormatException extends RuntimeException { public class AddressFormatException extends RuntimeException {
private static final long serialVersionUID = 6943764578672021573L;
public AddressFormatException(String message) { public AddressFormatException(String message) {
super(message); super(message);
} }

View File

@ -20,6 +20,8 @@ package ch.dissem.bitmessage.exception;
* @author Christian Basler * @author Christian Basler
*/ */
public class ApplicationException extends RuntimeException { public class ApplicationException extends RuntimeException {
private static final long serialVersionUID = 1796776684126759324L;
public ApplicationException(Throwable cause) { public ApplicationException(Throwable cause) {
super(cause); super(cause);
} }

View File

@ -17,4 +17,5 @@
package ch.dissem.bitmessage.exception; package ch.dissem.bitmessage.exception;
public class DecryptionFailedException extends Exception { public class DecryptionFailedException extends Exception {
private static final long serialVersionUID = 3241116253113872731L;
} }

View File

@ -22,6 +22,8 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
public class InsufficientProofOfWorkException extends IOException { public class InsufficientProofOfWorkException extends IOException {
private static final long serialVersionUID = 9105580366564571318L;
public InsufficientProofOfWorkException(byte[] target, byte[] hash) { public InsufficientProofOfWorkException(byte[] target, byte[] hash) {
super("Insufficient proof of work: " + Strings.hex(target) + " required, " + Strings.hex(Arrays.copyOfRange(hash, 0, 8)) + " achieved."); super("Insufficient proof of work: " + Strings.hex(target) + " required, " + Strings.hex(Arrays.copyOfRange(hash, 0, 8)) + " achieved.");
} }

View File

@ -22,6 +22,8 @@ package ch.dissem.bitmessage.exception;
* @author Ch. Basler * @author Ch. Basler
*/ */
public class NodeException extends RuntimeException { public class NodeException extends RuntimeException {
private static final long serialVersionUID = 2965325796118227802L;
public NodeException(String message) { public NodeException(String message) {
super(message); super(message);
} }

View File

@ -35,6 +35,8 @@ import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom; import java.security.SecureRandom;
import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES;
import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE;
import static ch.dissem.bitmessage.utils.Numbers.max; import static ch.dissem.bitmessage.utils.Numbers.max;
/** /**
@ -99,8 +101,8 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont
public void doProofOfWork(ObjectMessage object, long nonceTrialsPerByte, public void doProofOfWork(ObjectMessage object, long nonceTrialsPerByte,
long extraBytes, ProofOfWorkEngine.Callback callback) { long extraBytes, ProofOfWorkEngine.Callback callback) {
nonceTrialsPerByte = max(nonceTrialsPerByte, context.getNetworkNonceTrialsPerByte()); nonceTrialsPerByte = max(nonceTrialsPerByte, NETWORK_NONCE_TRIALS_PER_BYTE);
extraBytes = max(extraBytes, context.getNetworkExtraBytes()); extraBytes = max(extraBytes, NETWORK_EXTRA_BYTES);
byte[] initialHash = getInitialHash(object); byte[] initialHash = getInitialHash(object);
@ -125,8 +127,8 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont
@Override @Override
public byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) { public byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) {
if (nonceTrialsPerByte == 0) nonceTrialsPerByte = context.getNetworkNonceTrialsPerByte(); if (nonceTrialsPerByte == 0) nonceTrialsPerByte = NETWORK_NONCE_TRIALS_PER_BYTE;
if (extraBytes == 0) extraBytes = context.getNetworkExtraBytes(); if (extraBytes == 0) extraBytes = NETWORK_EXTRA_BYTES;
BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now()); BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now());
BigInteger numerator = TWO_POW_64; BigInteger numerator = TWO_POW_64;

View File

@ -31,16 +31,13 @@ import org.hamcrest.Description;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Collections; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import static ch.dissem.bitmessage.entity.payload.ObjectType.*; import static ch.dissem.bitmessage.entity.payload.ObjectType.*;
import static ch.dissem.bitmessage.utils.MessageMatchers.object; import static ch.dissem.bitmessage.utils.MessageMatchers.object;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
/** /**
@ -216,6 +213,38 @@ public class BitmessageContextTest {
assertTrue(chan.isChan()); assertTrue(chan.isChan());
} }
@Test
public void ensureDeterministicAddressesAreCreated() {
final int expected_size = 8;
List<BitmessageAddress> addresses = ctx.createDeterministicAddresses("test", expected_size, 4, 1, false);
assertEquals(expected_size, addresses.size());
Set<String> expected = new HashSet<>(expected_size);
expected.add("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F");
expected.add("BM-2cX8TF9vuQZEWvT7UrEeq1HN9dgiSUPLEN");
expected.add("BM-2cUzX8f9CKUU7L8NeB8GExZvf54PrcXq1S");
expected.add("BM-2cU7MAoQd7KE8SPF7AKFPpoEZKjk86KRqE");
expected.add("BM-2cVm8ByVBacc2DVhdTNs6rmy5ZQK6DUsrt");
expected.add("BM-2cW2af1vB6kWon2WkygDHqGwfcpfAFm2Jk");
expected.add("BM-2cWdWD7UtUN4gWChgNX9pvyvNPjUZvU8BT");
expected.add("BM-2cXkYgYcUrv4fGxSHzyEScW955Cc8sDteo");
for (BitmessageAddress a : addresses) {
assertTrue(expected.contains(a.getAddress()));
expected.remove(a.getAddress());
}
}
@Test
public void ensureShortDeterministicAddressesAreCreated() {
final int expected_size = 1;
List<BitmessageAddress> addresses = ctx.createDeterministicAddresses("test", expected_size, 4, 1, true);
assertEquals(expected_size, addresses.size());
Set<String> expected = new HashSet<>(expected_size);
expected.add("BM-NBGyBAEp6VnBkFWKpzUSgxuTqVdWPi78");
for (BitmessageAddress a : addresses) {
assertTrue(expected.contains(a.getAddress()));
expected.remove(a.getAddress());
}
}
@Test @Test
public void ensureChanIsCreated() { public void ensureChanIsCreated() {
BitmessageAddress chan = ctx.createChan("test"); BitmessageAddress chan = ctx.createChan("test");

View File

@ -40,8 +40,6 @@ import static ch.dissem.bitmessage.entity.Plaintext.Status.PUBKEY_REQUESTED;
import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST; import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST;
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
import static ch.dissem.bitmessage.utils.MessageMatchers.plaintext; import static ch.dissem.bitmessage.utils.MessageMatchers.plaintext;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
/** /**

View File

@ -34,10 +34,6 @@ import java.util.Arrays;
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
/** /**

View File

@ -57,7 +57,7 @@ public class EncodeTest {
checkBytes(stream, 4, 3, 2, 1); checkBytes(stream, 4, 3, 2, 1);
stream = new ByteArrayOutputStream(); stream = new ByteArrayOutputStream();
Encode.int32(3355443201l, stream); Encode.int32(3355443201L, stream);
checkBytes(stream, 200, 0, 0, 1); checkBytes(stream, 200, 0, 0, 1);
} }

View File

@ -21,7 +21,6 @@ import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.payload.ObjectType; import ch.dissem.bitmessage.entity.payload.ObjectType;
import org.hamcrest.BaseMatcher; import org.hamcrest.BaseMatcher;
import org.hamcrest.Description; import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.mockito.Matchers; import org.mockito.Matchers;
/** /**

View File

@ -36,7 +36,10 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
* @author Christian Basler * @author Christian Basler
*/ */
public class CryptoCustomMessage<T extends Streamable> extends CustomMessage { public class CryptoCustomMessage<T extends Streamable> extends CustomMessage {
private static final long serialVersionUID = 7395193565986284426L;
public static final String COMMAND = "ENCRYPTED"; public static final String COMMAND = "ENCRYPTED";
private final Reader<T> dataReader; private final Reader<T> dataReader;
private CryptoBox container; private CryptoBox container;
private BitmessageAddress sender; private BitmessageAddress sender;

View File

@ -32,6 +32,8 @@ import static ch.dissem.bitmessage.utils.Decode.*;
* @author Christian Basler * @author Christian Basler
*/ */
public class ProofOfWorkRequest implements Streamable { public class ProofOfWorkRequest implements Streamable {
private static final long serialVersionUID = 4729003751499662713L;
private final BitmessageAddress sender; private final BitmessageAddress sender;
private final byte[] initialHash; private final byte[] initialHash;
private final Request request; private final Request request;

View File

@ -41,6 +41,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES;
import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE;
import static ch.dissem.bitmessage.networking.Connection.Mode.CLIENT; import static ch.dissem.bitmessage.networking.Connection.Mode.CLIENT;
import static ch.dissem.bitmessage.networking.Connection.Mode.SYNC; import static ch.dissem.bitmessage.networking.Connection.Mode.SYNC;
import static ch.dissem.bitmessage.networking.Connection.State.*; import static ch.dissem.bitmessage.networking.Connection.State.*;
@ -247,7 +249,7 @@ class Connection {
} }
try { try {
listener.receive(objectMessage); listener.receive(objectMessage);
security().checkProofOfWork(objectMessage, ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes()); security().checkProofOfWork(objectMessage, NETWORK_NONCE_TRIALS_PER_BYTE, NETWORK_EXTRA_BYTES);
ctx.getInventory().storeObject(objectMessage); ctx.getInventory().storeObject(objectMessage);
// offer object to some random nodes so it gets distributed throughout the network: // offer object to some random nodes so it gets distributed throughout the network:
networkHandler.offer(objectMessage.getInventoryVector()); networkHandler.offer(objectMessage.getInventoryVector());

View File

@ -27,7 +27,6 @@ import ch.dissem.bitmessage.utils.Property;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.concurrent.Future; import java.util.concurrent.Future;

View File

@ -23,6 +23,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import static ch.dissem.bitmessage.utils.UnixTime.now; import static ch.dissem.bitmessage.utils.UnixTime.now;
@ -73,7 +74,7 @@ public class JdbcNodeRegistryTest extends TestBase {
List<NetworkAddress> knownAddresses = registry.getKnownAddresses(1000, 1); List<NetworkAddress> knownAddresses = registry.getKnownAddresses(1000, 1);
assertEquals(5, knownAddresses.size()); assertEquals(5, knownAddresses.size());
registry.offerAddresses(Arrays.asList( registry.offerAddresses(Collections.singletonList(
createAddress(1, 8445, 1, now()) createAddress(1, 8445, 1, now())
)); ));

View File

@ -60,6 +60,9 @@ public class WifExporter {
section.add("label", identity.getAlias()); section.add("label", identity.getAlias());
section.add("enabled", true); section.add("enabled", true);
section.add("decoy", false); section.add("decoy", false);
if (identity.isChan()) {
section.add("chan", identity.isChan());
}
section.add("noncetrialsperbyte", identity.getPubkey().getNonceTrialsPerByte()); section.add("noncetrialsperbyte", identity.getPubkey().getNonceTrialsPerByte());
section.add("payloadlengthextrabytes", identity.getPubkey().getExtraBytes()); section.add("payloadlengthextrabytes", identity.getPubkey().getExtraBytes());
section.add("privsigningkey", exportSecret(identity.getPrivateKey().getPrivateSigningKey())); section.add("privsigningkey", exportSecret(identity.getPrivateKey().getPrivateSigningKey()));

View File

@ -70,6 +70,9 @@ public class WifImporter {
section.get("payloadlengthextrabytes", long.class), section.get("payloadlengthextrabytes", long.class),
Pubkey.Feature.bitfield(features) Pubkey.Feature.bitfield(features)
); );
if (section.containsKey("chan")) {
address.setChan(section.get("chan", boolean.class));
}
address.setAlias(section.get("label")); address.setAlias(section.get("label"));
identities.add(address); identities.add(address);
} }

View File

@ -17,6 +17,7 @@
package ch.dissem.bitmessage.wif; package ch.dissem.bitmessage.wif;
import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
import org.junit.Before; import org.junit.Before;
@ -80,9 +81,27 @@ public class WifExporterTest {
"noncetrialsperbyte = 320" + System.lineSeparator() + "noncetrialsperbyte = 320" + System.lineSeparator() +
"payloadlengthextrabytes = 14000" + System.lineSeparator() + "payloadlengthextrabytes = 14000" + System.lineSeparator() +
"privsigningkey = 5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9" + System.lineSeparator() + "privsigningkey = 5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9" + System.lineSeparator() +
"privencryptionkey = 5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck" + System.lineSeparator() + System.lineSeparator(); "privencryptionkey = 5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck" + System.lineSeparator() +
System.lineSeparator();
importer = new WifImporter(ctx, expected); importer = new WifImporter(ctx, expected);
exporter.addIdentity(importer.getIdentities().get(0)); exporter.addIdentity(importer.getIdentities().get(0));
assertEquals(expected, exporter.toString()); assertEquals(expected, exporter.toString());
} }
@Test
public void ensureChanIsAdded() throws Exception {
String expected = "[BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r]" + System.lineSeparator() +
"label = general" + System.lineSeparator() +
"enabled = true" + System.lineSeparator() +
"decoy = false" + System.lineSeparator() +
"chan = true" + System.lineSeparator() +
"noncetrialsperbyte = 1000" + System.lineSeparator() +
"payloadlengthextrabytes = 1000" + System.lineSeparator() +
"privsigningkey = 5Jnbdwc4u4DG9ipJxYLznXSvemkRFueQJNHujAQamtDDoX3N1eQ" + System.lineSeparator() +
"privencryptionkey = 5JrDcFtQDv5ydcHRW6dfGUEvThoxCCLNEUaxQfy8LXXgTJzVAcq" + System.lineSeparator() +
System.lineSeparator();
BitmessageAddress chan = ctx.joinChan("general", "BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r");
exporter.addIdentity(chan);
assertEquals(expected, exporter.toString());
}
} }

View File

@ -25,9 +25,7 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
public class WifImporterTest { public class WifImporterTest {
@ -69,6 +67,7 @@ public class WifImporterTest {
assertNotNull("Private key", identity.getPrivateKey()); assertNotNull("Private key", identity.getPrivateKey());
assertEquals(32, identity.getPrivateKey().getPrivateEncryptionKey().length); assertEquals(32, identity.getPrivateKey().getPrivateEncryptionKey().length);
assertEquals(32, identity.getPrivateKey().getPrivateSigningKey().length); assertEquals(32, identity.getPrivateKey().getPrivateSigningKey().length);
assertFalse(identity.isChan());
} }
@Test @Test
@ -98,4 +97,20 @@ public class WifImporterTest {
importer.importIdentity(identities.get(0)); importer.importIdentity(identities.get(0));
verify(repo, times(1)).save(identities.get(0)); verify(repo, times(1)).save(identities.get(0));
} }
@Test
public void ensureChanIsImported() throws Exception {
importer = new WifImporter(ctx, "[BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r]\n" +
"label = [chan] general\n" +
"enabled = true\n" +
"decoy = false\n" +
"chan = true\n" +
"noncetrialsperbyte = 1000\n" +
"payloadlengthextrabytes = 1000\n" +
"privsigningkey = 5Jnbdwc4u4DG9ipJxYLznXSvemkRFueQJNHujAQamtDDoX3N1eQ\n" +
"privencryptionkey = 5JrDcFtQDv5ydcHRW6dfGUEvThoxCCLNEUaxQfy8LXXgTJzVAcq\n");
assertEquals(1, importer.getIdentities().size());
BitmessageAddress chan = importer.getIdentities().get(0);
assertTrue(chan.isChan());
}
} }