tls-psk: stub for tests
This commit is contained in:
parent
dc350cff50
commit
b58e30914c
|
@ -0,0 +1,122 @@
|
||||||
|
package org.sufficientlysecure.keychain.network;
|
||||||
|
|
||||||
|
|
||||||
|
import java.security.Security;
|
||||||
|
|
||||||
|
import android.os.Build.VERSION_CODES;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.shadows.ShadowLog;
|
||||||
|
import org.robolectric.shadows.ShadowLooper;
|
||||||
|
import org.sufficientlysecure.keychain.KeychainTestRunner;
|
||||||
|
import org.sufficientlysecure.keychain.network.KeyTransferInteractor.KeyTransferCallback;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
|
import static junit.framework.Assert.fail;
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
// disabled, because we can't easily mock the tls-psk ciphersuite (it's removed in bouncycastle) :(
|
||||||
|
//@RunWith(KeychainTestRunner.class)
|
||||||
|
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
|
||||||
|
public class KeyTransferInteractorTest {
|
||||||
|
private static final String DELIM_START = "--";
|
||||||
|
private static final String DELIM_END = "--";
|
||||||
|
|
||||||
|
private String receivedQrCodeData;
|
||||||
|
private boolean clientConnectionEstablished;
|
||||||
|
private boolean serverConnectionEstablished;
|
||||||
|
|
||||||
|
// @Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
ShadowLog.stream = System.out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
public void testServerShouldGiveSuccessCallback() {
|
||||||
|
KeyTransferInteractor serverKeyTransferInteractor = new KeyTransferInteractor(DELIM_START, DELIM_END);
|
||||||
|
|
||||||
|
serverKeyTransferInteractor.startServer(new SimpleKeyTransferCallback() {
|
||||||
|
@Override
|
||||||
|
public void onServerStarted(String qrCodeData) {
|
||||||
|
receivedQrCodeData = qrCodeData;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnectionEstablished(String otherName) {
|
||||||
|
serverConnectionEstablished = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
waitForLooperCallback();
|
||||||
|
Assert.assertNotNull(receivedQrCodeData);
|
||||||
|
|
||||||
|
final KeyTransferInteractor clientKeyTransferInteractor = new KeyTransferInteractor(DELIM_START, DELIM_END);
|
||||||
|
clientKeyTransferInteractor.connectToServer(receivedQrCodeData, new SimpleKeyTransferCallback() {
|
||||||
|
@Override
|
||||||
|
public void onConnectionEstablished(String otherName) {
|
||||||
|
clientConnectionEstablished = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
waitForLooperCallback();
|
||||||
|
waitForLooperCallback();
|
||||||
|
|
||||||
|
assertTrue(clientConnectionEstablished);
|
||||||
|
assertTrue(serverConnectionEstablished);
|
||||||
|
|
||||||
|
serverKeyTransferInteractor.sendData(new byte[] { (byte) 1, (byte) 2 }, "passthrough");
|
||||||
|
waitForLooperCallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitForLooperCallback() {
|
||||||
|
while (!ShadowLooper.getShadowMainLooper().getScheduler().runOneTask());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static class SimpleKeyTransferCallback implements KeyTransferCallback {
|
||||||
|
@Override
|
||||||
|
public void onServerStarted(String qrCodeData) {
|
||||||
|
fail("unexpected callback: onServerStarted");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnectionEstablished(String otherName) {
|
||||||
|
fail("unexpected callback: onConnectionEstablished");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnectionLost() {
|
||||||
|
fail("unexpected callback: onConnectionLost");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataReceivedOk(String receivedData) {
|
||||||
|
fail("unexpected callback: onDataReceivedOk");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataSentOk(String passthrough) {
|
||||||
|
fail("unexpected callback: onDataSentOk");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnectionErrorConnect() {
|
||||||
|
fail("unexpected callback: onConnectionErrorConnect");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnectionErrorListen() {
|
||||||
|
fail("unexpected callback: onConnectionErrorListen");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnectionError(String arg) {
|
||||||
|
fail("unexpected callback: onConnectionError");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue