forked from google/centipede
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.sh
127 lines (117 loc) · 4.1 KB
/
test_util.sh
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
#!/bin/bash
# Copyright 2022 The Centipede Authors.
#
# 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
#
# https://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.
# Prints "$@" and terminates the current shell.
function die() {
echo "FATAL: $*" >&2
# Kill our own shell or, if we're in a subshell, kill the parent (main) shell.
kill $$
# If we're in a subshell, exit it.
exit 1
}
# Executes a command line specified in $@ with a file operation, e.g.
# `rm <file>`. If a bundled executable `fileutil` is available, passed the
# command to it for extended file handling capabilities. Otherwise, just
# executes the command.
function fileop() {
if [[ -n "${TEST_SRCDIR+x}" ]] &&
[[ -x "${TEST_SRCDIR}/google3/file/util/fileutil" ]]; then
"${TEST_SRCDIR}/google3/file/util/fileutil" "$@"
else
"$@"
fi
}
# Returns the path to Centipede TEST_SRCDIR subdirectory.
function centipede::get_centipede_test_srcdir() {
set -u
echo "${TEST_SRCDIR}/${TEST_WORKSPACE}"
}
# Returns the path to llvm-symbolizer.
function centipede::get_llvm_symbolizer_path() {
set -u
local path
path="$(which llvm-symbolizer)"
if (( $? != 0 )); then
die "llvm-symbolizer must be installed and findable via" \
"PATH: use install_dependencies_debian.sh to fix"
fi
echo "${path}"
}
# If the var named "$1" is unset, then sets it to "$2". If the var is set,
# doesn't change it. In either case, verifies that the final value is a path to
# an executable file.
function centipede::maybe_set_var_to_executable_path() {
local var_name="$1"
# NOTE: `local -n` creates a reference to the var named "$1".
local -n var_ref="$1"
local path="$2"
if [[ -n "${var_ref+x}" ]]; then
echo "Not overriding ${var_name} -- already set to '${var_ref}'" >&2
else
echo "Setting ${var_name} to '${path}'" >&2
var_ref="${path}"
fi
if ! [[ -x "${var_ref}" ]]; then
die "Path '${var_ref}' doesn't exist or is not executable"
fi
}
# Same as centipede::maybe_set_var_to_executable_path(), but "$2" should be
# a command line that builds the executable and prints its path to stdout.
# TODO(ussuri): Reduce code duplication with the above.
function centipede::maybe_set_var_to_built_executable_path() {
local var_name="$1"
# NOTE: `local -n` creates a reference to the var named "$1".
local -n var_ref="$1"
local bazel_build_cmd="$2"
if [[ -n "${var_ref+x}" ]]; then
echo "Not overriding ${var_name} -- already set to '${var_ref}'" >&2
else
echo "Setting ${var_name} to output of '${bazel_build_cmd}'" >&2
var_ref="$(set -e; ${bazel_build_cmd})"
fi
if ! [[ -x "${var_ref}" ]]; then
die "Path '${var_ref}' doesn't exist or is not executable"
fi
}
# Makes sure that an empty directory "$1" exists. Works for local and CNS.
function centipede::ensure_empty_dir() {
fileop mkdir -p "$1"
fileop rm -R -f "${1:?}/*"
}
# Makes sure that string "$1" exists in file "$2". Works for local and CNS.
function centipede::assert_regex_in_file() {
local -r regex="$1"
local -r file="$2"
set -o pipefail
if ! fileop ls "${file}" > /dev/null; then
die "Expected file ${file} doesn't exist"
fi
if ! grep -q -- "${regex}" <(fileop cat "${file}"); then
echo
echo ">>>>>>>>>> BEGIN ${file} >>>>>>>>>>"
fileop cat "${file}"
echo "<<<<<<<<<< END ${file} <<<<<<<<<<"
echo
die "^^^ File ${file} doesn't contain expected regex /${regex}/"
fi
set +o pipefail
}
# For each of the logs in "$@", asserts that fuzzing started and successfully
# completed.
function centipede::assert_fuzzing_success() {
for log in "$@"; do
centipede::assert_regex_in_file "centipede.*init-done:" "${log}"
centipede::assert_regex_in_file "centipede.*end-fuzz:" "${log}"
done
}