Updated non-JUnit plugin tests and added tests for the modem plugin.

This commit is contained in:
akwizgran
2012-12-06 13:10:14 +00:00
parent dca9470c28
commit 51a4f2fd62
5 changed files with 164 additions and 70 deletions

View File

@@ -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,38 +19,48 @@ 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()) {
System.out.println("Plugin failed to start");
return;
}
try {
// Try to connect to the server // Try to connect to the server
System.out.println("Creating connection"); System.out.println("Creating connection");
DuplexTransportConnection d = plugin.createConnection(contactId); DuplexTransportConnection d = plugin.createConnection(contactId);
if(d == null) { if(d == null) {
System.out.println("Connection failed"); System.out.println("Connection failed");
return;
} else { } else {
System.out.println("Connection created"); System.out.println("Connection created");
receiveChallengeSendResponse(d); receiveChallengeSendResponse(d);
} }
// Try to send an invitation // Try to send an invitation
System.out.println("Sending invitation"); System.out.println("Sending invitation");
d = plugin.sendInvitation(getPseudoRandom(123), INVITATION_TIMEOUT); d = plugin.sendInvitation(getPseudoRandom(123), CONNECTION_TIMEOUT);
if(d == null) { if(d == null) {
System.out.println("Connection failed"); System.out.println("Connection failed");
return;
} else { } else {
System.out.println("Connection created"); System.out.println("Connection created");
receiveChallengeSendResponse(d); receiveChallengeSendResponse(d);
} }
// Try to accept an invitation // Try to accept an invitation
System.out.println("Accepting invitation"); System.out.println("Accepting invitation");
d = plugin.acceptInvitation(getPseudoRandom(456), INVITATION_TIMEOUT); d = plugin.acceptInvitation(getPseudoRandom(456),
CONNECTION_TIMEOUT);
if(d == null) { if(d == null) {
System.out.println("Connection failed"); System.out.println("Connection failed");
return;
} else { } else {
System.out.println("Connection created"); System.out.println("Connection created");
sendChallengeReceiveResponse(d); sendChallengeReceiveResponse(d);
} }
} finally {
// Stop the plugin // Stop the plugin
System.out.println("Stopping plugin"); System.out.println("Stopping plugin");
plugin.stop(); plugin.stop();
} }
}
protected static class ClientCallback implements DuplexPluginCallback { protected static class ClientCallback implements DuplexPluginCallback {

View File

@@ -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,33 +21,44 @@ 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()) {
System.out.println("Plugin failed to start");
return;
}
try {
// Wait for a connection // Wait for a connection
System.out.println("Waiting for connection"); System.out.println("Waiting for connection");
callback.latch.await(); if(!callback.latch.await(CONNECTION_TIMEOUT, MILLISECONDS)) {
System.out.println("No connection received");
return;
}
// Try to accept an invitation // Try to accept an invitation
System.out.println("Accepting invitation"); System.out.println("Accepting invitation");
DuplexTransportConnection d = plugin.acceptInvitation( DuplexTransportConnection d = plugin.acceptInvitation(
getPseudoRandom(123), INVITATION_TIMEOUT); getPseudoRandom(123), CONNECTION_TIMEOUT);
if(d == null) { if(d == null) {
System.out.println("Connection failed"); System.out.println("Connection failed");
return;
} else { } else {
System.out.println("Connection created"); System.out.println("Connection created");
sendChallengeReceiveResponse(d); sendChallengeReceiveResponse(d);
} }
// Try to send an invitation // Try to send an invitation
System.out.println("Sending invitation"); System.out.println("Sending invitation");
d = plugin.sendInvitation(getPseudoRandom(456), INVITATION_TIMEOUT); d = plugin.sendInvitation(getPseudoRandom(456), CONNECTION_TIMEOUT);
if(d == null) { if(d == null) {
System.out.println("Connection failed"); System.out.println("Connection failed");
return;
} else { } else {
System.out.println("Connection created"); System.out.println("Connection created");
receiveChallengeSendResponse(d); receiveChallengeSendResponse(d);
} }
} finally {
// Stop the plugin // Stop the plugin
System.out.println("Stopping plugin"); System.out.println("Stopping plugin");
plugin.stop(); plugin.stop();
} }
}
protected class ServerCallback implements DuplexPluginCallback { protected class ServerCallback implements DuplexPluginCallback {

View File

@@ -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() {
private static class TestPseudoRandom implements PseudoRandom {
private final Random r;
private TestPseudoRandom(int seed) {
r = new Random(seed);
}
public byte[] nextBytes(int bytes) { public byte[] nextBytes(int bytes) {
byte[] b = new byte[bytes]; byte[] b = new byte[bytes];
r.nextBytes(b); random.nextBytes(b);
return b; return b;
} }
};
} }
} }

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}