@@ -0,0 +1,2597 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one%0A * or more contributor license agreements.  See the NOTICE file%0A * distributed with this work for additional information%0A * regarding copyright ownership.  The ASF licenses this file%0A * to you under the Apache License, Version 2.0 (the%0A * %22License%22); you may not use this file except in compliance%0A * with the License.  You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on an%0A * %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A * KIND, either express or implied.  See the License for the%0A * specific language governing permissions and limitations%0A * under the License.%0A */%0Apackage org.apache.commons.codec.digest;%0A%0Aimport org.junit.Assert;%0Aimport org.junit.Assume;%0Aimport org.junit.Test;%0A%0Apublic class XXHash32OverflowTest %7B%0A%0A    /**%0A     * This test hits an edge case where a very large number of bytes is added to the incremental%0A     * hash. The data is constructed so that an integer counter of unprocessed bytes will%0A     * overflow. If this is not handled correctly then the code throws an exception when it%0A     * copies more data into the unprocessed bytes array.%0A     */%0A    @Test%0A    public void testIncrementalHashWithUnprocessedBytesAndHugeLengthArray() %7B%0A        // Assert the test precondition that a large array added to unprocessed bytes%0A        // will overflow an integer counter. We use the smallest hugeLength possible%0A        // as some VMs cannot allocate maximum length arrays.%0A        final int bufferSize = 16;%0A        final int unprocessedSize = bufferSize - 1;%0A        final int hugeLength = Integer.MAX_VALUE - (unprocessedSize - 1);%0A        Assert.assertTrue(%22This should overflow to negative%22, unprocessedSize + hugeLength %3C bufferSize);%0A%0A        // Check the test can be run%0A        byte%5B%5D bytes = null;%0A        try %7B%0A            bytes = new byte%5BhugeLength%5D;%0A        %7D catch (OutOfMemoryError ignore) %7B%0A            // Some VMs cannot allocate an array this large.%0A            // Some test environments may not have enough available memory for this.%0A        %7D%0A        Assume.assumeTrue(%22Cannot allocate array of length %22 + hugeLength, bytes != null);%0A%0A        final XXHash32 inc = new XXHash32();%0A        // Add bytes that should be unprocessed%0A        inc.update(bytes, 0, unprocessedSize);%0A        // Add a huge number of bytes to overflow an integer counter of unprocessed bytes.%0A        inc.update(bytes, 0, hugeLength);%0A    %7D%0A%7D%0A

Test XXHash32 with huge length array added to unprocessed bytes.