Skip to content

Commit

Permalink
add fuzz testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Mar 31, 2024
1 parent 2ffa072 commit bd4f491
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ jobs:
java: 11
- suite: caffeine:weakKeysAndSoftValuesSyncGuavaTest
java: 11
- suite: caffeine:fuzzTest
java: 11
env:
JAVA_VERSION: ${{ matrix.java }}
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build-cache
test-output
jitwatch.out
.DS_Store
.cifuzz-corpus
.classpath
.settings
.project
Expand Down
14 changes: 14 additions & 0 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
testImplementation(libs.ycsb) {
isTransitive = false
}
testImplementation(libs.jazzer)
testImplementation(libs.picocli)
testImplementation(libs.jctools)
testImplementation(libs.fastutil)
Expand Down Expand Up @@ -152,6 +153,18 @@ tasks.register<Test>("lincheckTest") {
}
}

tasks.register<Test>("fuzzTest") {
group = "Verification"
description = "Fuzz tests"

forkEvery = 1
failFast = true
useJUnitPlatform()
testLogging.events("started")
environment("JAZZER_FUZZ", "1")
include("com/github/benmanes/caffeine/fuzz/**")
}

val junitTest = tasks.register<Test>("junitTest") {
group = "Verification"
description = "JUnit tests"
Expand All @@ -162,6 +175,7 @@ val junitTest = tasks.register<Test>("junitTest") {
useJUnit()
failFast = true
maxHeapSize = "2g"
exclude("com/github/benmanes/caffeine/fuzz/**")
systemProperty("caffeine.osgi.jar", relativePath(jar.get().archiveFile.get().asFile.path))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void parseOption(String option) {

@SuppressWarnings("StringSplitter")
String[] keyAndValue = option.split(SPLIT_KEY_VALUE);
requireArgument(keyAndValue.length <= 2,
requireArgument((keyAndValue.length >= 1) && (keyAndValue.length <= 2),
"key-value pair %s with more than one equals sign", option);

String key = keyAndValue[0].trim();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Ben Manes. All Rights Reserved.
*
* 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.github.benmanes.caffeine.fuzz;

import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.junit.FuzzTest;
import com.github.benmanes.caffeine.cache.CaffeineSpec;

/**
* @author [email protected] (Ben Manes)
*/
public final class CaffeineSpecFuzzer {

// These tests require the environment variable JAZZER_FUZZ=1 to try new input arguments

@FuzzTest(maxDuration = "5m")
@SuppressWarnings("CheckReturnValue")
public void parse(FuzzedDataProvider data) {
try {
CaffeineSpec.parse(data.consumeRemainingAsString());
} catch (IllegalArgumentException e) { /* ignored */ }
}
}
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jakarta-inject = "2.0.1"
jamm = "0.4.0"
java-object-layout = "0.17"
javapoet = "1.13.0"
jazzer = "0.22.1"
jcache = "1.1.1"
jcommander = "1.82"
jctools = "4.0.3"
Expand All @@ -67,7 +68,7 @@ kotlin = "1.9.23"
lincheck = "2.27"
mockito = "5.11.0"
nexus-publish = "2.0.0-rc-2"
nullaway-core = "0.10.24"
nullaway-core = "0.10.25"
nullaway-plugin = "2.0.0"
okhttp-bom = "4.12.0"
okio-bom = "3.9.0"
Expand All @@ -82,7 +83,7 @@ protobuf = "4.26.1"
slf4j = "2.0.12"
slf4j-test = "3.0.1"
snakeyaml = "2.2"
sonarqube = "4.4.1.3373"
sonarqube = "5.0.0.4638"
spotbugs-contrib = "7.6.4"
spotbugs-core = "4.8.3"
spotbugs-plugin = "6.0.9"
Expand Down Expand Up @@ -156,6 +157,7 @@ jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version.ref = "
jamm = { module = "com.github.jbellis:jamm", version.ref = "jamm" }
java-object-layout = { module = "org.openjdk.jol:jol-cli", version.ref = "java-object-layout" }
javapoet = { module = "com.squareup:javapoet", version.ref = "javapoet" }
jazzer = { module = "com.code-intelligence:jazzer-junit", version.ref = "jazzer" }
jcache = { module = "javax.cache:cache-api", version.ref = "jcache" }
jcache-guice = { module = "org.jsr107.ri:cache-annotations-ri-guice", version.ref = "jcache" }
jcache-tck = { module = "javax.cache:cache-tests", version.ref = "jcache" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ plugins {

configurations.jmh {
extendsFrom(configurations.testImplementation.get())
exclude(module = "jazzer-junit")
exclude(module = "slf4j-test")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static boolean isDefinedExternally(CacheManager cacheManager, String cach
* @param cacheName the name of the cache
* @return a new cache instance or null if the named cache is not defined in the settings file
*/
@SuppressWarnings("resource")
public static @Nullable <K, V> CacheProxy<K, V> tryToCreateFromExternalSettings(
CacheManager cacheManager, String cacheName) {
return TypesafeConfigurator.<K, V>from(rootConfig(cacheManager), cacheName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,7 @@ public <C extends Configuration<K, V>> C getConfiguration(Class<C> clazz) {
}

/** Returns the updated expirable value after performing the post-processing actions. */
@SuppressWarnings({"fallthrough", "NullAway",
"PMD.MissingBreakInSwitch", "PMD.SwitchStmtsShouldHaveDefault"})
@SuppressWarnings({"fallthrough", "NullAway", "PMD.MissingBreakInSwitch"})
private @Nullable Expirable<V> postProcess(@Nullable Expirable<V> expirable,
EntryProcessorEntry<K, V> entry, long currentTimeMS) {
switch (entry.getAction()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ private void publish(Cache<K, V> cache, EventType eventType, K key,
JCacheEntryEvent<K, V> e = event;
var dispatchQueue = entry.getValue();
var future = dispatchQueue.compute(key, (k, queue) -> {
@SuppressWarnings("resource")
Runnable action = () -> registration.getCacheEntryListener().dispatch(e);
return (queue == null)
? CompletableFuture.runAsync(action, executor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public EventTypeAwareListener(CacheEntryListener<? super K, ? super V> listener)
}

/** Returns if the backing listener consumes this type of event. */
@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
public boolean isCompatible(EventType eventType) {
switch (eventType) {
case CREATED:
Expand All @@ -63,7 +62,6 @@ public boolean isCompatible(EventType eventType) {
}

/** Processes the event and logs if an exception is thrown. */
@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
public void dispatch(JCacheEntryEvent<K, V> event) {
try {
if (event.getSource().isClosed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public boolean evaluate(CacheEntryEvent<? extends K, ? extends V> event) {
return isCompatible(event) && filter.evaluate(event);
}

@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
private boolean isCompatible(CacheEntryEvent<? extends K, ? extends V> event) {
switch (event.getEventType()) {
case CREATED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public Stochastic(Config config) {
}

@Override
@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
protected double adjust(double hitRate) {
double currentMissRate = (1 - hitRate);
double previousMissRate = (1 - previousHitRate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public TwoQueuePolicy(Config config) {
}

@Override
@SuppressWarnings({"PMD.ConfusingTernary", "PMD.SwitchStmtsShouldHaveDefault"})
@SuppressWarnings("PMD.ConfusingTernary")
public void record(long key) {
// On accessing a page X :
// if X is in Am then
Expand Down Expand Up @@ -111,6 +111,7 @@ public void record(long key) {
policyStats.recordHit();
return;
}
throw new IllegalStateException("Unknown type: " + node.type);
} else {
node = new Node(key);
node.type = QueueType.IN;
Expand Down

0 comments on commit bd4f491

Please sign in to comment.