lhotari commented on code in PR #4196:
URL: https://github.com/apache/bookkeeper/pull/4196#discussion_r1477437303


##########
bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/checksum/CompositeByteBufUnwrapBugReproduceTest.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.bookkeeper.proto.checksum;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import com.scurrilous.circe.checksum.IntHash;
+import com.scurrilous.circe.checksum.Java8IntHash;
+import com.scurrilous.circe.checksum.Java9IntHash;
+import com.scurrilous.circe.checksum.JniIntHash;
+import com.scurrilous.circe.crc.Sse42Crc32C;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.CompositeByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.util.ReferenceCounted;
+import java.util.Arrays;
+import java.util.Collection;
+import org.apache.bookkeeper.proto.BookieProtoEncoding;
+import org.apache.bookkeeper.proto.BookieProtocol;
+import org.apache.bookkeeper.util.ByteBufList;
+import org.apache.commons.lang3.RandomUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/**
+ * This test class was added to reproduce a bug in the checksum calculation 
when
+ * the payload is a CompositeByteBuf and this buffer has a reader index state 
other than 0.
+ * The reader index state gets lost in the unwrapping process.
+ *
+ * There were at least 2 different bugs. One that occured when the
+ * payload was >= BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD and the other 
when
+ * it was < BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD.
+ * This test covers both useV2Protocol=true and useV2Protocol=false since the 
bug was triggered differently.
+ *
+ * The bug has been fixed and this test is here to make sure it doesn't happen 
again.
+ */
+@RunWith(Parameterized.class)
+public class CompositeByteBufUnwrapBugReproduceTest {
+    final byte[] testPayLoad;
+    final int bufferPrefixLength;
+    private final boolean useV2Protocol;
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> testScenarios() {
+        return Arrays.asList(new Object[][] {
+                {BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD - 1, true},
+                {BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD - 1, false},
+                {BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD, true},
+                {BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD, false}
+        });
+    }
+
+    public CompositeByteBufUnwrapBugReproduceTest(int payloadSize, boolean 
useV2Protocol) {
+        this.testPayLoad = createTestPayLoad(payloadSize);
+        this.bufferPrefixLength = payloadSize / 7;
+        this.useV2Protocol = useV2Protocol;
+    }
+
+    private static byte[] createTestPayLoad(int payloadSize) {
+        byte[] payload = new byte[payloadSize];
+        for (int i = 0; i < payloadSize; i++) {
+            payload[i] = (byte) i;
+        }
+        return payload;
+    }
+
+
+    /**
+     * A DigestManager that uses the given IntHash implementation for testing.
+     */
+    static class TestIntHashDigestManager extends DigestManager {
+        private final IntHash intHash;
+
+        public TestIntHashDigestManager(IntHash intHash, long ledgerId, 
boolean useV2Protocol,
+                                        ByteBufAllocator allocator) {
+            super(ledgerId, useV2Protocol, allocator);
+            this.intHash = intHash;
+        }
+
+        @Override
+        int getMacCodeLength() {
+            return 4;
+        }
+
+        @Override
+        boolean isInt32Digest() {
+            return true;
+        }
+
+        @Override
+        void populateValueAndReset(int digest, ByteBuf buf) {
+            buf.writeInt(digest);
+        }
+
+        @Override
+        int internalUpdate(int digest, ByteBuf data, int offset, int len) {
+            return intHash.resume(digest, data, offset, len);
+        }
+
+        @Override
+        int internalUpdate(int digest, byte[] buffer, int offset, int len) {
+            return intHash.resume(digest, buffer, offset, len);
+        }
+    }
+
+    @Test
+    public void shouldCalculateChecksumForCompositeBuffer() {
+        ByteBuf testPayload = Unpooled.wrappedBuffer(testPayLoad);
+        byte[] referenceOutput = computeDigestAndPackageForSending(new 
Java8IntHash(), testPayload.retainedDuplicate());
+        assertDigestAndPackageMatchesReference(new Java8IntHash(), 
testPayload, referenceOutput);
+        assertDigestAndPackageMatchesReference(new Java9IntHash(), 
testPayload, referenceOutput);
+        if (Sse42Crc32C.isSupported()) {
+            assertDigestAndPackageMatchesReference(new JniIntHash(), 
testPayload, referenceOutput);
+        }
+        testPayload.release();
+    }
+
+    private void assertDigestAndPackageMatchesReference(IntHash intHash, 
ByteBuf payload, byte[] referenceOutput) {
+        assertDigestAndPackageScenario(intHash, payload.retainedDuplicate(), 
referenceOutput,
+                "plain payload, no wrapping");
+
+        ByteBuf payload2 = 
wrapWithPrefixAndCompositeByteBufWithReaderIndexState(payload.retainedDuplicate());
+        assertDigestAndPackageScenario(intHash, payload2, referenceOutput,
+                "payload with prefix wrapped in CompositeByteBuf with 
readerIndex state");
+
+        ByteBuf payload3 = 
wrapWithPrefixAndMultipleCompositeByteBufWithReaderIndexStateAndMultipleLayersOfDuplicate(
+                payload.retainedDuplicate());
+        assertDigestAndPackageScenario(intHash, payload3, referenceOutput,
+                "payload with prefix wrapped in 2 layers of CompositeByteBuf 
with readerIndex state in the outer "
+                        + "composite. In addition, the outer composite is 
duplicated twice.");
+
+        /*

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to