|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Bazel runfiles resolution |
| 5 | +# shellcheck source=/dev/null |
| 6 | +source "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" 2>/dev/null || |
| 7 | + source "$(dirname "$0")/../bazel_tools/tools/bash/runfiles/runfiles.bash" 2>/dev/null || |
| 8 | + { echo >&2 "ERROR: cannot find runfiles.bash"; exit 1; } |
| 9 | + |
| 10 | +GAWK="$(rlocation gawk/gawk)" |
| 11 | +AWK_SCRIPT="$(rlocation distroless/private/util/validate_usr_symlinks.awk)" |
| 12 | + |
| 13 | +run() { |
| 14 | + printf '%s\n' "$1" | "$GAWK" -v validation_output_file=/dev/null -f "$AWK_SCRIPT" |
| 15 | +} |
| 16 | + |
| 17 | +fail() { echo "FAIL: $*" >&2; exit 1; } |
| 18 | + |
| 19 | +# --- passing cases --- |
| 20 | + |
| 21 | +run "./bin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/bin" \ |
| 22 | + || fail "./bin -> usr/bin should pass" |
| 23 | + |
| 24 | +run "/bin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/bin" \ |
| 25 | + || fail "/bin -> usr/bin should pass" |
| 26 | + |
| 27 | +run "bin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/bin" \ |
| 28 | + || fail "bin -> usr/bin should pass" |
| 29 | + |
| 30 | +run "./sbin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/sbin" \ |
| 31 | + || fail "./sbin -> usr/sbin should pass" |
| 32 | + |
| 33 | +run "./lib type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/lib" \ |
| 34 | + || fail "./lib -> usr/lib should pass" |
| 35 | + |
| 36 | +run "./lib32 type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/lib32" \ |
| 37 | + || fail "./lib32 -> usr/lib32 should pass" |
| 38 | + |
| 39 | +run "./lib64 type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/lib64" \ |
| 40 | + || fail "./lib64 -> usr/lib64 should pass" |
| 41 | + |
| 42 | +run "./lib64 type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/lib64 |
| 43 | +./usr/bin/ls type=file mode=0755 nlink=1 uid=0 gid=0 size=12345" \ |
| 44 | + || fail "content under ./usr/ alongside valid symlinks should pass" |
| 45 | + |
| 46 | +# --- failing cases --- |
| 47 | + |
| 48 | +run "./bin type=dir mode=0755 nlink=2 uid=0 gid=0" \ |
| 49 | + && fail "./bin as a directory should fail" || true |
| 50 | + |
| 51 | +run "./bin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/sbin" \ |
| 52 | + && fail "./bin -> usr/sbin should fail" || true |
| 53 | + |
| 54 | +run "bin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/fin" \ |
| 55 | + && fail "bin -> usr/fin should fail" || true |
| 56 | + |
| 57 | +run "/bin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/fin" \ |
| 58 | + && fail "/bin -> usr/fin should fail" || true |
| 59 | + |
| 60 | +run "./sbin type=link mode=0777 nlink=1 uid=0 gid=0 link=usr/bin" \ |
| 61 | + && fail "./sbin -> usr/bin should fail (Debian keeps sbin separate)" || true |
| 62 | + |
| 63 | +run "./lib/libfoo.so.1 type=file mode=0644 nlink=1 uid=0 gid=0 size=4096" \ |
| 64 | + && fail "content under ./lib/ should fail" || true |
| 65 | + |
| 66 | +run "lib/libfoo.so.1 type=file mode=0644 nlink=1 uid=0 gid=0 size=4096" \ |
| 67 | + && fail "content under lib/ should fail" || true |
| 68 | + |
| 69 | +run "/lib/libfoo.so.1 type=file mode=0644 nlink=1 uid=0 gid=0 size=4096" \ |
| 70 | + && fail "content under /lib/ should fail" || true |
| 71 | + |
| 72 | +run "./bin/ls type=file mode=0755 nlink=1 uid=0 gid=0 size=12345" \ |
| 73 | + && fail "content under ./bin/ should fail" || true |
| 74 | + |
| 75 | +echo "All tests passed." |
0 commit comments