Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions validate-action/tests/test-validate-k9.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,191 @@
fi
grep -q 'coordination.k9' "$FIXTURE_DIR/no-ignore.log"

# --- Regression cases from the 2026-11 review of the vendored copies ---
# Each case runs in a directory of its own so it is judged alone.
run_case() {
local name="$1"
CASE_DIR="$FIXTURE_DIR/case-$name"
mkdir -p "$CASE_DIR"
}

# A template placeholder level, quoted or not, reaches the placeholder notice
# whole. The capture used to stop at the first '}', so "{{LEVEL}}" became
# '{{level' and failed as an invalid level.
run_case placeholder
cat > "$CASE_DIR/quoted.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
pedigree = { name = "{{SERVICE}}", version = "1.0.0", leash = "{{LEVEL}}" },
}
EOF
cat > "$CASE_DIR/plain.k9" <<'EOF'
K9!
# SPDX-License-Identifier: MPL-2.0
metadata:
name: templated
version: 1.0.0
security_level: {{ level }}
EOF
placeholder_output=$(INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh")
grep -q 'Errors: 0' <<< "$placeholder_output"
[[ $(grep -c 'template placeholder' <<< "$placeholder_output") -ge 2 ]]

# A last line without a trailing newline is still read. It used to be
# dropped, so a final-line signature was invisible to the Hunt check.
run_case no-trailing-newline
printf 'K9!\n# SPDX-License-Identifier: MPL-2.0\nmetadata:\n name: last-line\n version: 1.0.0\n leash: hunt\n signature: abc123' \
> "$CASE_DIR/last-line.k9"
INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log"
grep -q 'Errors: 0' "$CASE_DIR/out.log"

# A Hunt level is not cancelled by a later, lower level elsewhere in the
# file. Every level seen is validated, and any Hunt needs a signature.
run_case hunt-then-kennel
cat > "$CASE_DIR/hunt.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
pedigree = { name = "unsigned-hunt", version = "1.0.0", leash = 'Hunt },
app = { leash = 'Kennel },
}
EOF
if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then
echo 'expected a later Kennel leash not to waive the Hunt signature rule' >&2
exit 1
fi
grep -q "must include a 'signature'" "$CASE_DIR/out.log"

# Nickel contract application with a pipe, `{ ... } | X.K9Pedigree`, is a
# pedigree and a format marker, as the marker error message promises.
run_case pipe-application
cat > "$CASE_DIR/pipe.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
let schemas = import "./schemas.ncl" in
{
metadata = { name = "piped", version = "1.0.0" },
leash = 'Yard,
} | schemas.K9Pedigree
EOF
INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log"
grep -q 'Errors: 0' "$CASE_DIR/out.log"

# A brace inside a quoted string does not end the policy block, so a later
# policy trust_level of Hunt is still seen and still needs a signature.
run_case quoted-brace
cat > "$CASE_DIR/policy.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
pedigree = { name = "quoted-brace", version = "1.0.0" },
policy = {
description = "}",
trust_level = 'Hunt,
},
}
EOF
if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then
echo 'expected a quoted brace not to hide a Hunt policy trust_level' >&2
exit 1
fi
grep -q "must include a 'signature'" "$CASE_DIR/out.log"

# `let leash = import ...` binds a module; it is not a leash value. It used to
# be read as the level 'import ../leash.ncl in' and fail as invalid.
run_case let-binding
cat > "$CASE_DIR/let.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
let leash = import "../leash.ncl" in
{
magic_number = "K9!",
pedigree = { name = "let-binding", version = "1.0.0", leash = 'Yard },
}
EOF
INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log"
grep -q 'Errors: 0' "$CASE_DIR/out.log"

# A binding and its body on one line: only the binding is dropped, so the
# body's Hunt level is still seen and still needs a signature.
run_case let-binding-same-line
cat > "$CASE_DIR/inline.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
let leash = import "../leash.ncl" in { leash = 'Hunt,
magic_number = "K9!",
metadata = { name = "inline", version = "1.0.0" },
} | K9Pedigree
EOF
if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then
echo 'expected a same-line binding not to hide the body Hunt level' >&2
exit 1
fi
grep -q "must include a 'signature'" "$CASE_DIR/out.log"

# Every level field on a line is recorded: a Hunt after a Kennel on the same
# line still needs a signature.
run_case two-levels-one-line
cat > "$CASE_DIR/oneline.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
pedigree = { name = "x", version = "1", leash = 'Kennel }, app = { leash = 'Hunt },
}
EOF
if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then
echo 'expected a second same-line level of Hunt to need a signature' >&2
exit 1
fi
grep -q "must include a 'signature'" "$CASE_DIR/out.log"

# A schema name inside a quoted string is prose, not a pipe application, so
# it does not count as a pedigree.
run_case quoted-pipe
cat > "$CASE_DIR/prose.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
description = "example | schemas.K9Pedigree",
name = "prose",
version = "1.0.0",
leash = 'Yard,
}
EOF
if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then
echo 'expected a quoted pipe application not to count as a pedigree' >&2
exit 1
fi
grep -q 'Missing pedigree' "$CASE_DIR/out.log"

# A level written inside a quoted string is prose, not a field: it must not
# turn a Yard file into one that needs a signature.
run_case quoted-level-prose
cat > "$CASE_DIR/prose-level.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
pedigree = { name = "x", version = "1", leash = 'Yard }, description = "example leash = Hunt",
}
EOF
INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log"
grep -q 'Errors: 0' "$CASE_DIR/out.log"

Check warning on line 362 in validate-action/tests/test-validate-k9.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using the literal 'Errors: 0' 6 times.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_k9-ecosystem&issues=AaHqa6McCaoNcqyuiFYt&open=AaHqa6McCaoNcqyuiFYt&pullRequest=51

# A policy trust_level after a leash is still recorded, so an unsigned Hunt
# policy is caught whichever order the declarations come in.
run_case leash-then-policy
cat > "$CASE_DIR/order.k9.ncl" <<'EOF'
# SPDX-License-Identifier: MPL-2.0
{
magic_number = "K9!",
pedigree = { name = "order", version = "1.0.0", leash = 'Kennel },
policy = {
trust_level = 'Hunt,
},
}
EOF
if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then
echo 'expected a Hunt policy after a Kennel leash to need a signature' >&2
exit 1
fi
grep -q "must include a 'signature'" "$CASE_DIR/out.log"

Check warning on line 381 in validate-action/tests/test-validate-k9.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using the literal 'must include a 'signature'' 6 times.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_k9-ecosystem&issues=AaHqftASXu4OEPvJc08T&open=AaHqftASXu4OEPvJc08T&pullRequest=51

echo 'K9 validator regression tests passed'
Loading
Loading