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

View File

@@ -55,7 +55,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
this.observer = observer;
if (inetAddress == null) {
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);
socketAddress = new InetSocketAddress(inetAddress, PORT);
@@ -69,7 +69,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
} catch (IOException e) {
LOG.warning(
"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
return;
}
@@ -86,7 +86,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
LOG.info("changing state to listening done");
} catch (FormatException e) {
LOG.warning("Error encoding QR code");
observer.onStateChanged(new State.Failure());
observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER));
return;
}
LOG.info("receiving payload");
@@ -140,14 +140,14 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
serverSocket.close();
observer.onStateChanged(new State.Success());
observer.onStateChanged(new State.Success(payloadClear));
} catch (IOException e) {
LOG.warning("IO Error receiving payload " + e.getMessage());
// TODO reasons
observer.onStateChanged(new State.Failure());
observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION));
} catch (GeneralSecurityException e) {
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 {
serverSocket.close();
} catch (IOException e) {
observer.onStateChanged(new State.Failure());
observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER));
}
}
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.net.InetAddress;
import java.util.Arrays;
import static junit.framework.TestCase.fail;
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ReturnShardIntegrationTest extends BrambleTestCase {
@@ -47,17 +49,22 @@ public class ReturnShardIntegrationTest extends BrambleTestCase {
public void testReturnShard() {
SecretOwnerTask secretOwnerTask = owner.getSecretOwnerTask();
CustodianTask custodianTask = custodian.getCustodianTask();
byte[] payload = "its nice to be important but its more important to be nice".getBytes();
SecretOwnerTask.Observer ownerObserver =
state -> {
if (state instanceof SecretOwnerTask.State.Listening) {
SecretOwnerTask.State.Listening listening =
(SecretOwnerTask.State.Listening) state;
byte[] payload = listening.getLocalPayload();
System.out.println(payload.length);
tansferQrCode(custodianTask, payload);
byte[] qrPayload = listening.getLocalPayload();
System.out.println(qrPayload.length);
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) {
System.out.println("owner state: failure");
System.out.println("Owner state: failure");
fail();
} else {
System.out.println(
@@ -66,8 +73,16 @@ public class ReturnShardIntegrationTest extends BrambleTestCase {
};
CustodianTask.Observer custodianObserver =
state -> System.out.println(
"custodian: " + state.getClass().getSimpleName());
state -> {
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(() -> {
try {
@@ -80,21 +95,25 @@ public class ReturnShardIntegrationTest extends BrambleTestCase {
custodian.getIoExecutor().execute(() -> {
try {
custodianTask.start(custodianObserver);
custodianTask.start(custodianObserver, payload);
} catch (Exception e) {
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) {
System.out.println("Calling qrCodeDecoded in executor()");
private void transferQrCode(CustodianTask custodianTask, byte[] payload) {
custodian.getIoExecutor().execute(() -> {
try {
System.out.println("Calling qrCodeDecoded()");
Thread.sleep(500);
custodianTask.qrCodeDecoded(payload);
System.out.println("qrCodeDecoded() done");
} catch (Exception e) {
e.printStackTrace();
fail();