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

Work on Tests. Disable costly freshkeys tests for now.

This commit is contained in:
Paul Schaub 2020-01-10 22:07:54 +01:00
parent 07e2488c80
commit 218c068ddb
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
12 changed files with 291 additions and 145 deletions

View file

@ -18,6 +18,7 @@ package org.pgpainless;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -25,8 +26,6 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -55,9 +54,11 @@ import org.pgpainless.util.BCUtil;
public class EncryptDecryptTest { public class EncryptDecryptTest {
private static final Logger LOGGER = Logger.getLogger(EncryptDecryptTest.class.getName()); private static final Logger LOGGER = Logger.getLogger(EncryptDecryptTest.class.getName());
// Don't use StandardCharsets.UTF_8 because of Android API level.
private static final Charset UTF8 = Charset.forName("UTF-8"); private static final Charset UTF8 = Charset.forName("UTF-8");
private static final String testMessage = "Ah, Juliet, if the measure of thy joy\n" + private static final String testMessage =
"Ah, Juliet, if the measure of thy joy\n" +
"Be heaped like mine, and that thy skill be more\n" + "Be heaped like mine, and that thy skill be more\n" +
"To blazon it, then sweeten with thy breath\n" + "To blazon it, then sweeten with thy breath\n" +
"This neighbor air, and let rich musics tongue\n" + "This neighbor air, and let rich musics tongue\n" +
@ -68,10 +69,10 @@ public class EncryptDecryptTest {
LOGGER.log(Level.INFO, "Plain Length: " + testMessage.getBytes(UTF8).length); LOGGER.log(Level.INFO, "Plain Length: " + testMessage.getBytes(UTF8).length);
} }
@Ignore
@Test @Test
public void freshKeysRsaToElGamalTest() public void freshKeysRsaToElGamalTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072); PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
PGPKeyRing recipient = PGPainless.generateKeyRing() PGPKeyRing recipient = PGPainless.generateKeyRing()
.withSubKey(KeySpec.getBuilder(ElGamal_GENERAL.withLength(ElGamalLength._3072)).withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS).withDefaultAlgorithms()) .withSubKey(KeySpec.getBuilder(ElGamal_GENERAL.withLength(ElGamalLength._3072)).withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS).withDefaultAlgorithms())
@ -81,49 +82,48 @@ public class EncryptDecryptTest {
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Ignore
@Test @Test
public void freshKeysRsaToRsaTest() public void freshKeysRsaToRsaTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._4096); PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._4096);
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._4096); PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._4096);
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Ignore
@Test @Test
public void freshKeysEcToEcTest() throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException, public void freshKeysEcToEcTest()
InvalidAlgorithmParameterException { throws IOException, PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit"); PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit"); PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Test
public void freshKeysEcToRsaTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._4096);
encryptDecryptForSecretKeyRings(sender, recipient);
}
@Test
public void freshKeysRsaToEcTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._4096);
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
encryptDecryptForSecretKeyRings(sender, recipient);
}
@Ignore @Ignore
@Test
public void freshKeysEcToRsaTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._4096);
encryptDecryptForSecretKeyRings(sender, recipient);
}
@Ignore
@Test
public void freshKeysRsaToEcTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._4096);
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
encryptDecryptForSecretKeyRings(sender, recipient);
}
private void encryptDecryptForSecretKeyRings(PGPKeyRing sender, PGPKeyRing recipient) private void encryptDecryptForSecretKeyRings(PGPKeyRing sender, PGPKeyRing recipient)
throws PGPException, throws PGPException, IOException {
IOException {
PGPSecretKeyRing recipientSec = recipient.getSecretKeys(); PGPSecretKeyRing recipientSec = recipient.getSecretKeys();
PGPSecretKeyRing senderSec = sender.getSecretKeys(); PGPSecretKeyRing senderSec = sender.getSecretKeys();
PGPPublicKeyRing recipientPub = recipient.getPublicKeys(); PGPPublicKeyRing recipientPub = recipient.getPublicKeys();
@ -179,7 +179,7 @@ public class EncryptDecryptTest {
Streams.pipeAll(decryptor, decryptedSecretMessage); Streams.pipeAll(decryptor, decryptedSecretMessage);
decryptor.close(); decryptor.close();
assertTrue(Arrays.equals(secretMessage, decryptedSecretMessage.toByteArray())); assertArrayEquals(secretMessage, decryptedSecretMessage.toByteArray());
OpenPgpMetadata result = decryptor.getResult(); OpenPgpMetadata result = decryptor.getResult();
assertTrue(result.containsVerifiedSignatureFrom(senderPub)); assertTrue(result.containsVerifiedSignatureFrom(senderPub));
assertTrue(result.isIntegrityProtected()); assertTrue(result.isIntegrityProtected());

View file

@ -1,63 +0,0 @@
/*
* Copyright 2018 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;
import static junit.framework.TestCase.assertTrue;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Arrays;
import java.util.Iterator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.junit.Test;
import org.pgpainless.key.collection.PGPKeyRing;
public class ImportExportKeyTest {
/**
* Test the export and import of a key ring with sub keys.
* @throws PGPException very
* @throws NoSuchAlgorithmException much
* @throws NoSuchProviderException some
* @throws InvalidAlgorithmParameterException annoying
* @throws IOException exceptions
*/
@Test
public void test()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException {
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("alice@bla.blub");
PGPSecretKeyRing secretKeys = keyRing.getSecretKeys();
PGPPublicKeyRing publicKeys = keyRing.getPublicKeys();
BcKeyFingerprintCalculator calc = new BcKeyFingerprintCalculator();
byte[] bytes = publicKeys.getEncoded();
PGPPublicKeyRing parsed = new PGPPublicKeyRing(bytes, calc);
assertTrue(Arrays.equals(publicKeys.getEncoded(), parsed.getEncoded()));
Iterator<PGPPublicKey> it = secretKeys.getPublicKeys();
assertTrue(it.hasNext());
it.next();
assertTrue(it.hasNext());
}
}

View file

@ -31,6 +31,7 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.Ignore; import org.junit.Ignore;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;

View file

@ -17,7 +17,10 @@ package org.pgpainless;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import java.security.Provider;
import org.junit.Test; import org.junit.Test;
import org.pgpainless.provider.BouncyCastleProviderFactory;
import org.pgpainless.provider.ProviderFactory; import org.pgpainless.provider.ProviderFactory;
public class ProviderFactoryTest { public class ProviderFactoryTest {
@ -26,4 +29,29 @@ public class ProviderFactoryTest {
public void providerFactoryDefaultIsBouncyCastleTest() { public void providerFactoryDefaultIsBouncyCastleTest() {
assertEquals("BC", ProviderFactory.getProviderName()); assertEquals("BC", ProviderFactory.getProviderName());
} }
@Test
public void setCustomProviderTest() {
ProviderFactory.setFactory(customProviderFactory);
assertEquals("PL", ProviderFactory.getProviderName());
// Reset back to BouncyCastle
ProviderFactory.setFactory(new BouncyCastleProviderFactory());
}
private ProviderFactory customProviderFactory = new ProviderFactory() {
Provider provider = new Provider("PL", "0.1", "PGPainlessTestProvider") {
};
@Override
protected Provider _getProvider() {
return provider;
}
@Override
protected String _getProviderName() {
return provider.getName();
}
};
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright 2018 Paul Schaub. * Copyright 2020 Paul Schaub.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless; package org.pgpainless.decryption_verification;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
@ -22,7 +22,7 @@ import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.Charset;
import java.util.Collections; import java.util.Collections;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
@ -30,44 +30,30 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.key.TestKeys;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
public class TestKeysTest { public class DecryptAndVerifyMessageTest {
private final PGPSecretKeyRing juliet; // Don't use StandardCharsets.UTF8 because of Android API level.
private final PGPSecretKeyRing romeo; private static final Charset UTF8 = Charset.forName("UTF-8");
public TestKeysTest() throws IOException, PGPException { private PGPSecretKeyRing juliet;
this.juliet = TestKeys.getJulietSecretKeyRing(); private PGPSecretKeyRing romeo;
this.romeo = TestKeys.getRomeoSecretKeyRing();
@Before
public void loadKeys() throws IOException, PGPException {
juliet = TestKeys.getJulietSecretKeyRing();
romeo = TestKeys.getRomeoSecretKeyRing();
} }
@Test @Test
public void julietKeyTest() { public void decryptMessageAndVerifySignatureTest() throws Exception {
assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID());
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet));
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getPublicKey()));
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getSecretKey()));
assertEquals(TestKeys.JULIET_KEY_ID, TestKeys.JULIET_FINGERPRINT.getKeyId());
}
@Test
public void romeoKeyTest() {
assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID());
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo));
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getPublicKey()));
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getSecretKey()));
assertEquals(TestKeys.ROMEO_KEY_ID, TestKeys.ROMEO_FINGERPRINT.getKeyId());
}
@Test
public void decryptVerifyTest() throws Exception {
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET; String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
DecryptionStream decryptor = PGPainless.createDecryptor() DecryptionStream decryptor = PGPainless.createDecryptor()
@ -83,7 +69,7 @@ public class TestKeysTest {
toPlain.close(); toPlain.close();
OpenPgpMetadata metadata = decryptor.getResult(); OpenPgpMetadata metadata = decryptor.getResult();
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(StandardCharsets.UTF_8); byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(UTF8);
byte[] actual = toPlain.toByteArray(); byte[] actual = toPlain.toByteArray();
assertArrayEquals(expected, actual); assertArrayEquals(expected, actual);
@ -98,6 +84,5 @@ public class TestKeysTest {
assertEquals(1, metadata.getVerifiedSignatureKeyFingerprints().size()); assertEquals(1, metadata.getVerifiedSignatureKeyFingerprints().size());
assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT)); assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT));
assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint()); assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint());
} }
} }

