|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.kafka.common.message; |
| 18 | + |
| 19 | +import org.apache.kafka.common.protocol.ByteBufferAccessor; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | + |
| 27 | +public class SimpleArraysMessageTest { |
| 28 | + @Test |
| 29 | + public void testArrayBoundsChecking() { |
| 30 | + // SimpleArraysMessageData takes 2 arrays |
| 31 | + final ByteBuffer buf = ByteBuffer.wrap(new byte[] { |
| 32 | + (byte) 0x7f, // Set size of first array to 126 which is larger than the size of this buffer |
| 33 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 |
| 34 | + }); |
| 35 | + final SimpleArraysMessageData out = new SimpleArraysMessageData(); |
| 36 | + ByteBufferAccessor accessor = new ByteBufferAccessor(buf); |
| 37 | + assertEquals("Tried to allocate a collection of size 126, but there are only 7 bytes remaining.", |
| 38 | + assertThrows(RuntimeException.class, () -> out.read(accessor, (short) 2)).getMessage()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testArrayBoundsCheckingOtherArray() { |
| 43 | + // SimpleArraysMessageData takes 2 arrays |
| 44 | + final ByteBuffer buf = ByteBuffer.wrap(new byte[] { |
| 45 | + (byte) 0x01, // Set size of first array to 0 |
| 46 | + (byte) 0x7e, // Set size of second array to 125 which is larger than the size of this buffer |
| 47 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 |
| 48 | + }); |
| 49 | + final SimpleArraysMessageData out = new SimpleArraysMessageData(); |
| 50 | + ByteBufferAccessor accessor = new ByteBufferAccessor(buf); |
| 51 | + assertEquals("Tried to allocate a collection of size 125, but there are only 6 bytes remaining.", |
| 52 | + assertThrows(RuntimeException.class, () -> out.read(accessor, (short) 2)).getMessage()); |
| 53 | + } |
| 54 | +} |
0 commit comments