From 985e830779a18990ca67b9e56ce49ec3bfc012dd Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 2 Feb 2016 21:05:14 +0100 Subject: [PATCH] Made TTLs easily changeable (albeit not for specific messages) This should make the system test run on Travis CI again --- .../dissem/bitmessage/BitmessageContext.java | 6 +-- .../ch/dissem/bitmessage/InternalContext.java | 7 ++-- .../java/ch/dissem/bitmessage/utils/TTL.java | 40 +++++++++++++++++++ .../bitmessage/security/CryptographyTest.java | 10 ++--- .../java/ch/dissem/bitmessage/SystemTest.java | 6 +++ 5 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 core/src/main/java/ch/dissem/bitmessage/utils/TTL.java diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index dde6d2d..c99acfb 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -28,6 +28,7 @@ import ch.dissem.bitmessage.entity.valueobject.PrivateKey; import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Property; +import ch.dissem.bitmessage.utils.TTL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -168,7 +169,7 @@ public class BitmessageContext { msg.getFrom(), to, new Msg(msg), - +2 * DAY + TTL.msg() ); msg.setStatus(SENT); msg.addLabels(ctx.getMessageRepository().getLabels(Label.Type.SENT)); @@ -288,7 +289,6 @@ public class BitmessageContext { int connectionLimit = 150; long connectionTTL = 30 * MINUTE; boolean sendPubkeyOnIdentityCreation = true; - long pubkeyTTL = 28; public Builder() { } @@ -383,7 +383,7 @@ public class BitmessageContext { */ public Builder pubkeyTTL(long 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; } diff --git a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java index 54e81a9..bfd4f30 100644 --- a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java @@ -22,6 +22,7 @@ import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.payload.*; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Singleton; +import ch.dissem.bitmessage.utils.TTL; import ch.dissem.bitmessage.utils.UnixTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,7 +59,6 @@ public class InternalContext { private final long clientNonce; private final long networkNonceTrialsPerByte = 1000; private final long networkExtraBytes = 1000; - private final long pubkeyTTL; private long connectionTTL; private int connectionLimit; @@ -78,7 +78,6 @@ public class InternalContext { this.port = builder.port; this.connectionLimit = builder.connectionLimit; this.connectionTTL = builder.connectionTTL; - this.pubkeyTTL = builder.pubkeyTTL; Singleton.initialize(cryptography); @@ -194,7 +193,7 @@ public class InternalContext { public void sendPubkey(final BitmessageAddress identity, final long targetStream) { try { - long expires = UnixTime.now(pubkeyTTL); + long expires = UnixTime.now(TTL.pubkey()); LOG.info("Expires at " + expires); final ObjectMessage response = new ObjectMessage.Builder() .stream(targetStream) @@ -233,7 +232,7 @@ public class InternalContext { addressRepository.save(contact); } - long expires = UnixTime.now(+pubkeyTTL); + long expires = UnixTime.now(TTL.getpubkey()); LOG.info("Expires at " + expires); final ObjectMessage request = new ObjectMessage.Builder() .stream(contact.getStream()) diff --git a/core/src/main/java/ch/dissem/bitmessage/utils/TTL.java b/core/src/main/java/ch/dissem/bitmessage/utils/TTL.java new file mode 100644 index 0000000..17e31e5 --- /dev/null +++ b/core/src/main/java/ch/dissem/bitmessage/utils/TTL.java @@ -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; + } +} diff --git a/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java b/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java index 3a68968..d4ab2ad 100644 --- a/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java +++ b/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java @@ -1,11 +1,11 @@ package ch.dissem.bitmessage.security; import ch.dissem.bitmessage.InternalContext; +import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.payload.GenericPayload; import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine; import ch.dissem.bitmessage.ports.ProofOfWorkEngine; -import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.utils.CallbackWaiter; import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.UnixTime; @@ -15,13 +15,13 @@ import javax.xml.bind.DatatypeConverter; import java.io.ByteArrayInputStream; 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.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** - * Created by chris on 19.07.15. + * @author Christian Basler */ public class CryptographyTest { public static final byte[] TEST_VALUE = "teststring".getBytes(); @@ -72,7 +72,7 @@ public class CryptographyTest { public void testProofOfWorkFails() throws IOException { ObjectMessage objectMessage = new ObjectMessage.Builder() .nonce(new byte[8]) - .expiresTime(UnixTime.now(+2 * DAY)) // 5 minutes + .expiresTime(UnixTime.now(+2 * MINUTE)) .objectType(0) .payload(GenericPayload.read(0, new ByteArrayInputStream(new byte[0]), 1, 0)) .build(); @@ -83,7 +83,7 @@ public class CryptographyTest { public void testDoProofOfWork() throws Exception { ObjectMessage objectMessage = new ObjectMessage.Builder() .nonce(new byte[8]) - .expiresTime(UnixTime.now(+2 * DAY)) + .expiresTime(UnixTime.now(+2 * MINUTE)) .objectType(0) .payload(GenericPayload.read(0, new ByteArrayInputStream(new byte[0]), 1, 0)) .build(); diff --git a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java index 5bf2949..fd73dcf 100644 --- a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java +++ b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java @@ -5,6 +5,8 @@ import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.networking.DefaultNetworkHandler; import ch.dissem.bitmessage.repository.*; +import ch.dissem.bitmessage.utils.TTL; +import ch.dissem.bitmessage.utils.UnixTime; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -12,6 +14,7 @@ import org.junit.Test; import java.util.UUID; import java.util.concurrent.TimeUnit; +import static ch.dissem.bitmessage.utils.UnixTime.MINUTE; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; @@ -29,6 +32,9 @@ public class SystemTest { @BeforeClass 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", ""); alice = new BitmessageContext.Builder() .addressRepo(new JdbcAddressRepository(aliceDB))