From 4f0b2cb8f86cfa85495de1099a1051900ba554af Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 6 May 2016 14:13:39 +0200 Subject: [PATCH] Fixed system test (this time for real) --- .../dissem/bitmessage/BitmessageContext.java | 3 +- .../bitmessage/DefaultMessageListener.java | 3 +- .../dissem/bitmessage/ProofOfWorkService.java | 2 - .../bitmessage/ports/DefaultLabeler.java | 2 +- .../ports/ProofOfWorkRepository.java | 1 - .../java/ch/dissem/bitmessage/SystemTest.java | 96 ++++++++++++++++++- 6 files changed, 97 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index d46c36c..23e8eff 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -153,7 +153,6 @@ public class BitmessageContext { .to(to) .message(subject, message) .build(); - labeler().markAsSending(msg); send(msg); } @@ -161,7 +160,7 @@ public class BitmessageContext { if (msg.getFrom() == null || msg.getFrom().getPrivateKey() == null) { throw new IllegalArgumentException("'From' must be an identity, i.e. have a private key."); } - msg.setStatus(Plaintext.Status.DRAFT); + labeler().markAsSending(msg); BitmessageAddress to = msg.getTo(); if (to != null) { if (to.getPubkey() == null) { diff --git a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index 14c405c..eb27dd9 100644 --- a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -24,6 +24,7 @@ import ch.dissem.bitmessage.entity.valueobject.InventoryVector; import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.ports.Labeler; import ch.dissem.bitmessage.ports.NetworkHandler; +import ch.dissem.bitmessage.utils.TTL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -111,7 +112,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { for (Plaintext msg : messages) { ctx.getLabeler().markAsSending(msg); ctx.getMessageRepository().save(msg); - ctx.send(msg, +2 * DAY); + ctx.send(msg, TTL.msg()); } } diff --git a/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java b/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java index 6df0cc0..e886c22 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java +++ b/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java @@ -1,9 +1,7 @@ package ch.dissem.bitmessage; import ch.dissem.bitmessage.entity.*; -import ch.dissem.bitmessage.entity.payload.Broadcast; import ch.dissem.bitmessage.entity.payload.Msg; -import ch.dissem.bitmessage.entity.payload.ObjectPayload; import ch.dissem.bitmessage.entity.payload.Pubkey; import ch.dissem.bitmessage.ports.Cryptography; import ch.dissem.bitmessage.ports.MessageRepository; diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/DefaultLabeler.java b/core/src/main/java/ch/dissem/bitmessage/ports/DefaultLabeler.java index 4f21b3e..e4c2105 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/DefaultLabeler.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/DefaultLabeler.java @@ -43,7 +43,7 @@ public class DefaultLabeler implements Labeler, InternalContext.ContextHolder { @Override public void markAsSending(Plaintext msg) { - if (msg.getTo() != null || msg.getTo().getPubkey() == null) { + if (msg.getTo() != null && msg.getTo().getPubkey() == null) { msg.setStatus(PUBKEY_REQUESTED); } else { msg.setStatus(DOING_PROOF_OF_WORK); diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/ProofOfWorkRepository.java b/core/src/main/java/ch/dissem/bitmessage/ports/ProofOfWorkRepository.java index 61dc2ab..b27a05d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/ProofOfWorkRepository.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/ProofOfWorkRepository.java @@ -1,6 +1,5 @@ package ch.dissem.bitmessage.ports; -import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.Plaintext; diff --git a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java index 06b6628..6751819 100644 --- a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java +++ b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java @@ -4,13 +4,19 @@ import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.networking.DefaultNetworkHandler; +import ch.dissem.bitmessage.ports.DefaultLabeler; import ch.dissem.bitmessage.repository.*; import ch.dissem.bitmessage.utils.TTL; -import org.junit.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.UUID; import java.util.concurrent.TimeUnit; +import static ch.dissem.bitmessage.entity.payload.Pubkey.Feature.DOES_ACK; import static ch.dissem.bitmessage.utils.UnixTime.MINUTE; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; @@ -47,9 +53,10 @@ public class SystemTest { .networkHandler(new DefaultNetworkHandler()) .cryptography(new BouncyCryptography()) .listener(aliceListener) + .labeler(new DebugLabeler("Alice")) .build(); alice.startup(); - aliceIdentity = alice.createIdentity(false); + aliceIdentity = alice.createIdentity(false, DOES_ACK); JdbcConfig bobDB = new JdbcConfig("jdbc:h2:mem:bob;DB_CLOSE_DELAY=-1", "sa", ""); bob = new BitmessageContext.Builder() @@ -62,9 +69,13 @@ public class SystemTest { .networkHandler(new DefaultNetworkHandler()) .cryptography(new BouncyCryptography()) .listener(bobListener) + .labeler(new DebugLabeler("Bob")) .build(); bob.startup(); - bobIdentity = bob.createIdentity(false); + bobIdentity = bob.createIdentity(false, DOES_ACK); + + ((DebugLabeler) alice.labeler()).init(aliceIdentity, bobIdentity); + ((DebugLabeler) bob.labeler()).init(aliceIdentity, bobIdentity); } @After @@ -95,4 +106,83 @@ public class SystemTest { assertThat(plaintext.getType(), equalTo(Plaintext.Type.BROADCAST)); assertThat(plaintext.getText(), equalTo(originalMessage)); } + + private static class DebugLabeler extends DefaultLabeler { + private final Logger LOG = LoggerFactory.getLogger("Labeler"); + final String name; + String alice; + String bob; + + private DebugLabeler(String name) { + this.name = name; + } + + private void init(BitmessageAddress alice, BitmessageAddress bob) { + this.alice = alice.getAddress(); + this.bob = bob.getAddress(); + } + + @Override + public void setLabels(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Received"); + super.setLabels(msg); + } + + @Override + public void markAsDraft(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Draft"); + super.markAsDraft(msg); + } + + @Override + public void markAsSending(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Sending"); + super.markAsSending(msg); + } + + @Override + public void markAsSent(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Sent"); + super.markAsSent(msg); + } + + @Override + public void markAsAcknowledged(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Acknowledged"); + super.markAsAcknowledged(msg); + } + + @Override + public void markAsRead(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Read"); + super.markAsRead(msg); + } + + @Override + public void markAsUnread(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Unread"); + super.markAsUnread(msg); + } + + @Override + public void delete(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Cleared"); + super.delete(msg); + } + + @Override + public void archive(Plaintext msg) { + LOG.info(name + ": From " + name(msg.getFrom()) + ": Archived"); + super.archive(msg); + } + + private String name(BitmessageAddress address) { + if (alice.equals(address.getAddress())) + return "Alice"; + else if (bob.equals(address.getAddress())) + return "Bob"; + else + return "Unknown (" + address.getAddress() + ")"; + } + } }