#!/bin/sh
# Validate a minimal SARIF log against the installed schema, and check that
# the schema rejects a log with the wrong version.
set -eu
schema=/usr/share/sarif/schemas/sarif-schema-2.1.0.json
cd "$AUTOPKGTEST_TMP"

check-jsonschema --check-metaschema /usr/share/sarif/schemas/*.json

cat > good.sarif <<'END'
{"version": "2.1.0", "runs": [{"tool": {"driver": {"name": "test"}},
 "results": [{"ruleId": "CVE-2024-0001", "level": "error",
  "message": {"text": "vulnerable package"},
  "locations": [{"physicalLocation": {"artifactLocation": {"uri": "go.mod"},
   "region": {"startLine": 1}}}]}]}]}
END
check-jsonschema --schemafile "$schema" good.sarif

sed 's/"2.1.0"/"2.0.0"/' good.sarif > bad.sarif
if check-jsonschema --schemafile "$schema" bad.sarif; then
	echo "schema accepted an invalid log" >&2
	exit 1
fi
