1 /* 2 * Copyright 2022 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example; 18 19 import com.code_intelligence.jazzer.junit.FuzzTest; 20 import java.io.IOException; 21 import org.junit.jupiter.api.AfterAll; 22 import org.junit.jupiter.api.AfterEach; 23 import org.junit.jupiter.api.Assertions; 24 import org.junit.jupiter.api.BeforeAll; 25 import org.junit.jupiter.api.BeforeEach; 26 import org.junit.jupiter.api.Disabled; 27 import org.junit.jupiter.api.MethodOrderer; 28 import org.junit.jupiter.api.TestMethodOrder; 29 import org.junit.jupiter.api.extension.ExtendWith; 30 import org.junit.jupiter.api.extension.ExtensionContext; 31 import org.junit.jupiter.api.extension.TestInstancePostProcessor; 32 33 @TestMethodOrder(MethodOrderer.MethodName.class) 34 @ExtendWith(LifecycleFuzzTest.LifecycleInstancePostProcessor.class) 35 class LifecycleFuzzTest { 36 // In fuzzing mode, the test is invoked once on the empty input and once with Jazzer. 37 private static final int EXPECTED_EACH_COUNT = 38 System.getenv().getOrDefault("JAZZER_FUZZ", "").isEmpty() ? 1 : 2; 39 40 private static int beforeAllCount = 0; 41 private static int beforeEachGlobalCount = 0; 42 private static int afterEachGlobalCount = 0; 43 private static int afterAllCount = 0; 44 45 private boolean beforeEachCalledOnInstance = false; 46 private boolean testInstancePostProcessorCalledOnInstance = false; 47 48 @BeforeAll beforeAll()49 static void beforeAll() { 50 beforeAllCount++; 51 } 52 53 @BeforeEach beforeEach()54 void beforeEach() { 55 beforeEachGlobalCount++; 56 beforeEachCalledOnInstance = true; 57 } 58 59 @Disabled 60 @FuzzTest disabledFuzz(byte[] data)61 void disabledFuzz(byte[] data) { 62 throw new AssertionError("This test should not be executed"); 63 } 64 65 @FuzzTest(maxDuration = "1s") lifecycleFuzz(byte[] data)66 void lifecycleFuzz(byte[] data) { 67 Assertions.assertEquals(1, beforeAllCount); 68 Assertions.assertEquals(beforeEachGlobalCount, afterEachGlobalCount + 1); 69 Assertions.assertTrue(beforeEachCalledOnInstance); 70 Assertions.assertTrue(testInstancePostProcessorCalledOnInstance); 71 } 72 73 @AfterEach afterEach()74 void afterEach() { 75 afterEachGlobalCount++; 76 } 77 78 @AfterAll afterAll()79 static void afterAll() throws IOException { 80 afterAllCount++; 81 Assertions.assertEquals(1, beforeAllCount); 82 Assertions.assertEquals(EXPECTED_EACH_COUNT, beforeEachGlobalCount); 83 Assertions.assertEquals(EXPECTED_EACH_COUNT, afterEachGlobalCount); 84 Assertions.assertEquals(1, afterAllCount); 85 throw new IOException(); 86 } 87 88 static class LifecycleInstancePostProcessor implements TestInstancePostProcessor { 89 @Override postProcessTestInstance(Object o, ExtensionContext extensionContext)90 public void postProcessTestInstance(Object o, ExtensionContext extensionContext) { 91 ((LifecycleFuzzTest) o).testInstancePostProcessorCalledOnInstance = true; 92 } 93 } 94 } 95