Fixed some bugs and some tests

This commit is contained in:
Christian Basler 2016-12-21 08:09:53 +01:00
parent 702ac6cb82
commit 732032b1b5
8 changed files with 170 additions and 140 deletions

View File

@ -254,9 +254,12 @@ public class BitmessageContext {
public void addContact(BitmessageAddress contact) { public void addContact(BitmessageAddress contact) {
ctx.getAddressRepository().save(contact); ctx.getAddressRepository().save(contact);
if (contact.getPubkey() == null) { if (contact.getPubkey() == null) {
BitmessageAddress stored = ctx.getAddressRepository().getAddress(contact.getAddress());
if (stored.getPubkey() == null) {
ctx.requestPubkey(contact); ctx.requestPubkey(contact);
} }
} }
}
public void addSubscribtion(BitmessageAddress address) { public void addSubscribtion(BitmessageAddress address) {
address.setSubscribed(true); address.setSubscribed(true);

View File

@ -377,6 +377,18 @@ public class Plaintext implements Streamable {
return extendedData; return extendedData;
} }
@SuppressWarnings("unchecked")
public <T extends ExtendedEncoding.ExtendedType> T getExtendedData(Class<T> type) {
ExtendedEncoding extendedData = getExtendedData();
if (extendedData == null) {
return null;
}
if (type == null || type.isInstance(extendedData.getContent())) {
return (T) extendedData.getContent();
}
return null;
}
public List<InventoryVector> getParents() { public List<InventoryVector> getParents() {
if (Message.TYPE.equals(getExtendedData().getType())) { if (Message.TYPE.equals(getExtendedData().getType())) {
return ((Message) extendedData.getContent()).getParents(); return ((Message) extendedData.getContent()).getParents();

View File

@ -37,15 +37,28 @@ public abstract class AbstractMessageRepository implements MessageRepository, In
this.ctx = context; this.ctx = context;
} }
/**
* @deprecated use {@link #saveContactIfNecessary(BitmessageAddress)} instead.
*/
@Deprecated
protected void safeSenderIfNecessary(Plaintext message) { protected void safeSenderIfNecessary(Plaintext message) {
if (message.getId() == null) { if (message.getId() == null) {
BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(message.getFrom().getAddress()); saveContactIfNecessary(message.getFrom());
}
}
protected void saveContactIfNecessary(BitmessageAddress contact) {
if (contact != null) {
BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(contact.getAddress());
if (savedAddress == null) { if (savedAddress == null) {
ctx.getAddressRepository().save(message.getFrom()); ctx.getAddressRepository().save(contact);
} else if (savedAddress.getPubkey() == null && message.getFrom().getPubkey() != null) { } else if (savedAddress.getPubkey() == null && contact.getPubkey() != null) {
savedAddress.setPubkey(message.getFrom().getPubkey()); savedAddress.setPubkey(contact.getPubkey());
ctx.getAddressRepository().save(savedAddress); ctx.getAddressRepository().save(savedAddress);
} }
if (savedAddress != null) {
contact.setAlias(savedAddress.getAlias());
}
} }
} }

View File

@ -52,6 +52,11 @@ public interface AddressRepository {
*/ */
List<BitmessageAddress> getContacts(); List<BitmessageAddress> getContacts();
/**
* Implementations must not delete cryptographic keys if they're not provided by <code>address</code>.
*
* @param address to save or update
*/
void save(BitmessageAddress address); void save(BitmessageAddress address);
void remove(BitmessageAddress address); void remove(BitmessageAddress address);

View File

@ -110,10 +110,11 @@ public class BitmessageContextTest {
@Test @Test
public void ensureContactIsSavedAndPubkeyRequested() { public void ensureContactIsSavedAndPubkeyRequested() {
BitmessageAddress contact = new BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT"); BitmessageAddress contact = new BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT");
when(ctx.addresses().getAddress(contact.getAddress())).thenReturn(contact);
ctx.addContact(contact); ctx.addContact(contact);
verify(ctx.addresses(), times(2)).save(contact); verify(ctx.addresses(), timeout(1000).atLeastOnce()).save(contact);
verify(ctx.internals().getProofOfWorkEngine()) verify(ctx.internals().getProofOfWorkEngine(), timeout(1000))
.calculateNonce(any(byte[].class), any(byte[].class), any(ProofOfWorkEngine.Callback.class)); .calculateNonce(any(byte[].class), any(byte[].class), any(ProofOfWorkEngine.Callback.class));
} }
@ -138,6 +139,7 @@ public class BitmessageContextTest {
.thenReturn(Collections.singletonList( .thenReturn(Collections.singletonList(
TestUtils.loadObjectMessage(2, "V2Pubkey.payload") TestUtils.loadObjectMessage(2, "V2Pubkey.payload")
)); ));
when(ctx.addresses().getAddress(contact.getAddress())).thenReturn(contact);
ctx.addContact(contact); ctx.addContact(contact);
@ -159,19 +161,7 @@ public class BitmessageContextTest {
ctx.addContact(contact); ctx.addContact(contact);
verify(ctx.addresses(), atLeastOnce()).save(argThat(new BaseMatcher<BitmessageAddress>() { verify(ctx.addresses(), atLeastOnce()).save(any(BitmessageAddress.class));
@Override
public boolean matches(Object item) {
return item instanceof BitmessageAddress
&& ((BitmessageAddress) item).getPubkey() != null
&& stored.getAlias().equals(((BitmessageAddress) item).getAlias());
}
@Override
public void describeTo(Description description) {
description.appendText("pubkey must not be null and alias must be ").appendValue(stored.getAlias());
}
}));
verify(ctx.internals().getProofOfWorkEngine(), never()) verify(ctx.internals().getProofOfWorkEngine(), never())
.calculateNonce(any(byte[].class), any(byte[].class), any(ProofOfWorkEngine.Callback.class)); .calculateNonce(any(byte[].class), any(byte[].class), any(ProofOfWorkEngine.Callback.class));
} }

View File

@ -54,6 +54,7 @@ public class SystemTest {
} }
@Parameterized.Parameters @Parameterized.Parameters
@SuppressWarnings("deprecation")
public static List<Object[]> parameters() { public static List<Object[]> parameters() {
return Arrays.asList(new Object[][]{ return Arrays.asList(new Object[][]{
{new NioNetworkHandler(), new DefaultNetworkHandler()}, {new NioNetworkHandler(), new DefaultNetworkHandler()},

View File

@ -148,7 +148,8 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
@Override @Override
public void save(Plaintext message) { public void save(Plaintext message) {
safeSenderIfNecessary(message); saveContactIfNecessary(message.getFrom());
saveContactIfNecessary(message.getTo());
try (Connection connection = config.getConnection()) { try (Connection connection = config.getConnection()) {
try { try {

View File

@ -109,6 +109,11 @@ public class JdbcNodeRegistry extends JdbcHelper implements NodeRegistry {
result.add(Collections.selectRandom(nodes)); result.add(Collections.selectRandom(nodes));
} }
} }
if (result.isEmpty()) {
// There might have been an error resolving domain names due to a missing internet exception.
// Try to load the stable nodes again next time.
stableNodes = null;
}
} }
return result; return result;
} }