1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-27 13:28:00 +01:00

Move UTC timestamp methods to DateUtil class

This commit is contained in:
Paul Schaub 2021-07-03 12:23:24 +02:00
parent 9b046a0cf1
commit c8a281d6a4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 81 additions and 48 deletions

View file

@ -0,0 +1,62 @@
/*
* 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.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateUtil {
public static SimpleDateFormat UTC_PARSER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
static {
UTC_PARSER.setTimeZone(TimeZone.getTimeZone("UTC"));
}
/**
* Parse a UTC timestamp into a date.
*
* @param dateString timestamp
* @return date
*/
public static Date parseUTCDate(String dateString) {
try {
return UTC_PARSER.parse(dateString);
} catch (ParseException e) {
return null;
}
}
/**
* Format a date as UTC timestamp.
*
* @param date date
* @return timestamp
*/
public static String formatUTCDate(Date date) {
return UTC_PARSER.format(date);
}
/**
* Return the current date "rounded" to UTC precision.
*
* @return now
*/
public static Date now() {
return parseUTCDate(formatUTCDate(new Date()));
}
}

View file

@ -44,8 +44,8 @@ import org.pgpainless.key.generation.type.ecc.EllipticCurve;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnlockSecretKey;
import org.pgpainless.util.DateUtil;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.TestUtils;
/**
* PGPainless offers a simple API to modify keys by adding and replacing signatures and/or subkeys.
@ -211,7 +211,7 @@ public class ModifyKeys {
*/
@Test
public void setKeyExpirationDate() throws PGPException {
Date expirationDate = TestUtils.getUTCDate("2030-06-24 12:44:56 UTC");
Date expirationDate = DateUtil.parseUTCDate("2030-06-24 12:44:56 UTC");
SecretKeyRingProtector protector = SecretKeyRingProtector
.unlockAllKeysWith(Passphrase.fromPassword(originalPassphrase), secretKey);
@ -221,9 +221,9 @@ public class ModifyKeys {
KeyRingInfo info = PGPainless.inspectKeyRing(secretKey);
assertEquals(TestUtils.formatUTCDate(expirationDate), TestUtils.formatUTCDate(info.getPrimaryKeyExpirationDate()));
assertEquals(TestUtils.formatUTCDate(expirationDate), TestUtils.formatUTCDate(info.getExpirationDateForUse(KeyFlag.ENCRYPT_COMMS)));
assertEquals(TestUtils.formatUTCDate(expirationDate), TestUtils.formatUTCDate(info.getExpirationDateForUse(KeyFlag.SIGN_DATA)));
assertEquals(DateUtil.formatUTCDate(expirationDate), DateUtil.formatUTCDate(info.getPrimaryKeyExpirationDate()));
assertEquals(DateUtil.formatUTCDate(expirationDate), DateUtil.formatUTCDate(info.getExpirationDateForUse(KeyFlag.ENCRYPT_COMMS)));
assertEquals(DateUtil.formatUTCDate(expirationDate), DateUtil.formatUTCDate(info.getExpirationDateForUse(KeyFlag.SIGN_DATA)));
}
/**
@ -233,7 +233,7 @@ public class ModifyKeys {
*/
@Test
public void setSubkeyExpirationDate() throws PGPException {
Date expirationDate = TestUtils.getUTCDate("2032-01-13 22:30:01 UTC");
Date expirationDate = DateUtil.parseUTCDate("2032-01-13 22:30:01 UTC");
SecretKeyRingProtector protector = SecretKeyRingProtector
.unlockAllKeysWith(Passphrase.fromPassword(originalPassphrase), secretKey);
@ -249,7 +249,7 @@ public class ModifyKeys {
KeyRingInfo info = PGPainless.inspectKeyRing(secretKey);
assertNull(info.getPrimaryKeyExpirationDate());
assertNull(info.getExpirationDateForUse(KeyFlag.SIGN_DATA));
assertEquals(TestUtils.formatUTCDate(expirationDate), TestUtils.formatUTCDate(info.getExpirationDateForUse(KeyFlag.ENCRYPT_COMMS)));
assertEquals(DateUtil.formatUTCDate(expirationDate), DateUtil.formatUTCDate(info.getExpirationDateForUse(KeyFlag.ENCRYPT_COMMS)));
}
/**

View file

@ -30,7 +30,7 @@ import org.pgpainless.PGPainless;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.util.ArmorUtils;
import org.pgpainless.util.CollectionUtils;
import org.pgpainless.util.TestUtils;
import org.pgpainless.util.DateUtil;
public class KeyRingValidatorTest {
@ -227,7 +227,7 @@ public class KeyRingValidatorTest {
PGPPublicKey unbound = CollectionUtils.iteratorToList(publicKeys.getPublicKeys()).get(2);
assertNotNull(unbound);
Date validationDate = TestUtils.getUTCDate("2019-10-15 10:18:26 UTC");
Date validationDate = DateUtil.parseUTCDate("2019-10-15 10:18:26 UTC");
KeyRingInfo info = new KeyRingInfo(publicKeys, validationDate);
for (PGPPublicKey publicKey : publicKeys) {
if (publicKey != unbound) {

View file

@ -52,8 +52,8 @@ import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.key.util.UserId;
import org.pgpainless.util.DateUtil;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.TestUtils;
public class KeyRingInfoTest {
@ -98,7 +98,7 @@ public class KeyRingInfoTest {
assertNull(sInfo.getRevocationDate());
assertNull(pInfo.getRevocationDate());
Date revocationDate = TestUtils.now();
Date revocationDate = DateUtil.now();
PGPSecretKeyRing revoked = PGPainless.modifyKeyRing(secretKeys).revoke(new UnprotectedKeysProtector()).done();
KeyRingInfo rInfo = PGPainless.inspectKeyRing(revoked);
assertNotNull(rInfo.getRevocationDate());
@ -217,7 +217,7 @@ public class KeyRingInfoTest {
.build();
Iterator<PGPSecretKey> keys = secretKeys.iterator();
Date now = TestUtils.now();
Date now = DateUtil.now();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);

View file

@ -32,7 +32,7 @@ import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.util.TestUtils;
import org.pgpainless.util.DateUtil;
public class ChangeExpirationTest {
@ -49,7 +49,7 @@ public class ChangeExpirationTest {
assertNull(sInfo.getPrimaryKeyExpirationDate());
assertNull(sInfo.getSubkeyExpirationDate(subKeyFingerprint));
Date date = TestUtils.getUTCDate("2020-11-27 16:10:32 UTC");
Date date = DateUtil.parseUTCDate("2020-11-27 16:10:32 UTC");
secretKeys = PGPainless.modifyKeyRing(secretKeys)
.setExpirationDate(date, new UnprotectedKeysProtector()).done();
sInfo = PGPainless.inspectKeyRing(secretKeys);
@ -82,7 +82,7 @@ public class ChangeExpirationTest {
assertNull(sInfo.getSubkeyExpirationDate(subKeyFingerprint));
assertNull(sInfo.getPrimaryKeyExpirationDate());
Date date = TestUtils.getUTCDate("2020-11-27 16:10:32 UTC");
Date date = DateUtil.parseUTCDate("2020-11-27 16:10:32 UTC");
secretKeys = PGPainless.modifyKeyRing(secretKeys)
.setExpirationDate(subKeyFingerprint, date, new UnprotectedKeysProtector()).done();
sInfo = PGPainless.inspectKeyRing(secretKeys);

View file

@ -29,7 +29,7 @@ import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.key.KeyRingValidator;
import org.pgpainless.policy.Policy;
import org.pgpainless.util.ArmorUtils;
import org.pgpainless.util.TestUtils;
import org.pgpainless.util.DateUtil;
public class KeyRingValidationTest {
@ -123,7 +123,7 @@ public class KeyRingValidationTest {
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(key);
Date validationDate = TestUtils.getUTCDate("2019-05-01 00:00:00 UTC");
Date validationDate = DateUtil.parseUTCDate("2019-05-01 00:00:00 UTC");
Policy policy = PGPainless.getPolicy();
PGPPublicKeyRing evaluated = KeyRingValidator.validate(publicKeys, policy, validationDate);

View file

@ -32,7 +32,7 @@ import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.util.KeyIdUtil;
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
import org.pgpainless.util.TestUtils;
import org.pgpainless.util.DateUtil;
public class SignatureStructureTest {
@ -80,7 +80,7 @@ public class SignatureStructureTest {
@Test
public void testGetSignatureCreationTime() {
assertEquals(TestUtils.getUTCDate("2021-06-08 14:56:55 UTC"), signature.getCreationTime());
assertEquals(DateUtil.parseUTCDate("2021-06-08 14:56:55 UTC"), signature.getCreationTime());
}
@Test

View file

@ -18,43 +18,14 @@ package org.pgpainless.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.TimeZone;
public class TestUtils {
public static SimpleDateFormat UTC_PARSER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
static {
UTC_PARSER.setTimeZone(TimeZone.getTimeZone("UTC"));
}
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final Random RANDOM = new Random();
public static Date getUTCDate(String dateString) {
try {
return UTC_PARSER.parse(dateString);
} catch (ParseException e) {
return null;
}
}
public static String formatUTCDate(Date date) {
return UTC_PARSER.format(date);
}
/**
* Return the current data "rounded" to UTC precision.
*
* @return now
*/
public static Date now() {
return getUTCDate(formatUTCDate(new Date()));
}
public static int getNumberOfItemsInIterator(Iterator<?> iterator) {
int num = 0;
while (iterator.hasNext()) {