diff options
Diffstat (limited to 'OpenKeychain/src/test')
10 files changed, 464 insertions, 103 deletions
diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/CertifyOperationTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/CertifyOperationTest.java index c1aa5a76f..1475deca9 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/CertifyOperationTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/CertifyOperationTest.java @@ -78,11 +78,11 @@ public class CertifyOperationTest { { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.DSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.DSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.ELGAMAL, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); + Algorithm.ELGAMAL, 2048, null, KeyFlags.ENCRYPT_COMMS, 0L)); parcel.mAddUserIds.add("derp"); parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhrase1); @@ -96,11 +96,11 @@ public class CertifyOperationTest { { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.DSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.DSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.ELGAMAL, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); + Algorithm.ELGAMAL, 2048, null, KeyFlags.ENCRYPT_COMMS, 0L)); parcel.mAddUserIds.add("ditz"); byte[] uatdata = new byte[random.nextInt(150)+10]; diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/ExportTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/ExportTest.java index a659dc7da..29ba2bd19 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/ExportTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/ExportTest.java @@ -17,40 +17,65 @@ package org.sufficientlysecure.keychain.operations; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.io.PrintStream; +import java.security.Security; +import java.util.Iterator; + +import android.app.Application; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.net.Uri; + import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.Robolectric; import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLog; import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.jce.provider.BouncyCastleProvider; import org.sufficientlysecure.keychain.WorkaroundBuildConfig; -import org.sufficientlysecure.keychain.operations.results.PgpEditKeyResult; +import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.operations.results.ExportResult; +import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog; +import org.sufficientlysecure.keychain.operations.results.PgpEditKeyResult; +import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel; +import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation; import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow; import org.sufficientlysecure.keychain.pgp.WrappedSignature; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.provider.TemporaryFileProvider; +import org.sufficientlysecure.keychain.service.BackupKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.Algorithm; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.ChangeUnlockParcel; +import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; +import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Passphrase; import org.sufficientlysecure.keychain.util.ProgressScaler; import org.sufficientlysecure.keychain.util.TestingUtils; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.security.Security; -import java.util.Iterator; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + @RunWith(RobolectricGradleTestRunner.class) @Config(constants = WorkaroundBuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml") @@ -75,16 +100,16 @@ public class ExportTest { { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.DSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.DSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.ELGAMAL, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); + Algorithm.ELGAMAL, 2048, null, KeyFlags.ENCRYPT_COMMS, 0L)); parcel.mAddUserIds.add("snips"); parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhrase1); PgpEditKeyResult result = op.createSecretKeyRing(parcel); - Assert.assertTrue("initial test key creation must succeed", result.success()); + assertTrue("initial test key creation must succeed", result.success()); Assert.assertNotNull("initial test key creation must succeed", result.getRing()); mStaticRing1 = result.getRing(); @@ -93,16 +118,16 @@ public class ExportTest { { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.DSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.DSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.ELGAMAL, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); + Algorithm.ELGAMAL, 2048, null, KeyFlags.ENCRYPT_COMMS, 0L)); parcel.mAddUserIds.add("snails"); parcel.mNewUnlock = new ChangeUnlockParcel(null, new Passphrase("1234")); PgpEditKeyResult result = op.createSecretKeyRing(parcel); - Assert.assertTrue("initial test key creation must succeed", result.success()); + assertTrue("initial test key creation must succeed", result.success()); Assert.assertNotNull("initial test key creation must succeed", result.getRing()); mStaticRing2 = result.getRing(); @@ -125,17 +150,17 @@ public class ExportTest { } @Test - public void testExportAll() throws Exception { - ExportOperation op = new ExportOperation(RuntimeEnvironment.application, + public void testExportAllLocalStripped() throws Exception { + BackupOperation op = new BackupOperation(RuntimeEnvironment.application, new ProviderHelper(RuntimeEnvironment.application), null); // make sure there is a local cert (so the later checks that there are none are meaningful) - Assert.assertTrue("second keyring has local certification", checkForLocal(mStaticRing2)); + assertTrue("second keyring has local certification", checkForLocal(mStaticRing2)); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ExportResult result = op.exportKeyRings(new OperationLog(), null, false, out); + boolean result = op.exportKeysToStream(new OperationLog(), null, false, out); - Assert.assertTrue("export must be a success", result.success()); + assertTrue("export must be a success", result); long masterKeyId1, masterKeyId2; if (mStaticRing1.getMasterKeyId() < mStaticRing2.getMasterKeyId()) { @@ -150,70 +175,209 @@ public class ExportTest { UncachedKeyRing.fromStream(new ByteArrayInputStream(out.toByteArray())); { - Assert.assertTrue("export must have two keys (1/2)", unc.hasNext()); + assertTrue("export must have two keys (1/2)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("first exported key has correct masterkeyid", masterKeyId1, ring.getMasterKeyId()); - Assert.assertFalse("first exported key must not be secret", ring.isSecret()); - Assert.assertFalse("there must be no local signatures in an exported keyring", + assertFalse("first exported key must not be secret", ring.isSecret()); + assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } { - Assert.assertTrue("export must have two keys (2/2)", unc.hasNext()); + assertTrue("export must have two keys (2/2)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("second exported key has correct masterkeyid", masterKeyId2, ring.getMasterKeyId()); - Assert.assertFalse("second exported key must not be secret", ring.isSecret()); - Assert.assertFalse("there must be no local signatures in an exported keyring", + assertFalse("second exported key must not be secret", ring.isSecret()); + assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } out = new ByteArrayOutputStream(); - result = op.exportKeyRings(new OperationLog(), null, true, out); + result = op.exportKeysToStream(new OperationLog(), null, true, out); - Assert.assertTrue("export must be a success", result.success()); + assertTrue("export must be a success", result); unc = UncachedKeyRing.fromStream(new ByteArrayInputStream(out.toByteArray())); { - Assert.assertTrue("export must have four keys (1/4)", unc.hasNext()); + assertTrue("export must have four keys (1/4)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("1/4 exported key has correct masterkeyid", masterKeyId1, ring.getMasterKeyId()); - Assert.assertFalse("1/4 exported key must not be public", ring.isSecret()); - Assert.assertFalse("there must be no local signatures in an exported keyring", + assertFalse("1/4 exported key must not be public", ring.isSecret()); + assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); - Assert.assertTrue("export must have four keys (2/4)", unc.hasNext()); + assertTrue("export must have four keys (2/4)", unc.hasNext()); ring = unc.next(); Assert.assertEquals("2/4 exported key has correct masterkeyid", masterKeyId1, ring.getMasterKeyId()); - Assert.assertTrue("2/4 exported key must be public", ring.isSecret()); - Assert.assertFalse("there must be no local signatures in an exported keyring", + assertTrue("2/4 exported key must be public", ring.isSecret()); + assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } { - Assert.assertTrue("export must have four keys (3/4)", unc.hasNext()); + assertTrue("export must have four keys (3/4)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("3/4 exported key has correct masterkeyid", masterKeyId2, ring.getMasterKeyId()); - Assert.assertFalse("3/4 exported key must not be public", ring.isSecret()); - Assert.assertFalse("there must be no local signatures in an exported keyring", + assertFalse("3/4 exported key must not be public", ring.isSecret()); + assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); - Assert.assertTrue("export must have four keys (4/4)", unc.hasNext()); + assertTrue("export must have four keys (4/4)", unc.hasNext()); ring = unc.next(); Assert.assertEquals("4/4 exported key has correct masterkeyid", masterKeyId2, ring.getMasterKeyId()); - Assert.assertTrue("4/4 exported key must be public", ring.isSecret()); - Assert.assertFalse("there must be no local signatures in an exported keyring", + assertTrue("4/4 exported key must be public", ring.isSecret()); + assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } } + @Test + public void testExportUnencrypted() throws Exception { + + ContentResolver mockResolver = mock(ContentResolver.class); + + Uri fakeOutputUri = Uri.parse("content://fake/out/1"); + ByteArrayOutputStream outStream1 = new ByteArrayOutputStream(); + when(mockResolver.openOutputStream(fakeOutputUri)).thenReturn(outStream1); + + Application spyApplication = spy(RuntimeEnvironment.application); + when(spyApplication.getContentResolver()).thenReturn(mockResolver); + + BackupOperation op = new BackupOperation(spyApplication, + new ProviderHelper(RuntimeEnvironment.application), null); + + BackupKeyringParcel parcel = new BackupKeyringParcel(null, + new long[] { mStaticRing1.getMasterKeyId() }, false, fakeOutputUri); + + ExportResult result = op.execute(parcel, null); + + verify(mockResolver).openOutputStream(fakeOutputUri); + + assertTrue("export must succeed", result.success()); + + TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header", + "-----BEGIN PGP PUBLIC KEY BLOCK-----\n".getBytes(), outStream1.toByteArray()); + TestingUtils.assertArrayEqualsSuffix("exported data must end with ascii armor header", + "-----END PGP PUBLIC KEY BLOCK-----\n".getBytes(), outStream1.toByteArray()); + + { + IteratorWithIOThrow<UncachedKeyRing> unc + = UncachedKeyRing.fromStream(new ByteArrayInputStream(outStream1.toByteArray())); + + assertTrue("export must have one key", unc.hasNext()); + UncachedKeyRing ring = unc.next(); + Assert.assertEquals("exported key has correct masterkeyid", + mStaticRing1.getMasterKeyId(), ring.getMasterKeyId()); + assertFalse("export must have exactly one key", unc.hasNext()); + } + + } + + @Test + public void testExportEncrypted() throws Exception { + + + Application spyApplication; + ContentResolver mockResolver = mock(ContentResolver.class); + + Uri fakePipedUri, fakeOutputUri; + ByteArrayOutputStream outStream; { + + fakePipedUri = Uri.parse("content://fake/pipe/1"); + PipedInputStream pipedInStream = new PipedInputStream(8192); + PipedOutputStream pipedOutStream = new PipedOutputStream(pipedInStream); + when(mockResolver.openOutputStream(fakePipedUri)).thenReturn(pipedOutStream); + when(mockResolver.openInputStream(fakePipedUri)).thenReturn(pipedInStream); + when(mockResolver.insert(eq(TemporaryFileProvider.CONTENT_URI), any(ContentValues.class))) + .thenReturn(fakePipedUri); + + fakeOutputUri = Uri.parse("content://fake/out/1"); + outStream = new ByteArrayOutputStream(); + when(mockResolver.openOutputStream(fakeOutputUri)).thenReturn(outStream); + + spyApplication = spy(RuntimeEnvironment.application); + when(spyApplication.getContentResolver()).thenReturn(mockResolver); + } + + Passphrase passphrase = new Passphrase("abcde"); + + { // export encrypted + BackupOperation op = new BackupOperation(spyApplication, + new ProviderHelper(RuntimeEnvironment.application), null); + + BackupKeyringParcel parcel = new BackupKeyringParcel(passphrase, + new long[] { mStaticRing1.getMasterKeyId() }, false, fakeOutputUri); + + ExportResult result = op.execute(parcel, null); + + verify(mockResolver).openOutputStream(fakePipedUri); + verify(mockResolver).openInputStream(fakePipedUri); + verify(mockResolver).openOutputStream(fakeOutputUri); + + assertTrue("export must succeed", result.success()); + TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header", + "-----BEGIN PGP MESSAGE-----\n".getBytes(), outStream.toByteArray()); + } + + { + PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(RuntimeEnvironment.application, + new ProviderHelper(RuntimeEnvironment.application), null); + + PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(outStream.toByteArray()); + input.setAllowSymmetricDecryption(true); + + { + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel()); + assertTrue("decryption must return pending without passphrase", result.isPending()); + Assert.assertTrue("should contain pending passphrase log entry", + result.getLog().containsType(LogType.MSG_DC_PENDING_PASSPHRASE)); + } + { + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(new Passphrase("bad"))); + assertFalse("decryption must fail with bad passphrase", result.success()); + Assert.assertTrue("should contain bad passphrase log entry", + result.getLog().containsType(LogType.MSG_DC_ERROR_SYM_PASSPHRASE)); + } + + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(passphrase)); + assertTrue("decryption must succeed with passphrase", result.success()); + + assertEquals("backup filename should be backup_keyid.pub.asc", + "backup_" + KeyFormattingUtils.convertKeyIdToHex(mStaticRing1.getMasterKeyId()) + ".pub.asc", + result.getDecryptionMetadata().getFilename()); + + assertEquals("mime type for pgp keys must be correctly detected", + "application/pgp-keys", result.getDecryptionMetadata().getMimeType()); + + TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header", + "-----BEGIN PGP PUBLIC KEY BLOCK-----\n".getBytes(), result.getOutputBytes()); + TestingUtils.assertArrayEqualsSuffix("exported data must end with ascii armor header", + "-----END PGP PUBLIC KEY BLOCK-----\n".getBytes(), result.getOutputBytes()); + + { + IteratorWithIOThrow<UncachedKeyRing> unc + = UncachedKeyRing.fromStream(new ByteArrayInputStream(result.getOutputBytes())); + + assertTrue("export must have one key", unc.hasNext()); + UncachedKeyRing ring = unc.next(); + Assert.assertEquals("exported key has correct masterkeyid", + mStaticRing1.getMasterKeyId(), ring.getMasterKeyId()); + assertFalse("export must have exactly one key", unc.hasNext()); + } + + } + + } + + /** This function checks whether or not there are any local signatures in a keyring. */ private boolean checkForLocal(UncachedKeyRing ring) { Iterator<WrappedSignature> sigs = ring.getPublicKey().getSignatures(); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/PromoteKeyOperationTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/PromoteKeyOperationTest.java index e6c8c0efe..717dfe508 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/PromoteKeyOperationTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/PromoteKeyOperationTest.java @@ -74,11 +74,11 @@ public class PromoteKeyOperationTest { { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.DSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.DSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.ELGAMAL, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); + Algorithm.ELGAMAL, 2048, null, KeyFlags.ENCRYPT_COMMS, 0L)); parcel.mAddUserIds.add("derp"); parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhrase1); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java index 38af88a18..84d396af5 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.WorkaroundBuildConfig; import org.sufficientlysecure.keychain.operations.InputDataOperation; import org.sufficientlysecure.keychain.operations.results.InputDataResult; import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider; +import org.sufficientlysecure.keychain.provider.TemporaryFileProvider; import org.sufficientlysecure.keychain.service.InputDataParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; @@ -117,7 +117,7 @@ public class InputDataOperationTest { Uri fakeOutputUri1 = Uri.parse("content://fake/out/1"); Uri fakeOutputUri2 = Uri.parse("content://fake/out/2"); - when(mockResolver.insert(eq(TemporaryStorageProvider.CONTENT_URI), any(ContentValues.class))) + when(mockResolver.insert(eq(TemporaryFileProvider.CONTENT_URI), any(ContentValues.class))) .thenReturn(fakeOutputUri1, fakeOutputUri2); // application which returns mockresolver @@ -145,10 +145,10 @@ public class InputDataOperationTest { ContentValues contentValues = new ContentValues(); contentValues.put("name", "data.txt"); contentValues.put("mimetype", "text/plain"); - verify(mockResolver).insert(TemporaryStorageProvider.CONTENT_URI, contentValues); + verify(mockResolver).insert(TemporaryFileProvider.CONTENT_URI, contentValues); contentValues.put("name", (String) null); contentValues.put("mimetype", "text/testvalue"); - verify(mockResolver).insert(TemporaryStorageProvider.CONTENT_URI, contentValues); + verify(mockResolver).insert(TemporaryFileProvider.CONTENT_URI, contentValues); // quoted-printable returns windows style line endings for some reason? Assert.assertEquals("first part must have expected content", diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java index cc5ef8038..47c7a20ea 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java @@ -27,6 +27,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Iterator; +import org.apache.tools.ant.util.StringUtils; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; @@ -45,6 +46,7 @@ import org.spongycastle.bcpg.PacketTags; import org.spongycastle.bcpg.PublicKeyEncSessionPacket; import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.jce.provider.BouncyCastleProvider; +import org.spongycastle.openpgp.PGPKeyFlags; import org.sufficientlysecure.keychain.WorkaroundBuildConfig; import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; @@ -55,6 +57,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.Algorithm; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.ChangeUnlockParcel; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyChange; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.RequiredInputType; import org.sufficientlysecure.keychain.support.KeyringTestingHelper; @@ -77,7 +80,7 @@ public class PgpEncryptDecryptTest { static UncachedKeyRing mStaticRing1, mStaticRing2, mStaticRingInsecure; static Passphrase mKeyPhrase1 = TestingUtils.genPassphrase(true); static Passphrase mKeyPhrase2 = TestingUtils.genPassphrase(true); - static Passphrase mKeyPhraseInsecure = TestingUtils.genPassphrase(true); +// static Passphrase mKeyPhraseInsecure = TestingUtils.genPassphrase(true); static PrintStream oldShadowStream; @@ -127,24 +130,24 @@ public class PgpEncryptDecryptTest { mStaticRing2 = result.getRing(); } - { - // insecure (1024 bit) RSA key - SaveKeyringParcel parcel = new SaveKeyringParcel(); - parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); - parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); - parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); - parcel.mAddUserIds.add("eve"); - parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhraseInsecure); - - PgpEditKeyResult result = op.createSecretKeyRing(parcel); - Assert.assertTrue("initial test key creation must succeed", result.success()); - Assert.assertNotNull("initial test key creation must succeed", result.getRing()); - - mStaticRingInsecure = result.getRing(); - } +// { +// // insecure (1024 bit) RSA key +// SaveKeyringParcel parcel = new SaveKeyringParcel(); +// parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( +// Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); +// parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( +// Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); +// parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( +// Algorithm.RSA, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); +// parcel.mAddUserIds.add("eve"); +// parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhraseInsecure); +// +// PgpEditKeyResult result = op.createSecretKeyRing(parcel); +// Assert.assertTrue("initial test key creation must succeed", result.success()); +// Assert.assertNotNull("initial test key creation must succeed", result.getRing()); +// +// mStaticRingInsecure = result.getRing(); +// } } @@ -287,7 +290,7 @@ public class PgpEncryptDecryptTest { } @Test - public void testAsymmetricSign() { + public void testAsymmetricSignLiteral() { String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true); byte[] ciphertext; @@ -341,6 +344,121 @@ public class PgpEncryptDecryptTest { } @Test + public void testAsymmetricSignCleartext() { + + String plaintext = "dies ist ein\r\nplaintext\n ☭" + TestingUtils.genPassphrase(true); + byte[] ciphertext; + + { // encrypt data with key + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes()); + + PgpSignEncryptOperation op = new PgpSignEncryptOperation(RuntimeEnvironment.application, + new ProviderHelper(RuntimeEnvironment.application), null); + + InputData data = new InputData(in, in.available()); + PgpSignEncryptInputParcel input = new PgpSignEncryptInputParcel(); + + // only sign, as cleartext + input.setSignatureMasterKeyId(mStaticRing1.getMasterKeyId()); + input.setSignatureSubKeyId(KeyringTestingHelper.getSubkeyId(mStaticRing1, 1)); + input.setCleartextSignature(true); + input.setEnableAsciiArmorOutput(true); + input.setDetachedSignature(false); + + PgpSignEncryptResult result = op.execute(input, new CryptoInputParcel(mKeyPhrase1), data, out); + Assert.assertTrue("signing must succeed", result.success()); + + ciphertext = out.toByteArray(); + } + + Assert.assertTrue("clearsigned text must contain plaintext (ignoring newlines)", + new String(ciphertext).replace("\r\n", "").contains(plaintext.replace("\r", "").replace("\n", ""))); + + { // verification should succeed + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(ciphertext); + InputData data = new InputData(in, in.available()); + + PgpDecryptVerifyOperation op = operationWithFakePassphraseCache(null, null, null); + PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(); + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(), data, out); + + Assert.assertTrue("verification must succeed", result.success()); + + Assert.assertTrue("verification text should equal plaintext (ignoring newlines)", + new String(out.toByteArray()).replace(StringUtils.LINE_SEP, "") + .equals(plaintext.replace("\r", "").replace("\n", ""))); + Assert.assertEquals("decryptionResult should be RESULT_NOT_ENCRYPTED", + OpenPgpDecryptionResult.RESULT_NOT_ENCRYPTED, result.getDecryptionResult().getResult()); + Assert.assertEquals("signatureResult should be RESULT_VALID_CONFIRMED", + OpenPgpSignatureResult.RESULT_VALID_CONFIRMED, result.getSignatureResult().getResult()); + + OpenPgpMetadata metadata = result.getDecryptionMetadata(); + Assert.assertEquals("filesize must be correct", + out.toByteArray().length, metadata.getOriginalSize()); + + } + + } + + @Test + public void testAsymmetricSignDetached() { + + String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true); + byte[] detachedSignature; + + { // encrypt data with key + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes()); + + PgpSignEncryptOperation op = new PgpSignEncryptOperation(RuntimeEnvironment.application, + new ProviderHelper(RuntimeEnvironment.application), null); + + InputData data = new InputData(in, in.available()); + PgpSignEncryptInputParcel input = new PgpSignEncryptInputParcel(); + + // only sign, as cleartext + input.setSignatureMasterKeyId(mStaticRing1.getMasterKeyId()); + input.setSignatureSubKeyId(KeyringTestingHelper.getSubkeyId(mStaticRing1, 1)); + input.setDetachedSignature(true); + + PgpSignEncryptResult result = op.execute(input, new CryptoInputParcel(mKeyPhrase1), data, out); + Assert.assertTrue("signing must succeed", result.success()); + + detachedSignature = result.getDetachedSignature(); + } + + { // verification should succeed + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes()); + InputData data = new InputData(in, in.available()); + + PgpDecryptVerifyOperation op = operationWithFakePassphraseCache(null, null, null); + PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(); + input.setDetachedSignature(detachedSignature); + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(), data, out); + + Assert.assertTrue("verification must succeed", result.success()); + Assert.assertArrayEquals("verification text should equal plaintext (save for a newline)", + plaintext.getBytes(), out.toByteArray()); + Assert.assertEquals("decryptionResult should be RESULT_NOT_ENCRYPTED", + OpenPgpDecryptionResult.RESULT_NOT_ENCRYPTED, result.getDecryptionResult().getResult()); + Assert.assertEquals("signatureResult should be RESULT_VALID_CONFIRMED", + OpenPgpSignatureResult.RESULT_VALID_CONFIRMED, result.getSignatureResult().getResult()); + + // TODO should detached verify return any metadata? + // OpenPgpMetadata metadata = result.getDecryptionMetadata(); + // Assert.assertEquals("filesize must be correct", + // out.toByteArray().length, metadata.getOriginalSize()); + + } + + } + + @Test public void testAsymmetricEncryptDecrypt() { String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true); @@ -438,12 +556,14 @@ public class PgpEncryptDecryptTest { } @Test - public void testAsymmetricMultiSubkeyEncrypt() throws Exception { + public void testMultiSubkeyEncryptSkipStripOrBadFlag() throws Exception { String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true); + byte[] ciphertext; + long encKeyId1; + { // encrypt data with key - byte[] ciphertext; ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes()); @@ -473,7 +593,7 @@ public class PgpEncryptDecryptTest { Packet p; p = new BCPGInputStream(new ByteArrayInputStream(enc1.buf)).readPacket(); Assert.assertTrue("first packet must be session packet", p instanceof PublicKeyEncSessionPacket); - long encKeyId1 = ((PublicKeyEncSessionPacket) p).getKeyID(); + encKeyId1 = ((PublicKeyEncSessionPacket) p).getKeyID(); p = new BCPGInputStream(new ByteArrayInputStream(enc2.buf)).readPacket(); Assert.assertTrue("second packet must be session packet", p instanceof PublicKeyEncSessionPacket); @@ -488,6 +608,57 @@ public class PgpEncryptDecryptTest { } + { // strip first encrypted subkey, decryption should skip it + + SaveKeyringParcel parcel = + new SaveKeyringParcel(mStaticRing1.getMasterKeyId(), mStaticRing1.getFingerprint()); + parcel.mChangeSubKeys.add(new SubkeyChange(encKeyId1, true, false)); + UncachedKeyRing modified = PgpKeyOperationTest.applyModificationWithChecks(parcel, mStaticRing1, + new ArrayList<RawPacket>(), new ArrayList<RawPacket>(), + new CryptoInputParcel(new Date(), mKeyPhrase1)); + + ProviderHelper providerHelper = new ProviderHelper(RuntimeEnvironment.application); + providerHelper.saveSecretKeyRing(modified, new ProgressScaler()); + + PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(RuntimeEnvironment.application, + new ProviderHelper(RuntimeEnvironment.application), null); + PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(ciphertext); + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(mKeyPhrase1)); + + Assert.assertTrue("decryption must succeed", result.success()); + Assert.assertTrue("decryption must have skipped first key", + result.getLog().containsType(LogType.MSG_DC_ASKIP_UNAVAILABLE)); + } + + { // change flags of second encrypted subkey, decryption should skip it + + SaveKeyringParcel parcel = + new SaveKeyringParcel(mStaticRing1.getMasterKeyId(), mStaticRing1.getFingerprint()); + parcel.mChangeSubKeys.add(new SubkeyChange(encKeyId1, KeyFlags.CERTIFY_OTHER, null)); + UncachedKeyRing modified = PgpKeyOperationTest.applyModificationWithChecks(parcel, mStaticRing1, + new ArrayList<RawPacket>(), new ArrayList<RawPacket>(), + new CryptoInputParcel(new Date(), mKeyPhrase1)); + + ProviderHelper providerHelper = new ProviderHelper(RuntimeEnvironment.application); + providerHelper.saveSecretKeyRing(modified, new ProgressScaler()); + + PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(RuntimeEnvironment.application, + new ProviderHelper(RuntimeEnvironment.application), null); + PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(ciphertext); + DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(mKeyPhrase1)); + + Assert.assertTrue("decryption must succeed", result.success()); + Assert.assertTrue("decryption must have skipped first key", + result.getLog().containsType(LogType.MSG_DC_ASKIP_BAD_FLAGS)); + } + + } + + @Test + public void testMultiSubkeyEncryptSkipRevoked() throws Exception { + + String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true); + { // revoke first encryption subkey of keyring in database SaveKeyringParcel parcel = new SaveKeyringParcel(mStaticRing1.getMasterKeyId(), mStaticRing1.getFingerprint()); parcel.mRevokeSubKeys.add(KeyringTestingHelper.getSubkeyId(mStaticRing1, 2)); @@ -500,7 +671,6 @@ public class PgpEncryptDecryptTest { } { // encrypt to this keyring, make sure it's not encrypted to the revoked subkey - byte[] ciphertext; ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes()); @@ -518,7 +688,7 @@ public class PgpEncryptDecryptTest { data, out); Assert.assertTrue("encryption must succeed", result.success()); - ciphertext = out.toByteArray(); + byte[] ciphertext = out.toByteArray(); Iterator<RawPacket> packets = KeyringTestingHelper.parseKeyring(ciphertext); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperationTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperationTest.java index c0e28cd4b..a62b4dbb2 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperationTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperationTest.java @@ -82,8 +82,8 @@ public class PgpKeyOperationTest { UncachedKeyRing ring; PgpKeyOperation op; SaveKeyringParcel parcel; - ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>(); - ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>(); + ArrayList<RawPacket> onlyA = new ArrayList<>(); + ArrayList<RawPacket> onlyB = new ArrayList<>(); static CryptoInputParcel cryptoInput; @@ -94,11 +94,11 @@ public class PgpKeyOperationTest { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.DSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.DSA, 3072, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( Algorithm.RSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L)); + Algorithm.RSA, 3072, null, KeyFlags.ENCRYPT_COMMS, 0L)); parcel.mAddUserIds.add("twi"); parcel.mAddUserIds.add("pink"); @@ -153,13 +153,13 @@ public class PgpKeyOperationTest { parcel.mNewUnlock = new ChangeUnlockParcel(passphrase); assertFailure("creating ring with < 512 bytes keysize should fail", parcel, - LogType.MSG_CR_ERROR_KEYSIZE_512); + LogType.MSG_CR_ERROR_KEYSIZE_2048); } { parcel.reset(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.ELGAMAL, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.ELGAMAL, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddUserIds.add("shy"); parcel.mNewUnlock = new ChangeUnlockParcel(passphrase); @@ -170,7 +170,7 @@ public class PgpKeyOperationTest { { parcel.reset(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, null)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, null)); parcel.mAddUserIds.add("lotus"); parcel.mNewUnlock = new ChangeUnlockParcel(passphrase); @@ -181,7 +181,7 @@ public class PgpKeyOperationTest { { parcel.reset(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddUserIds.add("shy"); parcel.mNewUnlock = new ChangeUnlockParcel(passphrase); @@ -192,7 +192,7 @@ public class PgpKeyOperationTest { { parcel.reset(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mNewUnlock = new ChangeUnlockParcel(passphrase); assertFailure("creating ring without user ids should fail", parcel, @@ -216,7 +216,7 @@ public class PgpKeyOperationTest { public void testMasterFlags() throws Exception { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA, 0L)); + Algorithm.RSA, 4096, null, KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA, 0L)); parcel.mAddUserIds.add("luna"); ring = assertCreateSuccess("creating ring with master key flags must succeed", parcel); @@ -256,8 +256,8 @@ public class PgpKeyOperationTest { List<UncachedPublicKey> subkeys = KeyringTestingHelper.itToList(ring.getPublicKeys()); Assert.assertEquals("number of subkeys must be three", 3, subkeys.size()); - Assert.assertTrue("key ring should have been created in the last 120 seconds", - ring.getPublicKey().getCreationTime().after(new Date(new Date().getTime()-1000*120))); + Assert.assertTrue("key ring should have been created in the last 360 seconds", + ring.getPublicKey().getCreationTime().after(new Date(new Date().getTime()-1000*360))); Assert.assertNull("key ring should not expire", ring.getPublicKey().getUnsafeExpiryTimeForTesting()); @@ -347,7 +347,7 @@ public class PgpKeyOperationTest { long expiry = new Date().getTime() / 1000 + 159; int flags = KeyFlags.SIGN_DATA; - int bits = 1024 + new Random().nextInt(8); + int bits = 2048 + new Random().nextInt(8); parcel.mAddSubKeys.add(new SubkeyAdd(Algorithm.RSA, bits, null, flags, expiry)); UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB); @@ -391,14 +391,14 @@ public class PgpKeyOperationTest { parcel.mAddSubKeys.add(new SubkeyAdd( Algorithm.RSA, new Random().nextInt(512), null, KeyFlags.SIGN_DATA, 0L)); assertModifyFailure("creating a subkey with keysize < 512 should fail", ring, parcel, - LogType.MSG_CR_ERROR_KEYSIZE_512); + LogType.MSG_CR_ERROR_KEYSIZE_2048); } { parcel.reset(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, null)); + Algorithm.RSA, 2048, null, KeyFlags.SIGN_DATA, null)); assertModifyFailure("creating master key with null expiry should fail", ring, parcel, LogType.MSG_MF_ERROR_NULL_EXPIRY); @@ -406,7 +406,7 @@ public class PgpKeyOperationTest { { // a past expiry should fail parcel.reset(); - parcel.mAddSubKeys.add(new SubkeyAdd(Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, + parcel.mAddSubKeys.add(new SubkeyAdd(Algorithm.RSA, 2048, null, KeyFlags.SIGN_DATA, new Date().getTime()/1000-10)); assertModifyFailure("creating subkey with past expiry date should fail", ring, parcel, LogType.MSG_MF_ERROR_PAST_EXPIRY); @@ -837,7 +837,7 @@ public class PgpKeyOperationTest { UncachedKeyRing modified; - { // keytocard should fail with BAD_NFC_SIZE when presented with the RSA-1024 key + { // keytocard should fail with BAD_NFC_SIZE when presented with the RSA-3072 key long keyId = KeyringTestingHelper.getSubkeyId(ring, 2); parcel.reset(); parcel.mChangeSubKeys.add(new SubkeyChange(keyId, false, true)); @@ -846,7 +846,7 @@ public class PgpKeyOperationTest { parcel, cryptoInput, LogType.MSG_MF_ERROR_BAD_NFC_SIZE); } - { // keytocard should fail with BAD_NFC_ALGO when presented with the DSA-1024 key + { // keytocard should fail with BAD_NFC_ALGO when presented with the DSA-3072 key long keyId = KeyringTestingHelper.getSubkeyId(ring, 0); parcel.reset(); parcel.mChangeSubKeys.add(new SubkeyChange(keyId, false, true)); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringCanonicalizeTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringCanonicalizeTest.java index 5e552fecc..a077f5398 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringCanonicalizeTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringCanonicalizeTest.java @@ -89,8 +89,8 @@ public class UncachedKeyringCanonicalizeTest { static UncachedKeyRing staticRing; static int totalPackets; UncachedKeyRing ring; - ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>(); - ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>(); + ArrayList<RawPacket> onlyA = new ArrayList<>(); + ArrayList<RawPacket> onlyB = new ArrayList<>(); OperationResult.OperationLog log = new OperationResult.OperationLog(); PGPSignatureSubpacketGenerator subHashedPacketsGen; PGPSecretKey secretKey; @@ -358,7 +358,7 @@ public class UncachedKeyringCanonicalizeTest { SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddUserIds.add("trix"); PgpKeyOperation op = new PgpKeyOperation(null); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringMergeTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringMergeTest.java index 90189e377..59397e642 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringMergeTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringMergeTest.java @@ -87,8 +87,8 @@ public class UncachedKeyringMergeTest { static UncachedKeyRing staticRingA, staticRingB; UncachedKeyRing ringA, ringB; - ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>(); - ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>(); + ArrayList<RawPacket> onlyA = new ArrayList<>(); + ArrayList<RawPacket> onlyB = new ArrayList<>(); OperationResult.OperationLog log = new OperationResult.OperationLog(); PgpKeyOperation op; SaveKeyringParcel parcel; @@ -234,7 +234,7 @@ public class UncachedKeyringMergeTest { parcel.reset(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, 0L)); + Algorithm.RSA, 2048, null, KeyFlags.SIGN_DATA, 0L)); modifiedA = op.modifySecretKeyRing(secretRing, new CryptoInputParcel(new Date(), new Passphrase()), parcel).getRing(); modifiedB = op.modifySecretKeyRing(secretRing, new CryptoInputParcel(new Date(), new Passphrase()), parcel).getRing(); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringTest.java index 4cdcf0117..b870e5494 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/UncachedKeyringTest.java @@ -28,6 +28,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLog; import org.spongycastle.bcpg.sig.KeyFlags; +import org.spongycastle.jce.provider.BouncyCastleProvider; import org.sufficientlysecure.keychain.BuildConfig; import org.sufficientlysecure.keychain.WorkaroundBuildConfig; import org.sufficientlysecure.keychain.operations.results.PgpEditKeyResult; @@ -41,6 +42,7 @@ import org.sufficientlysecure.keychain.util.Passphrase; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.security.Security; import java.util.Iterator; import java.util.Random; @@ -53,6 +55,7 @@ public class UncachedKeyringTest { @BeforeClass public static void setUpOnce() throws Exception { + Security.insertProviderAt(new BouncyCastleProvider(), 1); ShadowLog.stream = System.out; SaveKeyringParcel parcel = new SaveKeyringParcel(); diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java index 1d7952464..0b35aaf22 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java @@ -19,6 +19,9 @@ package org.sufficientlysecure.keychain.util; import java.util.Random; +import junit.framework.Assert; + + public class TestingUtils { public static Passphrase genPassphrase() { return genPassphrase(false); @@ -35,4 +38,25 @@ public class TestingUtils { System.out.println("Generated passphrase: '" + passbuilder.toString() + "'"); return new Passphrase(passbuilder.toString()); } + + public static void assertArrayEqualsPrefix(String msg, byte[] expected, byte[] actual) { + + Assert.assertTrue("exepected must be shorter or equal to actual array length", + expected.length <= actual.length); + for (int i = 0; i < expected.length; i++) { + Assert.assertEquals(msg, expected[i], actual[i]); + } + + } + + public static void assertArrayEqualsSuffix(String msg, byte[] expected, byte[] actual) { + + Assert.assertTrue("exepected must be shorter or equal to actual array length", + expected.length <= actual.length); + for (int i = 0; i < expected.length; i++) { + Assert.assertEquals(msg, expected[i], actual[actual.length -expected.length +i]); + } + + } + } |