install-jq/tests/unit/test-version-validation.sh
Daniel Carbone a9c73932fe
docs+tests: prepare v4 release
- Update README: fix jq project link to jqlang/jq, update version matrix
  with correct repo URLs per series (jqlang for 1.7+, stedolan for legacy),
  update example action ref @v3 → @v4, add Security section describing
  SHA-256 checksum verification and curl hardening introduced in v4.
- Fix example-windows.yaml: workflow name was 'Setup jq Example - Linux'.
- Update @v3 → @v4 in all three example workflow files.
- Add tests/unit/test-version-validation.sh: 30 unit tests covering the
  version-string regex used by all four install scripts (valid strings,
  empty, path traversal, malformed, flags, whitespace, etc.).
- Add tests/unit/test-arch-mapping.sh: 22 unit tests covering the
  RUNNER_ARCH → jq arch mapping in unixish-17.sh.
- Add .github/workflows/unit-tests.yaml: runs both unit-test scripts on
  ubuntu-22.04, ubuntu-24.04, macos-14, macos-15, and macos-26.
- Add test-invalid-version job to tests.yaml: verifies all four install
  scripts exit non-zero for a set of malformed/malicious version strings,
  covering Linux, macOS, and Windows runners.
- Extend tests.yaml push paths to include tests/unit/**.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-24 13:39:00 -05:00

94 lines
2 KiB
Shell
Executable file

#!/bin/sh
# Unit tests for JQ_VERSION input validation.
#
# Tests the regex used in unixish.sh and unixish-17.sh:
# ^[0-9]+\.[0-9]+(\.[0-9]+)*$
#
# Usage:
# sh tests/unit/test-version-validation.sh
#
# Exit code:
# 0 — all tests passed
# 1 — one or more tests failed
set -e
_pass=0
_fail=0
# _validate_version <version>
# Returns 0 (valid) or 1 (invalid), matching the logic in the install scripts.
_validate_version() {
printf '%s' "$1" | grep -Eq '^[0-9]+\.[0-9]+(\.[0-9]+)*$'
}
# assert_valid <version>
# Asserts that the version string is accepted by the validator.
assert_valid() {
_v="$1"
if _validate_version "$_v"; then
echo "PASS valid: '$_v'"
_pass=$((_pass + 1))
else
echo "FAIL valid: '$_v' (was unexpectedly rejected)"
_fail=$((_fail + 1))
fi
}
# assert_invalid <version>
# Asserts that the version string is rejected by the validator.
assert_invalid() {
_v="$1"
if _validate_version "$_v"; then
echo "FAIL invalid: '$_v' (was unexpectedly accepted)"
_fail=$((_fail + 1))
else
echo "PASS invalid: '$_v'"
_pass=$((_pass + 1))
fi
}
echo "=== Version validation unit tests ==="
echo ""
echo "--- Valid version strings ---"
assert_valid "1.5"
assert_valid "1.6"
assert_valid "1.7"
assert_valid "1.7.1"
assert_valid "1.8.0"
assert_valid "2.0"
assert_valid "2.0.0"
assert_valid "10.20.30"
assert_valid "1.2.3.4"
echo ""
echo "--- Invalid version strings ---"
assert_invalid ""
assert_invalid "."
assert_invalid ".."
assert_invalid "1"
assert_invalid "1."
assert_invalid ".7"
assert_invalid "1..7"
assert_invalid "1.7."
assert_invalid "1.7.1."
assert_invalid "abc"
assert_invalid "a.b.c"
assert_invalid "1.6-dirty"
assert_invalid "1.6 "
assert_invalid " 1.6"
assert_invalid "1 7"
assert_invalid "../../etc/passwd"
assert_invalid "../1.7"
assert_invalid "1.7/../etc"
assert_invalid "-n"
assert_invalid "-1.7"
assert_invalid "1.-7"
echo ""
echo "=== Results: ${_pass} passed, ${_fail} failed ==="
if [ "${_fail}" -gt 0 ]; then
exit 1
fi