|
| 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.record; |
| 18 | + |
| 19 | +import org.apache.kafka.common.errors.CorruptRecordException; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 22 | +import org.junit.jupiter.params.provider.Arguments; |
| 23 | +import org.junit.jupiter.params.provider.ArgumentsProvider; |
| 24 | + |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +public final class InvalidMemoryRecordsProvider implements ArgumentsProvider { |
| 30 | + // Use a baseOffset that's not zero so that it is less likely to match the LEO |
| 31 | + private static final long BASE_OFFSET = 1234; |
| 32 | + private static final int EPOCH = 4321; |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns a stream of arguments for invalid memory records and the expected exception. |
| 36 | + * |
| 37 | + * The first object in the {@code Arguments} is a {@code MemoryRecords}. |
| 38 | + * |
| 39 | + * The second object in the {@code Arguments} is an {@code Optional<Class<Exception>>} which is |
| 40 | + * the expected exception from the log layer. |
| 41 | + */ |
| 42 | + @Override |
| 43 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { |
| 44 | + return Stream.of( |
| 45 | + Arguments.of(MemoryRecords.readableRecords(notEnoughBytes()), Optional.empty()), |
| 46 | + Arguments.of(MemoryRecords.readableRecords(recordsSizeTooSmall()), Optional.of(CorruptRecordException.class)), |
| 47 | + Arguments.of(MemoryRecords.readableRecords(notEnoughBytesToMagic()), Optional.empty()), |
| 48 | + Arguments.of(MemoryRecords.readableRecords(negativeMagic()), Optional.of(CorruptRecordException.class)), |
| 49 | + Arguments.of(MemoryRecords.readableRecords(largeMagic()), Optional.of(CorruptRecordException.class)), |
| 50 | + Arguments.of(MemoryRecords.readableRecords(lessBytesThanRecordSize()), Optional.empty()) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private static ByteBuffer notEnoughBytes() { |
| 55 | + var buffer = ByteBuffer.allocate(Records.LOG_OVERHEAD - 1); |
| 56 | + buffer.limit(buffer.capacity()); |
| 57 | + |
| 58 | + return buffer; |
| 59 | + } |
| 60 | + |
| 61 | + private static ByteBuffer recordsSizeTooSmall() { |
| 62 | + var buffer = ByteBuffer.allocate(256); |
| 63 | + // Write the base offset |
| 64 | + buffer.putLong(BASE_OFFSET); |
| 65 | + // Write record size |
| 66 | + buffer.putInt(LegacyRecord.RECORD_OVERHEAD_V0 - 1); |
| 67 | + buffer.position(0); |
| 68 | + buffer.limit(buffer.capacity()); |
| 69 | + |
| 70 | + return buffer; |
| 71 | + } |
| 72 | + |
| 73 | + private static ByteBuffer notEnoughBytesToMagic() { |
| 74 | + var buffer = ByteBuffer.allocate(256); |
| 75 | + // Write the base offset |
| 76 | + buffer.putLong(BASE_OFFSET); |
| 77 | + // Write record size |
| 78 | + buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); |
| 79 | + buffer.position(0); |
| 80 | + buffer.limit(Records.HEADER_SIZE_UP_TO_MAGIC - 1); |
| 81 | + |
| 82 | + return buffer; |
| 83 | + } |
| 84 | + |
| 85 | + private static ByteBuffer negativeMagic() { |
| 86 | + var buffer = ByteBuffer.allocate(256); |
| 87 | + // Write the base offset |
| 88 | + buffer.putLong(BASE_OFFSET); |
| 89 | + // Write record size |
| 90 | + buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); |
| 91 | + // Write the epoch |
| 92 | + buffer.putInt(EPOCH); |
| 93 | + // Write magic |
| 94 | + buffer.put((byte) -1); |
| 95 | + buffer.position(0); |
| 96 | + buffer.limit(buffer.capacity()); |
| 97 | + |
| 98 | + return buffer; |
| 99 | + } |
| 100 | + |
| 101 | + private static ByteBuffer largeMagic() { |
| 102 | + var buffer = ByteBuffer.allocate(256); |
| 103 | + // Write the base offset |
| 104 | + buffer.putLong(BASE_OFFSET); |
| 105 | + // Write record size |
| 106 | + buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); |
| 107 | + // Write the epoch |
| 108 | + buffer.putInt(EPOCH); |
| 109 | + // Write magic |
| 110 | + buffer.put((byte) (RecordBatch.CURRENT_MAGIC_VALUE + 1)); |
| 111 | + buffer.position(0); |
| 112 | + buffer.limit(buffer.capacity()); |
| 113 | + |
| 114 | + return buffer; |
| 115 | + } |
| 116 | + |
| 117 | + private static ByteBuffer lessBytesThanRecordSize() { |
| 118 | + var buffer = ByteBuffer.allocate(256); |
| 119 | + // Write the base offset |
| 120 | + buffer.putLong(BASE_OFFSET); |
| 121 | + // Write record size |
| 122 | + buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); |
| 123 | + // Write the epoch |
| 124 | + buffer.putInt(EPOCH); |
| 125 | + // Write magic |
| 126 | + buffer.put(RecordBatch.CURRENT_MAGIC_VALUE); |
| 127 | + buffer.position(0); |
| 128 | + buffer.limit(buffer.capacity() - Records.LOG_OVERHEAD - 1); |
| 129 | + |
| 130 | + return buffer; |
| 131 | + } |
| 132 | +} |
0 commit comments