#!/usr/bin/env bash # lib/tests/ctl_cases_curfew.sh — assertions for the pure logic in the ctl # libraries: the zero-stripping helper, the night-curfew window, and the # seven pidfile helpers. # # Sourced by test_ctl_libs.sh after ctl_libs_harness.sh, which owns the stubs, # the subjects and the PASS/FAIL counters this file adds to. set -euo pipefail # --- _ctl_dec --------------------------------------------------------------- # Zero-padded times must not reach shell arithmetic as octal: "0830" is not a # valid octal number and would abort the comparison mid-check. _t_eq "830" "$(_ctl_dec 0830)" "_ctl_dec strips the leading zero from 0830" _t_eq "500" "$(_ctl_dec 0500)" "_ctl_dec strips the leading zero from 0500" _t_eq "0" "$(_ctl_dec 0000)" "_ctl_dec keeps a single digit for midnight" _t_eq "2300" "$(_ctl_dec 2300)" "_ctl_dec leaves an unpadded time alone" _t_eq "9" "$(_ctl_dec 0009)" "_ctl_dec strips several leading zeros" # --- ctl_is_curfew_now ------------------------------------------------------ # The window wraps past midnight, which a naive start<=now"${RUN}/state/daemon_answers" _t_eq "${ctl_answers}" "$(cat "${RUN}/state/daemon_answers")" \ "ctl_is_curfew_now and daemon is_curfew_now agree on every boundary" # 2259 closed, 2300/2359/0000/0430 open, 0500 closed (end is exclusive), # 1200 closed. Spelled out so the agreement check above cannot pass by # both copies being wrong in the same way. _t_eq "0111100" "${ctl_answers}" "the shared window answers match the 2300-0500 spec" # --- the seven pid helpers -------------------------------------------------- # One table rather than seven near-identical blocks: the duplication is in the # subject, and repeating it here would add nothing and trip the jscpd gate. _pid_case() { local fn="$1" var="$2" local file="${RUN}/state/${fn}.pid" # printf -v assigns to a name held in a variable without eval, which the # repo's shell rules forbid. printf -v "${var}" '%s' "${file}" rm -f "${file}" _t_eq "" "$(${fn})" "${fn} is empty with no pidfile" printf '999999\n' >"${file}" _t_eq "" "$(${fn})" "${fn} is empty when the recorded pid is dead" printf '%s\n' "$$" >"${file}" _t_eq "$$" "$(${fn})" "${fn} returns a live pid" } _pid_case hosts_enforcer_pid HOSTS_PIDFILE _pid_case dns_enforcer_pid DNS_PIDFILE _pid_case launcher_enforcer_pid LAUNCHER_PIDFILE _pid_case workout_detector_pid WORKOUT_PIDFILE _pid_case curfew_enforcer_pid CURFEW_PIDFILE _pid_case tether_enforcer_pid TETHER_PIDFILE _pid_case daemon_pid PIDFILE