1 // Copyright 2021 Code Intelligence GmbH 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 package com.code_intelligence.jazzer.autofuzz; 16 17 import static org.junit.Assert.assertArrayEquals; 18 import static org.junit.Assert.assertEquals; 19 20 import com.code_intelligence.jazzer.api.CannedFuzzedDataProvider; 21 import com.code_intelligence.jazzer.api.FuzzedDataProvider; 22 import java.io.ByteArrayInputStream; 23 import java.lang.reflect.Constructor; 24 import java.lang.reflect.Executable; 25 import java.lang.reflect.Method; 26 import java.lang.reflect.Type; 27 import java.util.List; 28 29 class TestHelpers { assertGeneralEquals(Object expected, Object actual)30 static void assertGeneralEquals(Object expected, Object actual) { 31 Class<?> type = expected != null ? expected.getClass() : Object.class; 32 if (type.isArray()) { 33 if (type.getComponentType() == boolean.class) { 34 assertArrayEquals((boolean[]) expected, (boolean[]) actual); 35 } else if (type.getComponentType() == char.class) { 36 assertArrayEquals((char[]) expected, (char[]) actual); 37 } else if (type.getComponentType() == short.class) { 38 assertArrayEquals((short[]) expected, (short[]) actual); 39 } else if (type.getComponentType() == long.class) { 40 assertArrayEquals((long[]) expected, (long[]) actual); 41 } else { 42 assertArrayEquals((Object[]) expected, (Object[]) actual); 43 } 44 } else if (type == ByteArrayInputStream.class) { 45 ByteArrayInputStream expectedStream = (ByteArrayInputStream) expected; 46 ByteArrayInputStream actualStream = (ByteArrayInputStream) actual; 47 assertArrayEquals(readAllBytes(expectedStream), readAllBytes(actualStream)); 48 } else { 49 assertEquals(expected, actual); 50 } 51 } 52 consumeTestCase( Object expectedResult, String expectedResultString, List<Object> cannedData)53 static void consumeTestCase( 54 Object expectedResult, String expectedResultString, List<Object> cannedData) { 55 Class<?> type = expectedResult != null ? expectedResult.getClass() : Object.class; 56 consumeTestCase(type, expectedResult, expectedResultString, cannedData); 57 } 58 consumeTestCase( Type type, Object expectedResult, String expectedResultString, List<Object> cannedData)59 static void consumeTestCase( 60 Type type, Object expectedResult, String expectedResultString, List<Object> cannedData) { 61 AutofuzzCodegenVisitor visitor = new AutofuzzCodegenVisitor(); 62 FuzzedDataProvider data = CannedFuzzedDataProvider.create(cannedData); 63 assertGeneralEquals(expectedResult, new Meta(null).consume(data, type, visitor)); 64 assertEquals(expectedResultString, visitor.generate()); 65 } 66 autofuzzTestCase(Object expectedResult, String expectedResultString, Executable func, List<Object> cannedData)67 static void autofuzzTestCase(Object expectedResult, String expectedResultString, Executable func, 68 List<Object> cannedData) { 69 AutofuzzCodegenVisitor visitor = new AutofuzzCodegenVisitor(); 70 FuzzedDataProvider data = CannedFuzzedDataProvider.create(cannedData); 71 if (func instanceof Method) { 72 assertGeneralEquals(expectedResult, new Meta(null).autofuzz(data, (Method) func, visitor)); 73 } else { 74 assertGeneralEquals( 75 expectedResult, new Meta(null).autofuzz(data, (Constructor<?>) func, visitor)); 76 } 77 assertEquals(expectedResultString, visitor.generate()); 78 } 79 readAllBytes(ByteArrayInputStream in)80 private static byte[] readAllBytes(ByteArrayInputStream in) { 81 byte[] result = new byte[in.available()]; 82 in.read(result, 0, in.available()); 83 return result; 84 } 85 } 86