1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 import java.nio.charset.StandardCharsets; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.UncheckedIOException; 21 22 import org.apache.commons.geometry.io.core.input.StreamGeometryInput; 23 import org.apache.commons.geometry.io.euclidean.threed.BoundaryReadHandler3D; 24 import org.apache.commons.geometry.io.euclidean.threed.stl.StlBoundaryReadHandler3D; 25 import org.apache.commons.numbers.core.Precision; 26 27 public class GeometryStlTextFuzzer { fuzzerTestOneInput(final byte[] data)28 public static void fuzzerTestOneInput(final byte[] data) { 29 // prepend the "solid" STL keyword to the input to ensure it is interpreted 30 // as text STL input 31 final byte[] dataWithPrefix = join("solid ".getBytes(StandardCharsets.US_ASCII), data); 32 33 try { 34 final BoundaryReadHandler3D handler = new StlBoundaryReadHandler3D(); 35 36 final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-20); 37 38 // check standard read 39 handler.read(new StreamGeometryInput(new ByteArrayInputStream(dataWithPrefix)), precision); 40 41 // check mesh read 42 handler.readTriangleMesh(new StreamGeometryInput(new ByteArrayInputStream(dataWithPrefix)), precision); 43 } catch (UncheckedIOException | IllegalArgumentException | IllegalStateException ignored) { 44 // expected exception types; ignore 45 } 46 } 47 join(final byte[] a, final byte[] b)48 private static byte[] join(final byte[] a, final byte[] b) { 49 final byte[] result = new byte[a.length + b.length]; 50 System.arraycopy(a, 0, result, 0, a.length); 51 System.arraycopy(b, 0, result, a.length, b.length); 52 53 return result; 54 } 55 } 56