test: put more stuff in helper method for neater tests

This commit is contained in:
Vincent Breitmoser 2014-07-11 13:28:28 +02:00
parent d6f3b4b879
commit 9bae53f101
2 changed files with 36 additions and 36 deletions

View File

@ -118,6 +118,9 @@ public class KeyringTestingHelper {
b.add(p); b.add(p);
} }
onlyA.clear();
onlyB.clear();
onlyA.addAll(a); onlyA.addAll(a);
onlyA.removeAll(b); onlyA.removeAll(b);
onlyB.addAll(b); onlyB.addAll(b);

View File

@ -44,6 +44,8 @@ public class PgpKeyOperationTest {
static UncachedKeyRing staticRing; static UncachedKeyRing staticRing;
UncachedKeyRing ring; UncachedKeyRing ring;
PgpKeyOperation op; PgpKeyOperation op;
ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>();
ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>();
@BeforeClass public static void setUpOnce() throws Exception { @BeforeClass public static void setUpOnce() throws Exception {
SaveKeyringParcel parcel = new SaveKeyringParcel(); SaveKeyringParcel parcel = new SaveKeyringParcel();
@ -100,7 +102,7 @@ public class PgpKeyOperationTest {
// an empty modification should change nothing. this also ensures the keyring // an empty modification should change nothing. this also ensures the keyring
// is constant through canonicalization. // is constant through canonicalization.
applyModificationWithChecks(parcel, ring); // applyModificationWithChecks(parcel, ring, onlyA, onlyB);
Assert.assertNotNull("key creation failed", ring); Assert.assertNotNull("key creation failed", ring);
@ -145,13 +147,7 @@ public class PgpKeyOperationTest {
parcel.mFingerprint = ring.getFingerprint(); parcel.mFingerprint = ring.getFingerprint();
parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null));
UncachedKeyRing modified = applyModificationWithChecks(parcel, ring); UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);
ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>();
ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>();
Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
ring.getEncoded(), modified.getEncoded(), onlyA, onlyB));
Assert.assertEquals("no extra packets in original", 0, onlyA.size()); Assert.assertEquals("no extra packets in original", 0, onlyA.size());
Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size()); Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size());
@ -183,13 +179,7 @@ public class PgpKeyOperationTest {
parcel.revokeSubKeys.add(it.next().getKeyId()); parcel.revokeSubKeys.add(it.next().getKeyId());
} }
UncachedKeyRing modified = applyModificationWithChecks(parcel, ring); UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);
ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>();
ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>();
Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
ring.getEncoded(), modified.getEncoded(), onlyA, onlyB));
Assert.assertEquals("no extra packets in original", 0, onlyA.size()); Assert.assertEquals("no extra packets in original", 0, onlyA.size());
Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size()); Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size());
@ -214,17 +204,11 @@ public class PgpKeyOperationTest {
parcel.mFingerprint = ring.getFingerprint(); parcel.mFingerprint = ring.getFingerprint();
parcel.addUserIds.add("rainbow"); parcel.addUserIds.add("rainbow");
UncachedKeyRing modified = applyModificationWithChecks(parcel, ring); UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);
Assert.assertTrue("keyring must contain added user id", Assert.assertTrue("keyring must contain added user id",
modified.getPublicKey().getUnorderedUserIds().contains("rainbow")); modified.getPublicKey().getUnorderedUserIds().contains("rainbow"));
ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>();
ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>();
Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
ring.getEncoded(), modified.getEncoded(), onlyA, onlyB));
Assert.assertEquals("no extra packets in original", 0, onlyA.size()); Assert.assertEquals("no extra packets in original", 0, onlyA.size());
Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size()); Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size());
@ -271,16 +255,10 @@ public class PgpKeyOperationTest {
parcel.mFingerprint = ring.getFingerprint(); parcel.mFingerprint = ring.getFingerprint();
parcel.changePrimaryUserId = "pink"; parcel.changePrimaryUserId = "pink";
modified = applyModificationWithChecks(parcel, modified); modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);
ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>(); Assert.assertEquals("old keyring must have two outdated certificates", 2, onlyA.size());
ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>(); Assert.assertEquals("new keyring must have two new packets", 2, onlyB.size());
Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
ring.getEncoded(), modified.getEncoded(), onlyA, onlyB));
Assert.assertEquals("old keyring must have one outdated certificate", 1, onlyA.size());
Assert.assertEquals("new keyring must have three new packets", 3, onlyB.size());
Assert.assertEquals("primary user id must be the one changed to", Assert.assertEquals("primary user id must be the one changed to",
"pink", modified.getPublicKey().getPrimaryUserId()); "pink", modified.getPublicKey().getPrimaryUserId());
@ -288,9 +266,21 @@ public class PgpKeyOperationTest {
} }
private static UncachedKeyRing applyModificationWithChecks(SaveKeyringParcel parcel,
UncachedKeyRing ring,
ArrayList<RawPacket> onlyA,
ArrayList<RawPacket> onlyB) {
return applyModificationWithChecks(parcel, ring, onlyA, onlyB, true, true);
}
// applies a parcel modification while running some integrity checks // applies a parcel modification while running some integrity checks
private static UncachedKeyRing applyModificationWithChecks(SaveKeyringParcel parcel, private static UncachedKeyRing applyModificationWithChecks(SaveKeyringParcel parcel,
UncachedKeyRing ring) { UncachedKeyRing ring,
ArrayList<RawPacket> onlyA,
ArrayList<RawPacket> onlyB,
boolean canonicalize,
boolean constantCanonicalize) {
try { try {
Assert.assertTrue("modified keyring must be secret", ring.isSecret()); Assert.assertTrue("modified keyring must be secret", ring.isSecret());
@ -300,15 +290,22 @@ public class PgpKeyOperationTest {
OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog(); OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog();
UncachedKeyRing rawModified = op.modifySecretKeyRing(secretRing, parcel, "swag", log, 0); UncachedKeyRing rawModified = op.modifySecretKeyRing(secretRing, parcel, "swag", log, 0);
Assert.assertNotNull("key modification failed", rawModified); Assert.assertNotNull("key modification failed", rawModified);
UncachedKeyRing modified = rawModified.canonicalize(log, 0);
ArrayList<RawPacket> onlyA = new ArrayList<RawPacket>(); if (!canonicalize) {
ArrayList<RawPacket> onlyB = new ArrayList<RawPacket>(); Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
ring.getEncoded(), rawModified.getEncoded(), onlyA, onlyB));
return rawModified;
}
UncachedKeyRing modified = rawModified.canonicalize(log, 0);
if (constantCanonicalize) {
Assert.assertTrue("key must be constant through canonicalization", Assert.assertTrue("key must be constant through canonicalization",
!KeyringTestingHelper.diffKeyrings( !KeyringTestingHelper.diffKeyrings(
modified.getEncoded(), rawModified.getEncoded(), onlyA, onlyB) modified.getEncoded(), rawModified.getEncoded(), onlyA, onlyB)
); );
}
Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
ring.getEncoded(), modified.getEncoded(), onlyA, onlyB));
return modified; return modified;
} catch (IOException e) { } catch (IOException e) {