summaryrefslogtreecommitdiffstats
path: root/debian/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--debian/tests/control13
-rwxr-xr-xdebian/tests/doc-example61
-rwxr-xr-xdebian/tests/doc-example-comments52
-rwxr-xr-xdebian/tests/multiple-pkgs60
-rwxr-xr-xdebian/tests/no-nsswitch-conf26
-rwxr-xr-xdebian/tests/reproducibility49
-rw-r--r--debian/tests/shared-functions.sh96
-rwxr-xr-xdebian/tests/shellcheck36
8 files changed, 393 insertions, 0 deletions
diff --git a/debian/tests/control b/debian/tests/control
new file mode 100644
index 0000000..d7796fe
--- /dev/null
+++ b/debian/tests/control
@@ -0,0 +1,13 @@
+Tests:
+ doc-example,
+ doc-example-comments,
+ multiple-pkgs,
+ no-nsswitch-conf,
+ reproducibility,
+Depends: @, debhelper-compat (= 13), build-essential
+Restrictions: allow-stderr, breaks-testbed, needs-root
+
+Tests:
+ shellcheck,
+Depends: @, debhelper-compat (= 13), build-essential, shellcheck
+Restrictions: allow-stderr, superficial
diff --git a/debian/tests/doc-example b/debian/tests/doc-example
new file mode 100755
index 0000000..184f091
--- /dev/null
+++ b/debian/tests/doc-example
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+set -eu
+
+. "$(dirname "$0")/shared-functions.sh"
+
+testname="$(basename "$0")"
+pkgname="libnss-test-$testname"
+
+nss_lines="
+hosts before=dns mdns4
+hosts before=mdns4 mdns4_minimal [NOTFOUND=return]
+hosts remove-only mdns # In case the user manually added it
+"
+
+expected_services="files mdns4_minimal [NOTFOUND=return] mdns4 dns"
+
+echo "Set up and build test packages..."
+cd "$AUTOPKGTEST_TMP"
+mkdir "$pkgname"
+cd "$pkgname"
+setup_pkg "$pkgname" "$testname"
+build_pkg "$pkgname" "1" "$nss_lines"
+build_pkg "$pkgname" "2" "$nss_lines"
+cd "$AUTOPKGTEST_TMP"
+
+echo "Set up /etc/nsswitch.conf..."
+echo "$NSSWITCH_CONF" > /etc/nsswitch.conf
+check_line "hosts" "files dns"
+
+echo "Test that installing the package installs the services..."
+dpkg --install "${pkgname}_1_all.deb"
+check_line "hosts" "$expected_services"
+
+echo "Test that updating the package does not install the services again..."
+dpkg --install "${pkgname}_2_all.deb"
+check_line "hosts" "$expected_services"
+
+echo "Test that removing the package removes the services..."
+dpkg --remove "$pkgname"
+check_line "hosts" "files dns"
+
+echo "Test that installing the package again reinstalls the services..."
+dpkg --install "${pkgname}_2_all.deb"
+check_line "hosts" "$expected_services"
+
+echo "Test that removing the package again removes the services..."
+dpkg --remove "$pkgname"
+check_line "hosts" "files dns"
+
+echo "Test that reinstalling the package injects the services..."
+dpkg --install "${pkgname}_1_all.deb"
+check_line "hosts" "$expected_services"
+
+echo "Manually remove services from /etc/nsswitch.conf..."
+echo "$NSSWITCH_CONF" > /etc/nsswitch.conf
+check_line "hosts" "files dns"
+
+echo "Test that upgrading the package after modifying nsswitch.conf does not inject the services again..."
+dpkg --install "${pkgname}_2_all.deb"
+check_line "hosts" "files dns"
diff --git a/debian/tests/doc-example-comments b/debian/tests/doc-example-comments
new file mode 100755
index 0000000..fe0351a
--- /dev/null
+++ b/debian/tests/doc-example-comments
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+set -eu
+
+. "$(dirname "$0")/shared-functions.sh"
+
+testname="$(basename "$0")"
+pkgname="libnss-test-$testname"
+
+nss_lines="
+hosts before=dns mdns4
+hosts before=mdns4 mdns4_minimal [NOTFOUND=return]
+hosts remove-only mdns # In case the user manually added it
+"
+
+expected_services="files mdns4_minimal [NOTFOUND=return] mdns4 dns"
+
+comments=" # comment mdns4 dns"
+
+echo "Set up and build test packages..."
+cd "$AUTOPKGTEST_TMP"
+mkdir "$pkgname"
+cd "$pkgname"
+setup_pkg "$pkgname" "$testname"
+build_pkg "$pkgname" "1" "$nss_lines"
+build_pkg "$pkgname" "2" "$nss_lines"
+cd "$AUTOPKGTEST_TMP"
+
+echo "Set up /etc/nsswitch.conf..."
+echo "$NSSWITCH_CONF" > /etc/nsswitch.conf
+sed -E -i /etc/nsswitch.conf -e "/^hosts:/ s/\$/$comments/"
+check_line "hosts" "files dns$comments"
+
+echo "Test that installing the package installs the services..."
+dpkg --install "${pkgname}_1_all.deb"
+check_line "hosts" "$expected_services$comments"
+
+echo "Test that updating the package does not install the services again..."
+dpkg --install "${pkgname}_2_all.deb"
+check_line "hosts" "$expected_services$comments"
+
+echo "Test that removing the package removes the services..."
+dpkg --remove "$pkgname"
+check_line "hosts" "files dns$comments"
+
+echo "Test that installing the package again reinstalls the services..."
+dpkg --install "${pkgname}_2_all.deb"
+check_line "hosts" "$expected_services$comments"
+
+echo "Test that removing the package again removes the services..."
+dpkg --remove "$pkgname"
+check_line "hosts" "files dns$comments"
diff --git a/debian/tests/multiple-pkgs b/debian/tests/multiple-pkgs
new file mode 100755
index 0000000..57ecab7
--- /dev/null
+++ b/debian/tests/multiple-pkgs
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+set -eu
+
+. "$(dirname "$0")/shared-functions.sh"
+
+testname1="$(basename "$0")-a"
+testname2="$(basename "$0")-b"
+pkgname1="libnss-test-$testname1"
+pkgname2="libnss-test-$testname2"
+
+nss_lines1="
+hosts first mynss_a1
+hosts last mynss_a2 [NOTFOUND=return]"
+
+nss_lines2="
+hosts after=mynss_a2 mynss_b1
+hosts before=dns mynss_b2"
+
+
+expected_services_initial="files dns"
+expected_services_after_inst1="mynss_a1 files dns mynss_a2 [NOTFOUND=return]"
+expected_services_after_inst2="mynss_a1 files mynss_b2 dns mynss_a2 [NOTFOUND=return] mynss_b1"
+expected_services_after_rm1="files mynss_b2 dns mynss_b1"
+expected_services_after_rm2="$expected_services_initial"
+
+echo "Set up and build test packages..."
+cd "$AUTOPKGTEST_TMP"
+mkdir "$pkgname1"
+cd "$pkgname1"
+setup_pkg "$pkgname1" "$testname1"
+build_pkg "$pkgname1" "1.1" "$nss_lines1"
+
+cd "$AUTOPKGTEST_TMP"
+mkdir "$pkgname2"
+cd "$pkgname2"
+setup_pkg "$pkgname2" "$testname2"
+build_pkg "$pkgname2" "2.2" "$nss_lines2"
+
+cd "$AUTOPKGTEST_TMP"
+
+echo "Set up /etc/nsswitch.conf..."
+echo "$NSSWITCH_CONF" > /etc/nsswitch.conf
+check_line "hosts" "$expected_services_initial"
+
+echo "Test that installing package A installs the services..."
+dpkg --install "${pkgname1}_1.1_all.deb"
+check_line "hosts" "$expected_services_after_inst1"
+
+echo "Test that installing package B installs the other services..."
+dpkg --install "${pkgname2}_2.2_all.deb"
+check_line "hosts" "$expected_services_after_inst2"
+
+echo "Test that removing package A removes its services..."
+dpkg --remove "$pkgname1"
+check_line "hosts" "$expected_services_after_rm1"
+
+echo "Test that removing package B removes its services..."
+dpkg --remove "$pkgname2"
+check_line "hosts" "$expected_services_after_rm2"
diff --git a/debian/tests/no-nsswitch-conf b/debian/tests/no-nsswitch-conf
new file mode 100755
index 0000000..2d020f3
--- /dev/null
+++ b/debian/tests/no-nsswitch-conf
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+set -eu
+
+. "$(dirname "$0")/shared-functions.sh"
+
+testname="$(basename "$0")"
+pkgname="libnss-test-$testname"
+
+nss_lines="hosts first mynss"
+
+echo "Set up and build test packages..."
+cd "$AUTOPKGTEST_TMP"
+mkdir "$pkgname"
+cd "$pkgname"
+setup_pkg "$pkgname" "$testname"
+build_pkg "$pkgname" "1" "$nss_lines"
+
+cd "$AUTOPKGTEST_TMP"
+
+echo "Remove /etc/nsswitch.conf..."
+rm /etc/nsswitch.conf
+
+echo "Test that installing the package does not recreate /etc/nsswitch.conf"
+dpkg --install "${pkgname}_1_all.deb"
+test ! -e /etc/nsswitch.conf && echo "OK"
diff --git a/debian/tests/reproducibility b/debian/tests/reproducibility
new file mode 100755
index 0000000..5572054
--- /dev/null
+++ b/debian/tests/reproducibility
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+set -eu
+
+. "$(dirname "$0")/shared-functions.sh"
+
+testname="$(basename "$0")"
+pkgname="libnss-test-$testname"
+
+nss_lines="
+passwd last mynss
+group last mynss
+shadow last mynss
+gshadow last mynss
+"
+
+echo "Enable fr_CH.UTF-8 locale..."
+echo "fr_CH.UTF-8 UTF-8" >> /etc/locale.gen
+locale-gen
+locale -a | grep -q "fr_CH.UTF-8" && echo "OK"
+
+echo "Set up and build test package LC_ALL=C.UTF-8..."
+cd "$AUTOPKGTEST_TMP"
+
+mkdir "$pkgname-c"
+cd "$pkgname-c"
+setup_pkg "$pkgname" "$testname"
+build_pkg "$pkgname" "1" "$nss_lines"
+cd "$AUTOPKGTEST_TMP"
+dpkg-deb -e "${pkgname}_1_all.deb" c
+dpkg-deb -x "${pkgname}_1_all.deb" c
+
+echo "Set up and build test package LC_ALL=fr_CH.UTF-8..."
+cd "$AUTOPKGTEST_TMP"
+
+mkdir "$pkgname-fr"
+cd "$pkgname-fr"
+export LC_ALL="fr_CH.UTF-8"
+export LANGUAGE="fr_CH.UTF-8:fr"
+setup_pkg "$pkgname" "$testname"
+build_pkg "$pkgname" "1" "$nss_lines"
+cd "$AUTOPKGTEST_TMP"
+dpkg-deb -e "${pkgname}_1_all.deb" fr
+dpkg-deb -x "${pkgname}_1_all.deb" fr
+
+echo "Test whether both packages are identical..."
+diff -ur c/ fr/ && echo "OK"
+
+
diff --git a/debian/tests/shared-functions.sh b/debian/tests/shared-functions.sh
new file mode 100644
index 0000000..13797d0
--- /dev/null
+++ b/debian/tests/shared-functions.sh
@@ -0,0 +1,96 @@
+# shellcheck shell=dash
+
+check_line () {
+ local db="$1"
+ local expected_services="$2"
+
+ local expected_line="$db: $expected_services"
+ local new_line
+
+ if ! new_line=$(grep "^$1:" /etc/nsswitch.conf) ; then
+ echo "ERROR: DB $db not found in /etc/nsswitch.conf" >&2
+ exit 2
+ fi
+
+ local num_db_lines
+ num_db_lines="$(echo "$new_line" | wc -l)"
+ if [ "$num_db_lines" -ne 1 ] ; then
+ echo "ERROR: Too many lines for DB $db: $num_db_lines" >&2
+ exit 3
+ fi
+
+ if [ "$new_line" != "$expected_line" ] ; then
+ echo "ERROR: Wrong db line in /etc/nsswitch.conf" >&2
+ echo " found: \`$new_line'" >&2
+ echo "expected: \`$expected_line'" >&2
+ exit 4
+ fi
+
+ echo "OK"
+}
+
+
+setup_pkg () {
+ local pkgname=$1
+ local testname=$2
+
+ mkdir debian
+
+ cat <<EOF > debian/control
+Source: $pkgname
+Section: devel
+Priority: optional
+Maintainer: Test User <test@example.org>
+Rules-Requires-Root: no
+Build-Depends: debhelper-compat (= 13), dh-sequence-installnss
+Standards-Version: 4.6.1
+
+Package: $pkgname
+Architecture: all
+Description: Test package for dh-nss ($testname)
+ Test package for dh-nss ($testname)
+EOF
+
+ cat <<EOF > debian/rules
+#!/usr/bin/make -f
+%:
+ dh \$@
+EOF
+ chmod +x debian/rules
+}
+
+build_pkg () {
+ pkgname="$1"
+ pkgversion="$2"
+ nss_lines="$3"
+
+ echo "$nss_lines" > debian/nss
+
+ cat <<EOF > debian/changelog
+$pkgname ($pkgversion) unstable; urgency=medium
+
+ * Test build
+
+ -- Test User <test@example.org> Sun, 06 Aug 2022 11:22:33 +0200
+EOF
+
+ dpkg-buildpackage --no-sign --build=binary
+}
+
+NSSWITCH_CONF="
+# /etc/nsswitch.conf
+
+passwd: files
+group: files
+shadow: files
+gshadow: files
+
+hosts: files dns
+networks: files
+
+protocols: db files
+services: db files
+ethers: db files
+rpc: db files
+
+netgroup: nis"
diff --git a/debian/tests/shellcheck b/debian/tests/shellcheck
new file mode 100755
index 0000000..e85e422
--- /dev/null
+++ b/debian/tests/shellcheck
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+set -eu
+
+. "$(dirname "$0")/shared-functions.sh"
+
+testname="$(basename "$0")"
+pkgname="libnss-test-$testname"
+
+nss_lines="
+passwd last mynss
+group before=files mynss
+shadow after=files mynss [!SUCCESS=notfound]
+gshadow first mynss [NOTFOUND=return]
+hosts remove-only mynss
+protocols after=db mynss skip-if-present=files
+"
+
+echo "Set up and build test package..."
+cd "$AUTOPKGTEST_TMP"
+mkdir "$pkgname"
+cd "$pkgname"
+setup_pkg "$pkgname" "$testname"
+build_pkg "$pkgname" "1" "$nss_lines"
+
+echo "Extract maintscripts..."
+cd "$AUTOPKGTEST_TMP"
+dpkg-deb -e "${pkgname}_1_all.deb" control
+
+echo "Check for errors and warnings..."
+cd control/
+test -s postinst
+test -s postrm
+shellcheck postinst
+shellcheck postrm
+echo "OK"