mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Add integration test for converting pending contacts.
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package org.briarproject.bramble.contact;
|
package org.briarproject.bramble.contact;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
|
import org.briarproject.bramble.api.contact.PendingContact;
|
||||||
|
import org.briarproject.bramble.api.contact.PendingContactState;
|
||||||
|
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
|
||||||
import org.briarproject.bramble.api.identity.Identity;
|
import org.briarproject.bramble.api.identity.Identity;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
@@ -16,14 +19,19 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
import static junit.framework.TestCase.assertNotNull;
|
||||||
|
import static junit.framework.TestCase.assertNull;
|
||||||
import static junit.framework.TestCase.fail;
|
import static junit.framework.TestCase.fail;
|
||||||
|
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
||||||
import static org.briarproject.bramble.test.TestDuplexTransportConnection.createPair;
|
import static org.briarproject.bramble.test.TestDuplexTransportConnection.createPair;
|
||||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@@ -35,9 +43,10 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
private final File aliceDir = new File(testDir, "alice");
|
private final File aliceDir = new File(testDir, "alice");
|
||||||
private final File bobDir = new File(testDir, "bob");
|
private final File bobDir = new File(testDir, "bob");
|
||||||
private final SecretKey masterKey = getSecretKey();
|
private final SecretKey masterKey = getSecretKey();
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
private ContactExchangeIntegrationTestComponent alice, bob;
|
private ContactExchangeIntegrationTestComponent alice, bob;
|
||||||
private Author aliceAuthor, bobAuthor;
|
private Identity aliceIdentity, bobIdentity;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@@ -52,11 +61,11 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
.build();
|
.build();
|
||||||
bob.injectBrambleCoreEagerSingletons();
|
bob.injectBrambleCoreEagerSingletons();
|
||||||
// Set up the devices and get the identities
|
// Set up the devices and get the identities
|
||||||
aliceAuthor = setUp(alice, "Alice");
|
aliceIdentity = setUp(alice, "Alice");
|
||||||
bobAuthor = setUp(bob, "Bob");
|
bobIdentity = setUp(bob, "Bob");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Author setUp(ContactExchangeIntegrationTestComponent device,
|
private Identity setUp(ContactExchangeIntegrationTestComponent device,
|
||||||
String name) throws Exception {
|
String name) throws Exception {
|
||||||
// Add an identity for the user
|
// Add an identity for the user
|
||||||
IdentityManager identityManager = device.getIdentityManager();
|
IdentityManager identityManager = device.getIdentityManager();
|
||||||
@@ -70,7 +79,7 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
ContactManager contactManager = device.getContactManager();
|
ContactManager contactManager = device.getContactManager();
|
||||||
assertEquals(0, contactManager.getPendingContacts().size());
|
assertEquals(0, contactManager.getPendingContacts().size());
|
||||||
assertEquals(0, contactManager.getContacts().size());
|
assertEquals(0, contactManager.getContacts().size());
|
||||||
return identity.getLocalAuthor();
|
return identity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -79,10 +88,12 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
TestDuplexTransportConnection aliceConnection = pair[0];
|
TestDuplexTransportConnection aliceConnection = pair[0];
|
||||||
TestDuplexTransportConnection bobConnection = pair[1];
|
TestDuplexTransportConnection bobConnection = pair[1];
|
||||||
CountDownLatch aliceFinished = new CountDownLatch(1);
|
CountDownLatch aliceFinished = new CountDownLatch(1);
|
||||||
|
boolean verified = random.nextBoolean();
|
||||||
|
|
||||||
alice.getIoExecutor().execute(() -> {
|
alice.getIoExecutor().execute(() -> {
|
||||||
try {
|
try {
|
||||||
alice.getContactExchangeManager().exchangeContacts(
|
alice.getContactExchangeManager().exchangeContacts(
|
||||||
aliceConnection, masterKey, true, true);
|
aliceConnection, masterKey, true, verified);
|
||||||
aliceFinished.countDown();
|
aliceFinished.countDown();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail();
|
fail();
|
||||||
@@ -92,7 +103,7 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
bob.getIoExecutor().execute(() -> {
|
bob.getIoExecutor().execute(() -> {
|
||||||
try {
|
try {
|
||||||
bob.getContactExchangeManager().exchangeContacts(bobConnection,
|
bob.getContactExchangeManager().exchangeContacts(bobConnection,
|
||||||
masterKey, false, true);
|
masterKey, false, verified);
|
||||||
bobFinished.countDown();
|
bobFinished.countDown();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail();
|
fail();
|
||||||
@@ -100,19 +111,107 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
});
|
});
|
||||||
aliceFinished.await(TIMEOUT, MILLISECONDS);
|
aliceFinished.await(TIMEOUT, MILLISECONDS);
|
||||||
bobFinished.await(TIMEOUT, MILLISECONDS);
|
bobFinished.await(TIMEOUT, MILLISECONDS);
|
||||||
assertContactsExchanged();
|
assertContacts(verified, false);
|
||||||
|
assertNoPendingContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertContactsExchanged() throws Exception {
|
@Test
|
||||||
Collection<Contact> aliceContacts =
|
public void testExchangeContactsFromPendingContacts() throws Exception {
|
||||||
alice.getContactManager().getContacts();
|
ContactManager aliceContactManager = alice.getContactManager();
|
||||||
assertEquals(1, aliceContacts.size());
|
ContactManager bobContactManager = bob.getContactManager();
|
||||||
Contact bobFromAlice = aliceContacts.iterator().next();
|
String aliceLink = aliceContactManager.getHandshakeLink();
|
||||||
assertEquals(bobAuthor, bobFromAlice.getAuthor());
|
String bobLink = bobContactManager.getHandshakeLink();
|
||||||
Collection<Contact> bobContacts = bob.getContactManager().getContacts();
|
PendingContact bobFromAlice =
|
||||||
assertEquals(1, bobContacts.size());
|
aliceContactManager.addPendingContact(bobLink, "Bob");
|
||||||
Contact aliceFromBob = bobContacts.iterator().next();
|
PendingContact aliceFromBob =
|
||||||
assertEquals(aliceAuthor, aliceFromBob.getAuthor());
|
bobContactManager.addPendingContact(aliceLink, "Alice");
|
||||||
|
assertPendingContacts();
|
||||||
|
|
||||||
|
TestDuplexTransportConnection[] pair = createPair();
|
||||||
|
TestDuplexTransportConnection aliceConnection = pair[0];
|
||||||
|
TestDuplexTransportConnection bobConnection = pair[1];
|
||||||
|
CountDownLatch aliceFinished = new CountDownLatch(1);
|
||||||
|
boolean verified = random.nextBoolean();
|
||||||
|
|
||||||
|
alice.getIoExecutor().execute(() -> {
|
||||||
|
try {
|
||||||
|
alice.getContactExchangeManager().exchangeContacts(
|
||||||
|
bobFromAlice.getId(), aliceConnection, masterKey, true,
|
||||||
|
verified);
|
||||||
|
aliceFinished.countDown();
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
CountDownLatch bobFinished = new CountDownLatch(1);
|
||||||
|
bob.getIoExecutor().execute(() -> {
|
||||||
|
try {
|
||||||
|
bob.getContactExchangeManager().exchangeContacts(
|
||||||
|
aliceFromBob.getId(), bobConnection, masterKey, false,
|
||||||
|
verified);
|
||||||
|
bobFinished.countDown();
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
aliceFinished.await(TIMEOUT, MILLISECONDS);
|
||||||
|
bobFinished.await(TIMEOUT, MILLISECONDS);
|
||||||
|
assertContacts(verified, true);
|
||||||
|
assertNoPendingContacts();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertContacts(boolean verified,
|
||||||
|
boolean withHandshakeKeys) throws Exception {
|
||||||
|
assertContact(alice, bobIdentity, verified, withHandshakeKeys);
|
||||||
|
assertContact(bob, aliceIdentity, verified, withHandshakeKeys);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertContact(ContactExchangeIntegrationTestComponent device,
|
||||||
|
Identity expectedIdentity, boolean verified,
|
||||||
|
boolean withHandshakeKeys) throws Exception {
|
||||||
|
Collection<Contact> contacts = device.getContactManager().getContacts();
|
||||||
|
assertEquals(1, contacts.size());
|
||||||
|
Contact contact = contacts.iterator().next();
|
||||||
|
assertEquals(expectedIdentity.getLocalAuthor(), contact.getAuthor());
|
||||||
|
assertEquals(verified, contact.isVerified());
|
||||||
|
PublicKey expectedPublicKey = expectedIdentity.getHandshakePublicKey();
|
||||||
|
PublicKey actualPublicKey = contact.getHandshakePublicKey();
|
||||||
|
assertNotNull(expectedPublicKey);
|
||||||
|
if (withHandshakeKeys) {
|
||||||
|
assertNotNull(actualPublicKey);
|
||||||
|
assertArrayEquals(expectedPublicKey.getEncoded(),
|
||||||
|
actualPublicKey.getEncoded());
|
||||||
|
} else {
|
||||||
|
assertNull(actualPublicKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertNoPendingContacts() throws Exception {
|
||||||
|
assertEquals(0, alice.getContactManager().getPendingContacts().size());
|
||||||
|
assertEquals(0, bob.getContactManager().getPendingContacts().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertPendingContacts() throws Exception {
|
||||||
|
assertPendingContact(alice, bobIdentity);
|
||||||
|
assertPendingContact(bob, aliceIdentity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertPendingContact(
|
||||||
|
ContactExchangeIntegrationTestComponent device,
|
||||||
|
Identity expectedIdentity) throws Exception {
|
||||||
|
Collection<Pair<PendingContact, PendingContactState>> pairs =
|
||||||
|
device.getContactManager().getPendingContacts();
|
||||||
|
assertEquals(1, pairs.size());
|
||||||
|
Pair<PendingContact, PendingContactState> pair =
|
||||||
|
pairs.iterator().next();
|
||||||
|
assertEquals(WAITING_FOR_CONNECTION, pair.getSecond());
|
||||||
|
PendingContact pendingContact = pair.getFirst();
|
||||||
|
assertEquals(expectedIdentity.getLocalAuthor().getName(),
|
||||||
|
pendingContact.getAlias());
|
||||||
|
PublicKey expectedPublicKey = expectedIdentity.getHandshakePublicKey();
|
||||||
|
assertNotNull(expectedPublicKey);
|
||||||
|
assertArrayEquals(expectedPublicKey.getEncoded(),
|
||||||
|
pendingContact.getPublicKey().getEncoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tearDown(ContactExchangeIntegrationTestComponent device)
|
private void tearDown(ContactExchangeIntegrationTestComponent device)
|
||||||
|
|||||||
Reference in New Issue
Block a user