#!/bin/sh
# Reproduces the dbconfig-common + database password-policy failure behind the
# large Launchpad cluster (e.g. LP #2090987, #2080448, #2137203):
#
#   granting access to database phpmyadmin for phpmyadmin@localhost: failed.
#   error encountered creating user:
#   mysql said: ERROR 1819 (HY000) at line 1: Your password does not satisfy
#   the current policy requirements
#   dbconfig-common: phpmyadmin configure: aborted.
#
# When the server enforces a password policy (MariaDB simple_password_check /
# MySQL validate_password), the control-user password dbconfig-common uses is
# rejected. This is NOT a phpMyAdmin bug -- the password is generated/handled by
# dbconfig-common -- but this test documents and reproduces the exact failure so
# it is not mistaken for a phpMyAdmin defect.
#
# The test PASSES when it reproduces the policy failure; it then cleans up the
# half-configured package.
set -eu

# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/helpers.sh"
# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/mariadb.sh"

PKG=phpmyadmin
# A dbconfig-style password: mixed case + digits but NO symbol, which fails the
# default simple_password_check policy (requires >=1 "other" character).
WEAK_PASS="phpMyAdmin2026dbconfig"
LOG_DIR="${AUTOPKGTEST_TMP}/password-policy"

mkdir -p "$LOG_DIR"

dep8_require mariadb mariadb-install-db apt-get debconf-set-selections

# --- Start the database server and enforce a password policy -----------------
mariadb_start_system

echo "Enabling the server password policy (simple_password_check) ..."
if ! mariadb_system_sql -e "INSTALL SONAME 'simple_password_check';"; then
    dep8_fail "simple_password_check plugin not available"
fi

# Sanity check: a symbol-less password must now be rejected with 1819. If the
# server accepts it, the testbed does not enforce the policy and nothing here is
# being tested.
if mariadb_system_sql -e "CREATE USER dep8probe@localhost IDENTIFIED BY '${WEAK_PASS}';"; then
    mariadb_system_sql -e "DROP USER dep8probe@localhost;" || true
    dep8_fail "the server did not enforce the expected password policy"
fi

# --- Preseed dbconfig with that weak app password, then install --------------
echo "Preseeding dbconfig with a policy-violating control-user password ..."
dep8_preseed "$PKG" "$PKG/dbconfig-install" boolean true
dep8_preseed "$PKG" "$PKG/database-type" select mysql
dep8_preseed "$PKG" "$PKG/mysql/app-pass" password "$WEAK_PASS"
dep8_preseed "$PKG" "$PKG/app-password-confirm" password "$WEAK_PASS"
dep8_preseed "$PKG" "$PKG/reconfigure-webserver" multiselect ""

cleanup() {
    dpkg --purge --force-all "$PKG" || true
    mariadb_system_sql -e "DROP DATABASE IF EXISTS phpmyadmin; DROP USER IF EXISTS 'phpmyadmin'@'localhost';" || true
    return 0
}
trap cleanup EXIT

echo "Installing $PKG (dbconfig will try to create the control user) ..."
log="$LOG_DIR/install.log"
# Under the noninteractive frontend dbconfig-common *ignores* the error and the
# postinst still returns 0, leaving the package without a working control user;
# interactively -- as the reporters hit it -- dbconfig-common instead aborts. The
# tell-tale in both cases is the policy rejection, which is what we assert.
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends "$PKG" \
    >>"$log" 2>&1 || true

echo "--- control-user creation result ---"
grep -iE "creating user|does not satisfy the current policy|ERROR 1819|granting access" "$log" | head || true

if grep -qiE "does not satisfy the current policy|ERROR 1819" "$log"; then
    dep8_pass "the server password policy rejected dbconfig-common's control-user password (ERROR 1819)"
else
    echo "--- last 40 lines of $log ---"
    tail -40 "$log"
    dep8_fail "did not observe the expected password-policy rejection (ERROR 1819)"
fi
