|
| 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.arrow.dataset.jni; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import org.apache.arrow.dataset.ParquetWriteSupport; |
| 24 | +import org.apache.arrow.dataset.file.FileFormat; |
| 25 | +import org.apache.arrow.dataset.file.FileSystemDatasetFactory; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | + |
| 29 | +/** |
| 30 | + * Regression test for the native {@code FromSchemaByteArray} helper (GH-1205). Passing malformed |
| 31 | + * serialized schema bytes to {@code JniWrapper#createDataset} must surface a Java exception rather |
| 32 | + * than crash, and repeated failures must not leak the pinned/copied Java byte-array elements |
| 33 | + * acquired via {@code GetByteArrayElements}. |
| 34 | + */ |
| 35 | +public class TestFromSchemaByteArray extends TestNativeDataset { |
| 36 | + |
| 37 | + @TempDir public File TMP; |
| 38 | + |
| 39 | + public static final String AVRO_SCHEMA_USER = "user.avsc"; |
| 40 | + |
| 41 | + private static long factoryId(NativeDatasetFactory factory) throws Exception { |
| 42 | + Field field = NativeDatasetFactory.class.getDeclaredField("datasetFactoryId"); |
| 43 | + field.setAccessible(true); |
| 44 | + return field.getLong(factory); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testCreateDatasetWithMalformedSchemaBytes() throws Exception { |
| 49 | + ParquetWriteSupport writeSupport = |
| 50 | + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP, 1, "a"); |
| 51 | + FileSystemDatasetFactory factory = |
| 52 | + new FileSystemDatasetFactory( |
| 53 | + rootAllocator(), |
| 54 | + NativeMemoryPool.getDefault(), |
| 55 | + FileFormat.PARQUET, |
| 56 | + writeSupport.getOutputURI()); |
| 57 | + try { |
| 58 | + final long datasetFactoryId = factoryId(factory); |
| 59 | + // Bytes that are not a valid serialized Arrow schema, so native ReadSchema fails and the |
| 60 | + // error path of FromSchemaByteArray is taken. |
| 61 | + final byte[] malformedSchemaBytes = new byte[] {0, 1, 2, 3, 4, 5, 6, 7}; |
| 62 | + |
| 63 | + // Repeat many times: before the fix each failed call leaked the acquired array elements. |
| 64 | + // The loop keeps the test meaningful as a leak regression while asserting graceful failure. |
| 65 | + for (int i = 0; i < 1000; i++) { |
| 66 | + assertThrows( |
| 67 | + RuntimeException.class, |
| 68 | + () -> JniWrapper.get().createDataset(datasetFactoryId, malformedSchemaBytes)); |
| 69 | + } |
| 70 | + } finally { |
| 71 | + factory.close(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments