1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-22 12:22:06 +01:00

Add OpenPgpV5Fingerprint constructor tests using mocked v5 keys

This commit is contained in:
Paul Schaub 2022-06-20 18:35:48 +02:00
parent 2873de0d05
commit 37441a81e8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -4,13 +4,20 @@
package org.pgpainless.key;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
public class OpenPgpV5FingerprintTest {
@ -92,4 +99,81 @@ public class OpenPgpV5FingerprintTest {
assertEquals(parsed.hashCode(), parsed2.hashCode());
assertEquals(0, parsed.compareTo(parsed2));
}
@Test
public void constructFromMockedPublicKey() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
PGPPublicKey publicKey = getMockedPublicKey(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(publicKey);
assertTrue(fingerprint instanceof OpenPgpV5Fingerprint);
assertEquals(5, fingerprint.getVersion());
assertEquals(hex, fingerprint.toString());
}
@Test
public void constructFromMockedSecretKey() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
PGPPublicKey publicKey = getMockedPublicKey(hex);
PGPSecretKey secretKey = mock(PGPSecretKey.class);
when(secretKey.getPublicKey()).thenReturn(publicKey);
OpenPgpFingerprint fingerprint = new OpenPgpV5Fingerprint(secretKey);
assertEquals(5, fingerprint.getVersion());
assertEquals(hex, fingerprint.toString());
}
@Test
public void constructFromMockedPublicKeyRing() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
PGPPublicKey publicKey = getMockedPublicKey(hex);
PGPPublicKeyRing publicKeys = mock(PGPPublicKeyRing.class);
when(publicKeys.getPublicKey()).thenReturn(publicKey);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(publicKeys);
assertEquals(5, fingerprint.getVersion());
assertEquals(hex, fingerprint.toString());
fingerprint = new OpenPgpV5Fingerprint(publicKeys);
assertEquals(hex, fingerprint.toString());
}
@Test
public void constructFromMockedSecretKeyRing() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
PGPPublicKey publicKey = getMockedPublicKey(hex);
PGPSecretKeyRing secretKeys = mock(PGPSecretKeyRing.class);
when(secretKeys.getPublicKey()).thenReturn(publicKey);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(secretKeys);
assertEquals(5, fingerprint.getVersion());
assertEquals(hex, fingerprint.toString());
fingerprint = new OpenPgpV5Fingerprint(secretKeys);
assertEquals(hex, fingerprint.toString());
}
@Test
public void constructFromMockedKeyRing() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
PGPPublicKey publicKey = getMockedPublicKey(hex);
PGPKeyRing keys = mock(PGPKeyRing.class);
when(keys.getPublicKey()).thenReturn(publicKey);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(keys);
assertEquals(5, fingerprint.getVersion());
assertEquals(hex, fingerprint.toString());
fingerprint = new OpenPgpV5Fingerprint(keys);
assertEquals(hex, fingerprint.toString());
}
private PGPPublicKey getMockedPublicKey(String hex) {
byte[] binary = Hex.decode(hex);
PGPPublicKey mocked = mock(PGPPublicKey.class);
when(mocked.getVersion()).thenReturn(5);
when(mocked.getFingerprint()).thenReturn(binary);
return mocked;
}
}