This is an automated email from the ASF dual-hosted git repository.

chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-jdkim.git


The following commit(s) were added to refs/heads/master by this push:
     new 32c54e6  [FIX] DKIMVerifier: add a policy for the body octets past l=
32c54e6 is described below

commit 32c54e60fd1db99b9d9b3367171de45ae1be8e68
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 10 09:09:31 2026 +0100

    [FIX] DKIMVerifier: add a policy for the body octets past l=
    
    A signature with l= covers only the first l= octets of the canonical body.
    LimitedOutputStream dropped the rest without counting it, and BodyHasherImpl
    kept no reference to it, so the verifier returned PASS for a replayed 
message
    with content appended after the signed length, and gave callers no way to
    notice (RFC 6376 section 8.2).
    
    LimitedOutputStream now counts the dropped octets, exposed by
    BodyHasherImpl.getUnsignedBodyOctets(). VerifierOptions gets a
    BodyLengthPolicy (james.dkim.body-length.policy): REJECT fails the 
signature,
    FLAG, the default for compatibility, keeps PASS with a warning, ACCEPT keeps
    the old behaviour. Trailing empty lines removed by canonicalization are not
    counted.
---
 .../java/org/apache/james/jdkim/DKIMVerifier.java  | 13 +++-
 .../apache/james/jdkim/api/BodyLengthPolicy.java   | 42 +++++++++++++
 .../java/org/apache/james/jdkim/api/Result.java    |  3 +-
 .../apache/james/jdkim/api/VerifierOptions.java    | 47 ++++++++++++++
 .../james/jdkim/canon/LimitedOutputStream.java     | 21 +++++--
 .../apache/james/jdkim/impl/BodyHasherImpl.java    | 17 ++++-
 .../test/java/org/apache/james/jdkim/DKIMTest.java | 73 ++++++++++++++++++++++
 .../james/jdkim/DKIMVerifierOptionsTest.java       | 21 +++++++
 .../james/jdkim/canon/LimitedOutputStreamTest.java |  9 +++
 9 files changed, 236 insertions(+), 10 deletions(-)

diff --git a/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java 
b/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java
index b83ff5e..1cef2da 100644
--- a/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java
+++ b/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java
@@ -28,6 +28,7 @@ import org.apache.james.jdkim.api.PublicKeyRecord;
 import org.apache.james.jdkim.api.PublicKeyRecordRetriever;
 import org.apache.james.jdkim.api.Result;
 import org.apache.james.jdkim.api.SignatureRecord;
+import org.apache.james.jdkim.api.BodyLengthPolicy;
 import org.apache.james.jdkim.api.UnsignedDuplicateFieldPolicy;
 import org.apache.james.jdkim.api.VerifierOptions;
 import org.apache.james.jdkim.exceptions.CompositeFailException;
