-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathcheck.sh
More file actions
executable file
·321 lines (289 loc) · 6.96 KB
/
Copy pathcheck.sh
File metadata and controls
executable file
·321 lines (289 loc) · 6.96 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
usage() {
cat <<'EOF'
Usage: ./check.sh [-v|--verbose] <target>
Options:
-v, --verbose
Print versions of tools used by the selected target.
Targets:
fmt Run formatting checks.
fmt-rust Run Rust formatting checks.
fmt-c Run C formatting checks.
lint-rust Run Rust formatting and clippy checks.
lint-c Run C formatting checks.
typos Run spelling checks.
lychee Run offline Markdown link checks.
license-headers
Run license header checks.
lint Run all lint checks.
ci-build-c Build C scanner targets in the CI environment.
ci-codeql-build-c
Build C scanner targets for CodeQL.
ci-railguard
Build and verify a railguard image. Requires RAILGUARD_SYSTEM.
ci-feed-syntax
Run scannerctl feed syntax checks.
ci-nasl-tests
Run NASL make check tests.
ci-nasl-lint
Run openvas-nasl-lint smoketest.
test-rust Run Rust unit tests.
test-rust-compose
Run Rust tests that require the compose test environment.
Set INSTA_FROM_COMPOSE=1 to collect pending snapshots from
compose and review them from the host.
test-c Run C unit tests.
test Run all tests.
local Run locally useful checks, fastest first.
EOF
}
verbose() {
[[ "${VERBOSE:-0}" == "1" ]]
}
version_command() {
if verbose; then
run "$@"
fi
}
fmt_rust() {
cd "$ROOT/rust"
version_command cargo --version
run cargo fmt --check
}
fmt_c() {
cd "$ROOT"
version_command clang-format --version
mapfile -d '' files < <(git ls-files -z -- '*.c' '*.h' ':(exclude).docker/**')
if ((${#files[@]} == 0)); then
return 0
fi
run clang-format --dry-run --Werror -style=file "${files[@]}"
}
run() {
printf 'Running command:'
printf ' %q' "$@"
printf '\n'
"$@"
}
lint_rust() {
cd "$ROOT/rust"
version_command cargo --version
version_command cargo clippy -V
run make
run cargo fmt --check
run cargo clippy --all-targets -- -D warnings --no-deps
run cargo clippy --all-targets --features native-rust-ssh -- -D warnings --no-deps
}
lint_c() {
fmt_c
}
typos_check() {
cd "$ROOT/rust"
version_command typos --version
run typos
}
lychee_check() {
cd "$ROOT"
version_command lychee --version
run lychee --files-from <(git ls-files '*.md' ':!.*') --offline --include-fragments --no-progress
}
license_headers() {
cd "$ROOT"
local ext f header failed=0
for ext in c h nasl cmake; do
while IFS= read -r -d '' f; do
header="$(head -n 3 "$f")"
if [[ ! "$header" =~ SPDX ]]; then
echo "File does not contain license header: $f"
failed=1
fi
done < <(
find . \
-not -path "./.docker/*" \
-not -path "./build/*" \
-not -path "./rust/target/*" \
-not -path "./rust/crates/nasl-c-lib/*" \
-regex ".*\.\($ext\)" \
-print0
)
done
return "$failed"
}
lint_all() {
lint_rust
lint_c
typos_check
license_headers
}
ci_build_c() {
cd "$ROOT"
version_command cmake --version
run cmake -Bbuild/c -DCMAKE_C_COMPILER=/usr/share/clang/scan-build-19/libexec/ccc-analyzer
run cmake --build build/c
}
ci_codeql_build_c() {
cd "$ROOT"
version_command cmake --version
run cmake -Bbuild/codeql -DCMAKE_BUILD_TYPE=Release
run cmake --build build/codeql --target install
}
test_rust() {
cd "$ROOT/rust"
version_command cargo --version
run make
run cargo test --lib --tests --workspace
run cargo test --lib --tests --workspace --features native-rust-ssh
}
test_rust_compose() {
if [[ "${INSTA_FROM_COMPOSE:-0}" == "1" ]]; then
local pending_dir="$ROOT/.insta-pending"
mkdir -p "$pending_dir"
cd "$ROOT/compose"
run make rust-test \
"RUST_TEST_RUN_ARGS=-e INSTA_UPDATE=new -e INSTA_FORCE_PASS=1 -e INSTA_PENDING_DIR=/insta-pending -v $pending_dir:/insta-pending"
cd "$ROOT/rust"
run env "INSTA_PENDING_DIR=$pending_dir" cargo insta review
else
cd "$ROOT/compose"
run make rust-test
fi
}
test_c() {
cd "$ROOT"
version_command cmake --version
run cmake -Bbuild/test-c -DCMAKE_BUILD_TYPE=Release
run env CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/test-c -- tests test
}
test_all() {
test_rust
test_c
}
ci_railguard() {
cd "$ROOT"
: "${RAILGUARD_SYSTEM:?RAILGUARD_SYSTEM is required}"
run docker build -t test -f ".docker/railguards/${RAILGUARD_SYSTEM}.Dockerfile" .
run docker run --rm test ldd /usr/local/sbin/openvas
run sh -c 'docker run --rm test ldd /usr/local/sbin/openvas | grep libopenvas_wmiclient'
run docker run --rm test /usr/local/bin/openvasd -h
run docker run --rm test /usr/local/bin/scannerctl -h
docker rmi test || true
}
ci_feed_syntax() {
version_command openvas --version
version_command scannerctl version
run sh -c 'scannerctl syntax --quiet "$(openvas -s | grep plugins_folder | sed '\''s/plugins_folder = //'\'')/"'
}
ci_nasl_tests() {
cd "$ROOT"
mkdir -p /etc/openvas
run sh -c 'cd nasl/tests && make check'
}
ci_nasl_lint() {
cd "$ROOT/smoketest_lint"
run make build
run ./run -e openvas-nasl-lint
}
local_all() {
fmt_rust
fmt_c
typos_check
license_headers
lychee_check
lint_rust
test_rust
test_c
}
VERBOSE=0
while (($# > 0)); do
case "$1" in
-v|--verbose)
VERBOSE=1
shift
;;
--)
shift
break
;;
*)
break
;;
esac
done
target="${1:-}"
case "$target" in
fmt)
fmt_rust
fmt_c
;;
fmt-rust)
fmt_rust
;;
fmt-c)
fmt_c
;;
lint-rust)
lint_rust
;;
lint-c)
lint_c
;;
typos)
typos_check
;;
lychee)
lychee_check
;;
license-headers)
license_headers
;;
lint)
lint_all
;;
ci-build-c)
ci_build_c
;;
ci-codeql-build-c)
ci_codeql_build_c
;;
ci-railguard)
ci_railguard
;;
ci-feed-syntax)
ci_feed_syntax
;;
ci-nasl-tests)
ci_nasl_tests
;;
ci-nasl-lint)
ci_nasl_lint
;;
test-rust)
test_rust
;;
test-rust-compose)
test_rust_compose
;;
test-c)
test_c
;;
test)
test_all
;;
local)
local_all
;;
-h|--help|help)
usage
;;
"")
usage
exit 2
;;
*)
printf 'Unknown target: %s\n\n' "$target" >&2
usage >&2
exit 2
;;
esac