DefaultMessageListener implements NetworkHandler.MessageListener, InternalContext.ContextHolder { @Override @SuppressWarnings("ConstantConditions") public void receive(ObjectMessage object) throws IOException { ObjectPayload payload = object.getPayload(); if (payload.getType() == null) { if (payload instanceof GenericPayload) { receive((GenericPayload) payload); } return; } switch (payload.getType()) { case GET_PUBKEY: { receive(object, (GetPubkey) payload); break; } case PUBKEY: { receive(object, (Pubkey) payload); break; } case MSG: { receive(object, (Msg) payload); break; } case BROADCAST: { receive(object, (Broadcast) payload); break; } default: { throw new IllegalArgumentException("Unknown payload type " + payload.getType()); } } } DefaultMessageListener(Labeler labeler, BitmessageContext.Listener listener); @Override void setContext(InternalContext context); @Override @SuppressWarnings("ConstantConditions") void receive(ObjectMessage object);  }
@Test public void ensurePubkeyIsSentOnRequest() throws Exception { BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"); when(addressRepo.findIdentity(any(byte[].class))) .thenReturn(identity); listener.receive(new ObjectMessage.Builder() .stream(2) .payload(new GetPubkey(new BitmessageAddress("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))) .build()); verify(ctx).sendPubkey(eq(identity), eq(2L)); }
@Test public void ensureIncomingPubkeyIsAddedToContact() throws Exception { BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"); BitmessageAddress contact = new BitmessageAddress(identity.getAddress()); when(addressRepo.findContact(any(byte[].class))) .thenReturn(contact); when(messageRepo.findMessages(eq(PUBKEY_REQUESTED), eq(contact))) .thenReturn(Collections.singletonList( new Plaintext.Builder(MSG).from(identity).to(contact).message("S", "T").build() )); ObjectMessage objectMessage = new ObjectMessage.Builder() .stream(2) .payload(identity.getPubkey()) .build(); objectMessage.sign(identity.getPrivateKey()); objectMessage.encrypt(Singleton.cryptography().createPublicKey(identity.getPublicDecryptionKey())); listener.receive(objectMessage); verify(addressRepo).save(any(BitmessageAddress.class)); }
@Test public void ensureIncomingMessageIsSaved() throws Exception { BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"); BitmessageAddress contact = new BitmessageAddress(identity.getAddress()); contact.setPubkey(identity.getPubkey()); when(addressRepo.getIdentities()).thenReturn(Collections.singletonList(identity)); ObjectMessage objectMessage = new ObjectMessage.Builder() .stream(2) .payload(new Msg(new Plaintext.Builder(MSG) .from(identity) .to(contact) .message("S", "T") .build())) .nonce(new byte[8]) .build(); objectMessage.sign(identity.getPrivateKey()); objectMessage.encrypt(identity.getPubkey()); listener.receive(objectMessage); verify(messageRepo, atLeastOnce()).save(plaintext(MSG)); }
@Test public void ensureIncomingBroadcastIsSaved() throws Exception { BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"); when(addressRepo.getSubscriptions(anyLong())).thenReturn(Collections.singletonList(identity)); Broadcast broadcast = Factory.getBroadcast(new Plaintext.Builder(BROADCAST) .from(identity) .message("S", "T") .build()); ObjectMessage objectMessage = new ObjectMessage.Builder() .stream(2) .payload(broadcast) .nonce(new byte[8]) .build(); objectMessage.sign(identity.getPrivateKey()); broadcast.encrypt(); listener.receive(objectMessage); verify(messageRepo, atLeastOnce()).save(plaintext(BROADCAST)); }