mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
Updated non-JUnit plugin tests and added tests for the modem plugin.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package net.sf.briar.plugins;
|
package net.sf.briar.plugins;
|
||||||
|
|
||||||
|
import static net.sf.briar.api.plugins.InvitationConstants.CONNECTION_TIMEOUT;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -17,37 +19,47 @@ public abstract class DuplexClientTest extends DuplexTest {
|
|||||||
assert plugin != null;
|
assert plugin != null;
|
||||||
// Start the plugin
|
// Start the plugin
|
||||||
System.out.println("Starting plugin");
|
System.out.println("Starting plugin");
|
||||||
plugin.start();
|
if(!plugin.start()) {
|
||||||
// Try to connect to the server
|
System.out.println("Plugin failed to start");
|
||||||
System.out.println("Creating connection");
|
return;
|
||||||
DuplexTransportConnection d = plugin.createConnection(contactId);
|
|
||||||
if(d == null) {
|
|
||||||
System.out.println("Connection failed");
|
|
||||||
} else {
|
|
||||||
System.out.println("Connection created");
|
|
||||||
receiveChallengeSendResponse(d);
|
|
||||||
}
|
}
|
||||||
// Try to send an invitation
|
try {
|
||||||
System.out.println("Sending invitation");
|
// Try to connect to the server
|
||||||
d = plugin.sendInvitation(getPseudoRandom(123), INVITATION_TIMEOUT);
|
System.out.println("Creating connection");
|
||||||
if(d == null) {
|
DuplexTransportConnection d = plugin.createConnection(contactId);
|
||||||
System.out.println("Connection failed");
|
if(d == null) {
|
||||||
} else {
|
System.out.println("Connection failed");
|
||||||
System.out.println("Connection created");
|
return;
|
||||||
receiveChallengeSendResponse(d);
|
} else {
|
||||||
|
System.out.println("Connection created");
|
||||||
|
receiveChallengeSendResponse(d);
|
||||||
|
}
|
||||||
|
// Try to send an invitation
|
||||||
|
System.out.println("Sending invitation");
|
||||||
|
d = plugin.sendInvitation(getPseudoRandom(123), CONNECTION_TIMEOUT);
|
||||||
|
if(d == null) {
|
||||||
|
System.out.println("Connection failed");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
System.out.println("Connection created");
|
||||||
|
receiveChallengeSendResponse(d);
|
||||||
|
}
|
||||||
|
// Try to accept an invitation
|
||||||
|
System.out.println("Accepting invitation");
|
||||||
|
d = plugin.acceptInvitation(getPseudoRandom(456),
|
||||||
|
CONNECTION_TIMEOUT);
|
||||||
|
if(d == null) {
|
||||||
|
System.out.println("Connection failed");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
System.out.println("Connection created");
|
||||||
|
sendChallengeReceiveResponse(d);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Stop the plugin
|
||||||
|
System.out.println("Stopping plugin");
|
||||||
|
plugin.stop();
|
||||||
}
|
}
|
||||||
// Try to accept an invitation
|
|
||||||
System.out.println("Accepting invitation");
|
|
||||||
d = plugin.acceptInvitation(getPseudoRandom(456), INVITATION_TIMEOUT);
|
|
||||||
if(d == null) {
|
|
||||||
System.out.println("Connection failed");
|
|
||||||
} else {
|
|
||||||
System.out.println("Connection created");
|
|
||||||
sendChallengeReceiveResponse(d);
|
|
||||||
}
|
|
||||||
// Stop the plugin
|
|
||||||
System.out.println("Stopping plugin");
|
|
||||||
plugin.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static class ClientCallback implements DuplexPluginCallback {
|
protected static class ClientCallback implements DuplexPluginCallback {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package net.sf.briar.plugins;
|
package net.sf.briar.plugins;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
import static net.sf.briar.api.plugins.InvitationConstants.CONNECTION_TIMEOUT;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
@@ -18,32 +21,43 @@ public abstract class DuplexServerTest extends DuplexTest {
|
|||||||
assert plugin != null;
|
assert plugin != null;
|
||||||
// Start the plugin
|
// Start the plugin
|
||||||
System.out.println("Starting plugin");
|
System.out.println("Starting plugin");
|
||||||
plugin.start();
|
if(!plugin.start()) {
|
||||||
// Wait for a connection
|
System.out.println("Plugin failed to start");
|
||||||
System.out.println("Waiting for connection");
|
return;
|
||||||
callback.latch.await();
|
|
||||||
// Try to accept an invitation
|
|
||||||
System.out.println("Accepting invitation");
|
|
||||||
DuplexTransportConnection d = plugin.acceptInvitation(
|
|
||||||
getPseudoRandom(123), INVITATION_TIMEOUT);
|
|
||||||
if(d == null) {
|
|
||||||
System.out.println("Connection failed");
|
|
||||||
} else {
|
|
||||||
System.out.println("Connection created");
|
|
||||||
sendChallengeReceiveResponse(d);
|
|
||||||
}
|
}
|
||||||
// Try to send an invitation
|
try {
|
||||||
System.out.println("Sending invitation");
|
// Wait for a connection
|
||||||
d = plugin.sendInvitation(getPseudoRandom(456), INVITATION_TIMEOUT);
|
System.out.println("Waiting for connection");
|
||||||
if(d == null) {
|
if(!callback.latch.await(CONNECTION_TIMEOUT, MILLISECONDS)) {
|
||||||
System.out.println("Connection failed");
|
System.out.println("No connection received");
|
||||||
} else {
|
return;
|
||||||
System.out.println("Connection created");
|
}
|
||||||
receiveChallengeSendResponse(d);
|
// Try to accept an invitation
|
||||||
|
System.out.println("Accepting invitation");
|
||||||
|
DuplexTransportConnection d = plugin.acceptInvitation(
|
||||||
|
getPseudoRandom(123), CONNECTION_TIMEOUT);
|
||||||
|
if(d == null) {
|
||||||
|
System.out.println("Connection failed");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
System.out.println("Connection created");
|
||||||
|
sendChallengeReceiveResponse(d);
|
||||||
|
}
|
||||||
|
// Try to send an invitation
|
||||||
|
System.out.println("Sending invitation");
|
||||||
|
d = plugin.sendInvitation(getPseudoRandom(456), CONNECTION_TIMEOUT);
|
||||||
|
if(d == null) {
|
||||||
|
System.out.println("Connection failed");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
System.out.println("Connection created");
|
||||||
|
receiveChallengeSendResponse(d);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Stop the plugin
|
||||||
|
System.out.println("Stopping plugin");
|
||||||
|
plugin.stop();
|
||||||
}
|
}
|
||||||
// Stop the plugin
|
|
||||||
System.out.println("Stopping plugin");
|
|
||||||
plugin.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class ServerCallback implements DuplexPluginCallback {
|
protected class ServerCallback implements DuplexPluginCallback {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ abstract class DuplexTest {
|
|||||||
|
|
||||||
protected static final String CHALLENGE = "Carrots!";
|
protected static final String CHALLENGE = "Carrots!";
|
||||||
protected static final String RESPONSE = "Potatoes!";
|
protected static final String RESPONSE = "Potatoes!";
|
||||||
protected static final long INVITATION_TIMEOUT = 30 * 1000;
|
|
||||||
|
|
||||||
protected final ContactId contactId = new ContactId(234);
|
protected final ContactId contactId = new ContactId(234);
|
||||||
|
|
||||||
@@ -78,21 +77,13 @@ abstract class DuplexTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected PseudoRandom getPseudoRandom(int seed) {
|
protected PseudoRandom getPseudoRandom(int seed) {
|
||||||
return new TestPseudoRandom(seed);
|
final Random random = new Random(seed);
|
||||||
}
|
return new PseudoRandom() {
|
||||||
|
public byte[] nextBytes(int bytes) {
|
||||||
private static class TestPseudoRandom implements PseudoRandom {
|
byte[] b = new byte[bytes];
|
||||||
|
random.nextBytes(b);
|
||||||
private final Random r;
|
return b;
|
||||||
|
}
|
||||||
private TestPseudoRandom(int seed) {
|
};
|
||||||
r = new Random(seed);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] nextBytes(int bytes) {
|
|
||||||
byte[] b = new byte[bytes];
|
|
||||||
r.nextBytes(b);
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package net.sf.briar.plugins.modem;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import net.sf.briar.api.ContactId;
|
||||||
|
import net.sf.briar.api.TransportConfig;
|
||||||
|
import net.sf.briar.api.TransportProperties;
|
||||||
|
import net.sf.briar.plugins.DuplexClientTest;
|
||||||
|
|
||||||
|
//This is not a JUnit test - it has to be run manually while the server test
|
||||||
|
//is running on another machine
|
||||||
|
public class ModemClientTest extends DuplexClientTest {
|
||||||
|
|
||||||
|
private ModemClientTest(Executor executor, String number) {
|
||||||
|
// Store the server's phone number
|
||||||
|
TransportProperties p = new TransportProperties();
|
||||||
|
p.put("number", number);
|
||||||
|
Map<ContactId, TransportProperties> remote =
|
||||||
|
Collections.singletonMap(contactId, p);
|
||||||
|
// Create the plugin
|
||||||
|
callback = new ClientCallback(new TransportConfig(),
|
||||||
|
new TransportProperties(), remote);
|
||||||
|
plugin = new ModemPlugin(executor, new ModemFactoryImpl(executor),
|
||||||
|
callback, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
if(args.length != 1) {
|
||||||
|
System.err.println("Please specify the server's phone number");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
ExecutorService executor = Executors.newCachedThreadPool();
|
||||||
|
try {
|
||||||
|
new ModemClientTest(executor, args[0]).run();
|
||||||
|
} finally {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package net.sf.briar.plugins.modem;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import net.sf.briar.api.TransportConfig;
|
||||||
|
import net.sf.briar.api.TransportProperties;
|
||||||
|
import net.sf.briar.plugins.DuplexServerTest;
|
||||||
|
|
||||||
|
//This is not a JUnit test - it has to be run manually while the client test
|
||||||
|
//is running on another machine
|
||||||
|
public class ModemServerTest extends DuplexServerTest {
|
||||||
|
|
||||||
|
private ModemServerTest(Executor executor) {
|
||||||
|
// Create the plugin
|
||||||
|
callback = new ServerCallback(new TransportConfig(),
|
||||||
|
new TransportProperties(), Collections.singletonMap(contactId,
|
||||||
|
new TransportProperties()));
|
||||||
|
plugin = new ModemPlugin(executor, new ModemFactoryImpl(executor),
|
||||||
|
callback, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ExecutorService executor = Executors.newCachedThreadPool();
|
||||||
|
try {
|
||||||
|
new ModemServerTest(executor).run();
|
||||||
|
} finally {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user