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 49a4986
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ jobs:
- caffeine:lincheckTest
- caffeine:isolatedTest
- caffeine:junitTest
- caffeine:fuzzTest
- simulator:check
- jcache:check
- guava:check
Expand Down Expand Up @@ -164,6 +165,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 fuzzTest(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

0 comments on commit 49a4986

Please sign in to comment.