mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-09 02:09:42 +02:00
Make UTCUtil.parseUTCDate() throw instead of returning null for malformed inputs
This commit is contained in:
parent
8b8863c6df
commit
0308732328
11 changed files with 85 additions and 37 deletions
|
@ -10,6 +10,7 @@ import sop.util.UTCUtil;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
|
@ -89,7 +90,7 @@ public class Verification {
|
|||
|
||||
if (split.length == 3) {
|
||||
return new Verification(
|
||||
UTCUtil.parseUTCDate(split[0]), // timestamp
|
||||
parseUTCDate(split[0]), // timestamp
|
||||
split[1], // key FP
|
||||
split[2] // cert FP
|
||||
);
|
||||
|
@ -111,7 +112,7 @@ public class Verification {
|
|||
}
|
||||
|
||||
return new Verification(
|
||||
UTCUtil.parseUTCDate(split[0]), // timestamp
|
||||
parseUTCDate(split[0]), // timestamp
|
||||
split[1], // key FP
|
||||
split[2], // cert FP
|
||||
mode, // signature mode
|
||||
|
@ -119,6 +120,14 @@ public class Verification {
|
|||
);
|
||||
}
|
||||
|
||||
private static Date parseUTCDate(String utcFormatted) {
|
||||
try {
|
||||
return UTCUtil.parseUTCDate(utcFormatted);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Malformed UTC timestamp.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the signatures' creation time.
|
||||
*
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
@ -33,15 +34,22 @@ public class UTCUtil {
|
|||
* @param dateString string
|
||||
* @return date
|
||||
*/
|
||||
public static Date parseUTCDate(String dateString) {
|
||||
@Nonnull
|
||||
public static Date parseUTCDate(String dateString) throws ParseException {
|
||||
ParseException exception = null;
|
||||
for (SimpleDateFormat parser : UTC_PARSERS) {
|
||||
try {
|
||||
return parser.parse(dateString);
|
||||
} catch (ParseException e) {
|
||||
// Store first exception (that of UTC_FORMATTER) to throw if we fail to parse the date
|
||||
if (exception == null) {
|
||||
exception = e;
|
||||
}
|
||||
// Try next parser
|
||||
}
|
||||
}
|
||||
return null;
|
||||
// No parser worked, so we throw the store exception
|
||||
throw exception;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.ParseException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -18,7 +19,7 @@ import sop.Verification;
|
|||
public class ByteArrayAndResultTest {
|
||||
|
||||
@Test
|
||||
public void testCreationAndGetters() {
|
||||
public void testCreationAndGetters() throws ParseException {
|
||||
byte[] bytes = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
||||
List<Verification> result = Collections.singletonList(
|
||||
new Verification(UTCUtil.parseUTCDate("2019-10-24T23:48:29Z"),
|
||||
|
|
|
@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.ParseException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -22,7 +23,7 @@ import sop.exception.SOPGPException;
|
|||
public class ReadyWithResultTest {
|
||||
|
||||
@Test
|
||||
public void testReadyWithResult() throws SOPGPException.NoSignature, IOException {
|
||||
public void testReadyWithResult() throws SOPGPException.NoSignature, IOException, ParseException {
|
||||
byte[] data = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
||||
List<Verification> result = Collections.singletonList(
|
||||
new Verification(UTCUtil.parseUTCDate("2019-10-24T23:48:29Z"),
|
||||
|
|
|
@ -4,12 +4,13 @@
|
|||
|
||||
package sop.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* Test parsing some date examples from the stateless OpenPGP CLI spec.
|
||||
|
@ -19,30 +20,29 @@ import org.junit.jupiter.api.Test;
|
|||
public class UTCUtilTest {
|
||||
|
||||
@Test
|
||||
public void parseExample1() {
|
||||
public void parseExample1() throws ParseException {
|
||||
String timestamp = "2019-10-29T12:11:04+00:00";
|
||||
Date date = UTCUtil.parseUTCDate(timestamp);
|
||||
assertEquals("2019-10-29T12:11:04Z", UTCUtil.formatUTCDate(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseExample2() {
|
||||
public void parseExample2() throws ParseException {
|
||||
String timestamp = "2019-10-24T23:48:29Z";
|
||||
Date date = UTCUtil.parseUTCDate(timestamp);
|
||||
assertEquals("2019-10-24T23:48:29Z", UTCUtil.formatUTCDate(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseExample3() {
|
||||
public void parseExample3() throws ParseException {
|
||||
String timestamp = "20191029T121104Z";
|
||||
Date date = UTCUtil.parseUTCDate(timestamp);
|
||||
assertEquals("2019-10-29T12:11:04Z", UTCUtil.formatUTCDate(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidDateReturnsNull() {
|
||||
public void invalidDateThrows() {
|
||||
String invalidTimestamp = "foobar";
|
||||
Date expectNull = UTCUtil.parseUTCDate(invalidTimestamp);
|
||||
assertNull(expectNull);
|
||||
assertThrows(ParseException.class, () -> UTCUtil.parseUTCDate(invalidTimestamp));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import sop.Verification;
|
|||
import sop.enums.SignatureMode;
|
||||
import sop.testsuite.assertions.VerificationAssert;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -16,7 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
public class VerificationTest {
|
||||
|
||||
@Test
|
||||
public void limitedConstructorTest() {
|
||||
public void limitedConstructorTest() throws ParseException {
|
||||
Date signDate = UTCUtil.parseUTCDate("2022-11-07T15:01:24Z");
|
||||
String keyFP = "F9E6F53F7201C60A87064EAB0B27F2B0760A1209";
|
||||
String certFP = "4E2C78519512C2AE9A8BFE7EB3298EB2FBE5F51B";
|
||||
|
@ -32,7 +33,7 @@ public class VerificationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void limitedParsingTest() {
|
||||
public void limitedParsingTest() throws ParseException {
|
||||
String string = "2022-11-07T15:01:24Z F9E6F53F7201C60A87064EAB0B27F2B0760A1209 4E2C78519512C2AE9A8BFE7EB3298EB2FBE5F51B";
|
||||
Verification verification = Verification.fromString(string);
|
||||
assertEquals(string, verification.toString());
|
||||
|
@ -44,7 +45,7 @@ public class VerificationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void parsingWithModeTest() {
|
||||
public void parsingWithModeTest() throws ParseException {
|
||||
String string = "2022-11-07T15:01:24Z F9E6F53F7201C60A87064EAB0B27F2B0760A1209 4E2C78519512C2AE9A8BFE7EB3298EB2FBE5F51B mode:text";
|
||||
Verification verification = Verification.fromString(string);
|
||||
assertEquals(string, verification.toString());
|
||||
|
@ -56,7 +57,7 @@ public class VerificationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void extendedConstructorTest() {
|
||||
public void extendedConstructorTest() throws ParseException {
|
||||
Date signDate = UTCUtil.parseUTCDate("2022-11-07T15:01:24Z");
|
||||
String keyFP = "F9E6F53F7201C60A87064EAB0B27F2B0760A1209";
|
||||
String certFP = "4E2C78519512C2AE9A8BFE7EB3298EB2FBE5F51B";
|
||||
|
@ -73,7 +74,7 @@ public class VerificationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void extendedParsingTest() {
|
||||
public void extendedParsingTest() throws ParseException {
|
||||
String string = "2022-11-07T15:01:24Z F9E6F53F7201C60A87064EAB0B27F2B0760A1209 4E2C78519512C2AE9A8BFE7EB3298EB2FBE5F51B mode:binary certificate from dkg.asc";
|
||||
Verification verification = Verification.fromString(string);
|
||||
assertEquals(string, verification.toString());
|
||||
|
|
|
@ -7,6 +7,7 @@ package sop.testsuite;
|
|||
import sop.util.UTCUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class TestData {
|
||||
|
@ -59,7 +60,7 @@ public class TestData {
|
|||
"sfcfswMA\n" +
|
||||
"=RDAo\n" +
|
||||
"-----END PGP MESSAGE-----";
|
||||
public static final Date ALICE_INLINE_SIGNED_MESSAGE_DATE = UTCUtil.parseUTCDate("2023-01-13T17:20:47Z");
|
||||
public static final Date ALICE_INLINE_SIGNED_MESSAGE_DATE = parseUTCDate("2023-01-13T17:20:47Z");
|
||||
// signature over PLAINTEXT
|
||||
public static final String ALICE_DETACHED_SIGNED_MESSAGE = "-----BEGIN PGP SIGNATURE-----\n" +
|
||||
"\n" +
|
||||
|
@ -68,7 +69,7 @@ public class TestData {
|
|||
"0K1UgT5roym9Fln8U5W8R03TSbfNiwE=\n" +
|
||||
"=bxPN\n" +
|
||||
"-----END PGP SIGNATURE-----";
|
||||
public static final Date ALICE_DETACHED_SIGNED_MESSAGE_DATE = UTCUtil.parseUTCDate("2023-01-13T16:57:57Z");
|
||||
public static final Date ALICE_DETACHED_SIGNED_MESSAGE_DATE = parseUTCDate("2023-01-13T16:57:57Z");
|
||||
|
||||
// 'Bob' key from draft-bre-openpgp-samples-00
|
||||
public static final String BOB_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
|
||||
|
@ -421,4 +422,12 @@ public class TestData {
|
|||
public static final byte[] BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n".getBytes(StandardCharsets.UTF_8);
|
||||
public static final byte[] END_PGP_SIGNATURE = "-----END PGP SIGNATURE-----".getBytes(StandardCharsets.UTF_8);
|
||||
public static final byte[] BEGIN_PGP_SIGNED_MESSAGE = "-----BEGIN PGP SIGNED MESSAGE-----\n".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private static Date parseUTCDate(String utcFormatted) {
|
||||
try {
|
||||
return UTCUtil.parseUTCDate(utcFormatted);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Malformed UTC timestamp.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import sop.util.UTCUtil;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -231,7 +232,7 @@ public class EncryptDecryptTest extends AbstractSOPTest {
|
|||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideInstances")
|
||||
public void decryptVerifyNotAfterTest(SOP sop) {
|
||||
public void decryptVerifyNotAfterTest(SOP sop) throws ParseException {
|
||||
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
|
||||
"\n" +
|
||||
"wV4DR2b2udXyHrYSAQdAwlOwwyxFDJta5+H9abgSj8jum9v7etUc9usdrElESmow\n" +
|
||||
|
@ -265,7 +266,7 @@ public class EncryptDecryptTest extends AbstractSOPTest {
|
|||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideInstances")
|
||||
public void decryptVerifyNotBeforeTest(SOP sop) {
|
||||
public void decryptVerifyNotBeforeTest(SOP sop) throws ParseException {
|
||||
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
|
||||
"\n" +
|
||||
"wV4DR2b2udXyHrYSAQdAwlOwwyxFDJta5+H9abgSj8jum9v7etUc9usdrElESmow\n" +
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue