diff --git a/tests/data/fuzz-test/README.md b/tests/data/fuzz-test/README.md new file mode 100644 index 0000000000000000000000000000000000000000..cca677a1b5ad99ee90b308b32a523ab06b014816 --- /dev/null +++ b/tests/data/fuzz-test/README.md @@ -0,0 +1,19 @@ +## 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 diff --git a/tests/data/fuzz-test/fuzz-test-builder/Fuzz b/tests/data/fuzz-test/fuzz-test-builder/Fuzz new file mode 100644 index 0000000000000000000000000000000000000000..92ec2b3dc182323e20a42f6c6c03e53d078ebead --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-builder/Fuzz @@ -0,0 +1,83 @@ +// 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 +} diff --git a/tests/data/fuzz-test/fuzz-test-builder/corpus/Dockerfile b/tests/data/fuzz-test/fuzz-test-builder/corpus/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..46679a3ff1a915e2cba651d24bffa5bada5813c7 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-builder/corpus/Dockerfile @@ -0,0 +1,18 @@ + +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" ] diff --git a/tests/data/fuzz-test/fuzz-test-builder/path b/tests/data/fuzz-test/fuzz-test-builder/path new file mode 100644 index 0000000000000000000000000000000000000000..28e3ee0c0b113ac361de18310fe955e1b3909eb5 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-builder/path @@ -0,0 +1 @@ +daemon diff --git a/tests/data/fuzz-test/fuzz-test-dockerignore/Fuzz b/tests/data/fuzz-test/fuzz-test-dockerignore/Fuzz new file mode 100644 index 0000000000000000000000000000000000000000..0c817d9ab9527314137e731a9be17f5734e7d9a5 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-dockerignore/Fuzz @@ -0,0 +1,50 @@ +// 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 + +} diff --git a/tests/data/fuzz-test/fuzz-test-dockerignore/corpus/case1 b/tests/data/fuzz-test/fuzz-test-dockerignore/corpus/case1 new file mode 100644 index 0000000000000000000000000000000000000000..e55b211f8c3713964d1ce66fe769abb412035a16 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-dockerignore/corpus/case1 @@ -0,0 +1,196 @@ +# 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 diff --git a/tests/data/fuzz-test/fuzz-test-dockerignore/path b/tests/data/fuzz-test/fuzz-test-dockerignore/path new file mode 100644 index 0000000000000000000000000000000000000000..310471a828abe66f0b3211ad5e2ded85d2569e4b --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-dockerignore/path @@ -0,0 +1 @@ +builder/dockerfile diff --git a/tests/data/fuzz-test/fuzz-test-parser/Fuzz b/tests/data/fuzz-test/fuzz-test-parser/Fuzz new file mode 100644 index 0000000000000000000000000000000000000000..4c82759d1d31a76bf27254d42691d4c5eb672569 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/Fuzz @@ -0,0 +1,32 @@ +// 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 + +} diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/Dockerfile b/tests/data/fuzz-test/fuzz-test-parser/corpus/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..2e8af2e84da10f08f31a750476908e50a9e393a8 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/Dockerfile @@ -0,0 +1,17 @@ +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 diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/Dockerfile-add-fuzz b/tests/data/fuzz-test/fuzz-test-parser/corpus/Dockerfile-add-fuzz new file mode 100644 index 0000000000000000000000000000000000000000..095869f40255d527ef3140ef670f8cfed07efc81 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/Dockerfile-add-fuzz @@ -0,0 +1,8 @@ +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 diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/add-chown-basic b/tests/data/fuzz-test/fuzz-test-parser/corpus/add-chown-basic new file mode 100644 index 0000000000000000000000000000000000000000..918e6e07e8dd19395e72826aca123e3f27b01848 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/add-chown-basic @@ -0,0 +1,18 @@ +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/"] diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/add_and_copy b/tests/data/fuzz-test/fuzz-test-parser/corpus/add_and_copy new file mode 100644 index 0000000000000000000000000000000000000000..8726b17e729143b5d4732ef1d0908d322fe5d847 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/add_and_copy @@ -0,0 +1,11 @@ +FROM ubuntu:latest +LABEL maintainer Jay + +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" ] diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox b/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox new file mode 100644 index 0000000000000000000000000000000000000000..4acfbfd556f4036f97a6f1989f247d631c3d7de3 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox @@ -0,0 +1,7 @@ +ARG testArg +FROM scratch +ADD busybox.tar.xz / +CMD ["sh"] +ENTRYPOINT ["sh"] +RUN ["ls"] +RUN echo "hello world" diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox_with_complex_line b/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox_with_complex_line new file mode 100644 index 0000000000000000000000000000000000000000..f859cead2eec2351b193cb1a0cd0b6b11d2bb9ed --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox_with_complex_line @@ -0,0 +1,19 @@ +# 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"] + + + diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox_with_directive b/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox_with_directive new file mode 100644 index 0000000000000000000000000000000000000000..872d06b640e26873439ed54d178c1e9d39a4c75f --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/busybox_with_directive @@ -0,0 +1,4 @@ +#escape=\\ +FROM scratch +ADD busybox.tar.xz / +CMD ["sh"] diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/cmd-basic b/tests/data/fuzz-test/fuzz-test-parser/corpus/cmd-basic new file mode 100644 index 0000000000000000000000000000000000000000..ea95607a2abd717b795bfa45eb6780f3953d4f96 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/cmd-basic @@ -0,0 +1,3 @@ +FROM alpine +ARG http +CMD ls \ No newline at end of file diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/lei b/tests/data/fuzz-test/fuzz-test-parser/corpus/lei new file mode 100644 index 0000000000000000000000000000000000000000..9fd6b7407e83f67b87a20c6afe2b3c34e37b6966 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/lei @@ -0,0 +1,21 @@ +# 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"] diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/multi-files-env b/tests/data/fuzz-test/fuzz-test-parser/corpus/multi-files-env new file mode 100644 index 0000000000000000000000000000000000000000..ce1f29549967987c944f33c8e10a89ed21780a9d --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/multi-files-env @@ -0,0 +1,9 @@ +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 diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/multi-stage-builds b/tests/data/fuzz-test/fuzz-test-parser/corpus/multi-stage-builds new file mode 100644 index 0000000000000000000000000000000000000000..2e8af2e84da10f08f31a750476908e50a9e393a8 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/multi-stage-builds @@ -0,0 +1,17 @@ +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 diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/onbuild b/tests/data/fuzz-test/fuzz-test-parser/corpus/onbuild new file mode 100644 index 0000000000000000000000000000000000000000..df3de66a4bd28f5ddf39f6543c31655868d5f9e9 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/onbuild @@ -0,0 +1,10 @@ +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" ] diff --git a/tests/data/fuzz-test/fuzz-test-parser/corpus/testssh b/tests/data/fuzz-test/fuzz-test-parser/corpus/testssh new file mode 100644 index 0000000000000000000000000000000000000000..bf3b8e2bf3a38eff080cfd31889463f2935d0850 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/corpus/testssh @@ -0,0 +1,25 @@ +# +# 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"] diff --git a/tests/data/fuzz-test/fuzz-test-parser/path b/tests/data/fuzz-test/fuzz-test-parser/path new file mode 100644 index 0000000000000000000000000000000000000000..6f7ebe1389f51116ba867abdb3a51fecda73f302 --- /dev/null +++ b/tests/data/fuzz-test/fuzz-test-parser/path @@ -0,0 +1 @@ +pkg/parser diff --git a/tests/lib/fuzz_commonlib.sh b/tests/lib/fuzz_commonlib.sh new file mode 100755 index 0000000000000000000000000000000000000000..b29d777561e69699d2390ae03a4a873ac677cfa3 --- /dev/null +++ b/tests/lib/fuzz_commonlib.sh @@ -0,0 +1,105 @@ +#!/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" +} diff --git a/tests/src/fuzz-test-builder.sh b/tests/src/fuzz-test-builder.sh new file mode 100755 index 0000000000000000000000000000000000000000..09423ae1a4865394228af1b9a64a7f9dd3b1646d --- /dev/null +++ b/tests/src/fuzz-test-builder.sh @@ -0,0 +1,56 @@ +#!/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" diff --git a/tests/src/fuzz-test-dockerignore.sh b/tests/src/fuzz-test-dockerignore.sh new file mode 100755 index 0000000000000000000000000000000000000000..c3f5f1577a8f7d59647e4539aa4688d5b9830f6d --- /dev/null +++ b/tests/src/fuzz-test-dockerignore.sh @@ -0,0 +1,56 @@ +#!/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" diff --git a/tests/src/fuzz-test-parser.sh b/tests/src/fuzz-test-parser.sh new file mode 100755 index 0000000000000000000000000000000000000000..294dc463b384a579407314a485b6c835ee6c8d4c --- /dev/null +++ b/tests/src/fuzz-test-parser.sh @@ -0,0 +1,56 @@ +#!/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" diff --git a/tests/src/fuzz-test-template.sh b/tests/src/fuzz-test-template.sh new file mode 100755 index 0000000000000000000000000000000000000000..98658dd73f0441985c1942e4e313dd4799fedc02 --- /dev/null +++ b/tests/src/fuzz-test-template.sh @@ -0,0 +1,64 @@ +#!/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" diff --git a/tests/test.sh b/tests/test.sh index 8a7694a4882852958f0b698f8cb6531de3749a10..0030e18869674c16c705c24994dad937693f6b61 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,16 +1,43 @@ #!/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 "$@"