#!/bin/sh
# Scan a local directory containing a planted credential and check that
# snaffler finds it with the expected rule, while ignoring a harmless file.
set -e

WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORKDIR"

mkdir share
cat > share/web.config <<'CFG'
<connectionStrings>
  <add name="db" connectionString="Server=sql01;Database=app;User ID=sa;Password=SuperSecret123!" />
</connectionStrings>
CFG
echo "hello world" > share/readme.txt

snaffler --local-fs "$WORKDIR/share" --no-banner --no-color \
    --state "$WORKDIR/state.db" -o "$WORKDIR/results.json"

cat results.json

grep '"file_match"' results.json | grep 'web.config' | grep -q '"rule_name": "KeepDbConnStringPw"'
grep -q 'Password=SuperSecret123!' results.json
if grep '"file_match"' results.json | grep -q 'readme.txt'; then
    echo "readme.txt should not have been flagged" >&2
    exit 1
fi
