#! /bin/sh

set -e

dir=$(pwd)/debian/tests
. $dir/textutils.sh

frame "Here we are testing the bounds allowed" \
      "for day of month and month fields, as stated in crontab(5)"

echo "================= adding user 'dummy' ====================="
getent passwd dummy || adduser --disabled-password --gecos "" dummy

echo "===== make user 'dummy' fill his crontab with dom = 0 ====="

# stderr is redirected towards stdout so the variable warning can catch it
bad=$(su dummy -c "(export LINES=' * * 0 * * echo hello'; echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 | grep bad)

echo "==== check that the error contains 'bad day-of-month' ======"
expected="bad day-of-month"
(echo "$bad" | grep -q "$expected") || (echo "bad = $bad"; \
     echo "The expected error about out-of-bound day number was not issued" >&2)

echo "===== make user 'dummy' fill his crontab with dom = 31 ====="

# stderr is redirected towards stdout so the variable warning can catch it
bad=$(su dummy -c "(export LINES=' * * 0 * * echo hello'; echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 | grep bad)

echo "==== check that the error contains 'bad day-of-month' ======"
expected="bad day-of-month"
(echo "$bad" | grep -q "$expected") || (echo "bad = $bad"; \
     echo "The expected error about out-of-bound day number was not issued" >&2)

echo "==== make user 'dummy' fill his crontab with month = 0 ====="

# stderr is redirected towards stdout so the variable warning can catch it
bad=$(su dummy -c "(export LINES=' * * * 0 * echo hello'; echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 | grep bad)

echo "======== check that the error contains 'bad month' ========="
expected="bad month"
(echo "$bad" | grep -q "$expected") || (echo "bad = $bad"; \
     echo "The expected error about out-of-bound day number was not issued" >&2)

echo "==== make user 'dummy' fill his crontab with month = 13 ===="

# stderr is redirected towards stdout so the variable warning can catch it
bad=$(su dummy -c "(export LINES=' * * * 13 * echo hello'; echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 | grep bad)

echo "======== check that the error contains 'bad month' ========="
expected="bad month"
(echo "$bad" | grep -q "$expected") || (echo "bad = $bad"; \
     echo "The expected error about out-of-bound day number was not issued" >&2)

