From 90f546a4e877972d6686745aabfac1fca1178b8c Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 10 Jul 2014 01:38:57 +0200 Subject: tests: add testSubkeyAdd --- .../keychain/support/KeyringTestingHelper.java | 23 +++- .../keychain/tests/PgpKeyOperationTest.java | 130 +++++++++++++++++++++ .../keychain/tests/UncachedKeyringTest.java | 94 --------------- 3 files changed, 147 insertions(+), 100 deletions(-) create mode 100644 OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java delete mode 100644 OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/UncachedKeyringTest.java (limited to 'OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/support/KeyringTestingHelper.java b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/support/KeyringTestingHelper.java index 4c779d2b7..ac9bed898 100644 --- a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/support/KeyringTestingHelper.java +++ b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/support/KeyringTestingHelper.java @@ -30,6 +30,7 @@ import java.io.InputStream; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.SortedSet; /** * Helper for tests of the Keyring import in ProviderHelper. @@ -66,10 +67,15 @@ public class KeyringTestingHelper { return saveSuccess; } - public static class Packet { - int tag; - int length; - byte[] buf; + public static class Packet implements Comparable { + public int position; + public int tag; + public int length; + public byte[] buf; + + public int compareTo(Packet other) { + return Integer.compare(position, other.position); + } public boolean equals(Object other) { return other instanceof Packet && Arrays.areEqual(this.buf, ((Packet) other).buf); @@ -81,7 +87,8 @@ public class KeyringTestingHelper { } } - public static boolean diffKeyrings(byte[] ringA, byte[] ringB, Set onlyA, Set onlyB) + public static boolean diffKeyrings(byte[] ringA, byte[] ringB, + SortedSet onlyA, SortedSet onlyB) throws IOException { InputStream streamA = new ByteArrayInputStream(ringA); InputStream streamB = new ByteArrayInputStream(ringB); @@ -89,18 +96,22 @@ public class KeyringTestingHelper { HashSet a = new HashSet(), b = new HashSet(); Packet p; + int pos = 0; while(true) { p = readPacket(streamA); if (p == null) { break; } + p.position = pos++; a.add(p); } + pos = 0; while(true) { p = readPacket(streamB); if (p == null) { break; } + p.position = pos++; b.add(p); } @@ -109,7 +120,7 @@ public class KeyringTestingHelper { onlyB.addAll(b); onlyB.removeAll(a); - return onlyA.isEmpty() && onlyB.isEmpty(); + return !onlyA.isEmpty() || !onlyB.isEmpty(); } private static Packet readPacket(InputStream in) throws IOException { diff --git a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java new file mode 100644 index 000000000..4d422f257 --- /dev/null +++ b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java @@ -0,0 +1,130 @@ +package org.sufficientlysecure.keychain.tests; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.robolectric.*; +import org.robolectric.shadows.ShadowLog; +import org.spongycastle.bcpg.PacketTags; +import org.spongycastle.bcpg.sig.KeyFlags; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.Constants.choice.algorithm; +import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.service.OperationResultParcel; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; +import org.sufficientlysecure.keychain.support.KeyringBuilder; +import org.sufficientlysecure.keychain.support.KeyringTestingHelper; +import org.sufficientlysecure.keychain.support.KeyringTestingHelper.Packet; +import org.sufficientlysecure.keychain.support.TestDataUtil; + +import java.util.Iterator; +import java.util.TreeSet; + +@RunWith(RobolectricTestRunner.class) +@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 +public class PgpKeyOperationTest { + + static WrappedSecretKeyRing staticRing; + WrappedSecretKeyRing ring; + PgpKeyOperation op; + + @BeforeClass public static void setUpOnce() throws Exception { + SaveKeyringParcel parcel = new SaveKeyringParcel(); + parcel.addSubKeys.add(new SaveKeyringParcel.SubkeyAdd( + Constants.choice.algorithm.rsa, 1024, KeyFlags.CERTIFY_OTHER, 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); + staticRing = new WrappedSecretKeyRing(ring.getEncoded(), false, 0); + } + + @Before public void setUp() throws Exception { + // show Log.x messages in system.out + ShadowLog.stream = System.out; + ring = staticRing; + + op = new PgpKeyOperation(null); + } + + @Test + public void testCreatedKey() throws Exception { + + // parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); + + Assert.assertNotNull("key creation failed", ring); + + Assert.assertEquals("incorrect primary user id", + "swagerinho", ring.getPrimaryUserId()); + + Assert.assertEquals("wrong number of subkeys", + 1, ring.getUncachedKeyRing().getAvailableSubkeys().size()); + + } + + @Test + public void testSubkeyAdd() throws Exception { + + SaveKeyringParcel parcel = new SaveKeyringParcel(); + parcel.mMasterKeyId = ring.getMasterKeyId(); + parcel.mFingerprint = ring.getUncached().getFingerprint(); + parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); + + OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog(); + UncachedKeyRing modified = op.modifySecretKeyRing(ring, parcel, "swag", log, 0); + + Assert.assertNotNull("key modification failed", modified); + + TreeSet onlyA = new TreeSet(); + TreeSet onlyB = new TreeSet(); + Assert.assertTrue("keyrings do not differ", KeyringTestingHelper.diffKeyrings( + ring.getUncached().getEncoded(), modified.getEncoded(), onlyA, onlyB)); + + Assert.assertEquals("no extra packets in original", onlyA.size(), 0); + Assert.assertEquals("two extra packets in modified", onlyB.size(), 2); + Iterator it = onlyB.iterator(); + Assert.assertEquals("first new packet must be secret subkey", it.next().tag, PacketTags.SECRET_SUBKEY); + Assert.assertEquals("second new packet must be signature", it.next().tag, PacketTags.SIGNATURE); + + } + + @Test + public void testVerifySuccess() throws Exception { + + UncachedKeyRing expectedKeyRing = KeyringBuilder.correctRing(); + UncachedKeyRing inputKeyRing = KeyringBuilder.ringWithExtraIncorrectSignature(); + + OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog(); + UncachedKeyRing canonicalizedRing = inputKeyRing.canonicalize(log, 0); + + if (canonicalizedRing == null) { + throw new AssertionError("Canonicalization failed; messages: [" + log + "]"); + } + + TreeSet onlyA = new TreeSet(); + TreeSet onlyB = new TreeSet(); + Assert.assertTrue("keyrings differ", !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)); + } + + +} diff --git a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/UncachedKeyringTest.java b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/UncachedKeyringTest.java deleted file mode 100644 index 1c8e6daf7..000000000 --- a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/UncachedKeyringTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package org.sufficientlysecure.keychain.tests; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.BeforeClass; -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.support.KeyringBuilder; -import org.sufficientlysecure.keychain.support.KeyringTestingHelper; -import org.sufficientlysecure.keychain.support.TestDataUtil; - -import java.util.HashSet; - -@RunWith(RobolectricTestRunner.class) -@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 -public class UncachedKeyringTest { - - static UncachedKeyRing staticRing; - UncachedKeyRing ring; - - @BeforeClass public static void setUpOnce() throws Exception { - SaveKeyringParcel parcel = new SaveKeyringParcel(); - parcel.addSubKeys.add(new SaveKeyringParcel.SubkeyAdd( - Constants.choice.algorithm.rsa, 1024, KeyFlags.CERTIFY_OTHER, null)); - - parcel.addUserIds.add("swagerinho"); - parcel.newPassphrase = "swag"; - PgpKeyOperation op = new PgpKeyOperation(null); - - OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog(); - staticRing = op.createSecretKeyRing(parcel, log, 0); - } - - @Before public void setUp() throws Exception { - // show Log.x messages in system.out - ShadowLog.stream = System.out; - ring = staticRing; - } - - @Test - public void testCreateKey() throws Exception { - - // parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); - - Assert.assertNotNull("key creation failed", ring); - - Assert.assertEquals("incorrect primary user id", - "swagerinho", ring.getPublicKey().getPrimaryUserId()); - - Assert.assertEquals("wrong number of subkeys", - 1, ring.getAvailableSubkeys().size()); - - } - - @Test - public void testVerifySuccess() throws Exception { - - UncachedKeyRing expectedKeyRing = KeyringBuilder.correctRing(); - UncachedKeyRing inputKeyRing = KeyringBuilder.ringWithExtraIncorrectSignature(); - - OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog(); - UncachedKeyRing canonicalizedRing = inputKeyRing.canonicalize(log, 0); - - if (canonicalizedRing == null) { - throw new AssertionError("Canonicalization failed; messages: [" + log + "]"); - } - - HashSet onlyA = new HashSet(); - HashSet onlyB = new HashSet(); - 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)); - } - - -} -- cgit v1.2.3