Make UTCUtil.parseUTCDate() throw instead of returning null for malformed inputs

This commit is contained in:
Paul Schaub 2023-04-27 13:07:08 +02:00
parent 8b8863c6df
commit 0308732328
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
11 changed files with 85 additions and 37 deletions

View file

@ -18,6 +18,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
@ -246,21 +247,36 @@ public abstract class AbstractSopCmd implements Runnable {
}
public Date parseNotAfter(String notAfter) {
Date date = notAfter.equals("now") ? new Date() : notAfter.equals("-") ? END_OF_TIME : UTCUtil.parseUTCDate(notAfter);
if (date == null) {
if (notAfter.equals("now")) {
return new Date();
}
if (notAfter.equals("-")) {
return END_OF_TIME;
}
try {
return UTCUtil.parseUTCDate(notAfter);
} catch (ParseException e) {
String errorMsg = getMsg("sop.error.input.malformed_not_after");
throw new IllegalArgumentException(errorMsg);
}
return date;
}
public Date parseNotBefore(String notBefore) {
Date date = notBefore.equals("now") ? new Date() : notBefore.equals("-") ? BEGINNING_OF_TIME : UTCUtil.parseUTCDate(notBefore);
if (date == null) {
if (notBefore.equals("now")) {
return new Date();
}
if (notBefore.equals("-")) {
return BEGINNING_OF_TIME;
}
try {
return UTCUtil.parseUTCDate(notBefore);
} catch (ParseException e) {
String errorMsg = getMsg("sop.error.input.malformed_not_before");
throw new IllegalArgumentException(errorMsg);
}
return date;
}
}

View file

@ -31,6 +31,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
@ -187,7 +188,7 @@ public class DecryptCmdTest {
}
@Test
public void assertSessionKeyAndVerificationsIsProperlyWrittenToSessionKeyFile() throws SOPGPException.CannotDecrypt, SOPGPException.MissingArg, SOPGPException.BadData, IOException {
public void assertSessionKeyAndVerificationsIsProperlyWrittenToSessionKeyFile() throws SOPGPException.CannotDecrypt, SOPGPException.MissingArg, SOPGPException.BadData, IOException, ParseException {
Date signDate = UTCUtil.parseUTCDate("2022-11-07T15:01:24Z");
String keyFP = "F9E6F53F7201C60A87064EAB0B27F2B0760A1209";
String certFP = "4E2C78519512C2AE9A8BFE7EB3298EB2FBE5F51B";
@ -281,7 +282,7 @@ public class DecryptCmdTest {
}
@Test
public void verifyOutIsProperlyWritten() throws IOException, SOPGPException.CannotDecrypt, SOPGPException.MissingArg, SOPGPException.BadData {
public void verifyOutIsProperlyWritten() throws IOException, SOPGPException.CannotDecrypt, SOPGPException.MissingArg, SOPGPException.BadData, ParseException {
File certFile = File.createTempFile("verify-out-cert", ".asc");
File verifyOut = new File(certFile.getParent(), "verify-out.txt");
if (verifyOut.exists()) {

View file

@ -16,6 +16,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
@ -41,7 +42,7 @@ public class VerifyCmdTest {
PrintStream originalSout;
@BeforeEach
public void prepare() throws SOPGPException.UnsupportedOption, SOPGPException.BadData, SOPGPException.NoSignature, IOException {
public void prepare() throws SOPGPException.UnsupportedOption, SOPGPException.BadData, SOPGPException.NoSignature, IOException, ParseException {
originalSout = System.out;
detachedVerify = mock(DetachedVerify.class);
@ -73,7 +74,7 @@ public class VerifyCmdTest {
}
@Test
public void notAfter_passedDown() throws SOPGPException.UnsupportedOption {
public void notAfter_passedDown() throws SOPGPException.UnsupportedOption, ParseException {
Date date = UTCUtil.parseUTCDate("2019-10-29T18:36:45Z");
SopCLI.main(new String[] {"verify", "--not-after", "2019-10-29T18:36:45Z", signature.getAbsolutePath(), cert.getAbsolutePath()});
verify(detachedVerify, times(1)).notAfter(date);
@ -100,7 +101,7 @@ public class VerifyCmdTest {
}
@Test
public void notBefore_passedDown() throws SOPGPException.UnsupportedOption {
public void notBefore_passedDown() throws SOPGPException.UnsupportedOption, ParseException {
Date date = UTCUtil.parseUTCDate("2019-10-29T18:36:45Z");
SopCLI.main(new String[] {"verify", "--not-before", "2019-10-29T18:36:45Z", signature.getAbsolutePath(), cert.getAbsolutePath()});
verify(detachedVerify, times(1)).notBefore(date);
@ -178,7 +179,7 @@ public class VerifyCmdTest {
}
@Test
public void resultIsPrintedProperly() throws SOPGPException.NoSignature, IOException, SOPGPException.BadData {
public void resultIsPrintedProperly() throws SOPGPException.NoSignature, IOException, SOPGPException.BadData, ParseException {
when(detachedVerify.data((InputStream) any())).thenReturn(Arrays.asList(
new Verification(UTCUtil.parseUTCDate("2019-10-29T18:36:45Z"),
"EB85BB5FA33A75E15E944E63F231550C4F47E38E",