1 // Copyright 2022 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.junit; 16 17 import static org.junit.Assume.assumeTrue; 18 import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; 19 import static org.junit.platform.testkit.engine.EventConditions.container; 20 import static org.junit.platform.testkit.engine.EventConditions.displayName; 21 import static org.junit.platform.testkit.engine.EventConditions.event; 22 import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully; 23 import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; 24 import static org.junit.platform.testkit.engine.EventConditions.test; 25 import static org.junit.platform.testkit.engine.EventConditions.type; 26 import static org.junit.platform.testkit.engine.EventConditions.uniqueIdSubstrings; 27 import static org.junit.platform.testkit.engine.EventType.DYNAMIC_TEST_REGISTERED; 28 import static org.junit.platform.testkit.engine.EventType.FINISHED; 29 import static org.junit.platform.testkit.engine.EventType.REPORTING_ENTRY_PUBLISHED; 30 import static org.junit.platform.testkit.engine.EventType.STARTED; 31 import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; 32 33 import com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow; 34 import java.nio.file.Path; 35 import java.util.regex.PatternSyntaxException; 36 import org.junit.Before; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.platform.testkit.engine.EngineExecutionResults; 40 import org.junit.platform.testkit.engine.EngineTestKit; 41 import org.junit.rules.TemporaryFolder; 42 43 public class HermeticInstrumentationTest { 44 private static final String ENGINE = "engine:junit-jupiter"; 45 private static final String CLAZZ = "class:com.example.HermeticInstrumentationFuzzTest"; 46 private static final String FUZZ_TEST_1 = "test-template:fuzzTest1([B)"; 47 private static final String FUZZ_TEST_2 = "test-template:fuzzTest2([B)"; 48 private static final String UNIT_TEST_1 = "method:unitTest1()"; 49 private static final String UNIT_TEST_2 = "method:unitTest2()"; 50 private static final String INVOCATION = "test-template-invocation:#1"; 51 @Rule public TemporaryFolder temp = new TemporaryFolder(); 52 Path baseDir; 53 54 @Before setup()55 public void setup() { 56 baseDir = temp.getRoot().toPath(); 57 } 58 executeTests()59 private EngineExecutionResults executeTests() { 60 return EngineTestKit.engine("junit-jupiter") 61 .selectors(selectClass("com.example.HermeticInstrumentationFuzzTest")) 62 .configurationParameter( 63 "jazzer.instrument", "com.other.package.**,com.example.**,com.yet.another.package.*") 64 .configurationParameter("jazzer.internal.basedir", baseDir.toAbsolutePath().toString()) 65 .configurationParameter("junit.jupiter.execution.parallel.enabled", "true") 66 .execute(); 67 } 68 69 @Test fuzzingDisabled()70 public void fuzzingDisabled() { 71 assumeTrue(System.getenv("JAZZER_FUZZ") == null); 72 73 EngineExecutionResults results = executeTests(); 74 75 results.containerEvents().assertEventsMatchLoosely(event(type(STARTED), container(ENGINE)), 76 event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ))), 77 event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_1))), 78 event(type(REPORTING_ENTRY_PUBLISHED), 79 container(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_1))), 80 event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_1))), 81 event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_2))), 82 event(type(REPORTING_ENTRY_PUBLISHED), 83 container(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_2))), 84 event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_2))), 85 event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ)), finishedSuccessfully()), 86 event(type(FINISHED), container(ENGINE), finishedSuccessfully())); 87 88 results.testEvents().assertEventsMatchLoosely( 89 event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_1))), 90 event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_1, INVOCATION)), 91 displayName("<empty input>")), 92 event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_1, INVOCATION)), 93 displayName("<empty input>"), 94 finishedWithFailure(instanceOf(FuzzerSecurityIssueLow.class))), 95 event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, UNIT_TEST_1))), 96 event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, UNIT_TEST_1)), 97 finishedWithFailure(instanceOf(PatternSyntaxException.class))), 98 event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_2))), 99 event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_2, INVOCATION)), 100 displayName("<empty input>")), 101 event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, FUZZ_TEST_2, INVOCATION)), 102 displayName("<empty input>"), 103 finishedWithFailure(instanceOf(FuzzerSecurityIssueLow.class))), 104 event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, UNIT_TEST_2))), 105 event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, UNIT_TEST_2)), 106 finishedWithFailure(instanceOf(PatternSyntaxException.class)))); 107 } 108 } 109