diff options
author | Fabian Meumertzheim <meumertzheim@code-intelligence.com> | 2023-05-09 08:41:14 +0200 |
---|---|---|
committer | Fabian Meumertzheim <fabian@meumertzhe.im> | 2023-05-22 08:57:35 +0200 |
commit | 37d26a3557173809dbae6792190e045907cde0fa (patch) | |
tree | 06dce250dd17f065fc7445899018ea3ec406120f | |
parent | ae80d4fb5b135d76bb5e9c22aeae3441f812d560 (diff) | |
download | jazzer-api-37d26a3557173809dbae6792190e045907cde0fa.tar.gz |
junit: Split FuzzTestArgumentsProvider into three providers
This improves readability and prepares us for an upcoming JUnit change
that allows us to retrieve the `ArgumentsProvider` instance that
resulted in a particular invocation, thus removing the need to hackily
mark the arguments.
-rw-r--r-- | src/main/java/com/code_intelligence/jazzer/junit/AgentConfiguringArgumentsProvider.java | 43 | ||||
-rw-r--r-- | src/main/java/com/code_intelligence/jazzer/junit/BUILD.bazel | 4 | ||||
-rw-r--r-- | src/main/java/com/code_intelligence/jazzer/junit/FuzzTest.java | 17 | ||||
-rw-r--r-- | src/main/java/com/code_intelligence/jazzer/junit/FuzzingArgumentsProvider.java | 42 | ||||
-rw-r--r-- | src/main/java/com/code_intelligence/jazzer/junit/SeedArgumentsProvider.java (renamed from src/main/java/com/code_intelligence/jazzer/junit/FuzzTestArgumentsProvider.java) | 29 |
5 files changed, 107 insertions, 28 deletions
diff --git a/src/main/java/com/code_intelligence/jazzer/junit/AgentConfiguringArgumentsProvider.java b/src/main/java/com/code_intelligence/jazzer/junit/AgentConfiguringArgumentsProvider.java new file mode 100644 index 00000000..e65f028b --- /dev/null +++ b/src/main/java/com/code_intelligence/jazzer/junit/AgentConfiguringArgumentsProvider.java @@ -0,0 +1,43 @@ +/* + * Copyright 2023 Code Intelligence GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.code_intelligence.jazzer.junit; + +import java.util.stream.Stream; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.support.AnnotationConsumer; + +public class AgentConfiguringArgumentsProvider + implements ArgumentsProvider, AnnotationConsumer<FuzzTest> { + private FuzzTest fuzzTest; + + @Override + public void accept(FuzzTest fuzzTest) { + this.fuzzTest = fuzzTest; + } + + @Override + public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) + throws Exception { + // FIXME(fmeum): Calling this here feels like a hack. There should be a lifecycle hook that runs + // before the argument discovery for a ParameterizedTest is kicked off, but I haven't found + // one. + FuzzTestExecutor.configureAndInstallAgent(extensionContext, fuzzTest.maxDuration()); + return Stream.empty(); + } +} diff --git a/src/main/java/com/code_intelligence/jazzer/junit/BUILD.bazel b/src/main/java/com/code_intelligence/jazzer/junit/BUILD.bazel index 6ed5caca..82f055f6 100644 --- a/src/main/java/com/code_intelligence/jazzer/junit/BUILD.bazel +++ b/src/main/java/com/code_intelligence/jazzer/junit/BUILD.bazel @@ -22,9 +22,11 @@ java_library( java_library( name = "fuzz_test", srcs = [ + "AgentConfiguringArgumentsProvider.java", "FuzzTest.java", - "FuzzTestArgumentsProvider.java", "FuzzTestExtensions.java", + "FuzzingArgumentsProvider.java", + "SeedArgumentsProvider.java", ], visibility = [ "//examples/junit/src/test/java/com/example:__pkg__", diff --git a/src/main/java/com/code_intelligence/jazzer/junit/FuzzTest.java b/src/main/java/com/code_intelligence/jazzer/junit/FuzzTest.java index 773e4a0d..041db96d 100644 --- a/src/main/java/com/code_intelligence/jazzer/junit/FuzzTest.java +++ b/src/main/java/com/code_intelligence/jazzer/junit/FuzzTest.java @@ -86,7 +86,9 @@ import org.junit.jupiter.params.provider.ArgumentsSource; */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) -@ArgumentsSource(FuzzTestArgumentsProvider.class) +@AgentConfiguringArgumentsProviderArgumentsSource +@ArgumentsSource(SeedArgumentsProvider.class) +@FuzzingArgumentsProviderArgumentsSource @ExtendWith(FuzzTestExtensions.class) // {0} is expanded to the basename of the seed by the ArgumentProvider. @ParameterizedTest(name = "{0}") @@ -105,3 +107,16 @@ public @interface FuzzTest { */ String maxDuration() default "5m"; } + +// Internal use only. +// These wrappers are needed only because the container annotation for @ArgumentsSource, +// @ArgumentsSources, can't be applied to annotations. +@Target({ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@ArgumentsSource(AgentConfiguringArgumentsProvider.class) +@interface AgentConfiguringArgumentsProviderArgumentsSource {} + +@Target({ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@ArgumentsSource(FuzzingArgumentsProvider.class) +@interface FuzzingArgumentsProviderArgumentsSource {} diff --git a/src/main/java/com/code_intelligence/jazzer/junit/FuzzingArgumentsProvider.java b/src/main/java/com/code_intelligence/jazzer/junit/FuzzingArgumentsProvider.java new file mode 100644 index 00000000..61a8d3fe --- /dev/null +++ b/src/main/java/com/code_intelligence/jazzer/junit/FuzzingArgumentsProvider.java @@ -0,0 +1,42 @@ +/* + * Copyright 2023 Code Intelligence GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.code_intelligence.jazzer.junit; + +import static com.code_intelligence.jazzer.junit.Utils.isFuzzing; + +import java.util.stream.Stream; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +class FuzzingArgumentsProvider implements ArgumentsProvider { + @Override + public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { + if (!isFuzzing(extensionContext)) { + return Stream.empty(); + } + + // When fuzzing, supply a special set of arguments that our InvocationInterceptor uses as a + // sign to start fuzzing. + // FIXME: This is a hack that is needed only because there does not seem to be a way to + // communicate out of band that a certain invocation was triggered by a particular argument + // provider. We should get rid of this hack as soon as + // https://github.com/junit-team/junit5/issues/3282 has been addressed. + return Stream.of( + Utils.getMarkedArguments(extensionContext.getRequiredTestMethod(), "Fuzzing...")); + } +} diff --git a/src/main/java/com/code_intelligence/jazzer/junit/FuzzTestArgumentsProvider.java b/src/main/java/com/code_intelligence/jazzer/junit/SeedArgumentsProvider.java index b8d06d71..50618252 100644 --- a/src/main/java/com/code_intelligence/jazzer/junit/FuzzTestArgumentsProvider.java +++ b/src/main/java/com/code_intelligence/jazzer/junit/SeedArgumentsProvider.java @@ -14,6 +14,7 @@ package com.code_intelligence.jazzer.junit; +import static com.code_intelligence.jazzer.junit.Utils.isFuzzing; import static org.junit.jupiter.api.Named.named; import static org.junit.jupiter.params.provider.Arguments.arguments; @@ -45,40 +46,16 @@ import java.util.stream.Stream; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsProvider; -import org.junit.jupiter.params.support.AnnotationConsumer; -class FuzzTestArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<FuzzTest> { +class SeedArgumentsProvider implements ArgumentsProvider { private static final String INCORRECT_PARAMETERS_MESSAGE = "Methods annotated with @FuzzTest must take at least one parameter"; private boolean invalidCorpusFilesPresent = false; - private FuzzTest fuzzTest; - - @Override - public void accept(FuzzTest annotation) { - this.fuzzTest = annotation; - } - @Override public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws IOException { - // FIXME(fmeum): Calling this here feels like a hack. There should be a lifecycle hook that runs - // before the argument discovery for a ParameterizedTest is kicked off, but I haven't found - // one. - FuzzTestExecutor.configureAndInstallAgent(extensionContext, fuzzTest.maxDuration()); - - if (Utils.isFuzzing(extensionContext)) { - // When fuzzing, supply a special set of arguments that our InvocationInterceptor uses as a - // sign to start fuzzing. - // FIXME: This is a hack that is needed only because there does not seem to be a way to - // communicate out of band that a certain invocation was triggered by a particular argument - // provider. We should get rid of this hack as soon as - // https://github.com/junit-team/junit5/issues/3282 has been addressed. - return Stream.of( - Utils.getMarkedArguments(extensionContext.getRequiredTestMethod(), "Fuzzing...")); - } else { - return provideSeedArguments(extensionContext); - } + return isFuzzing(extensionContext) ? Stream.empty() : provideSeedArguments(extensionContext); } private Stream<? extends Arguments> provideSeedArguments(ExtensionContext extensionContext) |