@@ -511,12 +512,22 @@ public class DKIMVerifier {
                 byte[] computedHash = bhj.getDigest();
                 byte[] expectedBodyHash = 
bhj.getSignatureRecord().getBodyHash();
 
+                // RFC 6376 section 8.2: the octets past l= are not signed, 
anyone can append them.
+                long unsignedOctets = bhj.getUnsignedBodyOctets();
+                String unsignedBody = "the body has " + unsignedOctets
+                        + " octets after the l= length (RFC 6376 section 8.2)";
                 if (!Arrays.equals(expectedBodyHash, computedHash)) {
                     job.setFailure(new SignatureMismatchException(
                             "Computed bodyhash is different from the expected 
one", bhj.getSignatureRecord()));
+                } else if (unsignedOctets > 0 && options.getBodyLengthPolicy() 
== BodyLengthPolicy.REJECT) {
+                    job.setFailure(new PermFailException(unsignedBody, 
bhj.getSignatureRecord()));
                 } else {
+                    String warning = job.getWarning();
+                    if (unsignedOctets > 0 && options.getBodyLengthPolicy() == 
BodyLengthPolicy.FLAG) {
+                        warning = warning == null ? unsignedBody : warning + 
"; " + unsignedBody;
+                    }
                     verifiedSignatures.add(bhj.getSignatureRecord());
-                    result.add(new Result(bhj.getSignatureRecord(), 
job.getWarning()));
+                    result.add(new Result(bhj.getSignatureRecord(), warning));
                     continue;
                 }
             }
diff --git 
a/main/src/main/java/org/apache/james/jdkim/api/BodyLengthPolicy.java 
b/main/src/main/java/org/apache/james/jdkim/api/BodyLengthPolicy.java
new file mode 100644
index 0000000..5108d14
--- /dev/null
+++ b/main/src/main/java/org/apache/james/jdkim/api/BodyLengthPolicy.java
@@ -0,0 +1,42 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.jdkim.api;
+
+/**
+ * What the verifier does with a signature whose l= tag leaves part of the 
body unsigned: the octets past
+ * the limit can be anything, so an attacker replaying a genuine l= signature 
can append content of their
+ * choice and keep its PASS result.
+ *
+ * @see <a 
href="https://datatracker.ietf.org/doc/html/rfc6376#section-8.2";>RFC 6376 
section 8.2</a>
+ */
+public enum BodyLengthPolicy {
+    /**
+     * The signature is evaluated as usual: unsigned body octets do not change 
its result.
+     */
+    ACCEPT,
+    /**
+     * The signature keeps its PASS result, which carries a warning (see 
{@link Result#getWarning()}).
+     */
+    FLAG,
+    /**
+     * The signature fails, as RFC 6376 section 8.2 lets verifiers ignore it.
+     */
+    REJECT
+}
diff --git a/main/src/main/java/org/apache/james/jdkim/api/Result.java 
b/main/src/main/java/org/apache/james/jdkim/api/Result.java
index b57465f..143f6c2 100644
--- a/main/src/main/java/org/apache/james/jdkim/api/Result.java
+++ b/main/src/main/java/org/apache/james/jdkim/api/Result.java
@@ -195,7 +195,8 @@ public class Result {
 
     /**
      * Get the concern the verifier has about a successful verification, for 
instance an unsigned
-     * extra instance of a single-instance field (see {@link 
VerifierOptions.Builder#withUnsignedDuplicateFieldPolicy}).
+     * extra instance of a single-instance field (see {@link 
VerifierOptions.Builder#withUnsignedDuplicateFieldPolicy})
+     * or body octets past the l= length (see {@link 
VerifierOptions.Builder#withBodyLengthPolicy}).
      *
      * @return the warning, or null if none
      */
diff --git a/main/src/main/java/org/apache/james/jdkim/api/VerifierOptions.java 
b/main/src/main/java/org/apache/james/jdkim/api/VerifierOptions.java
index 3c9dff9..ad879d5 100644
--- a/main/src/main/java/org/apache/james/jdkim/api/VerifierOptions.java
+++ b/main/src/main/java/org/apache/james/jdkim/api/VerifierOptions.java
@@ -52,6 +52,11 @@ public class VerifierOptions {
      */
     public static final String UNSIGNED_DUPLICATE_FIELD_POLICY_PROPERTY = 
"james.dkim.unsigned-duplicate-field.policy";
     private static final UnsignedDuplicateFieldPolicy 
DEFAULT_UNSIGNED_DUPLICATE_FIELD_POLICY = UnsignedDuplicateFieldPolicy.REJECT;
+    /**
+     * System property holding the default of {@link 
Builder#withBodyLengthPolicy(BodyLengthPolicy)}.
+     */
+    public static final String BODY_LENGTH_POLICY_PROPERTY = 
"james.dkim.body-length.policy";
+    private static final BodyLengthPolicy DEFAULT_BODY_LENGTH_POLICY = 
BodyLengthPolicy.FLAG;
     /**
      * The fields RFC 5322 section 3.6 allows once, for {@link 
Builder#withSingleInstanceFields(Collection)}.
      * Real-world mail often duplicates some of them (To, Cc, References...) 
and a signature leaving the
@@ -71,6 +76,7 @@ public class VerifierOptions {
     private final boolean rsaSha1Allowed;
     private final UnsignedDuplicateFieldPolicy unsignedDuplicateFieldPolicy;
     private final Set<String> singleInstanceFields;
+    private final BodyLengthPolicy bodyLengthPolicy;
     private final PublicKeyRecordRetriever publicKeyRecordRetriever;
     private final Resolver dnsResolver;
 
@@ -81,6 +87,7 @@ public class VerifierOptions {
         private boolean rsaSha1Allowed = 
!"false".equalsIgnoreCase(System.getProperty(RSA_SHA1_ALLOWED_PROPERTY, 
"true").trim());
         private UnsignedDuplicateFieldPolicy unsignedDuplicateFieldPolicy = 
defaultUnsignedDuplicateFieldPolicy();
         private Set<String> singleInstanceFields = 
DEFAULT_SINGLE_INSTANCE_FIELDS;
+        private BodyLengthPolicy bodyLengthPolicy = defaultBodyLengthPolicy();
         private Resolver dnsResolver = Lookup.getDefaultResolver();
         private PublicKeyRecordRetriever publicKeyRecordRetriever = new 
MultiplexingPublicKeyRecordRetriever(
                 "dns", new DNSPublicKeyRecordRetriever(this.dnsResolver));
@@ -190,6 +197,20 @@ public class VerifierOptions {
             return this;
         }
 
+        /**
+         * Sets what happens to a signature whose l= tag leaves body octets 
unsigned: anyone holding such a
+         * signature can append content of their choice after the signed 
length. See RFC 6376 section 8.2.
+         * The octets are counted after body canonicalization. Defaults to the
+         * {@value #BODY_LENGTH_POLICY_PROPERTY} system property, or {@link 
BodyLengthPolicy#FLAG}.
+         *
+         * @param bodyLengthPolicy a {@link BodyLengthPolicy}
+         * @return {@link Builder}
+         */
+        public Builder withBodyLengthPolicy(BodyLengthPolicy bodyLengthPolicy) 
{
+            this.bodyLengthPolicy = bodyLengthPolicy;
+            return this;
+        }
+
         /**
          * A custom dns resolver
          *
@@ -230,6 +251,10 @@ public class VerifierOptions {
             throw new IllegalArgumentException("singleInstanceFields can not 
be null");
         }
 
+        if (builder.bodyLengthPolicy == null) {
+            throw new IllegalArgumentException("bodyLengthPolicy can not be 
null");
+        }
+
         if (builder.publicKeyRecordRetriever == null) {
             throw new IllegalArgumentException("publicKeyRecordRetriever can 
not be null");
         }
@@ -244,6 +269,7 @@ public class VerifierOptions {
         this.rsaSha1Allowed = builder.rsaSha1Allowed;
         this.unsignedDuplicateFieldPolicy = 
builder.unsignedDuplicateFieldPolicy;
         this.singleInstanceFields = builder.singleInstanceFields;
+        this.bodyLengthPolicy = builder.bodyLengthPolicy;
         this.dnsResolver = builder.dnsResolver;
         this.publicKeyRecordRetriever = builder.publicKeyRecordRetriever;
     }
@@ -302,6 +328,27 @@ public class VerifierOptions {
         return singleInstanceFields;
     }
 
+    /**
+     * Gets what happens to a signature whose l= tag leaves body octets 
unsigned
+     *
+     * @return {@link BodyLengthPolicy}
+     */
+    public BodyLengthPolicy getBodyLengthPolicy() {
+        return bodyLengthPolicy;
+    }
+
+    private static BodyLengthPolicy defaultBodyLengthPolicy() {
+        String value = System.getProperty(BODY_LENGTH_POLICY_PROPERTY);
+        if (value == null) {
+            return DEFAULT_BODY_LENGTH_POLICY;
+        }
+        try {
+            return 
BodyLengthPolicy.valueOf(value.trim().toUpperCase(Locale.US));
+        } catch (IllegalArgumentException e) {
+            throw new IllegalArgumentException("Invalid " + 
BODY_LENGTH_POLICY_PROPERTY + " value: " + value, e);
+        }
+    }
+
     private static UnsignedDuplicateFieldPolicy 
defaultUnsignedDuplicateFieldPolicy() {
         String value = 
System.getProperty(UNSIGNED_DUPLICATE_FIELD_POLICY_PROPERTY);
         if (value == null) {
diff --git 
a/main/src/main/java/org/apache/james/jdkim/canon/LimitedOutputStream.java 
b/main/src/main/java/org/apache/james/jdkim/canon/LimitedOutputStream.java
index d6c8d44..23ac63b 100644
--- a/main/src/main/java/org/apache/james/jdkim/canon/LimitedOutputStream.java
+++ b/main/src/main/java/org/apache/james/jdkim/canon/LimitedOutputStream.java
@@ -30,6 +30,7 @@ public class LimitedOutputStream extends FilterOutputStream {
 
     private final int limit;
     private int computedBytes;
+    private long droppedBytes;
 
     /**
      * @param out
@@ -45,13 +46,12 @@ public class LimitedOutputStream extends FilterOutputStream 
{
     }
 
     public void write(byte[] b, int off, int len) throws IOException {
-        if (len > limit - computedBytes) {
-            len = limit - computedBytes;
-        }
-        if (len > 0) {
-            out.write(b, off, len);
-            computedBytes += len;
+        int kept = Math.max(0, Math.min(len, limit - computedBytes));
+        if (kept > 0) {
+            out.write(b, off, kept);
+            computedBytes += kept;
         }
+        droppedBytes += len - kept;
     }
 
     public void write(int b) throws IOException {
@@ -60,6 +60,8 @@ public class LimitedOutputStream extends FilterOutputStream {
         if (computedBytes < limit) {
             out.write(b);
             computedBytes++;
+        } else {
+            droppedBytes++;
         }
     }
 
@@ -70,6 +72,13 @@ public class LimitedOutputStream extends FilterOutputStream {
         return computedBytes;
     }
 
+    /**
+     * @return the number of bytes written past the limit, which the 
underlying stream did not get
+     */
+    public long getDroppedBytes() {
+        return droppedBytes;
+    }
+
     /**
      * @return true if the limit has been reached and no data is being passed 
to
      *         the underlying stream.
diff --git a/main/src/main/java/org/apache/james/jdkim/impl/BodyHasherImpl.java 
b/main/src/main/java/org/apache/james/jdkim/impl/BodyHasherImpl.java
index b058fad..6fd8d63 100644
--- a/main/src/main/java/org/apache/james/jdkim/impl/BodyHasherImpl.java
+++ b/main/src/main/java/org/apache/james/jdkim/impl/BodyHasherImpl.java
@@ -39,6 +39,7 @@ public class BodyHasherImpl implements BodyHasher {
     private SignatureRecord sign;
     private DigestOutputStream digesterOS;
     private OutputStream out;
+    private LimitedOutputStream limited;
 
     public BodyHasherImpl(SignatureRecord sign) throws PermFailException {
         MessageDigest md;
@@ -83,8 +84,10 @@ public class BodyHasherImpl implements BodyHasher {
     private OutputStream prepareCanonicalizerOutputStream(int limit,
             boolean relaxedBody, OutputStream dout) {
         OutputStream out = dout;
-        if (limit != -1)
-            out = new LimitedOutputStream(out, limit);
+        if (limit != -1) {
+            limited = new LimitedOutputStream(out, limit);
+            out = limited;
+        }
         if (relaxedBody)
             out = new RelaxedBodyCanonicalizer(out);
         else
@@ -103,6 +106,16 @@ public class BodyHasherImpl implements BodyHasher {
         return sign;
     }
 
+    /**
+     * The canonical body octets past the l= limit, which the body hash does 
not cover. Meaningful once the
+     * body is written. The trailing empty lines that canonicalization removes 
are not counted.
+     *
+     * @return a number of octets, 0 when the signature has no l= tag
+     */
+    public long getUnsignedBodyOctets() {
+        return limited == null ? 0 : limited.getDroppedBytes();
+    }
+
     public byte[] getDigest() {
         return digesterOS.getDigest();
     }
diff --git a/main/src/test/java/org/apache/james/jdkim/DKIMTest.java 
b/main/src/test/java/org/apache/james/jdkim/DKIMTest.java
index 12b9d36..318fd43 100644
--- a/main/src/test/java/org/apache/james/jdkim/DKIMTest.java
+++ b/main/src/test/java/org/apache/james/jdkim/DKIMTest.java
@@ -20,6 +20,7 @@
 package org.apache.james.jdkim;
 
 import org.apache.james.jdkim.api.BodyHasher;
+import org.apache.james.jdkim.api.BodyLengthPolicy;
 import java.util.stream.Collectors;
 import org.apache.james.mime4j.MimeException;
 import java.security.MessageDigest;
@@ -594,6 +595,78 @@ public class DKIMTest {
                 .satisfies(r -> assertThat(r.getWarning()).isNull());
     }
 
+    private static final String LIMITED_SIGNATURE_TEMPLATE = "v=1; 
a=rsa-sha256; c=simple; d=messiah.edu; h=from:subject; l=6; q=dns/txt; 
s=selector2;";
+
+    private String signWithBodyLength() throws Exception {
+        DKIMSigner signer = new DKIMSigner(LIMITED_SIGNATURE_TEMPLATE, 
TestKeys.privateKey);
+        String signature = signer.sign(new 
ByteArrayInputStream(SINGLE_FROM_MESSAGE.getBytes(StandardCharsets.US_ASCII)));
+        return signature + "\r\n" + SINGLE_FROM_MESSAGE;
+    }
+
+    private DKIMVerifier verifierWith(BodyLengthPolicy policy) {
+        return new DKIMVerifier(new VerifierOptions.Builder()
+                .withPublicKeyRecordRetriever(keyRecordRetriever)
+                .withBodyLengthPolicy(policy)
+                .build());
+    }
+
+    @Test
+    public void bodyAppendedAfterLengthShouldBeRejectedWhenConfigured() throws 
Exception {
+        String replayed = signWithBodyLength() + "EVIL\r\n";
+
+        DKIMVerifier strictVerifier = verifierWith(BodyLengthPolicy.REJECT);
+
+        assertThatThrownBy(() -> strictVerifier.verify(new 
ByteArrayInputStream(replayed.getBytes(StandardCharsets.US_ASCII))))
+                .isInstanceOf(PermFailException.class)
+                .hasMessageContaining("6 octets after the l= length");
+        assertThat(strictVerifier.hasAnyValidSignature()).isFalse();
+        assertThat(strictVerifier.getResults()).singleElement()
+                .satisfies(r -> 
assertThat(r.getResultType()).isEqualTo(Result.Type.PERMERROR));
+    }
+
+    @Test
+    public void bodyAppendedAfterLengthShouldBeFlaggedByDefault() throws 
Exception {
+        String replayed = signWithBodyLength() + "EVIL\r\n";
+
+        assertThat(verifier.verify(new 
ByteArrayInputStream(replayed.getBytes(StandardCharsets.US_ASCII)))).hasSize(1);
+        assertThat(verifier.getResults()).singleElement().satisfies(r -> {
+            assertThat(r.getResultType()).isEqualTo(Result.Type.PASS);
+            assertThat(r.getWarning()).contains("6 octets after the l= 
length");
+            assertThat(r.getHeaderTextWithReason()).contains("reason=\"valid 
signature, but the body has 6 octets");
+        });
+    }
+
+    @Test
+    public void bodyAppendedAfterLengthShouldPassWhenAccepted() throws 
Exception {
+        String replayed = signWithBodyLength() + "EVIL\r\n";
+
+        DKIMVerifier lenientVerifier = verifierWith(BodyLengthPolicy.ACCEPT);
+
+        assertThat(lenientVerifier.verify(new 
ByteArrayInputStream(replayed.getBytes(StandardCharsets.US_ASCII)))).hasSize(1);
+        assertThat(lenientVerifier.getResults()).singleElement()
+                .satisfies(r -> assertThat(r.getWarning()).isNull());
+    }
+
+    @Test
+    public void bodyMatchingLengthShouldPassWithoutWarning() throws Exception {
+        String signed = signWithBodyLength();
+
+        DKIMVerifier strictVerifier = verifierWith(BodyLengthPolicy.REJECT);
+
+        assertThat(strictVerifier.verify(new 
ByteArrayInputStream(signed.getBytes(StandardCharsets.US_ASCII)))).hasSize(1);
+        assertThat(strictVerifier.getResults()).singleElement()
+                .satisfies(r -> assertThat(r.getWarning()).isNull());
+    }
+
+    @Test
+    public void trailingEmptyLinesAfterLengthShouldNotCountAsUnsigned() throws 
Exception {
+        String padded = signWithBodyLength() + "\r\n\r\n";
+
+        DKIMVerifier strictVerifier = verifierWith(BodyLengthPolicy.REJECT);
+
+        assertThat(strictVerifier.verify(new 
ByteArrayInputStream(padded.getBytes(StandardCharsets.US_ASCII)))).hasSize(1);
+    }
+
     @Test
     public void unsignedExtraReplyToFieldShouldNotPassSilently() throws 
Exception {
         DKIMSigner signer = new DKIMSigner(
diff --git 
a/main/src/test/java/org/apache/james/jdkim/DKIMVerifierOptionsTest.java 
b/main/src/test/java/org/apache/james/jdkim/DKIMVerifierOptionsTest.java
index 5ef43f0..6413519 100644
--- a/main/src/test/java/org/apache/james/jdkim/DKIMVerifierOptionsTest.java
+++ b/main/src/test/java/org/apache/james/jdkim/DKIMVerifierOptionsTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.james.jdkim;
 
+import org.apache.james.jdkim.api.BodyLengthPolicy;
 import org.apache.james.jdkim.api.PublicKeyRecordRetriever;
 import org.apache.james.jdkim.api.UnsignedDuplicateFieldPolicy;
 import org.apache.james.jdkim.api.VerifierOptions;
@@ -165,6 +166,26 @@ public class DKIMVerifierOptionsTest {
         new 
VerifierOptions.Builder().withUnsignedDuplicateFieldPolicy(null).build();
     }
 
+    @Test
+    public void shouldFlagUnsignedBodyOctetsByDefault() {
+        assertEquals(BodyLengthPolicy.FLAG, new 
VerifierOptions.Builder().build().getBodyLengthPolicy());
+    }
+
+    @Test
+    public void shouldReadBodyLengthPolicyFromSystemProperty() {
+        System.setProperty(VerifierOptions.BODY_LENGTH_POLICY_PROPERTY, 
"reject");
+        try {
+            assertEquals(BodyLengthPolicy.REJECT, new 
VerifierOptions.Builder().build().getBodyLengthPolicy());
+        } finally {
+            System.clearProperty(VerifierOptions.BODY_LENGTH_POLICY_PROPERTY);
+        }
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void shouldRejectNullBodyLengthPolicy() {
+        new VerifierOptions.Builder().withBodyLengthPolicy(null).build();
+    }
+
     @Test
     public void shouldLowerCaseSingleInstanceFields() {
         VerifierOptions opt = new 
VerifierOptions.Builder().withSingleInstanceFields(Arrays.asList("From", " 
SUBJECT ")).build();
diff --git 
a/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java 
b/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java
index 05695ea..fa8c06f 100644
--- 
a/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java
+++ 
b/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java
@@ -47,6 +47,7 @@ public class LimitedOutputStreamTest extends 
AbstractOutputStreamTestCase {
         }
         os.close();
         assertEquals(30, os.getComputedBytes());
+        assertEquals(testData.length - 30, os.getDroppedBytes());
         assertArrayEquals(expectedData, bos.toByteArray());
     }
 
@@ -55,9 +56,17 @@ public class LimitedOutputStreamTest extends 
AbstractOutputStreamTestCase {
         LimitedOutputStream os = new LimitedOutputStream(bos, 30);
         chunker(testData, os);
         assertEquals(30, os.getComputedBytes());
+        assertEquals(testData.length - 30, os.getDroppedBytes());
         assertArrayEquals(expectedData, bos.toByteArray());
     }
 
+    public void testNothingDroppedUnderTheLimit() throws IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        LimitedOutputStream os = new LimitedOutputStream(bos, testData.length);
+        os.write(testData, 0, testData.length);
+        assertEquals(0, os.getDroppedBytes());
+    }
+
     protected OutputStream newInstance(ByteArrayOutputStream bos) {
         return new LimitedOutputStream(bos, 30);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to