aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain-Test/src/test
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2014-07-10 01:38:57 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2014-07-10 01:38:57 +0200
commit90f546a4e877972d6686745aabfac1fca1178b8c (patch)
tree67ea73808bc4577009ff8d2f9bd9a8e3e7e46ae5 /OpenKeychain-Test/src/test
parent0afd979665ca17ece970df5a5f0b466346be72f1 (diff)
downloadopen-keychain-90f546a4e877972d6686745aabfac1fca1178b8c.tar.gz
open-keychain-90f546a4e877972d6686745aabfac1fca1178b8c.tar.bz2
open-keychain-90f546a4e877972d6686745aabfac1fca1178b8c.zip
tests: add testSubkeyAdd
Diffstat (limited to 'OpenKeychain-Test/src/test')
-rw-r--r--OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/support/KeyringTestingHelper.java23
-rw-r--r--OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java (renamed from OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/UncachedKeyringTest.java)58
2 files changed, 64 insertions, 17 deletions
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<Packet> {
+ 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<Packet> onlyA, Set<Packet> onlyB)
+ public static boolean diffKeyrings(byte[] ringA, byte[] ringB,
+ SortedSet<Packet> onlyA, SortedSet<Packet> onlyB)
throws IOException {
InputStream streamA = new ByteArrayInputStream(ringA);
InputStream streamB = new ByteArrayInputStream(ringB);
@@ -89,18 +96,22 @@ public class KeyringTestingHelper {
HashSet<Packet> a = new HashSet<Packet>(), b = new HashSet<Packet>();
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/UncachedKeyringTest.java b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java
index 1c8e6daf7..4d422f257 100644
--- a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/UncachedKeyringTest.java
+++ b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/tests/PgpKeyOperationTest.java
@@ -7,24 +7,31 @@ 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.HashSet;
+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 UncachedKeyringTest {
+public class PgpKeyOperationTest {
- static UncachedKeyRing staticRing;
- UncachedKeyRing ring;
+ static WrappedSecretKeyRing staticRing;
+ WrappedSecretKeyRing ring;
+ PgpKeyOperation op;
@BeforeClass public static void setUpOnce() throws Exception {
SaveKeyringParcel parcel = new SaveKeyringParcel();
@@ -36,27 +43,56 @@ public class UncachedKeyringTest {
PgpKeyOperation op = new PgpKeyOperation(null);
OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog();
- staticRing = op.createSecretKeyRing(parcel, log, 0);
+ 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 testCreateKey() throws Exception {
+ 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.getPublicKey().getPrimaryUserId());
+ "swagerinho", ring.getPrimaryUserId());
Assert.assertEquals("wrong number of subkeys",
- 1, ring.getAvailableSubkeys().size());
+ 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<Packet> onlyA = new TreeSet<Packet>();
+ TreeSet<Packet> onlyB = new TreeSet<Packet>();
+ 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<Packet> 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);
}
@@ -73,9 +109,9 @@ public class UncachedKeyringTest {
throw new AssertionError("Canonicalization failed; messages: [" + log + "]");
}
- HashSet onlyA = new HashSet<KeyringTestingHelper.Packet>();
- HashSet onlyB = new HashSet<KeyringTestingHelper.Packet>();
- Assert.assertTrue(KeyringTestingHelper.diffKeyrings(
+ TreeSet onlyA = new TreeSet<KeyringTestingHelper.Packet>();
+ TreeSet onlyB = new TreeSet<KeyringTestingHelper.Packet>();
+ Assert.assertTrue("keyrings differ", !KeyringTestingHelper.diffKeyrings(
expectedKeyRing.getEncoded(), expectedKeyRing.getEncoded(), onlyA, onlyB));
}
ref='#n549'>549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782