1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-17 18:02:05 +01:00

Increase timeframe for some tests which check expiration dates

This commit is contained in:
Paul Schaub 2022-10-18 15:13:49 +02:00
parent 00eafc3957
commit 8834d8ad10
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 60 additions and 10 deletions

View file

@ -25,7 +25,7 @@ import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.key.util.UserId;
import org.pgpainless.util.DateUtil;
import org.pgpainless.timeframe.TestTimeFrameProvider;
import org.pgpainless.util.TestAllImplementations;
public class GenerateKeyWithAdditionalUserIdTest {
@ -33,11 +33,13 @@ public class GenerateKeyWithAdditionalUserIdTest {
@TestTemplate
@ExtendWith(TestAllImplementations.class)
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
Date expiration = new Date(DateUtil.now().getTime() + 60 * 1000);
Date now = new Date();
Date expiration = TestTimeFrameProvider.defaultExpirationForCreationDate(now);
PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(
KeyType.RSA(RsaLength._3072),
KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS))
KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS)
.setKeyCreationDate(now))
.addUserId(UserId.onlyEmail("primary@user.id"))
.addUserId(UserId.onlyEmail("additional@user.id"))
.addUserId(UserId.onlyEmail("additional2@user.id"))
@ -46,7 +48,7 @@ public class GenerateKeyWithAdditionalUserIdTest {
.build();
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
JUtils.assertEquals(expiration.getTime(), PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate().getTime(), 2000);
JUtils.assertDateEquals(expiration, PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate());
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
assertEquals("primary@user.id", userIds.next());

View file

@ -15,6 +15,7 @@ import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.decryption_verification.SignatureVerification;
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionResult;
import org.pgpainless.encryption_signing.EncryptionStream;
@ -25,7 +26,7 @@ import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
import org.pgpainless.key.generation.type.xdh.XDHSpec;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.DateUtil;
import org.pgpainless.timeframe.TestTimeFrameProvider;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -34,6 +35,7 @@ import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -42,11 +44,12 @@ public class GenerateKeyWithoutUserIdTest {
@Test
public void generateKeyWithoutUserId() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException {
Date expirationDate = DateUtil.toSecondsPrecision(new Date(DateUtil.now().getTime() + 1000 * 6000));
Date now = new Date();
Date expirationDate = TestTimeFrameProvider.defaultExpirationForCreationDate(now);
PGPSecretKeyRing secretKey = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.CERTIFY_OTHER))
.addSubkey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.SIGN_DATA))
.addSubkey(KeySpec.getBuilder(KeyType.XDH(XDHSpec._X25519), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE))
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.CERTIFY_OTHER).setKeyCreationDate(now))
.addSubkey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.SIGN_DATA).setKeyCreationDate(now))
.addSubkey(KeySpec.getBuilder(KeyType.XDH(XDHSpec._X25519), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE).setKeyCreationDate(now))
.setExpirationDate(expirationDate)
.build();
@ -87,7 +90,16 @@ public class GenerateKeyWithoutUserIdTest {
OpenPgpMetadata metadata = decryptionStream.getResult();
assertTrue(metadata.containsVerifiedSignatureFrom(certificate));
assertTrue(metadata.containsVerifiedSignatureFrom(certificate),
failuresToString(metadata.getInvalidInbandSignatures()));
assertTrue(metadata.isEncrypted());
}
private static String failuresToString(List<SignatureVerification.Failure> failureList) {
StringBuilder sb = new StringBuilder();
for (SignatureVerification.Failure failure : failureList) {
sb.append(failure.toString()).append('\n');
}
return sb.toString();
}
}

View file

@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.timeframe;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class TestTimeFrameProvider {
/**
* Return an expiration date which is 7h 13m and 31s from the given date.
*
* @param now t0
* @return t1 which is t0 +7h13m31s
*/
public static Date defaultExpirationForCreationDate(Date now) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(now);
calendar.add(Calendar.HOUR, 7);
calendar.add(Calendar.MINUTE, 13);
calendar.add(Calendar.SECOND, 31);
return calendar.getTime();
}
}

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Test classes for timeframes.
*/
package org.pgpainless.timeframe;