提交 6902ba35 编写于 作者: D DCCooper

gofuzz: add gofuzz test

Signed-off-by: NDCCooper <1866858@gmail.com>
上级 e3e2dbe4
## How to Construct the go fuzz test
1. make folder the form like `fuzz-test-xxx`
2. put the materials in the folder, they will be use for test script:
```bash
$ tree fuzz-test-builder
fuzz-test-builder # test case root dir
|-- corpus # dir to store mutation corpus
| `-- Dockerfile # mutation corpus
|-- Fuzz.go # fuzz go file
`-- path # record relative path to put the Fuzz.go
```
3. when the above meterials are ready, go to `isula-build/tests/src`
4. the **ONLY Three Things** you need to do is:
- copy `fuzz-test-template.sh` to the name you want(*must start with `fuzz-test`*), for example `fuzz-test-xxx.sh`
- change the variable `test_name` same as the name you just gave
- uncomment the last line `main "$1"`
5. run the go fuzz shell script by doing `$ bash fuzz-test-xxx.sh`, it will stop fuzzing after 1 minute.
If you want to change the default run time, you could do like `$ bash fuzz-test-xxx.sh 2h` to keep running 2 hours
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Xiang Li
// Create: 2020-8-29
// Description: Fuzz file for daemon
package daemon
import (
"bufio"
"bytes"
"context"
"io/ioutil"
"os"
"strings"
"github.com/containers/storage/pkg/reexec"
"github.com/sirupsen/logrus"
pb "isula.org/isula-build/api/services"
"isula.org/isula-build/store"
)
func Fuzz(data []byte) int {
if reexec.Init() {
return 0
}
logrus.SetLevel(logrus.DebugLevel)
data = append([]byte("FROM alpine\n"), data...)
logrus.Infof("===============Testing with: \n%s", data)
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "FROM scrath") {
return -1
}
}
dir, err := ioutil.TempDir("/tmp", "fuzzBuilder")
if err != nil {
logrus.Errorf("Fuzz creating TempDir failed: %v", err)
return 0
}
defer os.RemoveAll(dir)
req := pb.BuildRequest{
ContextDir: dir,
FileContent: string(data),
}
daemonOpts := Options{
Debug: false,
DataRoot: "/var/lib/isula-build",
RunRoot: "/var/run/isula-build",
}
store, err := store.GetStore()
if err != nil {
return 0
}
daemon, err := NewDaemon(daemonOpts, store)
if err != nil {
return 0
}
exec, err := daemon.NewBuilder(context.Background(), &req)
if err != nil {
return 0
}
if _, err = exec.Build(); err != nil {
return -1
}
if err = daemon.Cleanup(); err != nil {
return 0
}
return 1
}
LABEL os.type="linux"
LABEL platform.type="x86"
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
WORKDIR /a
WORKDIR b
WORKDIR c
ENV xxx=yyy
RUN echo $xxx
CMD [ "sleep", "999" ]
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Xiang Li
// Create: 2020-8-29
// Description: Fuzz file for dockerignore
package dockerfile
import (
"io/ioutil"
"os"
"path"
"github.com/sirupsen/logrus"
"isula.org/isula-build/pkg/parser"
)
func Fuzz(data []byte) int {
dir, err := ioutil.TempDir("/tmp", "fuzzIgnore")
if err != nil {
logrus.Errorf("Fuzz creating TempDir failed: %v", err)
return 0
}
defer os.RemoveAll(dir)
if err = ioutil.WriteFile(path.Join(dir, ".dockerignore"), data, 0755); err != nil {
logrus.Errorf("Fuzz write .dockerignore file failed: %v", err)
return 0
}
p, err := parser.NewParser("")
if err != nil {
return 0
}
_, err = p.ParseIgnore(dir)
if err != nil {
return 0
}
return 1
}
# comment
*/temp*
*/*/temp*
temp?
# ignore .git and .cache folders
.git
.cache
# ignore all *.class files in all folders, including build root
**/*.class
# ignore all markdown files (md) beside all README*.md other than README-secret.md
*.md
!README*.md
README-secret.md
**/.git
**/.DS_Store
**/node_modules
# Created by .ignore support plugin (hsz.mobi)
### Eclipse template
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
# Eclipse Core
.project
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# JDT-specific (Eclipse Java Development Tools)
.classpath
# Java annotation processor (APT)
.factorypath
# PDT-specific
.buildpath
# sbteclipse plugin
.target
# TeXlipse plugin
.texlipse
### Vim template
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~
### Node template
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
### Linux template
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
### NetBeans template
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
.nb-gradle/
### SublimeText template
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache
# workspace files are user-specific
*.sublime-workspace
# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project
# sftp configuration file
sftp-config.json
### Windows template
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
### OSX template
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### NotepadPP template
*.bak
### Xcode template
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
build/
DerivedData
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
## Other
*.xccheckout
*.moved-aside
*.xcuserstate
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Xiang Li
// Create: 2020-8-29
// Description: Fuzz file for parser
package parser
import (
"bytes"
)
func Fuzz(data []byte) int {
p, err := NewParser("")
if err != nil {
return 0
}
_, err = p.Parse(bytes.NewReader(data), false)
if err != nil {
return 0
}
return 1
}
FROM alpine AS uuid
COPY uuid /src/uuid
FROM alpine AS data
COPY data /src/data
FROM alpine AS foo
ARG USED_ARG="used_value"
RUN echo ${USED_ARG} > /root/value
RUN echo ${USED_ARG}
RUN sleep 5
COPY --from=uuid /src/uuid /root/uuid
COPY --from=data /src/data /root/data
FROM foo
ENV http_proxy=xxxproxy
EXPOSE 80
FROM alpine AS cho
ADD a[[]0].txt /tmp
COPY *b*.tar.gz .
COPY --chown=1555:bin a ./
ADD --chown=0:05 a?.txt /home/www
ADD --chown=mail:05 /home/ok:bb
COPY addchown.txt /root/${abc}/hello
ADD ./a/../b.*c /home/ok:bbc
\ No newline at end of file
FROM alpine AS cho
ADD --chown=2367:3267 addchown.txt /tmp
RUN stat -c "user:%u group:%g" /tmp/addchown.txt
WORKDIR /home/workdir
ARG foodir="foo"
COPY addchown.txt /root/${foodir}/foo
ADD addchown.txt /var
ADD ./* /home/tmp/
COPY --chown=root:bin testDir/*.txt /home/new/
COPY --chown=root:1 testDir/b.tx* /home/new/
ADD --chown=0:mail ./addchown.txt .
ADD --chown=0:0 ./testDir/a.txt ./
FROM cho
MAINTAINER isula "isula@huawei.com"
WORKDIR /tmp
CMD ls
VOLUME ["/var/lib/"]
FROM ubuntu:latest
LABEL maintainer Jay <jie@gmail.com>
COPY . /go/src/github.com/docker/docker
ADD --chown=1 . /
ADD null /
COPY --chown=1 --from=busybox nullfile /tmp
ADD [ "vimrc", "/tmp" ]
COPY [ "bashrc", "/tmp" ]
COPY [ "test file", "/tmp" ]
ADD [ "test file", "/tmp/test file" ]
ARG testArg
FROM scratch
ADD busybox.tar.xz /
CMD ["sh"]
ENTRYPOINT ["sh"]
RUN ["ls"]
RUN echo "hello world"
# escape = \
# make build busybox image
FROM scratch
# add busybox.tar.xz to the root dir
ADD busybox.tar.xz /
RUN set -eux; \
\
# see note below about "*.pyc" files
export PYTHONDONTWRITEBYTECODE=1; \
\
dpkgArch="$(dpkg --print-architecture)"
CMD ["sh"]
#escape=\\
FROM scratch
ADD busybox.tar.xz /
CMD ["sh"]
FROM alpine
ARG http
CMD ls
\ No newline at end of file
# cat Dockerfile.all
FROM busybox:latest
MAINTAINER "leizhongkai"
RUN touch foo
RUN ["touch", "bar"]
LABEL foo=bar
EXPOSE 8888/udp
EXPOSE 9999/tcp
ENV hello=my-hello
ARG word
ADD aaa /home/dir/
COPY bbb /home/dir1/
VOLUME ["var/log"]
USER leizk:root
WORKDIR /home/lzk
ONBUILD RUN ps aux
STOPSIGNAL 15
HEALTHCHECK --interval=5m --timeout=3s CMD ["pwd"]
SHELL ["pwd"]
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
FROM alpine AS builder
COPY file file2 /var/www/
ADD file file2 /var/html/
ENV HBASE_HOME="/usr/local/hbase"
ENV HBASE_CONF_DIR="/etc/hbase"
RUN mkdir $HBASE_HOME
RUN ln -s $HBASE_HOME $HBASE_CONF_DIR
COPY Dockerfile $HBASE_CONF_DIR
ENTRYPOINT /bin/sh
FROM alpine AS uuid
COPY uuid /src/uuid
FROM alpine AS data
COPY data /src/data
FROM alpine AS foo
ARG USED_ARG="used_value"
RUN echo ${USED_ARG} > /root/value
RUN echo ${USED_ARG}
RUN sleep 5
COPY --from=uuid /src/uuid /root/uuid
COPY --from=data /src/data /root/data
FROM foo
ENV http_proxy=xxxproxy
EXPOSE 80
FROM node:slim
RUN mkdir /app
WORKDIR /app
ONBUILD COPY ./package.json /app
ONBUILD RUN [ "npm", "install" ]
ONBUILD COPY . /app/
ONBUILD ENV key1=value1 key2=value2
ONBUILD ENV key3 value3
ONBUILD ADD --chown=1 . /app/src
CMD [ "npm", "start" ]
#
# Dockerfile for isula-build
#
#
FROM ubuntu:latest
MAINTAINER isula-build@isula.com
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH key
ADD ssh_id_rsa.pub /root/.ssh/authorized_keys
RUN chmod 700 /root/.ssh && chmod 600 /root/.ssh/authorized_keys
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
# isula-build licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Xiang Li
# Create: 2020-08-27
# Description: common functions for fuzz tests
# Description: check the log and return the result
# if crash, return 1
# if not, return 0
# Usage: check_result /path/to/log
# $1: the full path of log
function check_result() {
local log=$1
result=$(grep "crash" "$log" | tail -1 | awk '{print $10}')
result=${result%%,}
if [[ $result -eq 0 ]]; then
echo "PASS: No crash found"
return 0
else
echo "FAIL: Crash found at $(date), See detials in $log"
return 1
fi
}
# Description: sleep x s/m/h and kill the process
# Usage: check_timeout 1h 10232
# Input: $1: time to sleep
# $2: pid to kill
function check_timeout() {
local time_out=$1
local pid_to_kill=$2
sleep "$time_out"
for j in $(seq 1 100); do
kill -9 "$pid_to_kill" > /dev/null 2>&1
if pgrep -a "$pid_to_kill"; then
sleep 0.2
else
break
fi
if [[ $j -eq 100 ]]; then
return 1
fi
done
}
# Description: compile Fuzz.go
# Usage: make_fuzz_zip $fuzz_file $fuzz_dir $test_dir
# Input: $1: path to Fuzz.go file
# $2: dir to put the Fuzz.go file
# $3: dir store the build output
# Return: success 0; failed 1
# Warning: all input should be abs path :-)
function make_fuzz_zip() {
fuzz_file=$1
fuzz_dir=$2
data_dir=$3
cp "$fuzz_file" "$fuzz_dir"
pushd "$fuzz_dir" > /dev/null 2>&1 || return 1
mv Fuzz Fuzz.go
if ! go-fuzz-build "$fuzz_dir"; then
echo "go-fuzz-build failed" && return 1
fi
mv "$fuzz_dir"/*.zip "$data_dir"
rm "$fuzz_dir/Fuzz.go"
popd > /dev/null 2>&1 || return 1
}
# Description: set enviroment for go fuzz test
# Usage: set_env "fuzz-test-abc" $top_dir
# Input: $1: test name
# $2: abs path for isula-build project
# Note: 1. test_name must start with fuzz-test, for example fuzz-test-abc
# 2. go fuzz file must have name "Fuzz.go"
# 3. top_dir must be the abs path for the isula-build project
# shellcheck disable=SC2034
function set_env() {
test_name=$1
top_dir=$2
test_root=$top_dir/tests/data/fuzz-test
test_dir=$test_root/$test_name
fuzz_file=$test_dir/Fuzz
fuzz_dir="$top_dir"/"$(cat "$test_dir"/path)"
fuzz_corpus="$test_dir/corpus"
fuzz_log="$test_dir/$test_name.log"
fuzz_crashers="$test_dir/crashers"
fuzz_suppressions="$test_dir/suppressions"
fuzz_zip=""
}
function clean_env() {
rm -rf "$fuzz_zip" "$fuzz_log" "$fuzz_crashers" "$fuzz_suppressions"
}
#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
# isula-build licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Xiang Li
# Create: 2020-08-29
# Description: fuzz builder
top_dir=$(git rev-parse --show-toplevel)
test_name="fuzz-test-builder"
source "$top_dir"/tests/lib/fuzz_commonlib.sh
function pre_fun() {
set_env "${test_name}" "$top_dir"
make_fuzz_zip "$fuzz_file" "$fuzz_dir" "$test_dir"
fuzz_zip=$(ls "$test_dir"/*fuzz.zip)
if [[ -z "$fuzz_zip" ]]; then
echo "fuzz zip file not found"
exit 1
fi
}
function test_fun() {
local time=$1
if [[ -z "$time" ]]; then
time=1m
fi
go-fuzz -bin="$fuzz_zip" -workdir="$test_dir" &>> "$fuzz_log" &
pid=$!
if ! check_timeout $time $pid > /dev/null 2>&1; then
echo "Can not kill process $pid"
fi
check_result "$fuzz_log"
res=$?
return $res
}
function main() {
pre_fun
test_fun "$1"
res=$?
if [ $res -ne 0 ];then
exit 1
else
clean_env
fi
}
main "$1"
#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
# isula-build licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Xiang Li
# Create: 2020-08-29
# Description: fuzz dockerignore
top_dir=$(git rev-parse --show-toplevel)
test_name="fuzz-test-dockerignore"
source "$top_dir"/tests/lib/fuzz_commonlib.sh
function pre_fun() {
set_env "${test_name}" "$top_dir"
make_fuzz_zip "$fuzz_file" "$fuzz_dir" "$test_dir"
fuzz_zip=$(ls "$test_dir"/*fuzz.zip)
if [[ -z "$fuzz_zip" ]]; then
echo "fuzz zip file not found"
exit 1
fi
}
function test_fun() {
local time=$1
if [[ -z "$time" ]]; then
time=1m
fi
go-fuzz -bin="$fuzz_zip" -workdir="$test_dir" &>> "$fuzz_log" &
pid=$!
if ! check_timeout $time $pid > /dev/null 2>&1; then
echo "Can not kill process $pid"
fi
check_result "$fuzz_log"
res=$?
return $res
}
function main() {
pre_fun
test_fun "$1"
res=$?
if [ $res -ne 0 ];then
exit 1
else
clean_env
fi
}
main "$1"
#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
# isula-build licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Xiang Li
# Create: 2020-08-29
# Description: fuzz parser
top_dir=$(git rev-parse --show-toplevel)
test_name="fuzz-test-parser"
source "$top_dir"/tests/lib/fuzz_commonlib.sh
function pre_fun() {
set_env "${test_name}" "$top_dir"
make_fuzz_zip "$fuzz_file" "$fuzz_dir" "$test_dir"
fuzz_zip=$(ls "$test_dir"/*fuzz.zip)
if [[ -z "$fuzz_zip" ]]; then
echo "fuzz zip file not found"
exit 1
fi
}
function test_fun() {
local time=$1
if [[ -z "$time" ]]; then
time=1m
fi
go-fuzz -bin="$fuzz_zip" -workdir="$test_dir" &>> "$fuzz_log" &
pid=$!
if ! check_timeout $time $pid > /dev/null 2>&1; then
echo "Can not kill process $pid"
fi
check_result "$fuzz_log"
res=$?
return $res
}
function main() {
pre_fun
test_fun "$1"
res=$?
if [ $res -ne 0 ];then
exit 1
else
clean_env
fi
}
main "$1"
#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
# isula-build licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Xiang Li
# Create: 2020-08-29
# Description: fuzz script template
# top dir is path of where you put isula-build project
top_dir=$(git rev-parse --show-toplevel)
# keep the name same as the folder you created before like "fuzz-test-xxx"
test_name="fuzz-test-parser"
# get common functions used for test script
source "$top_dir"/tests/lib/fuzz_commonlib.sh
# prepare the env before fuzz start
function pre_fun() {
# prepare env
set_env "${test_name}" "$top_dir"
# make fuzz zip file
make_fuzz_zip "$fuzz_file" "$fuzz_dir" "$test_dir"
fuzz_zip=$(ls "$test_dir"/*fuzz.zip)
if [[ -z "$fuzz_zip" ]]; then
echo "fuzz zip file not found"
exit 1
fi
}
# run fuzz
function test_fun() {
local time=$1
if [[ -z "$time" ]]; then
time=1m
fi
go-fuzz -bin="$fuzz_zip" -workdir="$test_dir" &>> "$fuzz_log" &
pid=$!
if ! check_timeout $time $pid > /dev/null 2>&1; then
echo "Can not kill process $pid"
fi
check_result "$fuzz_log"
res=$?
return $res
}
function main() {
pre_fun
test_fun "$1"
res=$?
if [ $res -ne 0 ];then
exit 1
else
clean_env
fi
}
# uncomment following to make script working
# main "$1"
#!/bin/bash
top_dir=$(git rev-parse --show-toplevel)
source "$top_dir"/tests/lib/common.sh
pre_check
start_isula_builder
# normal test
function normal() {
source "$top_dir"/tests/lib/common.sh
pre_check
start_isula_builder
while IFS= read -r testfile; do
echo -e "test $testfile:\c"
if ! bash "$testfile"; then
exit 1
fi
done < <(find "$top_dir"/tests/src -maxdepth 1 -type f -print)
while IFS= read -r testfile; do
printf "%-45s" "test $(basename "$testfile"): "
if ! bash "$testfile"; then
exit 1
fi
done < <(find "$top_dir"/tests/src -maxdepth 1 -name "test-*" -type f -print)
cleanup
cleanup
}
# go fuzz test
function fuzz() {
while IFS= read -r testfile; do
printf "%-45s" "test $(basename "$testfile"): "
if ! bash "$testfile" "$1"; then
exit 1
fi
done < <(find "$top_dir"/tests/src -maxdepth 1 -name "fuzz-*" -type f -print)
}
# main function to chose which kind of test
function main() {
case "$1" in
fuzz)
fuzz "$2"
;;
*)
normal
;;
esac
}
main "$@"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册