aboutsummaryrefslogtreecommitdiff
path: root/examples/junit-spring-web/build-and-run-tests.sh
blob: a2c819faab7333ea749315cf40f2c99635138984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# 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.

# Development-only. This script builds the example project against the local version of Jazzer,
# runs its unit and fuzz tests, and compares the results with expected results.

set -e
( cd ../../ &&
 bazel build //...
)

# Update jazzer version used for building this project in the pom.xml
JAZZER_VERSION=$(grep -oP '(?<=JAZZER_VERSION = ")[^"]*' ../../maven.bzl)
# Find line with "<artifactId>jazzer-junit</artifactId>" and replace the version in the next line
sed -i "/<artifactId>jazzer-junit<\/artifactId>/ {n;s/<version>.*<\/version>/<version>$JAZZER_VERSION<\/version>/}" pom.xml

# Add locally-built Jazzer to the Maven repository
./mvnw install:install-file -Dfile=../../bazel-bin/deploy/jazzer-junit-project.jar -DpomFile=../../bazel-bin/deploy/jazzer-junit-pom.xml
./mvnw install:install-file -Dfile=../../bazel-bin/deploy/jazzer-project.jar       -DpomFile=../../bazel-bin/deploy/jazzer-pom.xml
./mvnw install:install-file -Dfile=../../bazel-bin/deploy/jazzer-api-project.jar   -DpomFile=../../bazel-bin/deploy/jazzer-api-pom.xml

## Regression and unit tests
echo "[SPRINGBOOT-JUNIT]: These unit and regression fuzz tests should pass"
./mvnw test -Dtest="JunitSpringWebApplicationTests#unitTestShouldPass+fuzzTestShouldPass"

echo "[SPRINGBOOT-JUNIT]: This regression fuzz test should fail."
# Temporarily disable exit on error.
set +e
./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestShouldFail"
declare -i exit_code=$?
set -e

# Assert that the test failed with exit code 1.
if [ $exit_code -eq 1 ]
then
  echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing"
else
  echo "[SPRINGBOOT-JUNIT]: Expected exit code 1, but got $exit_code"
  exit 1
fi

## Fuzz tests
echo "[SPRINGBOOT-JUNIT]: This fuzz test should pass"
JAZZER_FUZZ=1 ./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestShouldPass"

echo "[SPRINGBOOT-JUNIT]: This fuzz test should fail"
set +e
JAZZER_FUZZ=1 ./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestShouldFail"
declare -i exit_code=$?
set -e

if [ $exit_code -eq 1 ]
then
  echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing"
else
  echo "[SPRINGBOOT-JUNIT]: Expected exit code 1, but got $exit_code"
  exit 1
fi

echo "[SPRINGBOOT-JUNIT]: This fuzz test using autofuzz should fail"
set +e
JAZZER_FUZZ=1 ./mvnw test -Dtest="JunitSpringWebApplicationTests#fuzzTestWithDtoShouldFail"
declare -i exit_code=$?
set -e

if [ $exit_code -eq 1 ]
then
  echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing"
else
  echo "[SPRINGBOOT-JUNIT]: Expected exit code 1, but got $exit_code"
  exit 1
fi

## CLI tests
## Assert transitive JUnit dependencies are specified
assertDependency() {
  if ./mvnw dependency:tree | grep -q "$1"
  then
    echo "[SPRINGBOOT-JUNIT]: Found $1 dependency in project"
  else
    echo "[SPRINGBOOT-JUNIT]: Did not find $1 dependency in project"
    exit 1
  fi
}
assertDependency "org.junit.jupiter:junit-jupiter-api"
assertDependency "org.junit.jupiter:junit-jupiter-params"
assertDependency "org.junit.platform:junit-platform-launcher"

# Only build project and test jars, no need for a fat-jar or test execution
./mvnw jar:jar
./mvnw jar:test-jar

# Extract dependency locations
out=$(./mvnw dependency:build-classpath -DforceStdout)
deps=$(echo "$out" | sed '/^\[/d')

# Directly execute Jazzer without Maven
echo "[SPRINGBOOT-JUNIT]: Direct Jazzer execution of fuzz test should pass"
java -cp "target/*:${deps}" \
  com.code_intelligence.jazzer.Jazzer \
  --target_class=com.example.JunitSpringWebApplicationTests \
  --target_method=fuzzTestShouldPass \
  --instrumentation_includes=com.example.* \
  --custom_hook_includes=com.example.*


echo "[SPRINGBOOT-JUNIT]: Direct Jazzer execution of fuzz test using autofuzz should fail"
set +e
JAZZER_FUZZ=1 java -cp "target/*:${deps}" \
  com.code_intelligence.jazzer.Jazzer \
  --target_class=com.example.JunitSpringWebApplicationTests \
  --target_method=fuzzTestWithDtoShouldFail \
  --instrumentation_includes=com.example.* \
  --custom_hook_includes=com.example.*
declare -i exit_code=$?
set -e

if [ $exit_code -eq 77 ]
then
  echo "[SPRINGBOOT-JUNIT]: Expected failing fuzz tests: continuing"
else
  echo "[SPRINGBOOT-JUNIT]: Expected exit code 77, but got $exit_code"
  exit 1
fi

echo "[SPRINGBOOT-JUNIT]: All tests passed"