View file

@ -13,27 +13,28 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless; package org.pgpainless.encryption_signing;
import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.junit.Test; import org.junit.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
public class SymmetricTest { public class SymmetricEncryptionTest {
private static final Logger LOGGER = Logger.getLogger(SymmetricTest.class.getName()); private static final Logger LOGGER = Logger.getLogger(SymmetricEncryptionTest.class.getName());
private static final String message = "I grew up with the understanding that the world " + private static final String message =
"I grew up with the understanding that the world " +
"I lived in was one where people enjoyed a sort of freedom " + "I lived in was one where people enjoyed a sort of freedom " +
"to communicate with each other in privacy, without it " + "to communicate with each other in privacy, without it " +
"being monitored, without it being measured or analyzed " + "being monitored, without it being measured or analyzed " +
@ -58,6 +59,6 @@ public class SymmetricTest {
LOGGER.log(Level.INFO, new String(out.toByteArray())); LOGGER.log(Level.INFO, new String(out.toByteArray()));
byte[] plain2 = PGPainless.decryptWithPassword(enc, passphrase); byte[] plain2 = PGPainless.decryptWithPassword(enc, passphrase);
assertTrue(Arrays.equals(plain, plain2)); assertArrayEquals(plain, plain2);
} }
} }

View file

@ -0,0 +1,53 @@
/*
* Copyright 2018 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.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.junit.Test;
public class ImportExportKeyTest {
/**
* Test the export and import of a key ring with sub keys.
* @throws IOException in case of a IO error
*/
@Test
public void testExportImportPublicKeyRing() throws IOException {
PGPPublicKeyRing publicKeys = TestKeys.getJulietPublicKeyRing();
BcKeyFingerprintCalculator calc = new BcKeyFingerprintCalculator();
byte[] bytes = publicKeys.getEncoded();
PGPPublicKeyRing parsed = new PGPPublicKeyRing(bytes, calc);
assertArrayEquals(publicKeys.getEncoded(), parsed.getEncoded());
}
@Test
public void testExportImportSecretKeyRing() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getRomeoSecretKeyRing();
byte[] bytes = secretKeys.getEncoded();
PGPSecretKeyRing parsed = new PGPSecretKeyRing(bytes, new BcKeyFingerprintCalculator());
assertArrayEquals(secretKeys.getEncoded(), parsed.getEncoded());
assertEquals(secretKeys.getPublicKey().getKeyID(), parsed.getPublicKey().getKeyID());
}
}

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless; package org.pgpainless.key;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
@ -22,7 +22,6 @@ import java.io.IOException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.junit.Test; import org.junit.Test;
import org.pgpainless.key.OpenPgpV4Fingerprint;
public class OpenPgpV4FingerprintTest { public class OpenPgpV4FingerprintTest {
@ -68,7 +67,7 @@ public class OpenPgpV4FingerprintTest {
} }
@Test @Test
public void keyIdTest() throws IOException { public void assertFingerprintGetKeyIdEqualsKeyId() throws IOException {
PGPPublicKey key = TestKeys.getJulietPublicKeyRing().getPublicKey(); PGPPublicKey key = TestKeys.getJulietPublicKeyRing().getPublicKey();
long keyId = key.getKeyID(); long keyId = key.getKeyID();

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless; package org.pgpainless.key;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@ -26,7 +26,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil; import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.pgpainless.key.OpenPgpV4Fingerprint;
public class TestKeys { public class TestKeys {

View file

@ -0,0 +1,113 @@
/*
* Copyright 2018 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 junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import java.io.IOException;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.Test;
import org.pgpainless.util.TestUtils;
public class TestKeysTest {
private final PGPSecretKeyRing julietSecRing;
private final PGPSecretKeyRing romeoSecRing;
private final PGPPublicKeyRing julietPubRing;
private final PGPPublicKeyRing romeoPubRing;
public TestKeysTest() throws IOException, PGPException {
this.julietSecRing = TestKeys.getJulietSecretKeyRing();
this.romeoSecRing = TestKeys.getRomeoSecretKeyRing();
this.julietPubRing = TestKeys.getJulietPublicKeyRing();
this.romeoPubRing = TestKeys.getRomeoPublicKeyRing();
}
@Test
public void assertJulietsPublicKeyIsSameInPubRingAndSecRing() throws IOException {
assertArrayEquals(julietSecRing.getPublicKey().getEncoded(), julietPubRing.getPublicKey().getEncoded());
}
@Test
public void assertJulietsKeysIdEquals() {
assertEquals(TestKeys.JULIET_KEY_ID, julietSecRing.getSecretKey().getKeyID());
assertEquals(TestKeys.JULIET_KEY_ID, julietSecRing.getPublicKey().getKeyID());
assertEquals(TestKeys.JULIET_KEY_ID, julietPubRing.getPublicKey().getKeyID());
}
@Test
public void assertJulietsKeyUIDEquals() {
assertEquals(TestKeys.JULIET_UID, julietSecRing.getPublicKey().getUserIDs().next());
assertEquals(1, TestUtils.getNumberOfItemsInIterator(julietSecRing.getPublicKey().getUserIDs()));
}
@Test
public void assertJulietsKeyRingFingerprintMatches() {
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(julietSecRing));
}
@Test
public void assertJulietsPublicKeyFingerprintMatchesHerSecretKeyFingerprint() {
assertEquals(new OpenPgpV4Fingerprint(julietSecRing.getPublicKey()), new OpenPgpV4Fingerprint(julietSecRing.getSecretKey()));
}
@Test
public void assertJulietsFingerprintGetKeyIdMatches() {
assertEquals("calling getKeyId() on juliet's fingerprint must return her key id.",
TestKeys.JULIET_KEY_ID, TestKeys.JULIET_FINGERPRINT.getKeyId());
}
@Test
public void assertRomeosPublicKeyIsSameInPubRingAndSecRing() throws IOException {
assertArrayEquals(romeoSecRing.getPublicKey().getEncoded(), romeoPubRing.getPublicKey().getEncoded());
}
@Test
public void assertRomeosKeyIdEquals() {
assertEquals("Key ID of Romeo's secret key must match his key id.",
TestKeys.ROMEO_KEY_ID, romeoSecRing.getSecretKey().getKeyID());
}
@Test
public void assertRomeosKeyUIDMatches() {
assertEquals(TestKeys.ROMEO_UID, romeoSecRing.getPublicKey().getUserIDs().next());
}
@Test
public void assertRomeosKeyRingFingerprintMatches() {
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeoSecRing));
}
@Test
public void assertRomeosPublicKeyFingerprintMatchesHisSecretKeyFingerprint() {
assertEquals(new OpenPgpV4Fingerprint(romeoSecRing.getPublicKey()), new OpenPgpV4Fingerprint(romeoSecRing.getSecretKey()));
}
@Test
public void assertRomesKeysFingerprintMatches() {
assertEquals(TestKeys.ROMEO_KEY_ID, TestKeys.ROMEO_FINGERPRINT.getKeyId());
}
@Test
public void assertRomeosSecretKeyRingHasSamePublicKeyId() throws IOException {
PGPPublicKeyRing julietsPublicKeys = TestKeys.getJulietPublicKeyRing();
assertEquals(julietSecRing.getPublicKey().getKeyID(), julietsPublicKeys.getPublicKey().getKeyID());
}
}

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless; package org.pgpainless.util;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertFalse;
@ -21,6 +21,7 @@ import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull; import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail; import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertNotEquals;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -30,7 +31,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import org.junit.Test; import org.junit.Test;
import org.pgpainless.util.MultiMap;
public class MultiMapTest { public class MultiMapTest {
@ -79,9 +79,9 @@ public class MultiMapTest {
} }
MultiMap<String, String> empty = new MultiMap<>(); MultiMap<String, String> empty = new MultiMap<>();
assertFalse(multiMap.equals(empty)); assertNotEquals(multiMap, empty);
assertEquals(multiMap, multiMap); assertEquals(multiMap, multiMap);
assertFalse(multiMap.equals(null)); assertNotEquals(null, multiMap);
MultiMap<String, String> map2 = new MultiMap<>(); MultiMap<String, String> map2 = new MultiMap<>();
map2.put("alice", "schwarzer"); map2.put("alice", "schwarzer");
@ -127,6 +127,6 @@ public class MultiMapTest {
assertFalse(fromMap.isEmpty()); assertFalse(fromMap.isEmpty());
assertEquals(fromMap.get("key"), Collections.singleton("value")); assertEquals(fromMap.get("key"), Collections.singleton("value"));
assertFalse(fromMap.equals(map)); assertNotEquals(fromMap, map);
} }
} }

View file

@ -0,0 +1,30 @@
/*
* Copyright 2020 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.util;
import java.util.Iterator;
public class TestUtils {
public static int getNumberOfItemsInIterator(Iterator<?> iterator) {
int num = 0;
while (iterator.hasNext()) {
iterator.next();
num++;
}
return num;
}
}