put unit tests into external module (CAVEAT)

this requires a more up to date version of gradle-android-test-plugin
than is currently in the repositories. it must be added to the local
maven repo using ./install-custom-gradle-test-plugin.sh before
compiling.
This commit is contained in:
Vincent Breitmoser
2014-07-09 15:53:18 +02:00
parent 9320d2d8a2
commit 718acbf954
14 changed files with 148 additions and 16 deletions

View File

@@ -0,0 +1,36 @@
package org.sufficientlysecure.keychain.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 PgpDecryptVerifyTest {
@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);
}
}

View File

@@ -0,0 +1,84 @@
package org.sufficientlysecure.keychain.tests;
import java.util.Collections;
import java.util.Arrays;
import java.util.Collection;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.*;
import org.sufficientlysecure.keychain.testsupport.KeyringTestingHelper;
@RunWith(RobolectricTestRunner.class)
@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19
public class ProviderHelperKeyringTest {
@Test
public void testSavePublicKeyring() throws Exception {
Assert.assertTrue(new KeyringTestingHelper(Robolectric.application).addKeyring(Collections.singleton(
"/public-key-for-sample.blob"
)));
}
@Test
public void testSavePublicKeyringRsa() throws Exception {
Assert.assertTrue(new KeyringTestingHelper(Robolectric.application).addKeyring(prependResourcePath(Arrays.asList(
"000001-006.public_key",
"000002-013.user_id",
"000003-002.sig",
"000004-012.ring_trust",
"000005-002.sig",
"000006-012.ring_trust",
"000007-002.sig",
"000008-012.ring_trust",
"000009-002.sig",
"000010-012.ring_trust",
"000011-002.sig",
"000012-012.ring_trust",
"000013-014.public_subkey",
"000014-002.sig",
"000015-012.ring_trust"
))));
}
@Test
public void testSavePublicKeyringDsa() throws Exception {
Assert.assertTrue(new KeyringTestingHelper(Robolectric.application).addKeyring(prependResourcePath(Arrays.asList(
"000016-006.public_key",
"000017-002.sig",
"000018-012.ring_trust",
"000019-013.user_id",
"000020-002.sig",
"000021-012.ring_trust",
"000022-002.sig",
"000023-012.ring_trust",
"000024-014.public_subkey",
"000025-002.sig",
"000026-012.ring_trust"
))));
}
@Test
public void testSavePublicKeyringDsa2() throws Exception {
Assert.assertTrue(new KeyringTestingHelper(Robolectric.application).addKeyring(prependResourcePath(Arrays.asList(
"000027-006.public_key",
"000028-002.sig",
"000029-012.ring_trust",
"000030-013.user_id",
"000031-002.sig",
"000032-012.ring_trust",
"000033-002.sig",
"000034-012.ring_trust"
))));
}
private static Collection<String> prependResourcePath(Collection<String> files) {
Collection<String> prependedFiles = new ArrayList<String>();
for (String file: files) {
prependedFiles.add("/extern/OpenPGP-Haskell/tests/data/" + file);
}
return prependedFiles;
}
}

View File

@@ -0,0 +1,91 @@
package org.sufficientlysecure.keychain.tests;
import android.app.Activity;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.robolectric.*;
import org.robolectric.shadows.ShadowLog;
import org.spongycastle.bcpg.sig.KeyFlags;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.service.OperationResultParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.testsupport.KeyringBuilder;
import org.sufficientlysecure.keychain.testsupport.KeyringTestingHelper;
import org.sufficientlysecure.keychain.testsupport.TestDataUtil;
import org.sufficientlysecure.keychain.ui.KeyListActivity;
import java.util.HashSet;
@RunWith(RobolectricTestRunner.class)
@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19
public class UncachedKeyringTest {
@Before
public void setUp() throws Exception {
ShadowLog.stream = System.out;
}
@Test
public void testCreateKey() throws Exception {
Activity activity = Robolectric.buildActivity(KeyListActivity.class).create().get();
SaveKeyringParcel parcel = new SaveKeyringParcel();
parcel.addSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
Constants.choice.algorithm.rsa, 1024, KeyFlags.CERTIFY_OTHER, null));
// parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null));
parcel.addUserIds.add("swagerinho");
parcel.newPassphrase = "swag";
PgpKeyOperation op = new PgpKeyOperation(null);
OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog();
UncachedKeyRing ring = op.createSecretKeyRing(parcel, log, 0);
if (ring == null) {
log.print(activity);
throw new AssertionError("oh no");
}
if (!"swagerinho".equals(ring.getMasterKeyId())) {
log.print(activity);
throw new AssertionError("oh noo");
}
}
@Test
public void testVerifySuccess() throws Exception {
UncachedKeyRing expectedKeyRing = KeyringBuilder.ring2();
UncachedKeyRing inputKeyRing = KeyringBuilder.ring1();
// new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing);
OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog();
UncachedKeyRing canonicalizedRing = inputKeyRing.canonicalize(log, 0);
if (canonicalizedRing == null) {
throw new AssertionError("Canonicalization failed; messages: [" + log.toString() + "]");
}
HashSet onlyA = new HashSet<KeyringTestingHelper.Packet>();
HashSet onlyB = new HashSet<KeyringTestingHelper.Packet>();
Assert.assertTrue(KeyringTestingHelper.diffKeyrings(
expectedKeyRing.getEncoded(), expectedKeyRing.getEncoded(), onlyA, onlyB));
}
/**
* Just testing my own test code. Should really be using a library for this.
*/
@Test
public void testConcat() throws Exception {
byte[] actual = TestDataUtil.concatAll(new byte[]{1}, new byte[]{2,-2}, new byte[]{5},new byte[]{3});
byte[] expected = new byte[]{1,2,-2,5,3};
Assert.assertEquals(java.util.Arrays.toString(expected), java.util.Arrays.toString(actual));
}
}