1#!/usr/bin/env bash 2# Copyright 2023 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# Development-only. This script builds the example project against the local version of Jazzer, 17# runs its unit and fuzz tests, and compares the results with expected results. 18 19set -e 20( cd ../../ && 21 bazel build //... 22) 23 24# Update jazzer version used for building this project in the pom.xml 25JAZZER_VERSION=$(grep -oP '(?<=JAZZER_VERSION = ")[^"]*' ../../maven.bzl) 26# Find line with "<artifactId>jazzer-junit</artifactId>" and replace the version in the next line 27sed -i "/<artifactId>jazzer-junit<\/artifactId>/ {n;s/<version>.*<\/version>/<version>$JAZZER_VERSION<\/version>/}" pom.xml 28 29# Add locally-built Jazzer to the Maven repository 30./mvnw install:install-file -Dfile=../../bazel-bin/deploy/jazzer-junit-project.jar -DpomFile=../../bazel-bin/deploy/jazzer-junit-pom.xml 31./mvnw install:install-file -Dfile=../../bazel-bin/deploy/jazzer-project.jar -DpomFile=../../bazel-bin/deploy/jazzer-pom.xml 32./mvnw install:install-file -Dfile=../../bazel-bin/deploy/jazzer-api-project.jar -DpomFile=../../bazel-bin/deploy/jazzer-api-pom.xml 33 34## Regression and unit tests 35echo "[SPRINGBOOT-JUNIT]: These unit and regression fuzz tests should pass" 36./mvnw test -Dtest="JunitSpringWebApplicationTests#unitTestShouldPass+fuzzTestShouldPass" 37 38echo "[SPRINGBOOT-JUNIT]: This regression fuzz test should fail." 39# Temporarily disable exit on error. 40set +e 41./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestShouldFail" 42declare -i exit_code=$? 43set -e 44 45# Assert that the test failed with exit code 1. 46if [ $exit_code -eq 1 ] 47then 48 echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing" 49else 50 echo "[SPRINGBOOT-JUNIT]: Expected exit code 1, but got $exit_code" 51 exit 1 52fi 53 54## Fuzz tests 55echo "[SPRINGBOOT-JUNIT]: This fuzz test should pass" 56JAZZER_FUZZ=1 ./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestShouldPass" 57 58echo "[SPRINGBOOT-JUNIT]: This fuzz test should fail" 59set +e 60JAZZER_FUZZ=1 ./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestShouldFail" 61declare -i exit_code=$? 62set -e 63 64if [ $exit_code -eq 1 ] 65then 66 echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing" 67else 68 echo "[SPRINGBOOT-JUNIT]: Expected exit code 1, but got $exit_code" 69 exit 1 70fi 71 72echo "[SPRINGBOOT-JUNIT]: This fuzz test using autofuzz should fail" 73set +e 74JAZZER_FUZZ=1 ./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestWithDtoShouldFail" 75declare -i exit_code=$? 76set -e 77 78if [ $exit_code -eq 1 ] 79then 80 echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing" 81else 82 echo "[SPRINGBOOT-JUNIT]: Expected exit code 1, but got $exit_code" 83 exit 1 84fi 85 86## CLI tests 87## Assert transitive JUnit dependencies are specified 88assertDependency() { 89 if ./mvnw dependency:tree | grep -q "$1" 90 then 91 echo "[SPRINGBOOT-JUNIT]: Found $1 dependency in project" 92 else 93 echo "[SPRINGBOOT-JUNIT]: Did not find $1 dependency in project" 94 exit 1 95 fi 96} 97assertDependency "org.junit.jupiter:junit-jupiter-api" 98assertDependency "org.junit.jupiter:junit-jupiter-params" 99assertDependency "org.junit.platform:junit-platform-launcher" 100 101# Only build project and test jars, no need for a fat-jar or test execution 102./mvnw jar:jar 103./mvnw jar:test-jar 104 105# Extract dependency locations 106out=$(./mvnw dependency:build-classpath -DforceStdout) 107deps=$(echo "$out" | sed '/^\[/d') 108 109# Directly execute Jazzer without Maven 110echo "[SPRINGBOOT-JUNIT]: Direct Jazzer execution of fuzz test should pass" 111java -cp "target/*:${deps}" \ 112 com.code_intelligence.jazzer.Jazzer \ 113 --target_class=com.example.JunitSpringWebApplicationTests \ 114 --target_method=fuzzTestShouldPass \ 115 --instrumentation_includes=com.example.* \ 116 --custom_hook_includes=com.example.* 117 118 119echo "[SPRINGBOOT-JUNIT]: Direct Jazzer execution of fuzz test using autofuzz should fail" 120set +e 121JAZZER_FUZZ=1 java -cp "target/*:${deps}" \ 122 com.code_intelligence.jazzer.Jazzer \ 123 --target_class=com.example.JunitSpringWebApplicationTests \ 124 --target_method=fuzzTestWithDtoShouldFail \ 125 --instrumentation_includes=com.example.* \ 126 --custom_hook_includes=com.example.* 127declare -i exit_code=$? 128set -e 129 130if [ $exit_code -eq 77 ] 131then 132 echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing" 133else 134 echo "[SPRINGBOOT-JUNIT]: Expected exit code 77, but got $exit_code" 135 exit 1 136fi 137 138echo "[SPRINGBOOT-JUNIT]: All tests passed" 139