Made TTLs easily changeable (albeit not for specific messages)

This should make the system test run on Travis CI again
This commit is contained in:
Christian Basler 2016-02-02 21:05:14 +01:00
parent edd8045327
commit 985e830779
5 changed files with 57 additions and 12 deletions

View File

@ -28,6 +28,7 @@ import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.exception.DecryptionFailedException;
import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.utils.Property; import ch.dissem.bitmessage.utils.Property;
import ch.dissem.bitmessage.utils.TTL;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -168,7 +169,7 @@ public class BitmessageContext {
msg.getFrom(), msg.getFrom(),
to, to,
new Msg(msg), new Msg(msg),
+2 * DAY TTL.msg()
); );
msg.setStatus(SENT); msg.setStatus(SENT);
msg.addLabels(ctx.getMessageRepository().getLabels(Label.Type.SENT)); msg.addLabels(ctx.getMessageRepository().getLabels(Label.Type.SENT));
@ -288,7 +289,6 @@ public class BitmessageContext {
int connectionLimit = 150; int connectionLimit = 150;
long connectionTTL = 30 * MINUTE; long connectionTTL = 30 * MINUTE;
boolean sendPubkeyOnIdentityCreation = true; boolean sendPubkeyOnIdentityCreation = true;
long pubkeyTTL = 28;
public Builder() { public Builder() {
} }
@ -383,7 +383,7 @@ public class BitmessageContext {
*/ */
public Builder pubkeyTTL(long days) { public Builder pubkeyTTL(long days) {
if (days < 0 || days > 28 * DAY) throw new IllegalArgumentException("TTL must be between 1 and 28 days"); if (days < 0 || days > 28 * DAY) throw new IllegalArgumentException("TTL must be between 1 and 28 days");
this.pubkeyTTL = days; TTL.pubkey(days);
return this; return this;
} }

View File

@ -22,6 +22,7 @@ import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.*; import ch.dissem.bitmessage.entity.payload.*;
import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.Singleton;
import ch.dissem.bitmessage.utils.TTL;
import ch.dissem.bitmessage.utils.UnixTime; import ch.dissem.bitmessage.utils.UnixTime;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -58,7 +59,6 @@ public class InternalContext {
private final long clientNonce; private final long clientNonce;
private final long networkNonceTrialsPerByte = 1000; private final long networkNonceTrialsPerByte = 1000;
private final long networkExtraBytes = 1000; private final long networkExtraBytes = 1000;
private final long pubkeyTTL;
private long connectionTTL; private long connectionTTL;
private int connectionLimit; private int connectionLimit;
@ -78,7 +78,6 @@ public class InternalContext {
this.port = builder.port; this.port = builder.port;
this.connectionLimit = builder.connectionLimit; this.connectionLimit = builder.connectionLimit;
this.connectionTTL = builder.connectionTTL; this.connectionTTL = builder.connectionTTL;
this.pubkeyTTL = builder.pubkeyTTL;
Singleton.initialize(cryptography); Singleton.initialize(cryptography);
@ -194,7 +193,7 @@ public class InternalContext {
public void sendPubkey(final BitmessageAddress identity, final long targetStream) { public void sendPubkey(final BitmessageAddress identity, final long targetStream) {
try { try {
long expires = UnixTime.now(pubkeyTTL); long expires = UnixTime.now(TTL.pubkey());
LOG.info("Expires at " + expires); LOG.info("Expires at " + expires);
final ObjectMessage response = new ObjectMessage.Builder() final ObjectMessage response = new ObjectMessage.Builder()
.stream(targetStream) .stream(targetStream)
@ -233,7 +232,7 @@ public class InternalContext {
addressRepository.save(contact); addressRepository.save(contact);
} }
long expires = UnixTime.now(+pubkeyTTL); long expires = UnixTime.now(TTL.getpubkey());
LOG.info("Expires at " + expires); LOG.info("Expires at " + expires);
final ObjectMessage request = new ObjectMessage.Builder() final ObjectMessage request = new ObjectMessage.Builder()
.stream(contact.getStream()) .stream(contact.getStream())

View File

@ -0,0 +1,40 @@
package ch.dissem.bitmessage.utils;
import static ch.dissem.bitmessage.utils.UnixTime.DAY;
/**
* Stores times to live for different object types. Usually this shouldn't be messed with,
* but for tests it might be a good idea to reduce it to a minimum, and on mobile clients
* you might want to optimize it as well.
*
* @author Christian Basler
*/
public class TTL {
private static long msg = 2 * DAY;
private static long getpubkey = 2 * DAY;
private static long pubkey = 28 * DAY;
public static long msg() {
return msg;
}
public static void msg(long msg) {
TTL.msg = msg;
}
public static long getpubkey() {
return getpubkey;
}
public static void getpubkey(long getpubkey) {
TTL.getpubkey = getpubkey;
}
public static long pubkey() {
return pubkey;
}
public static void pubkey(long pubkey) {
TTL.pubkey = pubkey;
}
}

View File

@ -1,11 +1,11 @@
package ch.dissem.bitmessage.security; package ch.dissem.bitmessage.security;
import ch.dissem.bitmessage.InternalContext; import ch.dissem.bitmessage.InternalContext;
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.GenericPayload; import ch.dissem.bitmessage.entity.payload.GenericPayload;
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine; import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
import ch.dissem.bitmessage.ports.ProofOfWorkEngine; import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
import ch.dissem.bitmessage.utils.CallbackWaiter; import ch.dissem.bitmessage.utils.CallbackWaiter;
import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.Singleton;
import ch.dissem.bitmessage.utils.UnixTime; import ch.dissem.bitmessage.utils.UnixTime;
@ -15,13 +15,13 @@ import javax.xml.bind.DatatypeConverter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import static ch.dissem.bitmessage.utils.UnixTime.DAY; import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* Created by chris on 19.07.15. * @author Christian Basler
*/ */
public class CryptographyTest { public class CryptographyTest {
public static final byte[] TEST_VALUE = "teststring".getBytes(); public static final byte[] TEST_VALUE = "teststring".getBytes();
@ -72,7 +72,7 @@ public class CryptographyTest {
public void testProofOfWorkFails() throws IOException { public void testProofOfWorkFails() throws IOException {
ObjectMessage objectMessage = new ObjectMessage.Builder() ObjectMessage objectMessage = new ObjectMessage.Builder()
.nonce(new byte[8]) .nonce(new byte[8])
.expiresTime(UnixTime.now(+2 * DAY)) // 5 minutes .expiresTime(UnixTime.now(+2 * MINUTE))
.objectType(0) .objectType(0)
.payload(GenericPayload.read(0, new ByteArrayInputStream(new byte[0]), 1, 0)) .payload(GenericPayload.read(0, new ByteArrayInputStream(new byte[0]), 1, 0))
.build(); .build();
@ -83,7 +83,7 @@ public class CryptographyTest {
public void testDoProofOfWork() throws Exception { public void testDoProofOfWork() throws Exception {
ObjectMessage objectMessage = new ObjectMessage.Builder() ObjectMessage objectMessage = new ObjectMessage.Builder()
.nonce(new byte[8]) .nonce(new byte[8])
.expiresTime(UnixTime.now(+2 * DAY)) .expiresTime(UnixTime.now(+2 * MINUTE))
.objectType(0) .objectType(0)
.payload(GenericPayload.read(0, new ByteArrayInputStream(new byte[0]), 1, 0)) .payload(GenericPayload.read(0, new ByteArrayInputStream(new byte[0]), 1, 0))
.build(); .build();

View File

@ -5,6 +5,8 @@ import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.networking.DefaultNetworkHandler; import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
import ch.dissem.bitmessage.repository.*; import ch.dissem.bitmessage.repository.*;
import ch.dissem.bitmessage.utils.TTL;
import ch.dissem.bitmessage.utils.UnixTime;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -12,6 +14,7 @@ import org.junit.Test;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -29,6 +32,9 @@ public class SystemTest {
@BeforeClass @BeforeClass
public static void setUp() { public static void setUp() {
TTL.msg(5 * MINUTE);
TTL.getpubkey(5 * MINUTE);
TTL.pubkey(5 * MINUTE);
JdbcConfig aliceDB = new JdbcConfig("jdbc:h2:mem:alice;DB_CLOSE_DELAY=-1", "sa", ""); JdbcConfig aliceDB = new JdbcConfig("jdbc:h2:mem:alice;DB_CLOSE_DELAY=-1", "sa", "");
alice = new BitmessageContext.Builder() alice = new BitmessageContext.Builder()
.addressRepo(new JdbcAddressRepository(aliceDB)) .addressRepo(new JdbcAddressRepository(aliceDB))