diff options
author | Art O Cathain <art.home@gmail.com> | 2014-06-15 16:08:41 +0100 |
---|---|---|
committer | Art O Cathain <art.home@gmail.com> | 2014-06-16 18:58:41 +0100 |
commit | d06ba72edc3136ef2ff8a758e8d53fa13347c778 (patch) | |
tree | 85f122b507e66efca0843f50e81431815fa2c4a1 /OpenKeychain/src/main/java/org | |
parent | 21e9d0b7b4b0375c43287d4acd66ee01f2f1d5a0 (diff) | |
download | open-keychain-d06ba72edc3136ef2ff8a758e8d53fa13347c778.tar.gz open-keychain-d06ba72edc3136ef2ff8a758e8d53fa13347c778.tar.bz2 open-keychain-d06ba72edc3136ef2ff8a758e8d53fa13347c778.zip |
Start on Robolectric integration test
Diffstat (limited to 'OpenKeychain/src/main/java/org')
2 files changed, 89 insertions, 0 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java new file mode 100644 index 000000000..d5edae5f0 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java @@ -0,0 +1,82 @@ +package org.sufficientlysecure.keychain.testsupport; + +import android.content.Context; +import android.net.Uri; + +import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; +import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult; +import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; +import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.util.InputData; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * For functional tests of PgpDecryptVerify + */ +public class PgpVerifyTestingHelper { + + private final Context context; + + public PgpVerifyTestingHelper(Context robolectricContext) { + this.context=robolectricContext; + } + + public int doTestFile(String testFileName) throws Exception { + ProviderHelper providerHelper = new ProviderHelperStub(context); + + PgpDecryptVerify.PassphraseCache passphraseCache = new PgpDecryptVerify.PassphraseCache() { + public String getCachedPassphrase(long masterKeyId) { + return "I am a passphrase"; + } + }; + + InputStream sampleInput = getClass().getResourceAsStream(testFileName); + assert null != sampleInput; + InputData data = new InputData(sampleInput, 705); + OutputStream outStream = new ByteArrayOutputStream(); + + PgpDecryptVerify verify = new PgpDecryptVerify.Builder(providerHelper, passphraseCache, data, outStream).build(); + PgpDecryptVerifyResult result = verify.execute(); + + return result.getSignatureResult().getStatus(); + } + + + static class ProviderHelperStub extends ProviderHelper { + public ProviderHelperStub(Context context) { + super(context); + } + + @Override + public WrappedPublicKeyRing getWrappedPublicKeyRing(Uri id) throws NotFoundException { + try { + byte[] data = readFully(getClass().getResourceAsStream("/public-key-for-sample.blob")); + return new WrappedPublicKeyRing(data, false, 0); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public static byte[] readFully(InputStream input) throws IOException + { + byte[] buffer = new byte[8192]; + int bytesRead; + ByteArrayOutputStream output = new ByteArrayOutputStream(); + while ((bytesRead = input.read(buffer)) != -1) + { + output.write(buffer, 0, bytesRead); + } + return output.toByteArray(); + } + + + + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/package-info.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/package-info.java new file mode 100644 index 000000000..1cc0f9a95 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/package-info.java @@ -0,0 +1,7 @@ +/** + * Test support classes. + * This is only in main code because of gradle-Android Studio-robolectric issues. Having + * classes in main code means IDE autocomplete, class detection, etc., all works. + * TODO Move into test package when possible + */ +package org.sufficientlysecure.keychain.testsupport; |