|
| 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 static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth8.assertThat; |
| 21 | +import static java.util.stream.Collectors.collectingAndThen; |
| 22 | +import static java.util.stream.Collectors.toList; |
| 23 | + |
| 24 | +import com.code_intelligence.jazzer.junit.FuzzTest; |
| 25 | +import com.code_intelligence.jazzer.junit.Lifecycle; |
| 26 | +import com.example.PerExecutionLifecycleWithFindingFuzzTest.LifecycleCallbacks1; |
| 27 | +import com.example.PerExecutionLifecycleWithFindingFuzzTest.LifecycleCallbacks2; |
| 28 | +import com.example.PerExecutionLifecycleWithFindingFuzzTest.LifecycleCallbacks3; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.stream.Stream; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Disabled; |
| 39 | +import org.junit.jupiter.api.MethodOrderer; |
| 40 | +import org.junit.jupiter.api.TestMethodOrder; |
| 41 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 42 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 43 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 44 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 45 | +import org.junit.jupiter.api.extension.TestInstancePostProcessor; |
| 46 | + |
| 47 | +@TestMethodOrder(MethodOrderer.MethodName.class) |
| 48 | +@ExtendWith(PerExecutionLifecycleWithFindingFuzzTest.LifecycleInstancePostProcessor.class) |
| 49 | +@ExtendWith(LifecycleCallbacks1.class) |
| 50 | +@ExtendWith(LifecycleCallbacks2.class) |
| 51 | +@ExtendWith(LifecycleCallbacks3.class) |
| 52 | +class PerExecutionLifecycleWithFindingFuzzTest { |
| 53 | + private static final ArrayList<String> events = new ArrayList<>(); |
| 54 | + private static final long RUNS = 3; |
| 55 | + private static int nextInstanceId = 1; |
| 56 | + private final int instanceId = nextInstanceId++; |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + static void beforeAll() { |
| 60 | + events.add("beforeAll"); |
| 61 | + } |
| 62 | + |
| 63 | + private void addEvent(String what) { |
| 64 | + events.add(what + " on " + instanceId); |
| 65 | + } |
| 66 | + |
| 67 | + @BeforeEach |
| 68 | + void beforeEach1() { |
| 69 | + addEvent("beforeEach1"); |
| 70 | + } |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + void beforeEach2() { |
| 74 | + addEvent("beforeEach2"); |
| 75 | + } |
| 76 | + |
| 77 | + @BeforeEach |
| 78 | + void beforeEach3() { |
| 79 | + addEvent("beforeEach3"); |
| 80 | + } |
| 81 | + |
| 82 | + @Disabled |
| 83 | + @FuzzTest |
| 84 | + void disabledFuzz(byte[] data) { |
| 85 | + addEvent("disabledFuzz"); |
| 86 | + throw new AssertionError("This test should not be executed"); |
| 87 | + } |
| 88 | + |
| 89 | + @FuzzTest(maxExecutions = RUNS, lifecycle = Lifecycle.PER_EXECUTION) |
| 90 | + void lifecycleFuzz(byte[] data) throws IOException { |
| 91 | + addEvent("lifecycleFuzz"); |
| 92 | + if (data.length != 0) { |
| 93 | + throw new IOException( |
| 94 | + "Planted finding on first non-trivial input (second execution during fuzzing)"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @AfterEach |
| 99 | + void afterEach1() { |
| 100 | + addEvent("afterEach1"); |
| 101 | + } |
| 102 | + |
| 103 | + @AfterEach |
| 104 | + void afterEach2() { |
| 105 | + addEvent("afterEach2"); |
| 106 | + } |
| 107 | + |
| 108 | + @AfterEach |
| 109 | + void afterEach3() { |
| 110 | + addEvent("afterEach3"); |
| 111 | + } |
| 112 | + |
| 113 | + static List<String> expectedBeforeEachEvents(int instanceId) { |
| 114 | + return Stream.of( |
| 115 | + "beforeEachCallback1", |
| 116 | + "beforeEachCallback2", |
| 117 | + "beforeEachCallback3", |
| 118 | + "beforeEach1", |
| 119 | + "beforeEach2", |
| 120 | + "beforeEach3") |
| 121 | + .map(s -> s + " on " + instanceId) |
| 122 | + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
| 123 | + } |
| 124 | + |
| 125 | + static List<String> expectedAfterEachEvents(int instanceId) { |
| 126 | + return Stream.of( |
| 127 | + "afterEach1", |
| 128 | + "afterEach2", |
| 129 | + "afterEach3", |
| 130 | + "afterEachCallback3", |
| 131 | + "afterEachCallback2", |
| 132 | + "afterEachCallback1") |
| 133 | + .map(s -> s + " on " + instanceId) |
| 134 | + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
| 135 | + } |
| 136 | + |
| 137 | + @AfterAll |
| 138 | + static void afterAll() throws TestSuccessfulException { |
| 139 | + events.add("afterAll"); |
| 140 | + |
| 141 | + boolean isRegressionTest = "".equals(System.getenv("JAZZER_FUZZ")); |
| 142 | + boolean isFuzzingFromCommandLine = System.getenv("JAZZER_FUZZ") == null; |
| 143 | + boolean isFuzzingFromJUnit = !isFuzzingFromCommandLine && !isRegressionTest; |
| 144 | + |
| 145 | + ArrayList<String> expectedEvents = new ArrayList<>(); |
| 146 | + expectedEvents.add("beforeAll"); |
| 147 | + |
| 148 | + int firstFuzzingInstanceId = 1; |
| 149 | + // When run from the command-line, the fuzz test is not separately executed on the empty seed. |
| 150 | + if (isRegressionTest || isFuzzingFromJUnit) { |
| 151 | + expectedEvents.add("postProcessTestInstance on 1"); |
| 152 | + expectedEvents.addAll(expectedBeforeEachEvents(1)); |
| 153 | + expectedEvents.add("lifecycleFuzz on 1"); |
| 154 | + expectedEvents.addAll(expectedAfterEachEvents(1)); |
| 155 | + firstFuzzingInstanceId++; |
| 156 | + } |
| 157 | + if (isFuzzingFromJUnit || isFuzzingFromCommandLine) { |
| 158 | + expectedEvents.add("postProcessTestInstance on " + firstFuzzingInstanceId); |
| 159 | + expectedEvents.addAll(expectedBeforeEachEvents(firstFuzzingInstanceId)); |
| 160 | + // The fuzz test fails during the second run. |
| 161 | + for (int i = 1; i <= 2; i++) { |
| 162 | + int expectedId = firstFuzzingInstanceId + i; |
| 163 | + expectedEvents.add("postProcessTestInstance on " + expectedId); |
| 164 | + expectedEvents.addAll(expectedBeforeEachEvents(expectedId)); |
| 165 | + expectedEvents.add("lifecycleFuzz on " + expectedId); |
| 166 | + expectedEvents.addAll(expectedAfterEachEvents(expectedId)); |
| 167 | + } |
| 168 | + expectedEvents.addAll(expectedAfterEachEvents(firstFuzzingInstanceId)); |
| 169 | + } |
| 170 | + |
| 171 | + expectedEvents.add("afterAll"); |
| 172 | + |
| 173 | + assertThat(events).containsExactlyElementsIn(expectedEvents).inOrder(); |
| 174 | + throw new TestSuccessfulException("Lifecycle methods invoked as expected"); |
| 175 | + } |
| 176 | + |
| 177 | + static void assertConsistentTestInstances(ExtensionContext extensionContext) { |
| 178 | + assertThat(extensionContext.getTestInstance().get()) |
| 179 | + .isSameInstanceAs(extensionContext.getRequiredTestInstance()); |
| 180 | + assertThat(extensionContext.getTestInstances().get()) |
| 181 | + .isSameInstanceAs(extensionContext.getRequiredTestInstances()); |
| 182 | + assertThat(extensionContext.getRequiredTestInstances().getAllInstances()) |
| 183 | + .containsExactly(extensionContext.getRequiredTestInstance()); |
| 184 | + } |
| 185 | + |
| 186 | + static class LifecycleInstancePostProcessor implements TestInstancePostProcessor { |
| 187 | + @Override |
| 188 | + public void postProcessTestInstance(Object o, ExtensionContext extensionContext) { |
| 189 | + assertThat(extensionContext.getTestInstance()).isEmpty(); |
| 190 | + assertThat(extensionContext.getTestInstances()).isEmpty(); |
| 191 | + ((PerExecutionLifecycleWithFindingFuzzTest) o).addEvent("postProcessTestInstance"); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + static class LifecycleCallbacks1 implements BeforeEachCallback, AfterEachCallback { |
| 196 | + @Override |
| 197 | + public void beforeEach(ExtensionContext extensionContext) { |
| 198 | + assertConsistentTestInstances(extensionContext); |
| 199 | + ((PerExecutionLifecycleWithFindingFuzzTest) extensionContext.getRequiredTestInstance()) |
| 200 | + .addEvent("beforeEachCallback1"); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void afterEach(ExtensionContext extensionContext) { |
| 205 | + assertConsistentTestInstances(extensionContext); |
| 206 | + ((PerExecutionLifecycleWithFindingFuzzTest) extensionContext.getRequiredTestInstance()) |
| 207 | + .addEvent("afterEachCallback1"); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + static class LifecycleCallbacks2 implements BeforeEachCallback, AfterEachCallback { |
| 212 | + @Override |
| 213 | + public void beforeEach(ExtensionContext extensionContext) { |
| 214 | + assertConsistentTestInstances(extensionContext); |
| 215 | + ((PerExecutionLifecycleWithFindingFuzzTest) extensionContext.getRequiredTestInstance()) |
| 216 | + .addEvent("beforeEachCallback2"); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void afterEach(ExtensionContext extensionContext) { |
| 221 | + assertConsistentTestInstances(extensionContext); |
| 222 | + ((PerExecutionLifecycleWithFindingFuzzTest) extensionContext.getRequiredTestInstance()) |
| 223 | + .addEvent("afterEachCallback2"); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + static class LifecycleCallbacks3 implements BeforeEachCallback, AfterEachCallback { |
| 228 | + @Override |
| 229 | + public void beforeEach(ExtensionContext extensionContext) { |
| 230 | + assertConsistentTestInstances(extensionContext); |
| 231 | + ((PerExecutionLifecycleWithFindingFuzzTest) extensionContext.getRequiredTestInstance()) |
| 232 | + .addEvent("beforeEachCallback3"); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public void afterEach(ExtensionContext extensionContext) { |
| 237 | + assertConsistentTestInstances(extensionContext); |
| 238 | + ((PerExecutionLifecycleWithFindingFuzzTest) extensionContext.getRequiredTestInstance()) |
| 239 | + .addEvent("afterEachCallback3"); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments