tasks take and produce shard payload, improve integration test

This commit is contained in:
ameba23
2021-04-15 21:24:01 +02:00
parent 4ba3fdb1e3
commit fec74ed343
3 changed files with 42 additions and 24 deletions

View File

@@ -33,6 +33,7 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
private InetSocketAddress remoteSocketAddress; private InetSocketAddress remoteSocketAddress;
private final Socket socket = new Socket(); private final Socket socket = new Socket();
private final AuthenticatedCipher cipher; private final AuthenticatedCipher cipher;
private byte[] payload;
// private final StreamReaderFactory streamReaderFactory; // private final StreamReaderFactory streamReaderFactory;
// private final StreamWriterFactory streamWriterFactory; // private final StreamWriterFactory streamWriterFactory;
@@ -52,8 +53,9 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
} }
@Override @Override
public void start(Observer observer) { public void start(Observer observer, byte[] payload) {
this.observer = observer; this.observer = observer;
this.payload = payload;
observer.onStateChanged(new CustodianTask.State.Connecting()); observer.onStateChanged(new CustodianTask.State.Connecting());
} }
@@ -107,9 +109,6 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
OutputStream outputStream = socket.getOutputStream(); OutputStream outputStream = socket.getOutputStream();
// TODO insert the actual payload
byte[] payload = "crunchy".getBytes();
byte[] payloadNonce = generateNonce(); byte[] payloadNonce = generateNonce();
byte[] payloadEncrypted = encrypt(payload, payloadNonce); byte[] payloadEncrypted = encrypt(payload, payloadNonce);

View File

@@ -55,7 +55,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
this.observer = observer; this.observer = observer;
if (inetAddress == null) { if (inetAddress == null) {
LOG.warning("Cannot retrieve local IP address, failing."); LOG.warning("Cannot retrieve local IP address, failing.");
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION));
} }
LOG.info("InetAddress is " + inetAddress); LOG.info("InetAddress is " + inetAddress);
socketAddress = new InetSocketAddress(inetAddress, PORT); socketAddress = new InetSocketAddress(inetAddress, PORT);
@@ -69,7 +69,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
} catch (IOException e) { } catch (IOException e) {
LOG.warning( LOG.warning(
"IO Error when listening on local socket" + e.getMessage()); "IO Error when listening on local socket" + e.getMessage());
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION));
// TODO could try incrementing the port number // TODO could try incrementing the port number
return; return;
} }
@@ -86,7 +86,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
LOG.info("changing state to listening done"); LOG.info("changing state to listening done");
} catch (FormatException e) { } catch (FormatException e) {
LOG.warning("Error encoding QR code"); LOG.warning("Error encoding QR code");
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER));
return; return;
} }
LOG.info("receiving payload"); LOG.info("receiving payload");
@@ -140,14 +140,14 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
serverSocket.close(); serverSocket.close();
observer.onStateChanged(new State.Success()); observer.onStateChanged(new State.Success(payloadClear));
} catch (IOException e) { } catch (IOException e) {
LOG.warning("IO Error receiving payload " + e.getMessage()); LOG.warning("IO Error receiving payload " + e.getMessage());
// TODO reasons // TODO reasons
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION));
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
LOG.warning("Security Error receiving payload " + e.getMessage()); LOG.warning("Security Error receiving payload " + e.getMessage());
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.SECURITY));
} }
} }
@@ -159,12 +159,12 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
try { try {
serverSocket.close(); serverSocket.close();
} catch (IOException e) { } catch (IOException e) {
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER));
} }
} }
if (observer != null) { if (observer != null) {
observer.onStateChanged(new State.Failure()); observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER));
} }
} }
} }

View File

@@ -12,10 +12,12 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Arrays;
import static junit.framework.TestCase.fail; import static junit.framework.TestCase.fail;
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory; import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
import static org.briarproject.bramble.test.TestUtils.getTestDirectory; import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class ReturnShardIntegrationTest extends BrambleTestCase { public class ReturnShardIntegrationTest extends BrambleTestCase {
@@ -47,17 +49,22 @@ public class ReturnShardIntegrationTest extends BrambleTestCase {
public void testReturnShard() { public void testReturnShard() {
SecretOwnerTask secretOwnerTask = owner.getSecretOwnerTask(); SecretOwnerTask secretOwnerTask = owner.getSecretOwnerTask();
CustodianTask custodianTask = custodian.getCustodianTask(); CustodianTask custodianTask = custodian.getCustodianTask();
byte[] payload = "its nice to be important but its more important to be nice".getBytes();
SecretOwnerTask.Observer ownerObserver = SecretOwnerTask.Observer ownerObserver =
state -> { state -> {
if (state instanceof SecretOwnerTask.State.Listening) { if (state instanceof SecretOwnerTask.State.Listening) {
SecretOwnerTask.State.Listening listening = SecretOwnerTask.State.Listening listening =
(SecretOwnerTask.State.Listening) state; (SecretOwnerTask.State.Listening) state;
byte[] payload = listening.getLocalPayload(); byte[] qrPayload = listening.getLocalPayload();
System.out.println(payload.length); System.out.println(qrPayload.length);
tansferQrCode(custodianTask, payload); transferQrCode(custodianTask, qrPayload);
} else if (state instanceof SecretOwnerTask.State.Success) {
byte[] remotePayload = ((SecretOwnerTask.State.Success) state).getRemotePayload();
assertTrue(Arrays.equals(remotePayload, payload));
System.out.println("Success");
} else if (state instanceof SecretOwnerTask.State.Failure) { } else if (state instanceof SecretOwnerTask.State.Failure) {
System.out.println("owner state: failure"); System.out.println("Owner state: failure");
fail(); fail();
} else { } else {
System.out.println( System.out.println(
@@ -66,8 +73,16 @@ public class ReturnShardIntegrationTest extends BrambleTestCase {
}; };
CustodianTask.Observer custodianObserver = CustodianTask.Observer custodianObserver =
state -> System.out.println( state -> {
"custodian: " + state.getClass().getSimpleName()); if (state instanceof CustodianTask.State.Success) {
assertEquals(1, 1);
} else if (state instanceof CustodianTask.State.Failure) {
fail();
} else {
System.out.println(
"custodian: " + state.getClass().getSimpleName());
}
};
owner.getIoExecutor().execute(() -> { owner.getIoExecutor().execute(() -> {
try { try {
@@ -80,21 +95,25 @@ public class ReturnShardIntegrationTest extends BrambleTestCase {
custodian.getIoExecutor().execute(() -> { custodian.getIoExecutor().execute(() -> {
try { try {
custodianTask.start(custodianObserver); custodianTask.start(custodianObserver, payload);
} catch (Exception e) { } catch (Exception e) {
fail(); fail();
} }
}); });
// TODO how to get the test to wait for the io to finish
try {
Thread.sleep(1000);
} catch (Exception e) {
fail();
}
} }
private void tansferQrCode(CustodianTask custodianTask, byte[] payload) { private void transferQrCode(CustodianTask custodianTask, byte[] payload) {
System.out.println("Calling qrCodeDecoded in executor()");
custodian.getIoExecutor().execute(() -> { custodian.getIoExecutor().execute(() -> {
try { try {
System.out.println("Calling qrCodeDecoded()");
Thread.sleep(500);
custodianTask.qrCodeDecoded(payload); custodianTask.qrCodeDecoded(payload);
System.out.println("qrCodeDecoded() done");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(); fail();