From d06ba72edc3136ef2ff8a758e8d53fa13347c778 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 15 Jun 2014 16:08:41 +0100 Subject: Start on Robolectric integration test --- OpenKeychain/build.gradle | 53 ++++++------- .../src/androidTest/java/tests/SomeTest.java | 12 --- .../testsupport/PgpVerifyTestingHelper.java | 82 +++++++++++++++++++++ .../keychain/testsupport/package-info.java | 7 ++ OpenKeychain/src/test/java/tests/RoboTest.java | 37 ++++++++++ .../src/test/resources/public-key-for-sample.blob | Bin 0 -> 35198 bytes OpenKeychain/src/test/resources/sample-altered.txt | 26 +++++++ OpenKeychain/src/test/resources/sample.txt | 26 +++++++ 8 files changed, 201 insertions(+), 42 deletions(-) delete mode 100644 OpenKeychain/src/androidTest/java/tests/SomeTest.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/package-info.java create mode 100644 OpenKeychain/src/test/java/tests/RoboTest.java create mode 100644 OpenKeychain/src/test/resources/public-key-for-sample.blob create mode 100644 OpenKeychain/src/test/resources/sample-altered.txt create mode 100644 OpenKeychain/src/test/resources/sample.txt (limited to 'OpenKeychain') diff --git a/OpenKeychain/build.gradle b/OpenKeychain/build.gradle index ba527948a..b39c19bfa 100644 --- a/OpenKeychain/build.gradle +++ b/OpenKeychain/build.gradle @@ -1,13 +1,5 @@ apply plugin: 'android' -//apply plugin: 'android-test' - -sourceSets { - //androidTest { - //java.srcDir file('src/test/java') - // configure the set of classes for JUnit tests - // include '**/*Test.class' - //} -} +apply plugin: 'robolectric' dependencies { // NOTE: Always use fixed version codes not dynamic ones, e.g. 0.7.3 instead of 0.7.+, see README for more information @@ -29,26 +21,27 @@ dependencies { compile project(':extern:SuperToasts:supertoasts') compile project(':extern:dnsjava') - // Dependencies for the `instrumentTest` task, make sure to list all your global dependencies here as well - androidTestCompile 'junit:junit:4.11' - androidTestCompile 'org.robolectric:robolectric:2.3' - androidTestCompile 'com.squareup:fest-android:1.0.8' - androidTestCompile 'com.google.android:android:4.1.1.4' - androidTestCompile 'com.android.support:support-v4:19.1.0' - androidTestCompile 'com.android.support:appcompat-v7:19.1.0' - androidTestCompile project(':extern:openpgp-api-lib') - androidTestCompile project(':extern:openkeychain-api-lib') - androidTestCompile project(':extern:html-textview') - androidTestCompile project(':extern:StickyListHeaders:library') - androidTestCompile project(':extern:AndroidBootstrap:AndroidBootstrap') - androidTestCompile project(':extern:zxing-qr-code') - androidTestCompile project(':extern:zxing-android-integration') - androidTestCompile project(':extern:spongycastle:core') - androidTestCompile project(':extern:spongycastle:pg') - androidTestCompile project(':extern:spongycastle:pkix') - androidTestCompile project(':extern:spongycastle:prov') - androidTestCompile project(':extern:AppMsg:library') - androidTestCompile project(':extern:SuperToasts:supertoasts') + + // Unit tests are run with Robolectric + testCompile 'junit:junit:4.11' + testCompile 'org.robolectric:robolectric:2.3' + testCompile 'com.squareup:fest-android:1.0.8' + testCompile 'com.google.android:android:4.1.1.4' + testCompile 'com.android.support:support-v4:19.1.0' + testCompile 'com.android.support:appcompat-v7:19.1.0' + testCompile project(':extern:openpgp-api-lib') + testCompile project(':extern:openkeychain-api-lib') + testCompile project(':extern:html-textview') + testCompile project(':extern:StickyListHeaders:library') + testCompile project(':extern:AndroidBootstrap:AndroidBootstrap') + testCompile project(':extern:zxing-qr-code') + testCompile project(':extern:zxing-android-integration') + testCompile project(':extern:spongycastle:core') + testCompile project(':extern:spongycastle:pg') + testCompile project(':extern:spongycastle:pkix') + testCompile project(':extern:spongycastle:prov') + testCompile project(':extern:AppMsg:library') + testCompile project(':extern:SuperToasts:supertoasts') } @@ -57,7 +50,7 @@ android { buildToolsVersion "19.1" defaultConfig { - minSdkVersion 9 + minSdkVersion 15 targetSdkVersion 19 } diff --git a/OpenKeychain/src/androidTest/java/tests/SomeTest.java b/OpenKeychain/src/androidTest/java/tests/SomeTest.java deleted file mode 100644 index edf5f7bcc..000000000 --- a/OpenKeychain/src/androidTest/java/tests/SomeTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package tests; - -import org.junit.Assert; -import org.junit.Test; - -public class SomeTest { - @Test - public void willFail() { - // stub - // Assert.assertThat(); - } -} \ No newline at end of file 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; diff --git a/OpenKeychain/src/test/java/tests/RoboTest.java b/OpenKeychain/src/test/java/tests/RoboTest.java new file mode 100644 index 000000000..1a4f6b4aa --- /dev/null +++ b/OpenKeychain/src/test/java/tests/RoboTest.java @@ -0,0 +1,37 @@ +package tests; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.*; +import org.openintents.openpgp.OpenPgpSignatureResult; +import org.sufficientlysecure.keychain.testsupport.PgpVerifyTestingHelper; + +@RunWith(RobolectricTestRunner.class) +@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 +public class RoboTest { + + @Test + public void testVerifySuccess() throws Exception { + + String testFileName = "/sample.txt"; + int expectedSignatureResult = OpenPgpSignatureResult.SIGNATURE_SUCCESS_UNCERTIFIED; + + int status = new PgpVerifyTestingHelper(Robolectric.application).doTestFile(testFileName); + + Assert.assertEquals(expectedSignatureResult, status); + } + + + @Test + public void testVerifyFailure() throws Exception { + + String testFileName = "/sample-altered.txt"; + int expectedSignatureResult = OpenPgpSignatureResult.SIGNATURE_ERROR; + + int status = new PgpVerifyTestingHelper(Robolectric.application).doTestFile(testFileName); + + Assert.assertEquals(expectedSignatureResult, status); + } + +} diff --git a/OpenKeychain/src/test/resources/public-key-for-sample.blob b/OpenKeychain/src/test/resources/public-key-for-sample.blob new file mode 100644 index 000000000..4aa91510b Binary files /dev/null and b/OpenKeychain/src/test/resources/public-key-for-sample.blob differ diff --git a/OpenKeychain/src/test/resources/sample-altered.txt b/OpenKeychain/src/test/resources/sample-altered.txt new file mode 100644 index 000000000..458821f81 --- /dev/null +++ b/OpenKeychain/src/test/resources/sample-altered.txt @@ -0,0 +1,26 @@ +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +This is a simple text document, which is used to illustrate +the concept of signing simple text files. There are no +control characters or special formatting commands in this +text, just simple printable ASCII characters. +MALICIOUS TEXT +To make this a slightly less uninteresting document, there +follows a short shopping list. + + eggs, 1 doz + milk, 1 gal + bacon, 1 lb + olive oil + bread, 1 loaf + +That's all there is to this document. + +-----BEGIN PGP SIGNATURE----- +Version: PGPfreeware 5.5.5 for non-commercial use + +iQA/AwUBN78ib3S9RCOKzj55EQKqDACg1NV2/iyPKrDlOVJvJwz6ArcQ0UQAnjNC +CDxKAFyaaGa835l1vpbFkAJk +=3r/N +-----END PGP SIGNATURE----- diff --git a/OpenKeychain/src/test/resources/sample.txt b/OpenKeychain/src/test/resources/sample.txt new file mode 100644 index 000000000..c0065f78d --- /dev/null +++ b/OpenKeychain/src/test/resources/sample.txt @@ -0,0 +1,26 @@ +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +This is a simple text document, which is used to illustrate +the concept of signing simple text files. There are no +control characters or special formatting commands in this +text, just simple printable ASCII characters. + +To make this a slightly less uninteresting document, there +follows a short shopping list. + + eggs, 1 doz + milk, 1 gal + bacon, 1 lb + olive oil + bread, 1 loaf + +That's all there is to this document. + +-----BEGIN PGP SIGNATURE----- +Version: PGPfreeware 5.5.5 for non-commercial use + +iQA/AwUBN78ib3S9RCOKzj55EQKqDACg1NV2/iyPKrDlOVJvJwz6ArcQ0UQAnjNC +CDxKAFyaaGa835l1vpbFkAJk +=3r/N +-----END PGP SIGNATURE----- -- cgit v1.2.3