mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-22 19:08:00 +01:00
OpenPgpMessageInputStream: Return -1 instead of throwing MalformedOpenPgpMessageException when calling read() on drained stream
This commit is contained in:
parent
558036c485
commit
52fa7e4d46
3 changed files with 23 additions and 0 deletions
|
@ -744,6 +744,7 @@ public class OpenPgpMessageInputStream extends DecryptionStream {
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (nestedInputStream == null) {
|
if (nestedInputStream == null) {
|
||||||
if (packetInputStream != null) {
|
if (packetInputStream != null) {
|
||||||
|
syntaxVerifier.next(InputSymbol.EndOfSequence);
|
||||||
syntaxVerifier.assertValid();
|
syntaxVerifier.assertValid();
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -774,6 +775,7 @@ public class OpenPgpMessageInputStream extends DecryptionStream {
|
||||||
super.close();
|
super.close();
|
||||||
if (closed) {
|
if (closed) {
|
||||||
if (packetInputStream != null) {
|
if (packetInputStream != null) {
|
||||||
|
syntaxVerifier.next(InputSymbol.EndOfSequence);
|
||||||
syntaxVerifier.assertValid();
|
syntaxVerifier.assertValid();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -132,6 +132,10 @@ public class OpenPgpMessageSyntax implements Syntax {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
Transition fromValid(@Nonnull InputSymbol input, @Nullable StackSymbol stackItem)
|
Transition fromValid(@Nonnull InputSymbol input, @Nullable StackSymbol stackItem)
|
||||||
throws MalformedOpenPgpMessageException {
|
throws MalformedOpenPgpMessageException {
|
||||||
|
if (input == InputSymbol.EndOfSequence) {
|
||||||
|
// allow subsequent read() calls.
|
||||||
|
return new Transition(State.Valid);
|
||||||
|
}
|
||||||
// There is no applicable transition rule out of Valid
|
// There is no applicable transition rule out of Valid
|
||||||
throw new MalformedOpenPgpMessageException(State.Valid, input, stackItem);
|
throw new MalformedOpenPgpMessageException(State.Valid, input, stackItem);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ import org.bouncycastle.openpgp.PGPSignature;
|
||||||
import org.bouncycastle.util.io.Streams;
|
import org.bouncycastle.util.io.Streams;
|
||||||
import org.junit.JUtils;
|
import org.junit.JUtils;
|
||||||
import org.junit.jupiter.api.Named;
|
import org.junit.jupiter.api.Named;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.Arguments;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
@ -652,6 +653,22 @@ public class OpenPgpMessageInputStreamTest {
|
||||||
assertTrue(metadata.getRejectedInlineSignatures().isEmpty());
|
assertTrue(metadata.getRejectedInlineSignatures().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readAfterCloseTest() throws PGPException, IOException {
|
||||||
|
OpenPgpMessageInputStream pgpIn = get(SENC_LIT, ConsumerOptions.get()
|
||||||
|
.addDecryptionPassphrase(Passphrase.fromPassword(PASSPHRASE)));
|
||||||
|
Streams.drain(pgpIn); // read all
|
||||||
|
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
assertEquals(-1, pgpIn.read(buf));
|
||||||
|
assertEquals(-1, pgpIn.read());
|
||||||
|
assertEquals(-1, pgpIn.read(buf));
|
||||||
|
assertEquals(-1, pgpIn.read());
|
||||||
|
|
||||||
|
pgpIn.close();
|
||||||
|
pgpIn.getMetadata();
|
||||||
|
}
|
||||||
|
|
||||||
private static Tuple<String, MessageMetadata> processReadBuffered(String armoredMessage, ConsumerOptions options)
|
private static Tuple<String, MessageMetadata> processReadBuffered(String armoredMessage, ConsumerOptions options)
|
||||||
throws PGPException, IOException {
|
throws PGPException, IOException {
|
||||||
OpenPgpMessageInputStream in = get(armoredMessage, options);
|
OpenPgpMessageInputStream in = get(armoredMessage, options);
|
||||||
|
|
Loading…
Reference in a new issue