mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 12:52:07 +01:00
Add tests for SubkeyIdentifier
This commit is contained in:
parent
99ff6d537b
commit
1327e08ac3
2 changed files with 127 additions and 5 deletions
|
@ -18,7 +18,6 @@ package org.pgpainless.key;
|
|||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
|
||||
/**
|
||||
* Tuple class used to identify a subkey by fingerprints of the primary key of the subkeys key ring,
|
||||
|
@ -29,6 +28,16 @@ public class SubkeyIdentifier {
|
|||
private final OpenPgpV4Fingerprint primaryKeyFingerprint;
|
||||
private final OpenPgpV4Fingerprint subkeyFingerprint;
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} from a {@link PGPKeyRing}.
|
||||
* The identifier will point to the primary key of the provided ring.
|
||||
*
|
||||
* @param keyRing key ring
|
||||
*/
|
||||
public SubkeyIdentifier(PGPKeyRing keyRing) {
|
||||
this(keyRing, keyRing.getPublicKey().getKeyID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} from a {@link PGPKeyRing} and the subkeys key id.
|
||||
* {@link #getPrimaryKeyFingerprint()} will return the {@link OpenPgpV4Fingerprint} of the keyrings primary key,
|
||||
|
@ -41,6 +50,10 @@ public class SubkeyIdentifier {
|
|||
this(new OpenPgpV4Fingerprint(keyRing.getPublicKey()), new OpenPgpV4Fingerprint(keyRing.getPublicKey(keyId)));
|
||||
}
|
||||
|
||||
public SubkeyIdentifier(@Nonnull PGPKeyRing keyRing, @Nonnull OpenPgpV4Fingerprint subkeyFingerprint) {
|
||||
this(new OpenPgpV4Fingerprint(keyRing), subkeyFingerprint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} that identifies the primary key with the given fingerprint.
|
||||
* This means, both {@link #getPrimaryKeyFingerprint()} and {@link #getSubkeyFingerprint()} return the same.
|
||||
|
@ -63,10 +76,6 @@ public class SubkeyIdentifier {
|
|||
this.subkeyFingerprint = subkeyFingerprint;
|
||||
}
|
||||
|
||||
public SubkeyIdentifier(PGPSecretKeyRing secretKeys) {
|
||||
this(secretKeys, secretKeys.getPublicKey().getKeyID());
|
||||
}
|
||||
|
||||
public @Nonnull OpenPgpV4Fingerprint getFingerprint() {
|
||||
return getSubkeyFingerprint();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright 2021 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.key;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SubkeyIdentifierTest {
|
||||
|
||||
private static PGPPublicKeyRing CERT;
|
||||
private static final OpenPgpV4Fingerprint PRIMARY_FP = new OpenPgpV4Fingerprint("4F665C4DC2C4660BC6425E415736E6931ACF370C");
|
||||
private static final OpenPgpV4Fingerprint SUBKEY_FP = new OpenPgpV4Fingerprint("F73FDE6439ABE210B1AF4EDD273EF7A0C749807B");
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
CERT = TestKeys.getEmilPublicKeyRing();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromKeyRing() {
|
||||
SubkeyIdentifier identifier = new SubkeyIdentifier(CERT);
|
||||
|
||||
assertEquals(PRIMARY_FP, identifier.getPrimaryKeyFingerprint());
|
||||
assertEquals(PRIMARY_FP, identifier.getSubkeyFingerprint());
|
||||
assertEquals(PRIMARY_FP, identifier.getFingerprint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromKeyRingAndSubkeyId() {
|
||||
SubkeyIdentifier identifier = new SubkeyIdentifier(CERT, SUBKEY_FP);
|
||||
|
||||
assertEquals(PRIMARY_FP, identifier.getPrimaryKeyFingerprint());
|
||||
assertEquals(SUBKEY_FP, identifier.getSubkeyFingerprint());
|
||||
assertEquals(SUBKEY_FP, identifier.getFingerprint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromFingerprints() {
|
||||
SubkeyIdentifier identifier = new SubkeyIdentifier(PRIMARY_FP, SUBKEY_FP);
|
||||
|
||||
assertEquals(PRIMARY_FP, identifier.getPrimaryKeyFingerprint());
|
||||
assertEquals(SUBKEY_FP, identifier.getSubkeyFingerprint());
|
||||
assertEquals(SUBKEY_FP, identifier.getFingerprint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromFingerprint() {
|
||||
SubkeyIdentifier identifier = new SubkeyIdentifier(PRIMARY_FP);
|
||||
|
||||
assertEquals(PRIMARY_FP, identifier.getFingerprint());
|
||||
assertEquals(PRIMARY_FP, identifier.getSubkeyFingerprint());
|
||||
assertEquals(PRIMARY_FP, identifier.getPrimaryKeyFingerprint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetKeyIds() {
|
||||
SubkeyIdentifier identifier = new SubkeyIdentifier(CERT, SUBKEY_FP);
|
||||
assertEquals(PRIMARY_FP.getKeyId(), identifier.getPrimaryKeyId());
|
||||
assertEquals(SUBKEY_FP.getKeyId(), identifier.getKeyId());
|
||||
assertEquals(SUBKEY_FP.getKeyId(), identifier.getSubkeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
SubkeyIdentifier identifier = new SubkeyIdentifier(CERT, SUBKEY_FP);
|
||||
|
||||
assertEquals("F73FDE6439ABE210B1AF4EDD273EF7A0C749807B 4F665C4DC2C4660BC6425E415736E6931ACF370C", identifier.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
SubkeyIdentifier id1 = new SubkeyIdentifier(CERT, SUBKEY_FP);
|
||||
SubkeyIdentifier id2 = new SubkeyIdentifier(PRIMARY_FP, SUBKEY_FP);
|
||||
|
||||
assertEquals(id1, id1);
|
||||
assertEquals(id1, id2);
|
||||
|
||||
assertEquals(id1.hashCode(), id2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals() {
|
||||
SubkeyIdentifier id1 = new SubkeyIdentifier(CERT, SUBKEY_FP);
|
||||
SubkeyIdentifier id2 = new SubkeyIdentifier(PRIMARY_FP);
|
||||
SubkeyIdentifier id3 = new SubkeyIdentifier(SUBKEY_FP);
|
||||
|
||||
assertNotEquals(id1, id2);
|
||||
assertNotEquals(id2, id3);
|
||||
assertNotEquals(id1, id3);
|
||||
|
||||
assertNotEquals(id1, PRIMARY_FP);
|
||||
assertNotEquals(id1, null);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue