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.api; 16 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertNotNull; 19 import static org.junit.Assert.assertNull; 20 import static org.junit.Assert.fail; 21 22 import java.util.Arrays; 23 import java.util.Collections; 24 import org.junit.Test; 25 26 public class AutofuzzTest { 27 public interface UnimplementedInterface {} 28 29 public interface ImplementedInterface {} 30 public static class ImplementingClass implements ImplementedInterface {} 31 implIsNotNull(ImplementedInterface impl)32 private static boolean implIsNotNull(ImplementedInterface impl) { 33 return impl != null; 34 } 35 implIsNotNull(UnimplementedInterface impl)36 private static boolean implIsNotNull(UnimplementedInterface impl) { 37 return impl != null; 38 } 39 checkAllTheArguments( String arg1, int arg2, byte arg3, ImplementedInterface arg4)40 private static void checkAllTheArguments( 41 String arg1, int arg2, byte arg3, ImplementedInterface arg4) { 42 if (!arg1.equals("foobar") || arg2 != 42 || arg3 != 5 || arg4 == null) { 43 throw new IllegalArgumentException(); 44 } 45 } 46 47 @Test testConsume()48 public void testConsume() { 49 FuzzedDataProvider data = CannedFuzzedDataProvider.create( 50 Arrays.asList((byte) 1 /* do not return null */, 0 /* first class on the classpath */, 51 (byte) 1 /* do not return null */, 0 /* first constructor */)); 52 ImplementedInterface result = Autofuzz.consume(data, ImplementedInterface.class); 53 assertNotNull(result); 54 } 55 56 @Test testConsumeFailsWithoutException()57 public void testConsumeFailsWithoutException() { 58 FuzzedDataProvider data = CannedFuzzedDataProvider.create(Collections.singletonList( 59 (byte) 1 /* do not return null without searching for implementing classes */)); 60 assertNull(Autofuzz.consume(data, UnimplementedInterface.class)); 61 } 62 63 @Test testAutofuzz()64 public void testAutofuzz() { 65 FuzzedDataProvider data = CannedFuzzedDataProvider.create( 66 Arrays.asList((byte) 1 /* do not return null */, 0 /* first class on the classpath */, 67 (byte) 1 /* do not return null */, 0 /* first constructor */)); 68 assertEquals(Boolean.TRUE, 69 Autofuzz.autofuzz(data, (Function1<ImplementedInterface, ?>) AutofuzzTest::implIsNotNull)); 70 } 71 72 @Test testAutofuzzFailsWithException()73 public void testAutofuzzFailsWithException() { 74 FuzzedDataProvider data = CannedFuzzedDataProvider.create( 75 Collections.singletonList((byte) 1 /* do not return null */)); 76 try { 77 Autofuzz.autofuzz(data, (Function1<UnimplementedInterface, ?>) AutofuzzTest::implIsNotNull); 78 } catch (AutofuzzConstructionException e) { 79 // Pass. 80 return; 81 } 82 fail("should have thrown an AutofuzzConstructionException"); 83 } 84 85 @Test testAutofuzzConsumer()86 public void testAutofuzzConsumer() { 87 FuzzedDataProvider data = CannedFuzzedDataProvider.create( 88 Arrays.asList((byte) 1 /* do not return null */, 6 /* string length */, "foobar", 42, 89 (byte) 5, (byte) 1 /* do not return null */, 0 /* first class on the classpath */, 90 (byte) 1 /* do not return null */, 0 /* first constructor */)); 91 Autofuzz.autofuzz(data, AutofuzzTest::checkAllTheArguments); 92 } 93 94 @Test testAutofuzzConsumerThrowsException()95 public void testAutofuzzConsumerThrowsException() { 96 FuzzedDataProvider data = 97 CannedFuzzedDataProvider.create(Arrays.asList((byte) 1 /* do not return null */, 98 6 /* string length */, "foobar", 42, (byte) 5, (byte) 0 /* *do* return null */)); 99 try { 100 Autofuzz.autofuzz(data, AutofuzzTest::checkAllTheArguments); 101 } catch (IllegalArgumentException e) { 102 // Pass. 103 return; 104 } 105 fail("should have thrown an IllegalArgumentException"); 106 } 107 } 108